Począwszy od Androida 14 (interfejs API na poziomie 34), musisz zadeklarować odpowiedni typ usługi dla każdej usługi działającej na pierwszym planie. Oznacza to, że musisz zadeklarować typ usługi w pliku manifestu aplikacji, a także poprosić o odpowiednie uprawnienie usługi na pierwszym planie dla tego typu (oprócz prośby o uprawnienie FOREGROUND_SERVICE
). Dodatkowo w zależności od typu usługi na pierwszym planie przed jej uruchomieniem może być konieczne poproszenie o uprawnienia w czasie działania.
Aparat
- 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
CAMERA
runtime permission- Description
Continue to access the camera from the background, such as video chat apps that allow for multitasking.
Połączone urządzenie
- Typ usługi na pierwszym planie do zadeklarowania w pliku manifestu w sekcji
android:foregroundServiceType
connectedDevice
- Uprawnienia do zadeklarowania w pliku manifestu
FOREGROUND_SERVICE_CONNECTED_DEVICE
- stała wartość przekazywana do
startForeground()
, FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
- Wymagania wstępne środowiska wykonawczego
Musi być spełniony co najmniej 1 z tych warunków:
Zadeklaruj w pliku manifestu co najmniej 1 z tych uprawnień:
Wymagane jest co najmniej 1 z tych uprawnień:
Zadzwoń pod numer
UsbManager.requestPermission()
- Opis
Dotyczy to interakcji z urządzeniami zewnętrznymi, które wymagają łączności Bluetooth, NFC, podczerwieni, USB lub połączenia sieciowego.
- Alternatywy
Jeśli aplikacja musi ciągle przesyłać dane na urządzenie zewnętrzne, rozważ użycie menedżera urządzenia towarzyszącego. Użyj interfejsu API obecności urządzenia towarzyszącego, aby aplikacja działała, gdy urządzenie towarzyszące znajduje się w zasięgu.
Jeśli Twoja aplikacja musi skanować urządzenia Bluetooth, rozważ użycie interfejsu API skanowania Bluetooth.
Synchronizowanie danych
- Foreground service type to declare in manifest under
android:foregroundServiceType
dataSync
- 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.
Stan
- Typ usługi na pierwszym planie do zadeklarowania w pliku manifestu w sekcji
android:foregroundServiceType
health
- Uprawnienia do zadeklarowania w pliku manifestu
FOREGROUND_SERVICE_HEALTH
- Stała wartość przekazywana do funkcji
startForeground()
FOREGROUND_SERVICE_TYPE_HEALTH
- Wymagania wstępne środowiska wykonawczego
Musi być spełniony co najmniej 1 z tych warunków:
Zadeklaruj uprawnienie
HIGH_SAMPLING_RATE_SENSORS
w pliku manifestu.Wymagane jest co najmniej 1 z tych uprawnień:
BODY_SENSORS
w interfejsie API 35 lub starszymREAD_HEART_RATE
READ_SKIN_TEMPERATURE
READ_OXYGEN_SATURATION
ACTIVITY_RECOGNITION
- Opis
Wszystkie długotrwałe przypadki użycia, które obsługują aplikacje z kategorii fitness, np. monitory aktywności.
Lokalizacja
- Foreground service type to declare in manifest under
android:foregroundServiceType
location
- 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.
Multimedia
- Foreground service type to declare in manifest under
android:foregroundServiceType
mediaPlayback
- 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.
Przetwarzane materiały
- Foreground service type to declare in manifest under
android:foregroundServiceType
mediaProcessing
- 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
mediaProcessing
foreground 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.
Wyświetlanie multimediów
- Foreground service type to declare in manifest under
android:foregroundServiceType
mediaProjection
- 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
MediaProjection
APIs. This content doesn't have to be exclusively media content.- Alternatives
To stream media to another device, use the Google Cast SDK.
Mikrofon
- Typ usługi na pierwszym planie do zadeklarowania w pliku manifestu w sekcji
android:foregroundServiceType
microphone
- Uprawnienia do zadeklarowania w pliku manifestu
FOREGROUND_SERVICE_MICROPHONE
- Stała do przekazania do
startForeground()
FOREGROUND_SERVICE_TYPE_MICROPHONE
- Wymagania wstępne dotyczące środowiska wykonawczego
Poproś o uprawnienie czasu działania
RECORD_AUDIO
i uzyskaj je.- Opis
Dotyczy to ciągłego nagrywania dźwięku w tle, np. w przypadku dyktafonów lub aplikacji do komunikacji.
Rozmowa telefoniczna
- Foreground service type to declare in manifest under
android:foregroundServiceType
phoneCall
- 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_CALLS
permission in its manifest file.
- App has declared the
- App is the default dialer app through the
ROLE_DIALER
role.
- App is the default dialer app through the
- Description
Continue an ongoing call using the
ConnectionService
APIs.- Alternatives
If you need to make phone, video, or VoIP calls, consider using the
android.telecom
library.Consider using
CallScreeningService
to screen calls.
Zdalne przesyłanie wiadomości
- Foreground service type to declare in manifest under
android:foregroundServiceType
remoteMessaging
- 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.
Krótka obsługa
- Typ usługi na pierwszym planie do zadeklarowania w pliku manifestu w sekcji
android:foregroundServiceType
shortService
- Uprawnienia do zadeklarowania w pliku manifestu
- Brak
- stała wartość przekazywana do
startForeground()
, FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
- Wymagania wstępne środowiska wykonawczego
- Brak
- Opis
Szybkie ukończenie ważnych zadań, które nie mogą zostać przerwane ani przełożone.
Ten typ ma kilka unikalnych cech:
- może być uruchomiony tylko przez krótki czas (około 3 minut);
- Nieobsługiwane są przypięte usługi na pierwszym planie.
- Nie można uruchomić innych usług na pierwszym planie.
- Nie wymaga uprawnień związanych z typem, ale nadal wymaga uprawnień
FOREGROUND_SERVICE
. - Usługa
shortService
może zmienić typ usługi na inny tylko wtedy, gdy aplikacja może obecnie uruchomić nową usługę na pierwszym planie. - Usługa na pierwszym planie może w dowolnym momencie zmienić typ na
shortService
, co powoduje rozpoczęcie okresu oczekiwania.
Limit czasu dla krótkiej usługi rozpoczyna się od momentu wywołania funkcji
Service.startForeground()
. Aplikacja powinna wywołać funkcjęService.stopSelf()
lubService.stopForeground()
przed przekroczeniem limitu czasu. W przeciwnym razie wywoływana jest nowa funkcjaService.onTimeout()
, która daje aplikacjom krótką możliwość wywołania funkcjistopSelf()
lubstopForeground()
w celu zatrzymania usługi.Krótko po wywołaniu funkcji
Service.onTimeout()
aplikacja przechodzi w stan buforowania i nie jest już uważana za aplikację na pierwszym planie, chyba że użytkownik aktywnie z nią współpracuje. Krótko po buforowaniu aplikacji, jeśli usługa nie została zatrzymana, aplikacja otrzymuje ANR. Komunikat ANR zawiera wzmiankę oFOREGROUND_SERVICE_TYPE_SHORT_SERVICE
. Z tych powodów sprawdzoną metodą jest implementowanie funkcjiService.onTimeout()
callback.Funkcja
Service.onTimeout()
nie jest dostępna w Androidzie 13 i starszych. Jeśli ta sama usługa działa na takich urządzeniach, nie ma limitu czasu ani nie powoduje ANR. Upewnij się, że usługa zatrzymuje się, gdy tylko zakończy przetwarzanie zadania, nawet jeśli nie otrzymała jeszcze wywołania zwrotnegoService.onTimeout()
.Pamiętaj, że jeśli nie będziesz przestrzegać limitu czasu
shortService
, aplikacja będzie generować błąd ANR, nawet jeśli ma inne prawidłowe usługi na pierwszym planie lub inne procesy cyklu życia aplikacji.Jeśli aplikacja jest widoczna dla użytkownika lub spełnia jedno z wyjątków, które umożliwiają uruchamianie usług na pierwszym planie w tle, ponowne wywołanie funkcji
Service.StartForeground()
z parametremFOREGROUND_SERVICE_TYPE_SHORT_SERVICE
wydłuża czas oczekiwania o kolejne 3 minuty. Jeśli aplikacja jest niewidoczna dla użytkownika i nie spełnia żadnego z wyjątków, każda próba uruchomienia innej usługi na pierwszym planie, niezależnie od jej typu, powoduje błądForegroundServiceStartNotAllowedException
.Jeśli użytkownik wyłączy optymalizację baterii w aplikacji, nadal będzie ona podlegać limitowi czasu krótkiej usługi FGS.
Jeśli uruchomisz usługę na pierwszym planie, która obejmuje typ
shortService
i inny typ usługi na pierwszym planie, system zignoruje deklarację typushortService
. Usługa musi jednak spełniać wymagania wstępne innych zadeklarowanych typów. Więcej informacji znajdziesz w dokumentacji dotyczącej usług na pierwszym planie.
Specjalne zastosowanie
- Foreground service type to declare in manifest under
android:foregroundServiceType
specialUse
- 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_USE
foreground 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 thespecialUse
type.<service android:name="fooService" android:foregroundServiceType="specialUse"> <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="explanation_for_special_use"/> </service>
Wyjątek systemowy
- Foreground service type to declare in manifest under
android:foregroundServiceType
systemExempted
- 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_EMERGENCY
role - Device Admin apps
- Apps holding
SCHEDULE_EXACT_ALARM
orUSE_EXACT_ALARM
permission VPN apps (configured using Settings > Network & Internet > VPN)
Otherwise, declaring this type causes the system to throw a
ForegroundServiceTypeNotAllowedException
.
Egzekwowanie zasad Google Play dotyczących używania typów usług działających na pierwszym planie
Jeśli Twoja aplikacja jest kierowana na Androida 14 lub nowszego, musisz zadeklarować typy usług na pierwszym planie w Konsoli Play na stronie „Zawartość aplikacji” (Zasady > Zawartość aplikacji). Więcej informacji o deklarowaniu typów usług na pierwszym planie w Konsoli Play znajdziesz w artykule Omówienie wymagań dotyczących usług działających na pierwszym planie i intencji pełnoekranowych.