前景服務變更
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
前景服務說明文件說明瞭 Android 前景服務的目前行為。無論應用程式是否以最新版 Android 為目標,這份文件都提供適用於大多數應用程式的最佳做法指南。
本頁說明前台服務的最新重大變更,以及未指定最新版 Android 平台時,對應用程式的影響。在許多情況下,如果應用程式指定的 API 級別較高,則原本可選用的最佳做法會變成必要做法。
Android 16 (API 級別 36)
無論指定哪個 API 級別,在 Android 16 以上版本執行的應用程式都會受到下列變更影響:
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 以上版本,則須符合下列規定:
Android 9 (API 級別 28)
Android 9 導入了 FOREGROUND_SERVICE
權限。在 Android 9 上執行的應用程式如要使用前景服務,就必須具備這項權限。
如果指定 API 級別 28 以上版本的應用程式嘗試建立前景服務,但未要求 FOREGROUND_SERVICE
權限,系統會擲回 SecurityException
。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["# Changes to foreground services\n\nThe 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-------------------------\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-------------------------\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-------------------------\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-------------------------\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-------------------------\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-------------------------\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------------------------\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)."]]