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.
Ön plan hizmeti türlerini kullanmak için yeni izin bildir
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.
Çalışma zamanında ön plan hizmet türünü ekleme
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.
Sistem çalışma zamanı kontrolleri
Sistem, ön plan hizmeti türlerinin uygun şekilde kullanılıp kullanılmadığını kontrol eder ve uygulamanın, çalışma zamanında doğru izinleri istediğini veya gerekli API'leri kullandığını onaylar.
Örneğin, sistem, FOREGROUND_SERVICE_TYPE_LOCATION ön plan hizmet türünü kullanan uygulamaların ACCESS_COARSE_LOCATION veya ACCESS_FINE_LOCATION iznini istemesini bekler.
Bu, uygulamaların kullanıcıdan izin isterken ve ön plan hizmetlerini başlatırken çok özel bir işlem sırasını izlemesi gerektiği anlamına gelir. İzinler, uygulama startForeground() çağrısına başlamadan önce istenmeli ve verilmelidir. Ön plan hizmeti başlatıldıktan sonra uygun izinleri isteyen uygulamalar bu işlem sırasını değiştirmeli ve ön plan hizmetini başlatmadan önce izin istemelidir.
Platform yaptırımıyla ilgili ayrıntılar, bu sayfadaki her ön plan hizmeti türü için amaçlanan kullanım alanları ve yaptırım bölümündeki "çalışma zamanı gereksinimleri" etiketli bölümlerde yer alır.
Her ön plan hizmet türü için amaçlanan kullanım alanları ve yaptırım
Belirli bir ön plan hizmet türünü kullanmak için manifest dosyanızda belirli bir izni beyan etmeniz, belirli çalışma zamanı koşullarını karşılamanız ve uygulamanızın, söz konusu tür için amaçlanan kullanım alanı gruplarından birini karşılaması gerekir. Aşağıdaki bölümlerde, beyan etmeniz gereken izin, çalışma zamanı ön koşulları ve her türün amaçlanan kullanım alanları açıklanmaktadır.
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.
Bağlı cihaz
- Foreground service type to declare in manifest under
android:foregroundServiceTypeconnectedDevice- Permission to declare in your manifest
FOREGROUND_SERVICE_CONNECTED_DEVICE- Constant to pass to
startForeground() FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE- Runtime prerequisites
At least one of the following conditions must be true:
Declare at least one of the following permissions in your manifest:
Request and be granted at least one of the following runtime permissions:
- Description
Interactions with external devices that require a Bluetooth, NFC, IR, USB, or network connection.
- Alternatives
If your app needs to do continuous data transfer to an external device, consider using the companion device manager instead. Use the companion device presence API to help your app stay running while the companion device is in range.
If your app needs to scan for bluetooth devices, consider using the Bluetooth scan API instead.
Veri senkronizasyonu
- 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.
Sağlık
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypehealth- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_HEALTHstartForeground()işlevine iletilen sabit değerFOREGROUND_SERVICE_TYPE_HEALTH- Çalışma zamanı ön koşulları
Aşağıdaki koşullardan en az biri doğru olmalıdır:
Manifest'inizde
HIGH_SAMPLING_RATE_SENSORSiznini beyan edin.Aşağıdaki çalışma zamanı izinlerinden en az birini talep edin ve alın:
- API düzeyi 35 ve önceki sürümlerde
BODY_SENSORS READ_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
- API düzeyi 35 ve önceki sürümlerde
Not:
BODY_SENSORSve sensör tabanlı OKU çalışma zamanında istenen izinleri, kullanım sırasındaki kısıtlamalara tabidir. Bu nedenle,BODY_SENSORS_BACKGROUND(API düzeyi 33 ila 35) veyaREAD_HEALTH_DATA_IN_BACKGROUND(API düzeyi 36 ve üzeri) izinleri verilmedikçe uygulamanız arka plandayken vücut sensörlerini kullanan birhealthön plan hizmeti oluşturamazsınız. Daha fazla bilgi için Kullanımdayken izin gerektiren ön plan hizmetlerini başlatmayla ilgili kısıtlamalar başlıklı makaleyi inceleyin.- Açıklama
Fitness kategorisindeki uygulamaları (ör. egzersiz takipçileri) desteklemek için uzun süredir kullanılan tüm kullanım alanları.
Konum
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypelocation- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_LOCATIONstartForeground()işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_LOCATION- Çalışma zamanı ön koşulları
Kullanıcının konum hizmetlerini etkinleştirmiş olması ve uygulamaya aşağıdaki çalışma zamanında istenen izinlerden en az biri verilmiş olmalıdır:
Not: Kullanıcının konum hizmetlerini etkinleştirip etkinleştirmediğini ve çalışma zamanında izinlere erişim izni verip vermediğini kontrol etmek için
PermissionChecker#checkSelfPermission()Not: Çalışma zamanı konum izinleri, kullanım sırasındaki kısıtlamalara tabidir. Bu nedenle,
ACCESS_BACKGROUND_LOCATIONçalışma zamanında izin verilmedikçe uygulamanız arka plandaykenlocationön plan hizmeti oluşturamazsınız. Daha fazla bilgi için Kullanımdayken izin gerektiren ön plan hizmetlerini başlatmayla ilgili kısıtlamalar başlıklı makaleyi inceleyin.- Açıklama
Navigasyon ve konum paylaşımı gibi konum erişimi gerektiren uzun süreli kullanım alanları.
- Alternatifler
Kullanıcı belirli konumlara ulaştığında uygulamanızın tetiklenmesi gerekiyorsa bunun yerine coğrafi çit API'sini kullanabilirsiniz.
Medya
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypemediaPlayback- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_MEDIA_PLAYBACKstartForeground()işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK- Çalışma zamanı ön koşulları
- Yok
- Açıklama
- Ses veya video oynatmaya arka planda devam edin. Android TV'de Dijital Video Kaydı (DVR) işlevini destekleyin.
- Alternatifler
- Pencere içinde pencere modunda video gösteriyorsanız Pencere içinde pencere modunu kullanın.
Medya projeksiyonu
- 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
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypemicrophone- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_MICROPHONEstartForeground()işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_MICROPHONE- Çalışma zamanı ön koşulları
RECORD_AUDIOçalışma zamanı iznini isteyin ve izni alın.Not:
RECORD_AUDIOçalışma zamanı izni, kullanım sırasındaki kısıtlamalara tabidir. Bu nedenle, birkaç istisna dışında uygulamanız arka plandaykenmicrophoneön plan hizmeti oluşturamazsınız. Daha fazla bilgi için Kullanımdayken izin gerektiren ön plan hizmetlerini başlatmayla ilgili kısıtlamalar başlıklı makaleyi inceleyin.- Açıklama
Ses kaydediciler veya iletişim uygulamaları gibi mikrofon kayıtlarına arka planda devam edin.
Sesli arama
- 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.
Uzaktan mesajlaşma
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTyperemoteMessaging- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_REMOTE_MESSAGINGstartForeground()işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- Çalışma zamanı ön koşulları
- Yok
- Açıklama
- Kısa mesajları bir cihazdan diğerine aktarın. Cihaz değiştiren kullanıcıların mesajlaşma görevlerine devam etmesine yardımcı olun.
Kısa servis
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypeshortService- Manifest dosyanızda beyan etme izni
- Yok
startForeground()işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_SHORT_SERVICE- Çalışma zamanı ön koşulları
- Yok
- Açıklama
Kesintiye uğratılamayan veya ertelenemeyecek kritik işleri hızlıca tamamlayın.
Bu türün bazı benzersiz özellikleri vardır:
- Yalnızca kısa bir süre (yaklaşık 3 dakika) çalışabilir.
- Yapışkan ön plan hizmetleri desteklenmez.
- Diğer ön plan hizmetleri başlatılamaz.
- Türe özgü izin gerektirmez ancak
FOREGROUND_SERVICEiznini gerektirir. shortServiceyalnızca uygulama şu anda yeni bir ön plan hizmeti başlatmaya uygunsa başka bir hizmet türüne geçirilebilir.- Ön plan hizmetleri, türlerini istedikleri zaman
shortServiceolarak değiştirebilir. Bu durumda zaman aşımı süresi başlar.
shortService için zaman aşımı,
Service.startForeground()çağrıldığı andan itibaren başlar. Uygulamanın, zaman aşımı gerçekleşmeden önceService.stopSelf()veyaService.stopForeground()çağrısını yapması beklenir. Aksi takdirde yeniService.onTimeout()çağrılır ve uygulamalara, hizmetlerini durdurmak içinstopSelf()veyastopForeground()'yi aramak üzere kısa bir fırsat verilir.Service.onTimeout()çağrıldıktan kısa bir süre sonra uygulama önbelleğe alınmış bir duruma girer ve kullanıcı uygulamayla etkin bir şekilde etkileşimde bulunmadığı sürece artık ön planda olduğu kabul edilmez. Uygulama önbelleğe alındıktan ve hizmet durdurulmadıktan kısa bir süre sonra ANR alır. ANR mesajındaFOREGROUND_SERVICE_TYPE_SHORT_SERVICEgeçiyor. Bu nedenlerden dolayı,Service.onTimeout()geri çağırma işlevini uygulamak en iyi uygulama olarak kabul edilir.Service.onTimeout()geri arama işlevi Android 13 ve önceki sürümlerde mevcut değildir. Aynı hizmet bu tür cihazlarda çalışıyorsa zaman aşımı veya ANR almaz. İşleme görevini tamamlar tamamlamaz, henüzService.onTimeout()geri çağırma almamış olsa bile hizmetinizin durmasını sağlayın.shortServicezaman aşımına uyulmaması durumunda, geçerli başka ön plan hizmetleri veya çalışan başka uygulama yaşam döngüsü süreçleri olsa bile uygulamanın ANR'ye neden olacağını unutmayın.Bir uygulama kullanıcı tarafından görülebilirse veya ön plan hizmetlerinin arka plandan başlatılmasına izin veren istisnalardan birini karşılıyorsa
Service.StartForeground()işleviFOREGROUND_SERVICE_TYPE_SHORT_SERVICEparametresiyle tekrar çağrılırsa zaman aşımı 3 dakika daha uzatılır. Uygulama kullanıcı tarafından görülmüyorsa ve istisnalardan birini karşılamıyorsa türünden bağımsız olarak başka bir ön plan hizmeti başlatma girişimiForegroundServiceStartNotAllowedException'a neden olur.Bir kullanıcı uygulamanız için pil optimizasyonunu devre dışı bırakırsa uygulamanız shortService FGS zaman aşımından etkilenmeye devam eder.
shortServicetürünü ve başka bir ön plan hizmet türünü içeren bir ön plan hizmeti başlatırsanız sistem,shortServicetürü bildirimini yoksayar. Ancak hizmet, beyan edilen diğer türlerin ön koşullarına uymaya devam etmelidir. Daha fazla bilgi için Ön plan hizmetleri belgelerine bakın.
Özel kullanım
- 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 muaf
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypesystemExempted- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_SYSTEM_EXEMPTEDstartForeground()işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED- Çalışma zamanı ön koşulları
- Yok
- Açıklama
Ön plan hizmetlerini kullanmaya devam etmek için sistem uygulamaları ve belirli sistem entegrasyonları için ayrılmıştır.
Bu türü kullanmak isteyen uygulamaların aşağıdaki ölçütlerden en az birini karşılaması gerekir:
- Cihaz demo modunda
- Uygulama, cihaz sahibidir.
- Uygulama, Profil Oluşturucu Sahibi
ROLE_EMERGENCYrolüne sahip güvenlik uygulamaları- Cihaz Yöneticisi uygulamaları
SCHEDULE_EXACT_ALARMveyaUSE_EXACT_ALARMiznine sahip olan ve yalnızca dokunma alarmları da dahil olmak üzere alarmları arka planda devam ettirmek için ön plan hizmetini kullanan uygulamalar.VPN uygulamaları (Ayarlar > Ağ ve İnternet > VPN kullanılarak yapılandırılır)
Aksi takdirde, bu türün tanımlanması sistemin
ForegroundServiceTypeNotAllowedExceptionhatası vermesine neden olur.
Ön plan hizmet türlerinin kullanımıyla ilgili Google Play politika yaptırımı
Uygulamanız Android 14 veya sonraki sürümleri hedefliyorsa uygulamanızın ön plan hizmet türlerini Play Console'un uygulama içeriği sayfasında (Politika > Uygulama içeriği) belirtmeniz gerekir. Play Console'da ön plan hizmet türlerinizi nasıl beyan edeceğiniz hakkında daha fazla bilgi için Ön plan hizmetini ve tam ekran intent şartlarını anlama başlıklı makaleyi inceleyin.