Ön plan hizmetlerini beyan etme ve izin isteme
Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
Uygulamanızın manifest dosyasında, uygulamanızın her ön plan hizmetini <service>
öğesiyle bildirin. Her hizmet için, hizmetin ne tür bir iş yaptığına dair bilgi vermek üzere android:foregroundServiceType
özelliği kullanın.
Ayrıca, ön plan hizmetleriniz için gereken izinleri isteyin.
Sürüm uyumluluğu
Ön plan hizmetlerinizi bildirme ve izin isteme koşulları, uygulamanızın hedeflediği API düzeyine göre değişir. Bu sayfada, API düzeyi 34 veya sonraki sürümleri hedefleyen uygulamalarla ilgili şartlar açıklanmaktadır. Daha önceki platform sürümlerinde ön plan hizmetlerinde yapılan değişiklikler hakkında bilgi edinmek için Ön plan hizmetlerinde yapılan değişiklikler başlıklı makaleyi inceleyin.
Uygulama manifestinde ön plan hizmetlerini bildirin
Aşağıdaki kodda, medya oynatma ön plan hizmetinin nasıl beyan edileceği gösterilmektedir.
Müzik çalmak için bu tür bir hizmet kullanabilirsiniz.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<application ...>
<service
android:name=".MyMediaPlaybackService"
android:foregroundServiceType="mediaPlayback"
android:exported="false">
</service>
</application>
</manifest>
Kodla ilgili önemli noktalar
Bu örnekte hizmetin yalnızca bir türü vardır: media
. Hizmetiniz için birden fazla tür geçerliyse bunları |
operatörüyle ayırın. Örneğin, hizmetinizde kamera ve mikrofon kullanılıyorsa bunu şu şekilde bildirin:
android:foregroundServiceType="camera|microphone"
Uygulamanızın hedeflediği API düzeyine bağlı olarak, ön plan hizmetlerini uygulama manifestinde beyan etmeniz gerekebilir. Belirli API düzeyleriyle ilgili şartlar Ön plan hizmetlerinde yapılan değişiklikler bölümünde açıklanmıştır.
Ön plan hizmeti oluşturmaya çalıştığınızda ve türü manifest dosyasında bildirilmediğinde sistem, startForeground()
çağrıldığında MissingForegroundServiceTypeException
oluşturur.
Zorunlu olmasa bile tüm ön plan hizmetlerinizi beyan etmeniz ve hizmet türlerini belirtmeniz en iyi uygulamadır.
Ön plan hizmeti izinlerini isteme
Aşağıdaki kodda, kamerayı kullanan bir ön plan hizmeti için nasıl izin isteneceği gösterilmektedir.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA"/>
<application ...>
...
</application>
</manifest>
Kodla ilgili önemli noktalar
- Bu kod, API düzeyi 34 veya daha yüksek bir sürümü hedefleyen uygulamalar için en iyi uygulamaları kullanır.
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-08-27 UTC.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 2025-08-27 UTC."],[],[],null,["In your app's manifest, declare each of your app's foreground services\nwith a [`\u003cservice\u003e`](/guide/topics/manifest/service-element)\nelement. For each service, use an\n[`android:foregroundServiceType` attribute](/develop/background-work/services/fgs/service-types)\nto declare what kind of work the service does.\n\nIn addition, request any permissions needed by your foreground services.\n| **Important:** All foreground service declarations must comply with the requirements in the [Google Play Device and Network Abuse policy](https://support.google.com/googleplay/android-developer/answer/9888379) and the Google Play [Understanding foreground service requirements\n| documentation](https://support.google.com/googleplay/android-developer/answer/13392821).\n\nVersion compatibility\n\nThe requirements for declaring your foreground services and requesting\npermissions vary depending on what API level your app targets. This page\ndescribes the requirements for apps that target API level 34 or higher. For\ninformation about changes to foreground services in earlier platform versions,\nsee [Changes to foreground services](/develop/background-work/services/fgs/changes).\n\nDeclare foreground services in the app manifest\n\nThe following code shows how to declare a media playback foreground service.\nYou might use a service like this to play music. \n\n \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\" ...\u003e\n \u003capplication ...\u003e\n\n \u003cservice\n android:name=\".MyMediaPlaybackService\"\n android:foregroundServiceType=\"mediaPlayback\"\n android:exported=\"false\"\u003e\n \u003c/service\u003e\n \u003c/application\u003e\n \u003c/manifest\u003e\n\nKey points about the code\n\n- In this example, the service has only one type, `media`. If\n multiple types apply to your service, separate them with the `|`\n operator. For example, if your service uses the camera and microphone,\n declare it like this:\n\n android:foregroundServiceType=\"camera|microphone\"\n\n- Depending on what API level your app targets, you may be\n **required** to declare foreground services in the app\n manifest. The requirements for specific API levels are described in\n [Changes to foreground services](/develop/background-work/services/fgs/changes).\n\n If you try to create a foreground service and its type isn't declared\n in the manifest, the system throws a\n [`MissingForegroundServiceTypeException`](/reference/android/app/MissingForegroundServiceTypeException)\n upon calling `startForeground()`.\n\n Even when it isn't required, it's a best practice to declare\n all your foreground services and provide their service types.\n\nRequest the foreground service permissions\n\nThe following code shows how to request permissions for a foreground\nservice that uses the camera. \n\n \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\" ...\u003e\n\n \u003cuses-permission android:name=\"android.permission.FOREGROUND_SERVICE\"/\u003e\n \u003cuses-permission android:name=\"android.permission.FOREGROUND_SERVICE_CAMERA\"/\u003e\n\n \u003capplication ...\u003e\n ...\n \u003c/application\u003e\n \u003c/manifest\u003e\n\nKey points about the code\n\n- This code uses best practices for an app that targets API level 34 or higher."]]