宣告前景服務並要求權限
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在應用程式的資訊清單中,使用 <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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[null,null,["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["In 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\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\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\nKey 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\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\nKey points about the code\n\n- This code uses best practices for an app that targets API level 34 or higher."]]