À partir d'Android 14 (niveau d'API 34), vous devez déclarer un type de service approprié pour chaque service de premier plan. Cela signifie que vous devez déclarer le type de service dans le fichier manifeste de votre application, et demander également l'autorisation de service de premier plan appropriée pour ce type (en plus de demander l'autorisation FOREGROUND_SERVICE
). En outre, selon le type de service de premier plan, vous devrez peut-être demander des autorisations d'exécution avant de lancer le service.
Appareil photo
- 要在
android:foregroundServiceType
下在清单中声明的前台服务类型 camera
- 在清单中声明的权限
FOREGROUND_SERVICE_CAMERA
- 要传递给
startForeground()
的常量 FOREGROUND_SERVICE_TYPE_CAMERA
- 运行时前提条件
请求并获得
CAMERA
运行时权限- 说明
继续在后台访问相机,例如支持多任务的视频聊天应用。
Appareil connecté
- Type de service de premier plan à déclarer dans le fichier manifeste sous
android:foregroundServiceType
connectedDevice
- Autorisation de déclarer dans votre fichier manifeste
FOREGROUND_SERVICE_CONNECTED_DEVICE
- Constante à transmettre à
startForeground()
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
- Conditions requises pour l'environnement d'exécution
Vous devez remplir l'une des conditions suivantes :
Déclarez au moins l'une des autorisations suivantes dans votre fichier manifeste :
Demandez et recevez au moins l'une des autorisations d'exécution suivantes:
Appeler
UsbManager.requestPermission()
- Description
Interactions avec des appareils externes nécessitant une connexion Bluetooth, NFC, IR, USB ou réseau.
- Autres solutions
Si votre application doit effectuer un transfert de données continu vers un appareil externe, envisagez plutôt d'utiliser le gestionnaire d'appareils associés. Utilisez l'API de présence d'appareil associé pour permettre à votre application de rester exécutée lorsque l'appareil associé est à portée.
Si votre application doit rechercher des appareils Bluetooth, envisagez plutôt d'utiliser l'API Bluetooth Scan.
Synchroniser les données
- 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.
Santé
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
health
- 在清单中声明的权限
FOREGROUND_SERVICE_HEALTH
- 要传递给
startForeground()
的常量 FOREGROUND_SERVICE_TYPE_HEALTH
- 运行时前提条件
必须至少满足以下其中一个条件:
在清单中声明
HIGH_SAMPLING_RATE_SENSORS
权限。至少请求并被授予以下其中一项运行时权限:
- 说明
为健身类别的应用(例如锻炼追踪器)提供支持的所有长时间运行的用例。
Localisation
- Type de service de premier plan à déclarer dans le fichier manifeste sous
android:foregroundServiceType
location
- Autorisation à déclarer dans votre fichier manifeste
FOREGROUND_SERVICE_LOCATION
- Constante à transmettre à
startForeground()
FOREGROUND_SERVICE_TYPE_LOCATION
- Conditions préalables d'exécution
L'utilisateur doit avoir activé les services de localisation, et l'application doit disposer d'au moins l'une des autorisations d'exécution suivantes:
- Description
Cas d'utilisation de longue durée nécessitant un accès à la position, tels que la navigation et le partage de position.
- Autres solutions
Si votre application doit être déclenchée lorsque l'utilisateur atteint des emplacements spécifiques, envisagez plutôt d'utiliser l'API Geofence.
Contenus multimédias
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
mediaPlayback
- 在清单中声明的权限
FOREGROUND_SERVICE_MEDIA_PLAYBACK
- 要传递给
startForeground()
的常量 FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
- 运行时前提条件
- 无
- 说明
在后台继续播放音频或视频。在 Android TV 上支持数字视频录制 (DVR) 功能。
- 替代方案
如果您要显示画中画视频,请使用画中画模式。
Traitement multimédia
- 要在其清单中声明的前台服务类型
android:foregroundServiceType
mediaProcessing
- 在清单中声明的权限
FOREGROUND_SERVICE_MEDIA_PROCESSING
- 要传递给
startForeground()
的常量 FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
- 运行时前提条件
- 无
- 说明
用于对媒体资源执行耗时操作的服务,例如将媒体转换为不同格式。系统允许此服务有有限的运行时间;在正常情况下,此时间限制为每 24 小时 6 小时。(此限制由应用的所有
mediaProcessing
前台服务共享。)您的应用应在以下情况下手动停止媒体处理服务:
- 当转码操作完成或达到失败状态时,让服务调用
Service.stopForeground()
和Service.stopSelf()
以彻底停止服务。
- 当转码操作完成或达到失败状态时,让服务调用
如果达到超时期限,系统会调用服务的
Service.onTimeout(int, int)
方法。此时,服务有几秒钟时间来调用Service.stopSelf()
。如果服务未调用Service.stopSelf()
,将发生 ANR,并显示以下错误消息:“<fgs_type> 的前台服务未在其超时内停止:<component_name>”。注意:
Service.onTimeout(int, int)
不适用于 Android 14 或更低版本。在运行这些版本的设备上,如果媒体处理服务达到超时期限,系统会立即缓存应用。因此,您的应用不应等待收到超时通知。而是应尽快终止前台服务或将其更改为后台服务。
Projection multimédia
- 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.
Micro
- 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.
Appel téléphonique
- 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 has declared the
MANAGE_OWN_CALLS
permission in its manifest file.
- App has declared the
- App is the default dialer app through the
ROLE_DIALER
role.
- App is the default dialer app through the
- 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.
Messagerie à distance
- 要在其清单中声明的前台服务类型
android:foregroundServiceType
remoteMessaging
- 在清单中声明的权限
FOREGROUND_SERVICE_REMOTE_MESSAGING
- 要传递给
startForeground()
的常量 FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
- 运行时前提条件
- 无
- 说明
- 将短信从一台设备转移到另一台设备。在用户切换设备时,帮助确保用户消息任务的连续性。
Service court
- 要在其清单中声明的前台服务类型
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()
回调被视为一种最佳实践。Android 13 及更低版本中不存在
Service.onTimeout()
回调。如果同一服务在此类设备上运行,则不会出现超时,也不会发生 ANR。确保您的服务在完成处理任务后立即停止,即使它尚未收到Service.onTimeout()
回调也是如此。请务必注意,如果未遵循
shortService
的超时设置,即使应用还有其他有效的前台服务或其他应用生命周期进程,应用也会遇到 ANR。如果应用对用户可见,或满足允许从后台启动前台服务的某一豁免条件,则使用
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
参数再次调用Service.StartForeground()
会将超时时间再延长 3 分钟。如果应用对用户不可见且不满足其中一个豁免条件,则尝试启动其他前台服务(无论其类型如何)都会导致ForegroundServiceStartNotAllowedException
。即使用户为您的应用停用电池优化功能,仍然会受到 shortService FGS 的影响。
如果您启动包含
shortService
类型和另一个前台服务类型的前台服务,系统会忽略shortService
类型声明。不过,该服务仍必须遵守其他声明类型的前提条件。如需了解详情,请参阅前台服务文档。
Utilisation spéciale
- 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 thespecialUse
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>
Système exempté
- 要在其清单中声明的前台服务类型
android:foregroundServiceType
systemExempted
- 在清单中声明的权限
FOREGROUND_SERVICE_SYSTEM_EXEMPTED
- 要传递给
startForeground()
的常量 FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
- 运行时前提条件
- 无
- 说明
为系统应用和特定系统集成预留,以便继续使用前台服务。
如需使用此类型,应用必须至少满足以下条件之一:
- 设备处于演示模式状态
- 应用是设备所有者
- 应用是性能分析器所有者
- 属于具有
ROLE_EMERGENCY
角色的安全应用 - 属于设备管理应用
- 具有
SCHEDULE_EXACT_ALARM
或USE_EXACT_ALARM
权限的应用 VPN 应用(通过设置 > 网络和互联网 > VPN 进行配置)
否则,声明此类型会导致系统抛出
ForegroundServiceTypeNotAllowedException
。
Application des règles Google Play pour l'utilisation des types de services de premier plan
Si votre application cible Android 14 ou une version ultérieure, vous devez déclarer les types de services de premier plan de votre application sur la page "Contenu de l'application" de la Play Console (Règles > Contenu de l'application). Pour en savoir plus sur la déclaration de vos types de services de premier plan dans la Play Console, consultez Comprendre les exigences liées aux services de premier plan et à l'intent plein écran.