Mulai Android 14 (level API 34), Anda harus mendeklarasikan jenis layanan yang sesuai untuk setiap layanan latar depan. Artinya, Anda harus mendeklarasikan
jenis layanan dalam manifes aplikasi, dan juga meminta izin layanan latar depan yang sesuai untuk jenis tersebut (selain meminta izin
FOREGROUND_SERVICE). Selain itu, bergantung pada jenis layanan latar depan, Anda mungkin harus meminta izin runtime sebelum meluncurkan layanan.
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.
Perangkat yang terhubung
- Jenis layanan latar depan yang akan dideklarasikan dalam manifes di bagian
android:foregroundServiceTypeconnectedDevice- Izin untuk dideklarasikan dalam manifes
FOREGROUND_SERVICE_CONNECTED_DEVICE- Konstanta yang akan diteruskan ke
startForeground() FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE- Prasyarat runtime
Setidaknya salah satu syarat berikut harus terpenuhi:
Deklarasikan minimal satu izin berikut dalam manifes:
Minta dan dapatkan setidaknya salah satu izin runtime berikut:
Panggil
UsbManager.requestPermission()
- Deskripsi
Interaksi dengan perangkat eksternal yang memerlukan Bluetooth, NFC, IR, USB, atau koneksi jaringan.
- Alternatif
Jika aplikasi Anda perlu melakukan transfer data berkelanjutan ke perangkat eksternal, pertimbangkan untuk menggunakan pengelola perangkat pendamping. Gunakan API kehadiran perangkat pendamping untuk membantu aplikasi Anda tetap berjalan saat perangkat pendamping berada dalam jangkauan.
Jika aplikasi Anda perlu memindai perangkat Bluetooth, sebaiknya gunakan Bluetooth scan API.
Sinkronisasi data
- 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.
Kesehatan
- 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.
Lokasi
- 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.
Media
- 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.
Media sedang diproses
- 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.
Proyeksi media
- 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.
Panggilan telepon
- 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.
Pengiriman pesan jarak jauh
- 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.
Layanan singkat
- Jenis layanan latar depan yang akan dideklarasikan dalam manifes di bagian
android:foregroundServiceTypeshortService- Izin untuk dideklarasikan dalam manifes
- Tidak ada
- Konstanta yang akan diteruskan ke
startForeground() FOREGROUND_SERVICE_TYPE_SHORT_SERVICE- Prasyarat runtime
- Tidak ada
- Deskripsi
Menyelesaikan pekerjaan penting dengan cepat, yang tidak dapat disela atau ditunda.
Jenis ini memiliki beberapa karakteristik unik:
- Hanya dapat dijalankan dalam waktu singkat (sekitar 3 menit).
- Tidak ada dukungan untuk layanan latar depan melekat.
- Tidak dapat memulai layanan latar depan lainnya.
- Tidak memerlukan izin khusus jenis, meskipun masih
memerlukan izin
FOREGROUND_SERVICE. shortServicehanya dapat berubah menjadi jenis layanan lain jika aplikasi saat ini memenuhi syarat untuk memulai layanan latar depan baru.- Layanan latar depan dapat mengubah jenisnya menjadi
shortServicekapan saja, dan pada saat itulah periode waktu tunggu dimulai.
Waktu tunggu untuk shortService dimulai sejak
Service.startForeground()dipanggil. Aplikasi diharapkan memanggilService.stopSelf()atauService.stopForeground()sebelum waktu tunggu habis. Jika tidak,Service.onTimeout()baru akan dipanggil dan memberi aplikasi kesempatan singkat untuk memanggilstopSelf()ataustopForeground()guna menghentikan layanannya.Tidak lama setelah
Service.onTimeout()dipanggil, aplikasi akan memasuki status cache dan tidak lagi dianggap berada di latar depan, kecuali jika pengguna berinteraksi secara aktif dengan aplikasi. Tidak lama setelah aplikasi di-cache dan layanan tidak berhenti, aplikasi akan menerima ANR. Pesan ANR menyebutkanFOREGROUND_SERVICE_TYPE_SHORT_SERVICE. Karena alasan ini, mengimplementasikan callbackService.onTimeout()dianggap sebagai praktik terbaik.Callback
Service.onTimeout()tidak ada di Android 13 dan yang lebih rendah. Jika layanan yang sama berjalan di perangkat tersebut, layanan tidak akan mengalami waktu tunggu, begitu juga ANR. Pastikan layanan Anda berhenti begitu selesai memproses tugas, meskipun belum menerima callbackService.onTimeout().Penting untuk diperhatikan bahwa jika waktu tunggu
shortServicetidak dipatuhi, aplikasi akan ANR meskipun memiliki layanan latar depan lain yang valid atau proses siklus proses aplikasi lainnya yang berjalan.Jika aplikasi dapat dilihat oleh pengguna atau memenuhi salah satupengecualian yang memungkinkan layanan latar depan dimulai dari latar belakang, memanggil
Service.StartForeground()sekali lagi dengan parameterFOREGROUND_SERVICE_TYPE_SHORT_SERVICEakan memperpanjang waktu tunggu 3 menit lagi. Jika aplikasi tidak terlihat oleh pengguna dan tidak memenuhi salah satu pengecualian, setiap upaya untuk memulai layanan latar depan lainnya, apa pun jenisnya, akan menyebabkanForegroundServiceStartNotAllowedException.Jika pengguna menonaktifkan pengoptimalan baterai untuk aplikasi Anda, aplikasi tersebut masih terpengaruh oleh waktu tunggu shortService FGS.
Jika Anda memulai layanan latar depan yang menyertakan jenis
shortServicedan jenis layanan latar depan lainnya, sistem akan mengabaikan deklarasi jenisshortService. Namun, layanan tetap harus mematuhi prasyarat jenis lain yang dideklarasikan. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi layanan latar depan.
Penggunaan khusus
- 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>
Sistem dikecualikan
- Jenis layanan latar depan untuk dinyatakan dalam manifes di bawah
android:foregroundServiceTypesystemExempted- Izin untuk dideklarasikan dalam manifes Anda
FOREGROUND_SERVICE_SYSTEM_EXEMPTED- Konstanta untuk diteruskan ke
startForeground() FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED- Prasyarat runtime
- Tidak ada
- Deskripsi
Ditujukan bagi aplikasi sistem dan integrasi sistem tertentu, untuk terus menggunakan layanan latar depan.
Untuk menggunakan jenis ini, aplikasi harus memenuhi setidaknya salah satu kriteria berikut:
- Perangkat dalam status mode demo
- Aplikasi adalah Pemilik Perangkat
- Aplikasi adalah Pemilik Profiler
- Aplikasi Keselamatan yang memiliki peran
ROLE_EMERGENCY - Aplikasi Admin Perangkat
- Aplikasi yang memiliki izin
SCHEDULE_EXACT_ALARMatauUSE_EXACT_ALARM Aplikasi VPN (dikonfigurasi menggunakan Setelan > Jaringan & Internet > VPN)
Jika tidak, mendeklarasikan jenis ini akan menyebabkan sistem menampilkan
ForegroundServiceTypeNotAllowedException.
Penegakan kebijakan Google Play untuk menggunakan jenis layanan latar depan
Jika aplikasi menargetkan Android 14 atau yang lebih baru, Anda harus menyatakan jenis layanan latar depan aplikasi di halaman konten aplikasi Konsol Play (Kebijakan > Konten aplikasi). Untuk informasi selengkapnya tentang cara mendeklarasikan jenis layanan latar depan di Konsol Play, lihat Memahami persyaratan layanan latar depan dan intent layar penuh.