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

اعتبارًا من الإصدار 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.

جهاز متّصل

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

يجب أن يكون هناك شرط واحد على الأقل من الشروط التالية صحيحًا:

الوصف

التفاعلات مع الأجهزة الخارجية التي تتطلّب اتصالاً عبر البلوتوث أو تقنية NFC أو الأشعة تحت الحمراء أو USB أو الشبكة

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

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

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

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

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.

الصحة

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

يجب أن يكون هناك شرط واحد على الأقل من الشروط التالية صحيحًا:

الوصف

أيّ حالات استخدام طويلة الأمد لتفعيل التطبيقات في فئة اللياقة البدنية، مثل أجهزة تتبُّع التمارين الرياضية

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

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

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

الوصف

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

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

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

الوسائط

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

مواصلة تشغيل الصوت أو الفيديو في الخلفية توفير وظائف تسجيل الفيديو الرقمي (DVR) على Android TV

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

إذا كنت تعرض فيديو في وضع "نافذة ضمن النافذة"، استخدِم وضع "نافذة ضمن النافذة".

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

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

خدمة لتنفيذ عمليات تستغرق وقتًا طويلاً على مواد عرض الوسائط، مثل تحويل الوسائط إلى تنسيقات مختلفة يسمح النظام لهذه الخدمة بوقت محدود للعمل. وفي الظروف العادية، يكون هذا الحدّ الزمني 6 ساعات من كل 24 ساعة. (تتم مشاركة هذا الحدّ بين جميع خدمات mediaProcessing التي تعمل في المقدّمة في التطبيق).

يجب أن يوقف تطبيقك يدويًا خدمة معالجة الوسائط في السيناريو التالي:

  • عند اكتمال عملية تحويل الترميز أو وصولها إلى حالة تعذُّر، اطلب من العميل الاتصال بخدمة Service.stopForeground() وService.stopSelf() لإيقاف الخدمة بالكامل.

في حال بلوغ فترة المهلة، يستدعي النظام طريقة Service.onTimeout(int, int) في الخدمة. في هذه اللحظة، تتوفّر للخدمة بضع ثواني للاتصال بالرقم Service.stopSelf(). إذا لم تطلب الخدمة Service.stopSelf()، سيحدث خطأ ANR مع ظهور رسالة الخطأ التالية: "لم يتم إيقاف الخدمة التي تعمل في المقدّمة من النوع <fgs_type> خلال مهلة الانتظار: <component_name>".

ملاحظة: لا تتوفّر Service.onTimeout(int, int) على الإصدار 14 من نظام التشغيل Android أو الإصدارات الأقدم. على الأجهزة التي تعمل بالإصدارات السابقة، إذا انتهت مهلة خدمة معالجة الوسائط، يُخزِّن النظام التطبيق في ذاكرة التخزين المؤقت على الفور. لهذا السبب، يجب ألا ينتظر تطبيقك تلقّي إشعار بانتهاء المهلة. بدلاً من ذلك، يجب إنهاء الخدمة التي تعمل في المقدّمة أو تغييرها إلى خدمة تعمل في الخلفية في أقرب وقت ممكن.

عرض الوسائط

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.

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

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

خدمة قصيرة

Foreground service type to declare in manifest under
android:foregroundServiceType
shortService
Permission to declare in your manifest
None
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
Runtime prerequisites
None
Description

Quickly finish critical work that cannot be interrupted or postponed.

This type has some unique characteristics:

  • Can only run for a short period of time (about 3 minutes).
  • No support for sticky foreground services.
  • Cannot start other foreground services.
  • Doesn't require a type-specific permission, though it still requires the FOREGROUND_SERVICE permission.
  • A shortService can only change to another service type if the app is currently eligible to start a new foreground service.
  • A foreground service can change its type to shortService at any time, at which point the timeout period begins.

The timeout for shortService begins from the moment that Service.startForeground() is called. The app is expected to call Service.stopSelf() or Service.stopForeground() before the timeout occurs. Otherwise, the new Service.onTimeout() is called, giving apps a brief opportunity to call stopSelf() or stopForeground() to stop their service.

A short time after Service.onTimeout() is called, the app enters a cached state and is no longer considered to be in the foreground, unless the user is actively interacting with the app. A short time after the app is cached and the service has not stopped, the app receives an ANR. The ANR message mentions FOREGROUND_SERVICE_TYPE_SHORT_SERVICE. For these reasons, it's considered best practice to implement the Service.onTimeout() callback.

The Service.onTimeout() callback doesn't exist on Android 13 and lower. If the same service runs on such devices, it doesn't receive a timeout, nor does it ANR. Make sure that your service stops as soon as it finishes the processing task, even if it hasn't received the Service.onTimeout() callback yet.

It's important to note that if the timeout of the shortService is not respected, the app will ANR even if it has other valid foreground services or other app lifecycle processes running.

If an app is visible to the user or satisfies one of the exemptions that allow foreground services to be started from the background, calling Service.StartForeground() again with the FOREGROUND_SERVICE_TYPE_SHORT_SERVICE parameter extends the timeout by another 3 minutes. If the app isn't visible to the user and doesn't satisfy one of the exemptions, any attempt to start another foreground service, regardless of type, causes a ForegroundServiceStartNotAllowedException.

If a user disables battery optimization for your app, it's still affected by the timeout of shortService FGS.

If you start a foreground service that includes the shortService type and another foreground service type, the system ignores the shortService type declaration. However, the service must still adhere to the prerequisites of the other declared types. For more information, see the Foreground services documentation.

استخدام خاص

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

تشمل أي حالات استخدام صالحة للخدمة التي تعمل في المقدّمة ولا تغطيها أنواع الخدمات التي تعمل في المقدّمة.

بالإضافة إلى تقديم بيان عن FOREGROUND_SERVICE_TYPE_SPECIAL_USE نوع الخدمة التي تعمل في المقدّمة، على المطوّرين تقديم بيان عن حالات الاستخدام في ملف FOREGROUND_SERVICE_TYPE_SPECIAL_USE البيان. ولإجراء ذلك، يحددون عنصر <property> في <service>. تُعد هذه القيم وحالات الاستخدام المقابلة أن تتم مراجعتها عند إرسال تطبيقك في Google Play Console. الاستخدام الحالات التي تقدمها تكون حرة الشكل، ويجب أن تتأكد من توفير ما يكفي من للسماح للمراجع بمعرفة سبب احتياجك إلى استخدام specialUse الكتابة.

<service android:name="fooService" android:foregroundServiceType="specialUse">
  <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
      android:value="explanation_for_special_use"/>
</service>

معفى من النظام

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

محجوزة لتطبيقات النظام وعمليات تكامل النظام المحدّدة لمواصلة استخدام الخدمات التي تعمل في المقدّمة.

لاستخدام هذا النوع، يجب أن يستوفي التطبيق معيارًا واحدًا على الأقل من المعايير التالية:

  • الجهاز في حالة الوضع التجريبي
  • التطبيق مالك الجهاز
  • التطبيق مالك ملف شخصي
  • تطبيقات الأمان التي لها الدور ROLE_EMERGENCY
  • تطبيقات مشرف الجهاز
  • التطبيقات التي تحمل إذن SCHEDULE_EXACT_ALARM أو USE_EXACT_ALARM
  • تطبيقات الشبكة الافتراضية الخاصة (يتم ضبطها باستخدام الإعدادات > الشبكة والإنترنت > الشبكة الافتراضية الخاصة)

    وإلا، سيؤدي تعريف هذا النوع إلى عرض النظام للسمة ForegroundServiceTypeNotAllowedException.

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

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.