ต้องระบุประเภทบริการที่ทำงานอยู่เบื้องหน้า

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:

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_LOCATIONACCESS_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.

กล้อง

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

Note: The CAMERA runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a camera foreground 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 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、IR、USB 或网络连接的外部设备进行互动。

替代方案

如果您的应用需要向外部设备持续传输数据,请考虑改用配套设备管理器。使用配套设备感知 API,可帮助您的应用在配套设备在范围内时保持运行。

如果您的应用需要扫描蓝牙设备,请考虑改用 Bluetooth Scan API

การซิงค์ข้อมูล

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
运行时前提条件

必须至少满足以下其中一个条件:

注意BODY_SENSORS 和基于传感器的读取运行时权限受“在使用时”限制。因此,除非您已获得 BODY_SENSORS_BACKGROUND(API 级别 33 到 35)或 READ_HEALTH_DATA_IN_BACKGROUND(API 级别 36 及更高级别)权限,否则您无法创建在应用处于后台运行时使用身体传感器的 health 前台服务。如需了解详情,请参阅与启动需要使用时权限的前台服务相关的限制

说明

为健身类别的应用(例如锻炼追踪器)提供支持的所有长时间运行的用例。

ตำแหน่ง

要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
location
在清单中声明的权限
FOREGROUND_SERVICE_LOCATION
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_LOCATION
运行时前提条件

用户必须已启用位置信息服务,并且应用必须至少获得以下一项运行时权限:

注意:如需检查用户是否已启用位置信息服务以及是否已授予对运行时权限的访问权限,请使用 PermissionChecker#checkSelfPermission()

注意:位置信息运行时权限受“使用期间”限制。因此,除非您已获得 ACCESS_BACKGROUND_LOCATION 运行时权限,否则无法在应用在后台运行时创建 location 前台服务。如需了解详情,请参阅与启动需要“使用时”权限的前台服务相关的限制

说明

需要位置信息使用权的长时间运行的用例,例如导航和位置信息分享。

替代方案

如果您的应用需要在用户到达特定位置时触发,请考虑改用 Geofence API

สื่อ

要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
mediaPlayback
在清单中声明的权限
FOREGROUND_SERVICE_MEDIA_PLAYBACK
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
运行时前提条件
说明
在后台继续播放音频或视频。在 Android TV 上支持数字视频录制 (DVR) 功能。
替代方案
如果您要显示画中画视频,请使用画中画模式

การฉายภาพสื่อ

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.

Note: The RECORD_AUDIO runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a microphone foreground 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.

การโทร

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
运行时前提条件
说明
将短信从一台设备转移到另一台设备。在用户切换设备时,帮助确保用户消息任务的连续性。

บริการสั้นๆ

要在清单中的以下位置声明的前台服务类型
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 类型声明。不过,该服务仍必须遵守其他声明类型的先决条件。如需了解详情,请参阅前台服务文档

การใช้งานพิเศษ

ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ภายใต้
android:foregroundServiceType
specialUse
สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_SPECIAL_USE
ค่าคงที่ที่จะส่งผ่านไปยัง startForeground()
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
ข้อกำหนดเบื้องต้นของรันไทม์
ไม่มี
คำอธิบาย

ครอบคลุมกรณีการใช้งานบริการที่ทำงานอยู่เบื้องหน้าที่ถูกต้องซึ่งไม่ครอบคลุมในกรณีการใช้งานอื่น ประเภทบริการที่ทำงานอยู่เบื้องหน้า

นอกจากการประกาศFOREGROUND_SERVICE_TYPE_SPECIAL_USE ประเภทบริการที่ทำงานอยู่เบื้องหน้า นักพัฒนาแอปควรประกาศกรณีการใช้งานใน ไฟล์ Manifest ซึ่งวิธีการคือการระบุองค์ประกอบ <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>

ได้รับการยกเว้นจากระบบ

ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ในส่วน
android:foregroundServiceType
systemExempted
สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_SYSTEM_EXEMPTED
ค่าคงที่ที่จะส่งไปยัง startForeground()
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
ข้อกําหนดเบื้องต้นของรันไทม์
ไม่มี
คำอธิบาย

สงวนไว้สำหรับแอปพลิเคชันของระบบและการผสานรวมระบบที่เฉพาะเจาะจง เพื่อใช้บริการที่ทำงานอยู่เบื้องหน้าต่อไป

หากต้องการใช้ประเภทนี้ แอปต้องเป็นไปตามเกณฑ์ข้อใดข้อหนึ่งต่อไปนี้เป็นอย่างน้อย

  • อุปกรณ์อยู่ในสถานะโหมดสาธิต
  • แอปเป็นเจ้าของอุปกรณ์
  • แอปเป็นเจ้าของเครื่องมือวิเคราะห์
  • แอปความปลอดภัยที่มีบทบาท ROLE_EMERGENCY
  • แอปผู้ดูแลระบบอุปกรณ์
  • แอปที่มีสิทธิ์ SCHEDULE_EXACT_ALARM หรือ USE_EXACT_ALARM และใช้บริการที่ทำงานอยู่เบื้องหน้าเพื่อส่งเสียงปลุกในเบื้องหลังต่อไป รวมถึงการปลุกด้วยระบบสัมผัสเท่านั้น
  • แอป VPN (กำหนดค่าโดยใช้การตั้งค่า > เครือข่ายและอินเทอร์เน็ต > VPN)

    มิเช่นนั้น การประกาศประเภทนี้จะทำให้ระบบแสดงข้อผิดพลาด ForegroundServiceTypeNotAllowedException

การบังคับใช้นโยบาย Google Play สำหรับการใช้ประเภทบริการที่ทำงานอยู่เบื้องหน้า

หากแอปกำหนดเป้าหมายเป็น Android 14 ขึ้นไป คุณจะต้องประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าของแอปในหน้าเนื้อหาแอปของ Play Console (นโยบาย > เนื้อหาแอป) ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าใน Play Console ได้ที่การทำความเข้าใจบริการที่ทำงานอยู่เบื้องหน้าและข้อกำหนดของ Intent แบบเต็มหน้าจอ