Ön plan hizmeti türleri

Android 14'ten (API düzeyi 34) itibaren her ön plan hizmeti için uygun bir hizmet türü belirtmeniz gerekir. Bu nedenle, uygulama manifest dosyanızda hizmet türünü belirtmeniz ve FOREGROUND_SERVICE iznini istemenin yanı sıra bu tür için uygun ön plan hizmet iznini de istemeniz gerekir. Ayrıca, ön plan hizmet türüne bağlı olarak hizmeti başlatmadan önce çalışma zamanında istenen izinleri istemeniz gerekir.

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 CAMERA runtime permission

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:foregroundServiceType
connectedDevice
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:

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

Manifest dosyasında tanımlanacak ön plan hizmet türü
android:foregroundServiceType
dataSync
Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_DATA_SYNC
startForeground() noktasına ulaşılmasına kalan süre
FOREGROUND_SERVICE_TYPE_DATA_SYNC
Çalışma zamanı ön koşulları
Yok
Açıklama

Aşağıdakiler gibi veri aktarımı 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

Veri senkronizasyonu ön plan hizmetlerine alternatifler bölümüne bakın inceleyebilirsiniz.

Sağlık

Manifest dosyasında aşağıdaki altında beyan edilecek ön plan hizmet türü
android:foregroundServiceType
health
Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_HEALTH
startForeground() işlevine iletilecek sabit değer
FOREGROUND_SERVICE_TYPE_HEALTH
Çalışma zamanı ön koşulları

Aşağıdaki koşullardan en az biri doğru olmalıdır:

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:foregroundServiceType
location
Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_LOCATION
startForeground() işlevine iletilecek sabit değer
FOREGROUND_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 izinlerden en az biri verilmiş olmalıdır:

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

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.

Medya işleniyor

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:

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 call Service.stopSelf(). If the service does not call Service.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.

Medya projeksiyonu

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

Manifest dosyasında tanımlanacak ön plan hizmet türü
android:foregroundServiceType
microphone
Manifest'inizde beyan etme izni
FOREGROUND_SERVICE_MICROPHONE
startForeground() işlevine iletilecek sabit değer
FOREGROUND_SERVICE_TYPE_MICROPHONE
Çalışma zamanı ön koşulları

RECORD_AUDIO çalışma zamanı iznini isteyin ve izni alın.

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: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 is the default dialer app through the ROLE_DIALER role.
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

Manifestte bildirilecek ön plan hizmeti türü
android:foregroundServiceType
remoteMessaging
Manifest dosyanızda beyan etme izni
FOREGROUND_SERVICE_REMOTE_MESSAGING
startForeground() için sabit geçiş
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
Çalışma zamanı ön koşulları
Hiçbiri
Açıklama
Kısa mesajları bir cihazdan diğerine aktarın. Cihaz değiştirdiğinde kullanıcıların mesajlaşma görevlerinin devam etmesine yardımcı olur.

Kısa servis

Manifestte bildirilecek ön plan hizmeti türü
android:foregroundServiceType
shortService
Manifest dosyanızda beyan etme izni
Hiçbiri
startForeground() için sabit geçiş
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
Çalışma zamanı ön koşulları
Hiçbiri
Açıklama

Kesilemeyen veya ertelenemeyen kritik işleri hızla bitirin.

Bu türün bazı benzersiz özellikleri vardır:

  • Yalnızca kısa bir süre (yaklaşık 3 dakika) için çalıştırılabilir.
  • Yapışkan ön plan hizmetleri için destek sunulmaz.
  • Diğer ön plan hizmetleri başlatılamaz.
  • Türe özel izin gerektirmez ancak yine de FOREGROUND_SERVICE iznini gerektirir.
  • shortService öğesinin başka bir hizmet türüne geçmesi, yalnızca uygulamanın şu anda yeni bir ön plan hizmeti başlatmaya uygun olması durumunda mümkündür.
  • Bir ön plan hizmeti, türünü istediği zaman shortService olarak değiştirebilir. Bu noktada zaman aşımı süresi başlar.

ShortService için zaman aşımı Service.startForeground() hizmetinin çağrıldığı anda başlar. Uygulamanın, zaman aşımı gerçekleşmeden önce Service.stopSelf() veya Service.stopForeground() yöntemini çağırması beklenir. Aksi takdirde, yeni Service.onTimeout() çağrılır; bu da uygulamalara, hizmetlerini durdurmaları için kısa bir süre içinde stopSelf() veya stopForeground() numarasını arama fırsatı verir.

Service.onTimeout() çağrıldıktan kısa bir süre sonra uygulama önbelleğe alınmış duruma girer ve kullanıcı etkin bir şekilde uygulamayla etkileşimde bulunmadığı sürece artık ön planda kabul edilmez. Uygulama önbelleğe alındıktan ve hizmet durdurulmadan kısa bir süre sonra uygulama bir ANR alır. ANR mesajında FOREGROUND_SERVICE_TYPE_SHORT_SERVICE bahsediliyor. Bu nedenlerle, Service.onTimeout() geri çağırmanın uygulanması en iyi uygulama olarak kabul edilir.

Service.onTimeout() geri çağırma özelliği Android 13 ve önceki sürümlerde mevcut değildir. Aynı hizmet bu tür cihazlarda çalışıyorsa zaman aşımı olmaz ve ANR de yaşanmaz. Hizmetinizin işleme görevini tamamlar tamamlamaz (Service.onTimeout() geri çağırma işlemini henüz almamış olsa bile) durduğundan emin olun.

shortService zaman aşımına uyulmadığı takdirde, başka geçerli ön plan hizmetleri veya çalışan başka uygulama yaşam döngüsü süreçleri olsa bile uygulamanın ANR oluşturacağını unutmayın.

Bir uygulama kullanıcı tarafından görülebiliyorsa veya ön plan hizmetlerinin arka plandan başlatılmasına izin veren muafiyetlerden birini karşılıyorsa FOREGROUND_SERVICE_TYPE_SHORT_SERVICE parametresiyle Service.StartForeground() öğesinin tekrar çağrılması zaman aşımını 3 dakika daha uzatır. Uygulama kullanıcı tarafından görülmüyorsa ve muafiyetlerden birini karşılamıyorsa türden bağımsız olarak başka bir ön plan hizmeti başlatma girişimi ForegroundServiceStartNotAllowedException hatasına neden olur.

Bir kullanıcı uygulamanız için pil optimizasyonunu devre dışı bıraksa bile, shortService ön plan hizmetlerini devre dışı bırakan uygulama, yine de kısa süreli bir çözüm olarak kullanılır.

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ü beyanını yoksayar. Ancak hizmetin yine de bildirilen diğer türlerin ön koşullarına uyması gerekir. Daha fazla bilgi için Ön plan hizmetleri belgelerine bakın.

Özel kullanım

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 the specialUse 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>

Sistem 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:

Ön plan hizmet türlerinin kullanımıyla ilgili Google Play politika yaptırımı

If your app targets Android 14 or higher, you'll need to declare your app's foreground service types in the Play Console's app content page (Policy > App content). For more information on how to declare your foreground service types in Play Console, see Understanding foreground service and full-screen intent requirements.