การเปลี่ยนแปลงบริการที่ทำงานอยู่เบื้องหน้า
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เอกสารประกอบเกี่ยวกับบริการที่ทำงานอยู่เบื้องหน้าจะอธิบายลักษณะการทำงานปัจจุบันของ
บริการที่ทำงานอยู่เบื้องหน้าของ Android เอกสารประกอบจะให้คำแนะนำเกี่ยวกับแนวทางปฏิบัติแนะนำสำหรับแอปส่วนใหญ่ ไม่ว่าแอปเหล่านั้นจะกำหนดเป้าหมายเป็น Android เวอร์ชันล่าสุดหรือไม่ก็ตาม
หน้านี้อธิบายการเปลี่ยนแปลงที่สำคัญล่าสุดบางอย่างเกี่ยวกับบริการที่ทำงานอยู่เบื้องหน้า และผลกระทบต่อแอปที่ไม่ได้กำหนดเป้าหมายเป็นแพลตฟอร์ม Android เวอร์ชันล่าสุด ในหลายกรณี แนวทางปฏิบัติแนะนำที่
เป็นทางเลือกสำหรับแอปที่กำหนดเป้าหมายเป็น API ระดับต่ำกว่าจะกลายเป็นข้อบังคับสำหรับแอป
ที่กำหนดเป้าหมายเป็น API ระดับสูงกว่า
Android 16 (API ระดับ 36)
การเปลี่ยนแปลงต่อไปนี้มีผลกับแอปที่ทำงานบน Android 16 ขึ้นไป
โดยไม่คำนึงถึงระดับ API ที่แอปกำหนดเป้าหมาย
ตอนนี้งานที่ทำงานอยู่เบื้องหลังซึ่งเริ่มจากบริการที่ทำงานอยู่เบื้องหน้าต้องเป็นไปตามโควต้ารันไทม์ของตนเอง ซึ่งรวมถึงงานที่กำหนดเวลาโดยตรงด้วย JobScheduler
รวมถึงงานที่สร้างโดยไลบรารีอื่นๆ เช่น WorkManager หรือ DownloadManager
หากต้องการโอนข้อมูลเพื่อตอบสนองต่อการดำเนินการของผู้ใช้ ให้พิจารณาใช้งานการโอนข้อมูลที่เริ่มต้นโดยผู้ใช้ งานเหล่านี้ได้รับการยกเว้นจาก
โควต้างานปกติ
Android 15 (API ระดับ 35)
ข้อกำหนดต่อไปนี้ใช้กับแอปที่กำหนดเป้าหมายเป็น API
ระดับ 35 ขึ้นไป
Android 14 (API ระดับ 34)
ข้อกำหนดต่อไปนี้มีผลกับแอปที่กำหนดเป้าหมายเป็น API
ระดับ 34 ขึ้นไป
Android 12 (API ระดับ 31)
ข้อกำหนดต่อไปนี้มีผลกับแอปที่กำหนดเป้าหมายเป็น API
ระดับ 31 ขึ้นไป
Android 11 (API ระดับ 30)
ข้อกำหนดต่อไปนี้มีผลกับแอปที่กำหนดเป้าหมายเป็น API
ระดับ 30 ขึ้นไป
Android 10 (API ระดับ 29)
ข้อกำหนดต่อไปนี้มีผลกับแอปที่กำหนดเป้าหมายเป็น API
ระดับ 29 ขึ้นไป
- หากบริการที่ทำงานอยู่เบื้องหน้าของแอปใช้ข้อมูลตำแหน่ง แอปจะต้อง
ประกาศบริการด้วยประเภทบริการ
location
Android 9 (API ระดับ 28)
Android 9 เปิดตัวสิทธิ์
FOREGROUND_SERVICE
แอปที่ทำงานบน Android 9 ซึ่งใช้บริการที่ทำงานอยู่เบื้องหน้าต้องมีสิทธิ์ดังกล่าว
หากแอปที่กำหนดเป้าหมายเป็น API ระดับ 28 ขึ้นไปพยายาม
สร้างบริการที่ทำงานอยู่เบื้องหน้าโดยไม่ขอFOREGROUND_SERVICE
สิทธิ์ ระบบจะแสดง SecurityException
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 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-27 UTC"],[],[],null,["The foreground service documentation describes the current behavior of\nAndroid foreground services. The documentation gives guidance on best\npractices for most apps, whether or not they target the most recent version\nof Android.\n\nThis page describes some of the most recent important changes to foreground\nservices, and the implications for apps that aren't targeting the most\nrecent version of the Android platform. In many cases, best practices that\nwere optional for apps targeting lower API levels become mandatory for apps\nthat target higher API levels.\n\nAndroid 16 (API level 36)\n\nThe following changes apply to apps that run on Android 16 or higher,\nregardless of what API level they target:\n\n- Background jobs started from a foreground service now must adhere to their\n respective runtime quotas. This includes jobs scheduled directly with\n [`JobScheduler`](/reference/android/app/job/JobScheduler), as well as jobs created by other libraries like\n [WorkManager](/develop/background-work/background-tasks/persistent) or [`DownloadManager`](/reference/android/app/DownloadManager).\n\n To transfer data in response to a user action, consider using a\n [user-initiated data transfer job](/develop/background-work/background-tasks/uidt). These jobs are exempt from the\n ordinary job quotas.\n\nAndroid 15 (API level 35)\n\nThe following requirements apply to apps that target API\nlevel 35 or higher:\n\n- There are new restrictions on how long a `dataSync` foreground service can run. These restrictions are described in [Foreground service timeout\n behavior](/develop/background-work/services/fgs/timeout). Similar restrictions apply to the (new in Android 15) `mediaProcessing` foreground service type.\n- [`BOOT_COMPLETED` foreground services are no longer allowed to launch certain\n foreground services](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n- Apps that hold the `SYSTEM_ALERT_WINDOW` permission are only allowed to launch foreground services from the background if they currently have a visible overlay window (or if they meet one of the other [exemptions from background\n start restrictions](/develop/background-work/services/fgs/restrictions-bg-start#background-start-restriction-exemptions)). Previously, the exemption for those apps was broader.\n\nAndroid 14 (API level 34)\n\nThe following requirements apply to apps that target API\nlevel 34 or higher:\n\n- You must [declare all foreground services](/develop/background-work/services/fgs/declare) with their service types.\n- Apps must request the appropriate permission type for the kind of work the foreground service will be doing. Each [foreground service type](/develop/background-work/services/fgs/service-types) has a corresponding permission type. For example, if an app launches a foreground service that uses the camera, you must request both the [`FOREGROUND_SERVICE`](/reference/android/Manifest.permission#FOREGROUND_SERVICE) and [`FOREGROUND_SERVICE_CAMERA`](/reference/android/Manifest.permission#FOREGROUND_SERVICE_CAMERA) permissions. If an app targets API level 34 or higher and doesn't request the appropriate specific permission, the system throws a `SecurityException`.\n\nAndroid 12 (API level 31)\n\nThe following requirements apply to apps that target API\nlevel 31 or higher:\n\n- Apps are not allowed to launch foreground services while the app is in the background, with a few specific exceptions. For more information, and information about the exceptions to this rule, see [Restrictions on starting\n a foreground service from the background](/develop/background-work/services/fgs/restrictions-bg-start).\n\nAndroid 11 (API level 30)\n\nThe following requirements apply to apps that target API\nlevel 30 or higher:\n\n- If an app's foreground services use the camera or microphone, the app must [declare the service](/develop/background-work/services/fgs/declare) with the [`camera`](/develop/background-work/services/fgs/service-types#camera) or [`microphone`](/develop/background-work/services/fgs/service-types#microphone) service type, respectively.\n\nAndroid 10 (API level 29)\n\nThe following requirements apply to apps that target API\nlevel 29 or higher:\n\n- If an app's foreground services use location information, the app must [declare the service](/develop/background-work/services/fgs/declare) with the [`location`](/develop/background-work/services/fgs/service-types#location) service type.\n\nAndroid 9 (API level 28)\n\nAndroid 9 introduces the\n[`FOREGROUND_SERVICE`](/reference/android/Manifest.permission#FOREGROUND_SERVICE) permission. Apps running on\nAndroid 9 that use foreground services must have that permission.\n\nIf an app that targets API level 28 or higher attempts\nto create a foreground service without requesting the `FOREGROUND_SERVICE`\npermission, the system throws a [`SecurityException`](/reference/java/lang/SecurityException)."]]