أنواع الخدمات التي تعمل في المقدّمة مطلوبة

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:

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.

إعلان الإذن الجديد لاستخدام أنواع الخدمات التي تعمل في المقدّمة

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.

تضمين نوع الخدمة التي تعمل في المقدّمة أثناء التشغيل

من أفضل الممارسات للتطبيقات التي تبدأ الخدمات التي تعمل في المقدّمة استخدام الإصدار ServiceCompat من startForeground() (متاح في androidx-core 1.12 والإصدارات الأحدث) حيث يتم تمرير عددٍ كاملٍ مبني على الأعداد الثنائية لأنواع الخدمات التي تعمل في المقدّمة. يمكنك اختيار تمرير قيمة نوع واحدة أو أكثر.

وعادةً ما يجب الإفصاح عن الأنواع المطلوبة فقط لحالة استخدام معيّنة. ويسهِّل ذلك تلبية توقّعات النظام لكل نوع من أنواع الخدمات التي تعمل في المقدّمة. في الحالات التي يتم فيها بدء الخدمة التي تعمل في المقدّمة بعدة أنواع، يجب أن تلتزم الخدمة التي تعمل في المقدّمة بمتطلبات التنفيذ على مستوى النظام الأساسي لجميع الأنواع.

ServiceCompat.startForeground(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION)

إذا لم يتم تحديد نوع الخدمة التي تعمل في المقدّمة في الطلب، سيكون النوع التلقائي على القيم المحدّدة في البيان. إذا لم تحدِّد نوع الخدمة في البيان، سيُرسِل النظام خطأ MissingForegroundServiceTypeException.

إذا كانت الخدمة التي تعمل في المقدّمة تحتاج إلى أذونات جديدة بعد تفعيلها، عليك طلب startForeground() مجددًا وإضافة أنواع الخدمات الجديدة. على سبيل المثال، لنفترض أنّ تطبيق لياقة بدنية يشغّل خدمة تتبُّع الركض التي تحتاج دائمًا إلى معلومات location، ولكن قد تحتاج أو لا تحتاج إلى أذونات media. عليك الإفصاح عن كل من location وmediaPlayback في البيان. إذا بدأ أحد المستخدمين جولة ركض وأردت تتبُّع موقعه الجغرافي فقط، يجب أن يتصل تطبيقك بواجهة برمجة التطبيقات startForeground() ويُرسل نوع الخدمة location فقط. بعد ذلك، إذا أراد المستخدم بدء تشغيل الصوت، يتصل بـ startForeground() مرة أخرى ويمرّر location|mediaPlayback.

عمليات التحقّق من وقت تشغيل النظام

يتحقّق النظام من الاستخدام الصحيح لأنواع الخدمات التي تعمل في المقدّمة ويؤكّد أنّه قد طلب التطبيق أذونات التشغيل المناسبة أو يستخدم واجهات برمجة التطبيقات المطلوبة. على سبيل المثال، يتوقّع النظام أن تطلب التطبيقات التي تستخدم نوع الخدمة التي تعمل في المقدّمة FOREGROUND_SERVICE_TYPE_LOCATION إما ACCESS_COARSE_LOCATION أو ACCESS_FINE_LOCATION.

ويشير ذلك إلى أنّ التطبيقات يجب أن تتّبع ترتيبًا محدّدًا جدًا للعمليات عند طلب الأذونات من المستخدم وبدء الخدمات التي تعمل في المقدّمة. يجب طلب الأذونات ومنحها قبل أن يحاول التطبيق الاتصال بخدمة startForeground(). على التطبيقات التي تطلب الأذونات المناسبة بعد بدء الخدمة التي تعمل في المقدّمة تغيير ترتيب هذه العمليات وطلب الإذن قبل بدء الخدمة التي تعمل في المقدّمة.

تظهر تفاصيل تنفيذ المنصة في الأقسام التي تحمل التصنيف "متطلبات وقت التشغيل" في قسم حالات الاستخدام المقصودة وتنفيذ كل نوع من الخدمات التي تعمل في المقدّمة في هذه الصفحة.

حالات الاستخدام المقصودة وفرضها لكل نوع من أنواع الخدمات التي تعمل في المقدّمة

لاستخدام نوع معيّن من الخدمات التي تعمل في المقدّمة، يجب تقديم بيان عن إذن معيّن في ملف البيان، ويجب استيفاء متطلبات محددة أثناء التشغيل ، ويجب أن يستوفي تطبيقك إحدى المجموعات المقصودة من حالات الاستخدام لهذا النوع. توضّح الأقسام التالية الإذن الذي يجب إعلام المستخدمين به، ومتطلبات التشغيل الأساسية، وحالات الاستخدام المقصودة لكل نوع.

الكاميرا

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

Note: The CAMERA runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a camera 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 to access the camera from the background, such as video chat apps that allow for multitasking.

جهاز متّصل

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.

مزامنة البيانات

Foreground service type to declare in manifest under
android:foregroundServiceType
dataSync
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.

الصحة

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:

Note: The BODY_SENSORS and sensor-based READ runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a health foreground service that uses body sensors while your app is in the background unless you've been granted the BODY_SENSORS_BACKGROUND (API level 33 to 35) or READ_HEALTH_DATA_IN_BACKGROUND (API level 36 and higher) permissions. 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.

الموقع الجغرافي

نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنه في البيان ضمن
android:foregroundServiceType
location
الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_LOCATION
ثابت يتم تمريره إلى startForeground()
FOREGROUND_SERVICE_TYPE_LOCATION
المتطلبات الأساسية لوقت التشغيل

يجب أن يكون المستخدم قد فعّل خدمات الموقع الجغرافي وأن يتم منح التطبيق إذن تشغيل واحدًا على الأقل من الأذونات التالية:

ملاحظة: للتحقّق من أنّ المستخدم قد فعّل خدمات الموقع الجغرافي بالإضافة إلى منح الإذن بالوصول إلى أذونات التشغيل، استخدِم PermissionChecker#checkSelfPermission().

ملاحظة: تخضع أذونات التشغيل الخاصة بالموقع الجغرافي لقيود الاستخدام أثناء التشغيل. لهذا السبب، لا يمكنك إنشاء location خدمة تعمل في المقدّمة عندما يكون تطبيقك قيد التشغيل في الخلفية، ما لم يتم منحك إذن ACCESS_BACKGROUND_LOCATION وقت التشغيل. لمزيد من المعلومات، يُرجى الاطّلاع على القيود المفروضة على بدء الخدمات التي تعمل في المقدّمة والتي تحتاج إلى أذونات أثناء الاستخدام.

الوصف

حالات الاستخدام التي تستغرق وقتًا طويلاً وتتطلّب الوصول إلى الموقع الجغرافي، مثل التنقّل ومشاركة الموقع الجغرافي

الحلول البديلة

إذا كان تطبيقك يحتاج إلى التفعيل عندما يصل المستخدم إلى مواقع جغرافية معيّنة، ننصحك باستخدام Geofence API بدلاً من ذلك.

الوسائط

نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceType
mediaPlayback
الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_MEDIA_PLAYBACK
ثابت يتم تمريره إلى startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
المتطلبات الأساسية لوقت التشغيل
بدون
الوصف
مواصلة تشغيل الصوت أو الفيديو في الخلفية أن تتيح وظائف تسجيل الفيديو الرقمي (DVR) على Android TV
الحلول البديلة
إذا كنت تعرض فيديو في وضع "نافذة ضمن النافذة"، استخدِم وضع "نافذة ضمن النافذة".

عرض الوسائط

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.

الميكروفون

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 a microphone 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.

المكالمات الهاتفية

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.

المراسلة عن بُعد

نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceType
remoteMessaging
الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_REMOTE_MESSAGING
ثابت يتم تمريره إلى startForeground()
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
المتطلبات الأساسية لوقت التشغيل
بدون
الوصف
نقل الرسائل النصية من جهاز إلى آخر تقدِّم هذه الخدمة المساعدة في مواصلة مهام المراسلة من المستخدم عند تبديل الأجهزة.

خدمة قصيرة

نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceType
shortService
الإذن المطلوب تضمينه في بيان التطبيق
بدون
ثابت يتم تمريره إلى startForeground()
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
المتطلبات الأساسية لوقت التشغيل
بدون
الوصف

إنهاء العمل المهم بسرعة والذي لا يمكن إيقافه أو تأجيله

يتميز هذا النوع ببعض الخصائص الفريدة:

  • لا يمكن تشغيله إلا لفترة قصيرة (حوالي 3 دقائق).
  • لا تتوفّر خدمات ثابتة تعمل في المقدّمة.
  • لا يمكن بدء خدمات أخرى تعمل في المقدّمة.
  • لا يتطلّب إذنًا خاصًا بالنوع، إلا أنّه يتطلّب إذن FOREGROUND_SERVICE.
  • لا يمكن تغيير shortService إلى نوع خدمة آخر إلا إذا كان التطبيق مؤهلاً حاليًا لبدء خدمة جديدة تعمل في المقدّمة.
  • يمكن لخدمة تعمل في المقدّمة تغيير نوعها إلى shortService في أي وقت، وعندها تبدأ فترة المهلة.

يبدأ مهلة shortService من لحظة استدعاء Service.startForeground(). من المتوقّع أن يتصل التطبيق Service.stopSelf() أو Service.stopForeground() قبل حدوث مهلة. بخلاف ذلك، يتم استدعاء Service.onTimeout() الجديد، ما يمنح التطبيقات فرصة قصيرة للاتصال بـ stopSelf() أو stopForeground() لإيقاف خدمتها.

بعد وقت قصير من استدعاء Service.onTimeout()، يدخل التطبيق في حالة تخزين مؤقت ولا يعود في المقدّمة، ما لم يكن المستخدم يتفاعل مع التطبيق بشكل نشط. بعد وقت قصير من تخزين التطبيق مؤقتًا وعدم توقّف الخدمة، يتلقّى التطبيق خطأ ANR. تشير رسالة ANR إلى FOREGROUND_SERVICE_TYPE_SHORT_SERVICE. لهذه الأسباب، يُعدّ تنفيذ Service.onTimeout() callback من أفضل الممارسات.

لا يتوفّر خيار الاتصال التلقائي Service.onTimeout() على الإصدار 13 من نظام التشغيل Android والإصدارات الأقدم. إذا كانت الخدمة نفسها تعمل على هذه الأجهزة، لا تتلقّى مهلة ولا تتلقّى ANR. تأكَّد من أنّ الخدمة تتوقف فور الانتهاء من مهمة المعالجة، حتى إذا لم تتلقّ Service.onTimeout() طلب إعادة الاتصال بعد.

من المهمّ ملاحظة أنّه في حال عدم الالتزام بالمهلة الزمنية لshortService، سيظهر خطأ ANR في التطبيق حتى إذا كان لديه خدمات صالحة أخرى في المقدّمة أو عمليات أخرى لدورة حياة التطبيق قيد التشغيل.

إذا كان التطبيق مرئيًا للمستخدم أو يستوفي أحد الإعفاءات التي تسمح ببدء الخدمات التي تعمل في المقدّمة من الخلفية، يؤدي استدعاء Service.StartForeground() مرة أخرى باستخدام المَعلمة FOREGROUND_SERVICE_TYPE_SHORT_SERVICE إلى تمديد مهلة الانتظار مدّة 3 دقائق أخرى. إذا لم يكن التطبيق مرئيًا للمستخدم ولم يكن يستوفي أحد الإعفاءات، تؤدي أي محاولة لبدء خدمة أخرى تعمل في المقدّمة، بغض النظر عن نوعها، إلى ForegroundServiceStartNotAllowedException.

إذا أوقف المستخدم ميزة تحسين البطارية لتطبيقك، سيظلّ يتأثر بوقت الاستراحة لـ FGS في shortService.

إذا بدأت خدمة تعمل في المقدّمة تتضمّن نوع shortService و نوعًا آخر من الخدمات التي تعمل في المقدّمة، يتجاهل النظام بيان نوع shortService. ومع ذلك، يجب أن تلتزم الخدمة بالشروط الأساسية للأنواع الأخرى المعلَن عنها. لمزيد من المعلومات، يُرجى الاطّلاع على مستندات خدمات المقدّمة.

استخدام خاص

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>

تمّت إعفاءها من خلال النظام

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 or USE_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.

فرض سياسة Google Play لاستخدام أنواع الخدمات التي تعمل في المقدّمة

إذا كان تطبيقك يستهدف الإصدار 14 من نظام التشغيل Android أو الإصدارات الأحدث، عليك الإفصاح عن أنواع الخدمات التي تعمل في المقدّمة في صفحة محتوى التطبيق في Play Console (السياسة > محتوى التطبيق). لمزيد من المعلومات حول كيفية توضيح أنواع الخدمات التي تعمل في المقدّمة في Play Console، يُرجى الاطّلاع على مقالة التعرّف على متطلّبات الخدمة التي تعمل في المقدّمة والعرض بملء الشاشة.