前台服务的变更
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
前台服务文档介绍了 Android 前台服务的当前行为。该文档针对大多数应用(无论它们是否以最新版 Android 为目标平台)提供了最佳实践指南。
本页介绍了对前台服务的一些最新重大更改,以及这些更改对未以最新版 Android 平台为目标平台的应用的影响。在许多情况下,对于以较低 API 级别为目标的应用来说,一些最佳实践是可选的,但对于以较高 API 级别为目标的应用来说,这些最佳实践则成为强制性的。
Android 16(API 级别 36)
以下变更适用于在 Android 16 或更高版本上运行的应用,无论这些应用以哪个 API 级别为目标平台:
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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):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)."]]