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

اعتبارًا من الإصدار 14 من نظام التشغيل Android (المستوى 34 لواجهة برمجة التطبيقات)، يجب تقديم بيان عن نوع الخدمة المناسب لكل خدمة تعمل في المقدّمة. وهذا يعني أنّه يجب تعريف نوع الخدمة في بيان تطبيقك، وطلب الإذن المناسب لاستخدام الخدمة التي تعمل في المقدّمة لهذا النوع (بالإضافة إلى طلب الإذن FOREGROUND_SERVICE). بالإضافة إلى ذلك، قد تحتاج إلى طلب أذونات وقت التشغيل قبل إطلاق الخدمة، وذلك حسب نوع الخدمة التي تعمل في المقدّمة.

الكاميرا

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.

جهاز متّصل

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:

Description

Any long-running use cases to support apps in the fitness category such as exercise trackers.

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

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:

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.

الوسائط

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.

جارٍ معالجة الوسائط

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.

عرض الوسائط

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.

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.

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

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

خدمة قصيرة

نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
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:

تنفيذ سياسة Google Play بشأن استخدام أنواع الخدمات التي تعمل في المقدّمة

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