声明前台服务并请求权限
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在应用的清单中,使用 <service>
元素声明应用的每项前台服务。对于每项服务,请使用 android:foregroundServiceType
属性来声明该服务执行的工作类型。
此外,还需请求前台服务所需的任何权限。
版本兼容性
声明前台服务和请求权限的要求因应用的目标 API 级别而异。本页介绍了以 API 级别 34 或更高级别为目标平台的应用应满足的要求。如需了解早期平台版本中前台服务的变更,请参阅前台服务变更。
在应用清单中声明前台服务
以下代码展示了如何声明媒体播放前台服务。您可以使用此类服务来播放音乐。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<application ...>
<service
android:name=".MyMediaPlaybackService"
android:foregroundServiceType="mediaPlayback"
android:exported="false">
</service>
</application>
</manifest>
代码要点
在此示例中,服务只有一种类型,即 media
。如果您的服务适用多种类型,请使用 |
运算符分隔这些类型。例如,如果您的服务使用摄像头和麦克风,请按如下方式声明:
android:foregroundServiceType="camera|microphone"
根据应用的目标 API 级别,您可能需要在应用清单中声明前台服务。有关特定 API 级别的要求,请参阅对前台服务的更改。
如果您尝试创建前台服务,但未在清单中声明其类型,系统会在调用 startForeground()
时抛出 MissingForegroundServiceTypeException
。
即使不是必需的,最好也声明所有前台服务并提供其服务类型。
请求前台服务权限
以下代码展示了如何为使用摄像头的服务请求权限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA"/>
<application ...>
...
</application>
</manifest>
代码要点
- 此代码针对以 API 级别 34 或更高级别为目标平台的应用采用了最佳实践。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Declare foreground services and request permissions\n\nIn your app's manifest, declare each of your app's foreground services\nwith a [`\u003cservice\u003e`](/guide/topics/manifest/service-element)\nelement. For each service, use an\n[`android:foregroundServiceType` attribute](/develop/background-work/services/fgs/service-types)\nto declare what kind of work the service does.\n\nIn addition, request any permissions needed by your foreground services.\n| **Important:** All foreground service declarations must comply with the requirements in the [Google Play Device and Network Abuse policy](https://support.google.com/googleplay/android-developer/answer/9888379) and the Google Play [Understanding foreground service requirements\n| documentation](https://support.google.com/googleplay/android-developer/answer/13392821).\n\nVersion compatibility\n---------------------\n\nThe requirements for declaring your foreground services and requesting\npermissions vary depending on what API level your app targets. This page\ndescribes the requirements for apps that target API level 34 or higher. For\ninformation about changes to foreground services in earlier platform versions,\nsee [Changes to foreground services](/develop/background-work/services/fgs/changes).\n\nDeclare foreground services in the app manifest\n-----------------------------------------------\n\nThe following code shows how to declare a media playback foreground service.\nYou might use a service like this to play music. \n\n \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\" ...\u003e\n \u003capplication ...\u003e\n\n \u003cservice\n android:name=\".MyMediaPlaybackService\"\n android:foregroundServiceType=\"mediaPlayback\"\n android:exported=\"false\"\u003e\n \u003c/service\u003e\n \u003c/application\u003e\n \u003c/manifest\u003e\n\n### Key points about the code\n\n- In this example, the service has only one type, `media`. If\n multiple types apply to your service, separate them with the `|`\n operator. For example, if your service uses the camera and microphone,\n declare it like this:\n\n android:foregroundServiceType=\"camera|microphone\"\n\n- Depending on what API level your app targets, you may be\n **required** to declare foreground services in the app\n manifest. The requirements for specific API levels are described in\n [Changes to foreground services](/develop/background-work/services/fgs/changes).\n\n If you try to create a foreground service and its type isn't declared\n in the manifest, the system throws a\n [`MissingForegroundServiceTypeException`](/reference/android/app/MissingForegroundServiceTypeException)\n upon calling `startForeground()`.\n\n Even when it isn't required, it's a best practice to declare\n all your foreground services and provide their service types.\n\nRequest the foreground service permissions\n------------------------------------------\n\nThe following code shows how to request permissions for a foreground\nservice that uses the camera. \n\n \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\" ...\u003e\n\n \u003cuses-permission android:name=\"android.permission.FOREGROUND_SERVICE\"/\u003e\n \u003cuses-permission android:name=\"android.permission.FOREGROUND_SERVICE_CAMERA\"/\u003e\n\n \u003capplication ...\u003e\n ...\n \u003c/application\u003e\n \u003c/manifest\u003e\n\n### Key points about the code\n\n- This code uses best practices for an app that targets API level 34 or higher."]]