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
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ı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
android:foregroundServiceTypealtında manifest'de beyan edilecek ön plan hizmet türücamera- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_CAMERAstartForeground()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:foregroundServiceTypeconnectedDevice- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_CONNECTED_DEVICEstartForeground()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
- 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
- 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:
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
locationforeground service while your app is in the background, unless you've been granted theACCESS_BACKGROUND_LOCATIONruntime 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
- 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.
Medya projeksiyonu
- Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceTypemediaProjection- Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_MEDIA_PROJECTIONstartForeground()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
MediaProjectionAPI'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
- 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
- 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.
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
- 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 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 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.