Ab Android 14 (API-Ebene 34) müssen Sie für jeden Dienst im Vordergrund einen geeigneten Diensttyp deklarieren. Das bedeutet, dass Sie den Diensttyp in Ihrem App-Manifest deklarieren und die entsprechende Berechtigung für Dienste im Vordergrund für diesen Typ anfordern müssen (zusätzlich zur Anforderung der Berechtigung FOREGROUND_SERVICE). Außerdem müssen Sie je nach Diensttyp möglicherweise Laufzeitberechtigungen anfordern, bevor Sie den Dienst starten.
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 permission- 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
- Foreground service type to declare in manifest under
android:foregroundServiceTypedataSync- Permission to declare in your manifest
FOREGROUND_SERVICE_DATA_SYNC- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_DATA_SYNC- Runtime prerequisites
- None
- Description
Data transfer operations, such as the following:
- Data upload or download
- Backup-and-restore operations
- Import or export operations
- Fetch data
- Local file processing
- Transfer data between a device and the cloud over a network
- Alternatives
See Alternatives to data sync foreground services for detailed information.
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 35 and lowerREAD_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
- 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:
- 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.
Medien werden verarbeitet.
- Foreground service type to declare in manifest under
android:foregroundServiceTypemediaProcessing- Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PROCESSING- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING- Runtime prerequisites
- None
- Description
Service for performing time-consuming operations on media assets, like converting media to different formats. The system allows this service a limited time to run; under normal circumstances, this time limit would be 6 hours out of every 24. (This limit is shared by all of an app's
mediaProcessingforeground services.)Your app should manually stop the media processing service in the following scenario:
- When the transcoding operation finishes or reaches a failure state, have the
service call
Service.stopForeground()andService.stopSelf()to stop the service completely.
- When the transcoding operation finishes or reaches a failure state, have the
service call
If the timeout period is reached, the system calls the service's
Service.onTimeout(int, int)method. At this time, the service has a few seconds to callService.stopSelf(). If the service does not callService.stopSelf(), an ANR will occur with this error message: "A foreground service of <fgs_type> did not stop within its timeout: <component_name>".Note:
Service.onTimeout(int, int)is not available on Android 14 or lower. On devices running those versions, if a media processing service reaches the timeout period, the system immediately caches the app. For this reason, your app shouldn't wait to get a timeout notification. Instead, it should terminate the foreground service or change it to a background service as soon as appropriate.
Medienprojektion
- Foreground service type to declare in manifest under
android:foregroundServiceTypemediaProjection- Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PROJECTION- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION- Runtime prerequisites
Call the
createScreenCaptureIntent()method before starting the foreground service. Doing so shows a permission notification to the user; the user must grant the permission before you can create the service.After you have created the foreground service, you can call
MediaProjectionManager.getMediaProjection().- Description
Project content to non-primary display or external device using the
MediaProjectionAPIs. This content doesn't have to be exclusively media content.- Alternatives
To stream media to another device, use the 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.- 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
- Typ des Dienstes im Vordergrund, der im Manifest unter
android:foregroundServiceTyperemoteMessaging- Berechtigung, die in Ihrem Manifest deklariert werden muss
FOREGROUND_SERVICE_REMOTE_MESSAGING- Konstante, die an
startForeground()übergeben werden soll FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- Laufzeitvoraussetzungen
- Keine
- Beschreibung
- Sie können Nachrichten von einem Gerät auf ein anderes übertragen. Unterstützt Nutzer beim Wechseln des Geräts, ohne dass die Messaging-Aufgaben weiter ausgeführt werden.
Kurzer Dienst
- 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 VPN apps (configured using Settings > Network & Internet > VPN)
Otherwise, declaring this type causes the system to throw a
ForegroundServiceTypeNotAllowedException.
Durchsetzung der Google Play-Richtlinien für die Verwendung von Diensttypen 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.