ภาพรวมบริการที่ทำงานอยู่เบื้องหน้า
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บริการที่ทำงานอยู่เบื้องหน้าช่วยให้คุณดำเนินการแบบไม่พร้อมกันซึ่ง
ผู้ใช้สังเกตเห็นได้ บริการที่ทำงานอยู่เบื้องหน้าจะแสดงการแจ้งเตือนในแถบสถานะเพื่อให้ผู้ใช้ทราบว่าแอปของคุณ
กำลังทำงานในเบื้องหน้าและใช้ทรัพยากรของระบบ
ตัวอย่างแอปที่ใช้บริการที่ทำงานอยู่เบื้องหน้า ได้แก่
- แอปเครื่องเล่นเพลงที่เล่นเพลงในบริการที่ใช้งานอยู่ การแจ้งเตือน
อาจแสดงเพลงที่กำลังเล่นอยู่
- แอปออกกำลังกายที่บันทึกการวิ่งของผู้ใช้ในบริการที่ทำงานอยู่เบื้องหน้าหลังจากได้รับสิทธิ์จากผู้ใช้ การแจ้งเตือนอาจแสดงระยะทาง
ที่ผู้ใช้เดินทางในเซสชันการออกกำลังกายปัจจุบัน
ใช้บริการที่ทำงานอยู่เบื้องหน้าเฉพาะเมื่อแอปต้องทำงานที่ผู้ใช้สังเกตเห็นได้ แม้ว่าผู้ใช้จะไม่ได้โต้ตอบกับแอปโดยตรงก็ตาม หากการดำเนินการมีความสำคัญต่ำพอที่คุณต้องการใช้การแจ้งเตือนที่มีลำดับความสำคัญต่ำสุด คุณอาจต้องใช้ตัวเลือกงานในเบื้องหลังอื่น
คู่มือนี้จะอธิบายในส่วนต่อไปนี้
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-21 UTC"],[],[],null,["Foreground services let you asynchronously perform operations that are\nnoticeable to the user. Foreground services show a [status bar\nnotification](/develop/ui/views/notifications), to make users aware that your\napp is performing a task in the foreground and is consuming system resources.\n\nExamples of apps that use foreground services include the following:\n\n- A music player app that plays music in a foreground service. The notification might show the current song being played.\n- A fitness app that records a user's run in a foreground service, after receiving permission from the user. The notification might show the distance that the user has traveled during the current fitness session.\n\nOnly use a foreground service when your app needs to perform a task\nthat is noticeable by the user, even when they're not directly interacting with\nthe app. If the action is of low enough importance that you want to use a\nminimum-priority notification, you probably want to use a different\n[background work option](/develop/background-work/background-tasks).\n\nThis guide explains the following areas:\n\n- [Declare foreground services and request\n permissions](/develop/background-work/services/fgs/declare)\n- [Launch a foreground service](/develop/background-work/services/fgs/launch)\n- [Stop a foreground service](/develop/background-work/services/fgs/stop-fgs)\n- [Handle when a user stops an app that has a foreground\n service](/develop/background-work/services/fgs/handle-user-stopping)\n- [Restrictions on starting a foreground service from the background](/develop/background-work/services/fgs/restrictions-bg-start)\n- [Foreground service types](/develop/background-work/services/fgs/service-types)\n- [Foreground service timeout behavior](/develop/background-work/services/fgs/timeout)\n- [Foreground service troubleshooting](/develop/background-work/services/fgs/troubleshooting)\n- [Changes to foreground services](/develop/background-work/services/fgs/changes)"]]