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:
cameraconnectedDevicedataSynchealthlocationmediaPlaybackmediaProjectionmicrophonephoneCallremoteMessagingshortServicespecialUsesystemExempted
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.
إعلان الإذن الجديد لاستخدام أنواع الخدمات التي تعمل في المقدّمة
如果以 Android 14 为目标平台的应用使用前台服务,则必须根据前台服务类型声明 Android 14 中引入的特定权限。这些权限显示在本页每种前台服务类型的预期用例和强制执行部分中标记为“您必须在清单文件中声明的权限”的部分。
所有这些权限都定义为一般权限,并默认授予。用户无法撤消这些权限。
تضمين نوع الخدمة التي تعمل في المقدّمة أثناء التشغيل
The best practice for applications starting foreground services is to use the
ServiceCompat version of startForeground() (available in androidx-core
1.12 and higher) where you pass in a bitwise
integer of foreground service types. You can choose to pass one or more type
values.
Usually, you should declare only the types required for a particular use case. This makes it easier to meet the system's expectations for each foreground service type. In cases where a foreground service is started with multiple types, then the foreground service must adhere to the platform enforcement requirements of all types.
ServiceCompat.startForeground(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION)
If the foreground service type is not specified in the call, the type defaults
to the values defined in the manifest. If you didn't specify the service
type in the manifest, the system throws
MissingForegroundServiceTypeException.
If the foreground service needs new permissions after you launch it, you
should call startForeground() again and add the new service types. For
example, suppose a fitness app runs a running-tracker service that always needs
location information, but might or might not need media permissions. You
would need to declare both location and mediaPlayback in the manifest. If a
user starts a run and just wants their location tracked, your app should call
startForeground() and pass just the location service type. Then, if the user
wants to start playing audio, call startForeground() again and pass
location|mediaPlayback.
عمليات التحقّق من وقت تشغيل النظام
系统会检查前台服务类型的使用是否恰当,并确认应用是否已请求适当的运行时权限或使用所需的 API。例如,系统希望使用前台服务类型 FOREGROUND_SERVICE_TYPE_LOCATION 的应用请求 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION。
这意味着,在向用户请求权限和启动前台服务时,应用必须遵循非常具体的操作顺序。应用在尝试调用 startForeground() 之前,必须先请求并获得所需的权限。在启动前台服务后请求相应权限的应用必须更改此顺序,并在启动前台服务之前请求该权限。
本页面的每种前台服务类型的预期用例和强制执行部分中标记为“运行时要求”的部分列出了平台强制执行的具体内容。
حالات الاستخدام المقصودة وفرضها لكل نوع من أنواع الخدمات التي تعمل في المقدّمة
In order to use a given foreground service type, you must declare a particular permission in your manifest file, you must fulfill specific runtime requirements, and your app must fulfill one of the intended sets of use cases for that type. The following sections explain the permission that you must declare, the runtime prerequisites, and the intended use cases for each type.
الكاميرا
- 要在
android:foregroundServiceType下在清单中声明的前台服务类型 camera- 在清单中声明的权限
FOREGROUND_SERVICE_CAMERA- 要传递给
startForeground()的常量 FOREGROUND_SERVICE_TYPE_CAMERA- 运行时前提条件
请求并获得
CAMERA运行时权限注意:
CAMERA运行时权限受使用时限制的约束。因此,除少数例外情况外,您无法在应用在后台运行时创建camera前台服务。如需了解详情,请参阅与启动需要“使用时”权限的前台服务相关的限制。- 说明
继续在后台访问相机,例如支持多任务的视频聊天应用。
جهاز متّصل
- 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
-
ملاحظة: تخضع أذونات التشغيل
BODY_SENSORSوأذونات القراءة المستندة إلى أجهزة الاستشعار لقيود أثناء الاستخدام. لهذا السبب، لا يمكنك إنشاءhealthخدمة تعمل في المقدّمة وتستخدم أدوات استشعار الجسم عندما يكون تطبيقك في الخلفية ما لم يتم منحك إذنَيBODY_SENSORS_BACKGROUND(المستوى 33 إلى 35 لواجهة برمجة التطبيقات) أوREAD_HEALTH_DATA_IN_BACKGROUND(المستوى 36 لواجهة برمجة التطبيقات والمستويات الأعلى). لمزيد من المعلومات، يُرجى الاطّلاع على القيود المفروضة على بدء الخدمات التي تعمل في المقدّمة والتي تحتاج إلى أذونات أثناء الاستخدام.- الوصف
أيّ حالات استخدام طويلة الأمد لتفعيل التطبيقات في فئة اللياقة البدنية، مثل أجهزة تتبُّع التمارين الرياضية
الموقع الجغرافي
- Foreground service type to declare in manifest under
android:foregroundServiceTypelocation- 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:
Note: In order to check that the user has enabled location services as well as granted access to the runtime permissions, use
PermissionChecker#checkSelfPermission()Note: The location runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a
locationforeground service while your app is in the background, unless you've been granted theACCESS_BACKGROUND_LOCATIONruntime permission. For more information, see Restrictions on starting foreground services that need while-in-use 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: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: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.Note: The
RECORD_AUDIOruntime permission is subject to while-in-use restrictions. For this reason, you cannot create amicrophoneforeground 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.
المكالمات الهاتفية
- نوع الخدمة التي تعمل في المقدّمة المطلوب تقديم بيان عنها في البيان ضمن
android:foregroundServiceTypephoneCall- الإذن المطلوب تضمينه في بيان التطبيق
FOREGROUND_SERVICE_PHONE_CALL- ثابت يتم تمريره إلى
startForeground() FOREGROUND_SERVICE_TYPE_PHONE_CALL- المتطلبات الأساسية لوقت التشغيل
يجب أن يكون أحد الشروط التالية صحيحًا على الأقل:
- أعلن التطبيق عن إذن
MANAGE_OWN_CALLSفي ملف البيان.
- أعلن التطبيق عن إذن
- التطبيق هو تطبيق الاتصال التلقائي من خلال دور
ROLE_DIALER.
- التطبيق هو تطبيق الاتصال التلقائي من خلال دور
- الوصف
مواصلة مكالمة جارية باستخدام واجهات برمجة تطبيقات
ConnectionService- الحلول البديلة
إذا كنت بحاجة إلى إجراء مكالمات هاتفية أو مكالمات فيديو أو مكالمات عبر بروتوكول الصوت على الإنترنت، ننصحك باستخدام مكتبة
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()回调被视为一种最佳实践。Android 13 及更低版本中不存在
Service.onTimeout()回调。如果同一服务在此类设备上运行,则不会出现超时,也不会发生 ANR。确保您的服务在完成处理任务后立即停止,即使它尚未收到Service.onTimeout()回调也是如此。请务必注意,如果未遵循
shortService的超时设置,即使应用还有其他有效的前台服务或其他应用生命周期进程,应用也会遇到 ANR。如果应用对用户可见,或满足允许从后台启动前台服务的某一豁免条件,则使用
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE参数再次调用Service.StartForeground()会将超时时间再延长 3 分钟。如果应用对用户不可见且不满足其中一个豁免条件,则尝试启动其他前台服务(无论其类型如何)都会导致ForegroundServiceStartNotAllowedException。即使用户为您的应用停用电池优化功能,仍然会受到 shortService FGS 的影响。
如果您启动包含
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 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 لاستخدام أنواع الخدمات التي تعمل في المقدّمة
如果您的应用以 Android 14 或更高版本为目标平台,您需要在 Play 管理中心的“应用内容”页面(政策 > 应用内容)中声明应用的前台服务类型。如需详细了解如何在 Play 管理中心内声明前台服务类型,请参阅了解前台服务和全屏 intent 要求。