To help developers be more intentional with defining user-facing foreground
services, Android 10 introduced the android:foregroundServiceType
attribute within the <service> element.
If your app targets Android 14, it must specify appropriate foreground service types. As in previous versions of Android, multiple types can be combined. This list shows the foreground service types to choose from:
cameraconnectedDevicedataSynchealthlocationmediaPlaybackmediaProjectionmicrophonephoneCallremoteMessagingshortServicespecialUsesystemExempted
If a use case in your app isn't associated with any of these types, we strongly recommend that you migrate your logic to use WorkManager or user-initiated data transfer jobs.
The health, remoteMessaging, shortService, specialUse, and systemExempted
types are new in Android 14.
The following code snippet provides an example of a foreground service type declaration in the manifest:
<manifest ...>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<application ...>
<service
android:name=".MyMediaPlaybackService"
android:foregroundServiceType="mediaPlayback"
android:exported="false">
</service>
</application>
</manifest>
If an app that targets Android 14 doesn't define types for a given service in
the manifest, then the system will raise MissingForegroundServiceTypeException
upon calling startForeground() for that service.
Neue Berechtigung für die Verwendung von Diensten im Vordergrund erklären
If apps that target Android 14 use a foreground service, they must declare a specific permission, based on the foreground service type, that Android 14 introduces. These permissions appear in the sections labeled "permission that you must declare in your manifest file" in the intended use cases and enforcement for each foreground service type section on this page.
All of the permissions are defined as normal permissions and are granted by default. Users cannot revoke these permissions.
Typ des Diensts im Vordergrund zur Laufzeit angeben
The best practice for applications starting foreground services is to use the
ServiceCompat version of startForeground() (available in androidx-core
1.12 and higher) where you pass in a bitwise
integer of foreground service types. You can choose to pass one or more type
values.
Usually, you should declare only the types required for a particular use case. This makes it easier to meet the system's expectations for each foreground service type. In cases where a foreground service is started with multiple types, then the foreground service must adhere to the platform enforcement requirements of all types.
ServiceCompat.startForeground(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION)
If the foreground service type is not specified in the call, the type defaults
to the values defined in the manifest. If you didn't specify the service
type in the manifest, the system throws
MissingForegroundServiceTypeException.
If the foreground service needs new permissions after you launch it, you
should call startForeground() again and add the new service types. For
example, suppose a fitness app runs a running-tracker service that always needs
location information, but might or might not need media permissions. You
would need to declare both location and mediaPlayback in the manifest. If a
user starts a run and just wants their location tracked, your app should call
startForeground() and pass just the location service type. Then, if the user
wants to start playing audio, call startForeground() again and pass
location|mediaPlayback.
Systemlaufzeitprüfungen
The system checks for proper use of foreground service types and confirms that
the app has requested the proper runtime permissions or uses the required APIs.
For instance, the system expects apps that use the foreground service type
FOREGROUND_SERVICE_TYPE_LOCATION type to request either
ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
This implies that apps must follow a very specific
order of operations when requesting permissions from the user and starting
foreground services. Permissions must be requested and granted before the
app attempts to call startForeground(). Apps that request the appropriate
permissions after the foreground service has been started must change this order
of operations and request the permission before starting the foreground service.
The specifics of platform enforcement appear in the sections labeled "runtime requirements" in the intended use cases and enforcement for each foreground service type section on this page.
Vorgesehene Anwendungsfälle und Erzwingung für jeden Typ von Diensten im Vordergrund
In order to use a given foreground service type, you must declare a particular permission in your manifest file, you must fulfill specific runtime requirements, and your app must fulfill one of the intended sets of use cases for that type. The following sections explain the permission that you must declare, the runtime prerequisites, and the intended use cases for each type.
Kamera
- Foreground service type to declare in manifest under
android:foregroundServiceType camera- Permission to declare in your manifest
FOREGROUND_SERVICE_CAMERA- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_CAMERA- Runtime prerequisites
Request and be granted the
CAMERAruntime permissionNote: The
CAMERAruntime permission is subject to while-in-use restrictions. For this reason, you cannot create acameraforeground service while your app is in the background, with a few exceptions. For more information, see Restrictions on starting foreground services that need while-in-use permissions.- Description
Continue to access the camera from the background, such as video chat apps that allow for multitasking.
Verbundenes Gerät
- Typ des Dienstes im Vordergrund, der im Manifest unter
android:foregroundServiceTypeconnectedDevice- Berechtigung, die in Ihrem Manifest deklariert werden muss
FOREGROUND_SERVICE_CONNECTED_DEVICE- Konstante, die an
startForeground()übergeben werden soll FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE- Laufzeitvoraussetzungen
Mindestens eine der folgenden Bedingungen muss erfüllt sein:
Deklarieren Sie mindestens eine der folgenden Berechtigungen in Ihrem Manifest:
Sie müssen mindestens eine der folgenden Laufzeitberechtigungen anfordern und erhalten:
UsbManager.requestPermission()anrufen
- Beschreibung
Interaktionen mit externen Geräten, die eine Bluetooth‑, NFC‑, IR‑, USB‑ oder Netzwerkverbindung erfordern.
- Alternativen
Wenn Ihre App eine kontinuierliche Datenübertragung an ein externes Gerät ausführen muss, sollten Sie stattdessen den Companion Device Manager verwenden. Mit der API zur Anwesenheit von Companion-Geräten können Sie dafür sorgen, dass Ihre App weiter ausgeführt wird, solange sich das Companion-Gerät in Reichweite befindet.
Wenn Ihre App nach Bluetooth-Geräten suchen muss, können Sie stattdessen die Bluetooth Scan API verwenden.
Datensynchronisierung
- Typ des Dienstes im Vordergrund, der im Manifest unter
android:foregroundServiceTypedataSync- Berechtigung, die in Ihrem Manifest deklariert werden muss
FOREGROUND_SERVICE_DATA_SYNC- Konstante, die an
startForeground()übergeben werden soll FOREGROUND_SERVICE_TYPE_DATA_SYNC- Laufzeitvoraussetzungen
- Keine
- Beschreibung
Datenübertragungsvorgänge, z. B.:
- Datenupload oder -download
- Sicherungs- und Wiederherstellungsvorgänge
- Import- oder Exportvorgänge
- Daten abrufen
- Lokale Dateiverarbeitung
- Daten über ein Netzwerk zwischen einem Gerät und der Cloud übertragen
- Alternativen
Ausführliche Informationen finden Sie unter Alternativen zu Diensten im Vordergrund für die Datensynchronisierung.
Zustand
- Foreground service type to declare in manifest under
android:foregroundServiceTypehealth- Permission to declare in your manifest
FOREGROUND_SERVICE_HEALTH- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_HEALTH- Runtime prerequisites
At least one of the following conditions must be true:
Declare the
HIGH_SAMPLING_RATE_SENSORSpermission in your manifest.Request and be granted at least one of the following runtime permissions:
BODY_SENSORSon API level 35 and lowerREAD_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
Note: The
BODY_SENSORSand sensor-based READ runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create ahealthforeground service that uses body sensors while your app is in the background unless you've been granted theBODY_SENSORS_BACKGROUND(API level 33 to 35) orREAD_HEALTH_DATA_IN_BACKGROUND(API level 36 and higher) permissions. For more information, see Restrictions on starting foreground services that need while-in-use permissions.- Description
Any long-running use cases to support apps in the fitness category such as exercise trackers.
Standort
- Foreground service type to declare in manifest under
android:foregroundServiceTypelocation- Permission to declare in your manifest
FOREGROUND_SERVICE_LOCATION- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_LOCATION- Runtime prerequisites
The user must have enabled location services and the app must be granted at least one of the following runtime permissions:
Note: In order to check that the user has enabled location services as well as granted access to the runtime permissions, use
PermissionChecker#checkSelfPermission()Note: The location runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a
locationforeground service while your app is in the background, unless you've been granted theACCESS_BACKGROUND_LOCATIONruntime permission. For more information, see Restrictions on starting foreground services that need while-in-use permissions.- Description
Long-running use cases that require location access, such as navigation and location sharing.
- Alternatives
If your app needs to be triggered when the user reaches specific locations, consider using the geofence API instead.
Medien
- Foreground service type to declare in manifest under
android:foregroundServiceTypemediaPlayback- Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PLAYBACK- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK- Runtime prerequisites
- None
- Description
- Continue audio or video playback from the background. Support Digital Video Recording (DVR) functionality on Android TV.
- Alternatives
- If you're showing picture-in-picture video, use Picture-in-Picture mode.
Medienprojektion
- Typ des Dienstes im Vordergrund, der im Manifest unter
android:foregroundServiceTypemediaProjection- Berechtigung, die in Ihrem Manifest deklariert werden muss
FOREGROUND_SERVICE_MEDIA_PROJECTION- Konstante, die an
startForeground()übergeben werden soll FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION- Laufzeitvoraussetzungen
Rufen Sie die Methode
createScreenCaptureIntent()auf, bevor Sie den Dienst im Vordergrund starten. Daraufhin wird dem Nutzer eine Berechtigungsbenachrichtigung angezeigt. Der Nutzer muss die Berechtigung erteilen, bevor Sie den Dienst erstellen können.Nachdem Sie den Dienst im Vordergrund erstellt haben, können Sie
MediaProjectionManager.getMediaProjection()aufrufen.- Beschreibung
Übertragung von Inhalten mit
MediaProjectionAPIs auf nicht primäre Displays oder externe Geräte. Diese Inhalte müssen nicht ausschließlich Medieninhalte sein.- Alternativen
Wenn Sie Medien auf ein anderes Gerät streamen möchten, verwenden Sie das Google Cast SDK.
Mikrofon
- Foreground service type to declare in manifest under
android:foregroundServiceTypemicrophone- Permission to declare in your manifest
FOREGROUND_SERVICE_MICROPHONE- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_MICROPHONE- Runtime prerequisites
Request and be granted the
RECORD_AUDIOruntime permission.Note: The
RECORD_AUDIOruntime permission is subject to while-in-use restrictions. For this reason, you cannot create amicrophoneforeground service while your app is in the background, with a few exceptions. For more information, see Restrictions on starting foreground services that need while-in-use permissions.- Description
Continue microphone capture from the background, such as voice recorders or communication apps.
Telefonanruf
- Foreground service type to declare in manifest under
android:foregroundServiceTypephoneCall- Permission to declare in your manifest
FOREGROUND_SERVICE_PHONE_CALL- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_PHONE_CALL- Runtime prerequisites
At least one of these conditions must be true:
- App has declared the
MANAGE_OWN_CALLSpermission in its manifest file.
- App has declared the
- App is the default dialer app through the
ROLE_DIALERrole.
- App is the default dialer app through the
- Description
Continue an ongoing call using the
ConnectionServiceAPIs.- Alternatives
If you need to make phone, video, or VoIP calls, consider using the
android.telecomlibrary.Consider using
CallScreeningServiceto screen calls.
Remote-Messaging
- Foreground service type to declare in manifest under
android:foregroundServiceTyperemoteMessaging- Permission to declare in your manifest
FOREGROUND_SERVICE_REMOTE_MESSAGING- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- Runtime prerequisites
- None
- Description
- Transfer text messages from one device to another. Assists with continuity of a user's messaging tasks when they switch devices.
Kurze Verbindung
- Typ des Dienstes im Vordergrund, der im Manifest unter
android:foregroundServiceTypeshortService- Berechtigung, die in Ihrem Manifest deklariert werden muss
- Keine
- Konstante, die an
startForeground()übergeben werden soll FOREGROUND_SERVICE_TYPE_SHORT_SERVICE- Laufzeitvoraussetzungen
- Keine
- Beschreibung
Sie können wichtige Aufgaben, die nicht unterbrochen oder verschoben werden können, schnell erledigen.
Dieser Typ hat einige einzigartige Eigenschaften:
- Kann nur für einen kurzen Zeitraum (ca. 3 Minuten) ausgeführt werden.
- Keine Unterstützung für persistente Dienste im Vordergrund.
- Andere Dienste im Vordergrund können nicht gestartet werden.
- Erfordert keine typspezifische Berechtigung, aber die Berechtigung
FOREGROUND_SERVICE. - Ein
shortServicekann nur zu einem anderen Diensttyp wechseln, wenn die App derzeit berechtigt ist, einen neuen Dienst im Vordergrund zu starten. - Ein Dienst im Vordergrund kann seinen Typ jederzeit in
shortServiceändern. Ab diesem Zeitpunkt beginnt die Zeitüberschreitung.
Das Zeitlimit für shortService beginnt, sobald
Service.startForeground()aufgerufen wird. Die App sollteService.stopSelf()oderService.stopForeground()aufrufen, bevor die Zeitüberschreitung eintritt. Andernfalls wird der neueService.onTimeout()aufgerufen, wodurch Apps kurzzeitig die Möglichkeit haben,stopSelf()oderstopForeground()aufzurufen, um ihren Dienst zu beenden.Kurz nach dem Aufruf von
Service.onTimeout()wechselt die App in den Cache-Status und wird nicht mehr als App im Vordergrund betrachtet, es sei denn, der Nutzer interagiert aktiv mit der App. Kurz nachdem die App im Cache gespeichert wurde und der Dienst nicht beendet wurde, erhält die App eine ANR. In der ANR-Nachricht wirdFOREGROUND_SERVICE_TYPE_SHORT_SERVICEerwähnt. Aus diesen Gründen wird empfohlen, denService.onTimeout()-Callback zu implementieren.Der
Service.onTimeout()-Callback ist unter Android 13 und niedriger nicht vorhanden. Wenn derselbe Dienst auf solchen Geräten ausgeführt wird, erhält er weder ein Zeitlimit noch eine ANR. Der Dienst muss beendet werden, sobald die Verarbeitungsaufgabe abgeschlossen ist, auch wenn derService.onTimeout()-Callback noch nicht empfangen wurde.Wenn das Zeitlimit der
shortServicenicht eingehalten wird, wird für die App ein ANR ausgegeben, auch wenn andere gültige Dienste im Vordergrund oder andere App-Lebenszyklus-Prozesse ausgeführt werden.Wenn eine App für den Nutzer sichtbar ist oder eine der Ausnahmen erfüllt, die es ermöglichen, Dienste im Vordergrund aus dem Hintergrund zu starten, wird die Zeitüberschreitung durch einen erneuten Aufruf von
Service.StartForeground()mit dem ParameterFOREGROUND_SERVICE_TYPE_SHORT_SERVICEum weitere drei Minuten verlängert. Wenn die App für den Nutzer nicht sichtbar ist und keinen der Ausnahmen erfüllt, führt jeder Versuch, einen weiteren Dienst im Vordergrund zu starten, unabhängig vom Typ, zu einerForegroundServiceStartNotAllowedException.Wenn ein Nutzer die Akku-Optimierung für Ihre App deaktiviert, ist sie weiterhin vom Zeitlimit für shortService-FGS betroffen.
Wenn Sie einen Dienst im Vordergrund starten, der den Typ
shortServiceund einen anderen Typ von Dienst im Vordergrund enthält, wird die Deklaration des TypsshortServicevom System ignoriert. Der Dienst muss jedoch weiterhin die Voraussetzungen der anderen angegebenen Typen erfüllen. Weitere Informationen finden Sie in der Dokumentation zu Diensten im Vordergrund.
Spezielle Anwendungsfälle
- Foreground service type to declare in manifest under
android:foregroundServiceTypespecialUse- Permission to declare in your manifest
FOREGROUND_SERVICE_SPECIAL_USE- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_SPECIAL_USE- Runtime prerequisites
- None
- Description
Covers any valid foreground service use cases that aren't covered by the other foreground service types.
In addition to declaring the
FOREGROUND_SERVICE_TYPE_SPECIAL_USEforeground service type, developers should declare use cases in the manifest. To do so, they specify the<property>element within the<service>element. These values and corresponding use cases are reviewed when you submit your app in the Google Play Console. The use cases you provide are free-form, and you should make sure to provide enough information to let the reviewer see why you need to use thespecialUsetype.<service android:name="fooService" android:foregroundServiceType="specialUse"> <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="explanation_for_special_use"/> </service>
Vom System ausgenommen
- Foreground service type to declare in manifest under
android:foregroundServiceTypesystemExempted- Permission to declare in your manifest
FOREGROUND_SERVICE_SYSTEM_EXEMPTED- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED- Runtime prerequisites
- None
- Description
Reserved for system applications and specific system integrations, to continue to use foreground services.
To use this type, an app must meet at least one of the following criteria:
- Device is in demo mode state
- App is a Device Owner
- App is a Profiler Owner
- Safety Apps that have the
ROLE_EMERGENCYrole - Device Admin apps
- Apps holding
SCHEDULE_EXACT_ALARMorUSE_EXACT_ALARMpermission and are using Foreground Service to continue alarms in the background, including haptics-only alarms. VPN apps (configured using Settings > Network & Internet > VPN)
Otherwise, declaring this type causes the system to throw a
ForegroundServiceTypeNotAllowedException.
Google Play-Richtlinienerzwingung für die Verwendung von Typen von Diensten im Vordergrund
Wenn Ihre App auf Android 14 oder höher ausgerichtet ist, müssen Sie die Typen der Dienste im Vordergrund Ihrer App auf der Seite „App-Inhalte“ in der Play Console (Richtlinie > App-Inhalte) deklarieren. Weitere Informationen dazu, wie Sie die Typen Ihrer Dienste im Vordergrund in der Play Console deklarieren, finden Sie unter Anforderungen für Dienste im Vordergrund und Full-Screen Intents.