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:
camera
connectedDevice
dataSync
health
location
mediaPlayback
mediaProjection
microphone
phoneCall
remoteMessaging
shortService
specialUse
systemExempted
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 hizmet türlerini kullanmak için yeni izin bildirme
Android 14'ü hedefleyen uygulamalarda bir ön plan hizmeti kullanılacaksa Android 14'te kullanıma sunulan, ön plan hizmet türüne bağlı belirli bir izni belirtmeleri gerekir. Bu izinler, bu sayfadaki her ön plan hizmeti türü için amaçlanan kullanım alanları ve yaptırım bölümündeki "manifest dosyanızda beyan etmeniz gereken izin" etiketli bölümlerde görünür.
Tüm izinler normal izinler olarak tanımlanır ve varsayılan olarak verilir. Kullanıcılar bu izinleri iptal edemez.
Çalışma zamanında ön plan hizmet türünü dahil et
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
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.
Her ön plan hizmet türü için amaçlanan kullanım alanları ve yaptırımlar
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
android:foregroundServiceType
altında manifest'de beyan edilecek ön plan hizmet türücamera
- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_CAMERA
startForeground()
işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_CAMERA
- Çalışma zamanı ön koşulları
CAMERA
çalışma zamanı iznini isteyip alınNot:
CAMERA
çalışma zamanı izni, kullanım sırasındaki kısıtlamalara tabidir. Bu nedenle, birkaç istisna dışında uygulamanız arka plandaykencamera
ö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
Çoklu görev gerçekleştirmeye izin veren görüntülü sohbet uygulamaları gibi kameraya arka planda erişmeye devam edin.
Bağlı cihaz
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceType
connectedDevice
- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_CONNECTED_DEVICE
startForeground()
işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
- Çalışma zamanı ön koşulları
Aşağıdaki koşullardan en az biri doğru olmalıdır:
Manifest dosyanızda aşağıdaki izinlerden en az birini belirtin:
Aşağıdaki çalışma zamanı izinlerinden en az birini talep edin ve alın:
UsbManager.requestPermission()
numaralı telefonu arayın
- Açıklama
Bluetooth, NFC, IR, USB veya ağ bağlantısı gerektiren harici cihazlarla olan etkileşimler.
- Alternatifler
Uygulamanızın harici bir cihaza sürekli veri aktarması gerekiyorsa bunun yerine companion device manager'ı kullanabilirsiniz. Tamamlayıcı cihaz kapsam içindeyken uygulamanızın çalışmaya devam etmesine yardımcı olmak için tamamlayıcı cihaz varlığı API'sini kullanın.
Uygulamanızın Bluetooth cihazları taraması gerekiyorsa bunun yerine Bluetooth tarama API'sini kullanabilirsiniz.
Veri senkronizasyonu
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceType
dataSync
- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_DATA_SYNC
startForeground()
işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_DATA_SYNC
- Çalışma zamanı ön koşulları
- Yok
- Açıklama
Aşağıdakiler gibi veri aktarma işlemleri:
- Veri yükleme veya indirme
- Yedekleme ve geri yükleme işlemleri
- İçe veya dışa aktarma işlemleri
- Veri getirme
- Yerel dosya işleme
- Ağ üzerinden bir cihaz ile bulut arasında veri aktarma
- Alternatifler
Ayrıntılı bilgi için Veri senkronizasyonu ön plan hizmetlerine alternatifler başlıklı makaleyi inceleyin.
Sağlık
- Foreground service type to declare in manifest under
android:foregroundServiceType
health
- 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_SENSORS
permission in your manifest.Request and be granted at least one of the following runtime permissions:
Note: The
BODY_SENSORS
runtime permission is subject to while-in-use restrictions. For this reason, you cannot create ahealth
foreground service that uses body sensors 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
Any long-running use cases to support apps in the fitness category such as exercise trackers.
Konum
- 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:
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
location
foreground service while your app is in the background, unless you've been granted theACCESS_BACKGROUND_LOCATION
runtime 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.
Medya
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceType
mediaPlayback
- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_MEDIA_PLAYBACK
startForeground()
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
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceType
mediaProjection
- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_MEDIA_PROJECTION
startForeground()
işlevine iletilecek sabit değerFOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
- Çalışma zamanı ön koşulları
Ön plan hizmetini başlatmadan önce
createScreenCaptureIntent()
yöntemini çağırın. Bu işlem, kullanıcıya bir izin bildirimi gösterir. Hizmeti oluşturabilmeniz için kullanıcının izin vermesi gerekir.Ön plan hizmetini oluşturduktan sonra
MediaProjectionManager.getMediaProjection()
işlevini çağırabilirsiniz.- Açıklama
MediaProjection
API'lerini kullanarak içeriği birincil olmayan ekrana veya harici cihaza yansıtın. Bu içeriğin yalnızca medya içeriği olması gerekmez.- Alternatifler
Medyayı başka bir cihaza aktarmak için Google Cast SDK'sını kullanın.
Mikrofon
- Foreground service type to declare in manifest under
android:foregroundServiceType
microphone
- 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_AUDIO
runtime permission.Note: The
RECORD_AUDIO
runtime permission is subject to while-in-use restrictions. For this reason, you cannot create amicrophone
foreground 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.
Sesli arama
- 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.
Uzaktan mesajlaşma
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceType
remoteMessaging
- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_REMOTE_MESSAGING
startForeground()
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:foregroundServiceType
shortService
- 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_SERVICE
iznini gerektirir. shortService
yalnı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
shortService
olarak 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_SERVICE
geç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.shortService
zaman 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_SERVICE
parametresiyle 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.
shortService
türünü ve başka bir ön plan hizmet türünü içeren bir ön plan hizmeti başlatırsanız sistem,shortService
tü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
- Manifest dosyasında tanımlanacak ön plan hizmet türü
android:foregroundServiceType
specialUse
- Manifest'inizde beyan etme izni
FOREGROUND_SERVICE_SPECIAL_USE
startForeground()
noktasına ulaşılmasına kalan süreFOREGROUND_SERVICE_TYPE_SPECIAL_USE
- Çalışma zamanı ön koşulları
- Yok
- Açıklama
Diğer ön plan hizmeti kapsamı dışındaki geçerli ön plan hizmeti kullanım alanlarını kapsar. ön plan hizmet türleri için kullanılır.
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
beyana ek olarak, ön plan hizmet türüyle birlikte, geliştiricilerin kullanım alanlarını manifest'ini kullanabilirsiniz. Bunu yapmak için<property>
<service>
öğesi. Bu değerler ve karşılık gelen kullanım alanları şunlardır: incelenir. Kullanım sağladığınız örnekler serbest biçimli olmalıdır ve bunun için yeterli miktarda incelemecininspecialUse
öğesini neden kullanmanız gerektiğini bilmesi için gerekli bilgileri türü.<service android:name="fooService" android:foregroundServiceType="specialUse"> <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="explanation_for_special_use"/> </service>
Sistemden muaf
- 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 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
.
Ön plan hizmeti türlerini kullanmayla 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.