اعتبارًا من الإصدار 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
CAMERAruntime 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:foregroundServiceTypeconnectedDevice- 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:
Declare at least one of the following permissions in your manifest:
Request and be granted at least one of the following runtime permissions:
- 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:foregroundServiceTypedataSync- 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:foregroundServiceTypehealth- الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_HEALTH- ثابت يتم تمريره إلى
startForeground() FOREGROUND_SERVICE_TYPE_HEALTH- المتطلبات الأساسية لوقت التشغيل
يجب أن يكون هناك شرط واحد على الأقل من الشروط التالية صحيحًا:
أدخِل الإذن
HIGH_SAMPLING_RATE_SENSORSفي ملف البيان.طلب الحصول على إذن واحد على الأقل من أذونات وقت التشغيل التالية والحصول عليه:
-
BODY_SENSORSعلى الإصدار 35 من واجهة برمجة التطبيقات والإصدارات الأقدم READ_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
-
- الوصف
أيّ حالات استخدام طويلة الأمد لتفعيل التطبيقات في فئة اللياقة البدنية، مثل أجهزة تتبُّع التمارين الرياضية
الموقع الجغرافي
- نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceTypelocation- الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_LOCATION- ثابت يتم تمريره إلى
startForeground() FOREGROUND_SERVICE_TYPE_LOCATION- المتطلبات الأساسية لوقت التشغيل
يجب أن يكون المستخدم قد فعّل خدمات الموقع الجغرافي وأن يتم منح التطبيق إذن تشغيل واحدًا على الأقل من الأذونات التالية:
- الوصف
حالات الاستخدام التي تستغرق وقتًا طويلاً وتتطلّب الوصول إلى الموقع الجغرافي، مثل التنقّل ومشاركة الموقع الجغرافي
- الحلول البديلة
إذا كان تطبيقك يحتاج إلى البدء عندما يصل المستخدم إلى مواقع جغرافية معيّنة، ننصحك باستخدام Geofence API بدلاً من ذلك.
الوسائط
- Foreground service type to declare in manifest under
android:foregroundServiceTypemediaPlayback- 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:foregroundServiceTypemediaProcessing- 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
mediaProcessingforeground services.)Your app should manually stop the media processing service in the following scenario:
- When the transcoding operation finishes or reaches a failure state, have the
service call
Service.stopForeground()andService.stopSelf()to stop the service completely.
- When the transcoding operation finishes or reaches a failure state, have the
service call
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 callService.stopSelf(). If the service does not callService.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:foregroundServiceTypemediaProjection- 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
MediaProjectionAPIs. 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:foregroundServiceTypemicrophone- 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_AUDIOruntime permission.- Description
Continue microphone capture from the background, such as voice recorders or communication apps.
المكالمات الهاتفية
- نوع الخدمة التي تعمل في المقدّمة المطلوب تعريفها في البيان ضمن
android:foregroundServiceTypephoneCall- الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_PHONE_CALL- الثابت الذي سيتم تمريره إلى
startForeground() FOREGROUND_SERVICE_TYPE_PHONE_CALL- المتطلبات الأساسية لبيئة التشغيل
يجب أن يكون أحد الشرطين التاليين صحيحًا:
- أعلن التطبيق عن الإذن
MANAGE_OWN_CALLSفي ملف البيان الخاص به.
- أعلن التطبيق عن الإذن
- التطبيق هو تطبيق الاتصال التلقائي من خلال الدور
ROLE_DIALER.
- التطبيق هو تطبيق الاتصال التلقائي من خلال الدور
- الوصف
مواصلة مكالمة جارية باستخدام واجهات برمجة التطبيقات
ConnectionService- الحلول البديلة
إذا كنت بحاجة إلى إجراء مكالمات هاتفية أو مكالمات فيديو أو مكالمات عبر بروتوكول الصوت على الإنترنت (VoIP)، ننصحك باستخدام مكتبة
android.telecom.ننصحك باستخدام
CallScreeningServiceلفحص المكالمات.
المراسلة عن بُعد
- نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceTyperemoteMessaging- الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_REMOTE_MESSAGING- ثابت يتم تمريره إلى
startForeground() FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- المتطلبات الأساسية لوقت التشغيل
- بدون
- الوصف
- نقل الرسائل النصية من جهاز إلى آخر تقدِّم هذه الخدمة المساعدة في مواصلة مهام المراسلة من المستخدم عند تبديل الأجهزة.
خدمة قصيرة
- نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceTypeshortService- الإذن المطلوب تضمينه في بيان التطبيق
- بدون
- ثابت يتم تمريره إلى
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. ومع ذلك، يجب أن تلتزم الخدمة بالشروط الأساسية للأنواع الأخرى المعلَن عنها. لمزيد من المعلومات، يُرجى الاطّلاع على مستندات خدمات المقدّمة.
استخدام خاص
- نوع الخدمة التي تعمل في المقدّمة للإفصاح عنها في البيان ضمن
android:foregroundServiceTypespecialUse- إذن بالبيان في بيان التطبيق
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>
معفى من النظام
- Foreground service type to declare in manifest under
android:foregroundServiceTypesystemExempted- 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_EMERGENCYrole - Device Admin apps
- Apps holding
SCHEDULE_EXACT_ALARMorUSE_EXACT_ALARMpermission 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، يُرجى الاطّلاع على مقالة التعرّف على متطلّبات الخدمة التي تعمل في المقدّمة والعرض بملء الشاشة.