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

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() 之前,必须先请求并获得所需的权限。在启动前台服务后请求相应权限的应用必须更改此顺序,并在启动前台服务之前请求该权限。

本页面的每种前台服务类型的预期用例和强制执行部分中标记为“运行时要求”的部分列出了平台强制执行的具体内容。

กรณีการใช้งานและการบังคับใช้ที่กำหนดไว้สำหรับบริการที่ทำงานอยู่เบื้องหน้าแต่ละประเภท

หากต้องการใช้บริการที่ทำงานอยู่เบื้องหน้าประเภทหนึ่งๆ คุณต้องประกาศสิทธิ์ที่เฉพาะเจาะจงในไฟล์ Manifest, ต้องปฏิบัติตามข้อกำหนดรันไทม์ที่เฉพาะเจาะจง และแอปต้องเป็นไปตามชุด Use Case ที่ต้องการสำหรับประเภทนั้น ส่วนต่อไปนี้อธิบายสิทธิ์ที่คุณต้องประกาศ ข้อกําหนดเบื้องต้นรันไทม์ และกรณีการใช้งานที่ต้องการสําหรับแต่ละประเภท

กล้อง

要在 android:foregroundServiceType 下在清单中声明的前台服务类型
camera
在清单中声明的权限
FOREGROUND_SERVICE_CAMERA
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_CAMERA
运行时前提条件

请求并获得 CAMERA 运行时权限

注意CAMERA 运行时权限受使用时限制的约束。因此,除少数例外情况外,您无法在应用在后台运行时创建 camera 前台服务。如需了解详情,请参阅与启动需要“使用时”权限的前台服务相关的限制

说明

继续在后台访问相机,例如支持多任务的视频聊天应用。

อุปกรณ์ที่เชื่อมต่อ

Foreground service type to declare in manifest under
android:foregroundServiceType
connectedDevice
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:

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

สุขภาพ

Foreground service type to declare in manifest under
android:foregroundServiceType
health
Permission to declare in your manifest
FOREGROUND_SERVICE_HEALTH
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_HEALTH
Runtime prerequisites

At least one of the following conditions must be true:

Note: The BODY_SENSORS and sensor-based READ runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a health foreground service that uses body sensors while your app is in the background unless you've been granted the BODY_SENSORS_BACKGROUND (API level 33 to 35) or READ_HEALTH_DATA_IN_BACKGROUND (API level 36 and higher) permissions. For more information, see Restrictions on starting foreground services that need while-in-use permissions.

Description

Any long-running use cases to support apps in the fitness category such as exercise trackers.

ตำแหน่ง

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

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

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

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

说明

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

替代方案

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

สื่อ

Foreground service type to declare in manifest under
android:foregroundServiceType
mediaPlayback
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.

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

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

เรียกใช้เมธอด createScreenCaptureIntent() ก่อนเริ่มบริการที่ทำงานอยู่เบื้องหน้า ซึ่งจะแสดงการแจ้งเตือนสิทธิ์ให้ผู้ใช้ทราบ ผู้ใช้ต้องให้สิทธิ์ก่อนที่คุณจึงจะสร้างบริการได้

หลังจากสร้างบริการที่ทำงานอยู่เบื้องหน้าแล้ว คุณจะเรียกใช้ MediaProjectionManager.getMediaProjection() ได้

คำอธิบาย

ฉายเนื้อหาไปยังจอแสดงผลที่ไม่ใช่อุปกรณ์หลักหรืออุปกรณ์ภายนอกโดยใช้ MediaProjection API เนื้อหานี้ไม่จำเป็นต้องเป็นเนื้อหาสื่อโดยเฉพาะ

ทางเลือก

หากต้องการสตรีมสื่อไปยังอุปกรณ์อื่น ให้ใช้ Google Cast SDK

ไมโครโฟน

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

ขอและได้รับสิทธิ์รันไทม์ RECORD_AUDIO

หมายเหตุ: สิทธิ์รันไทม์ RECORD_AUDIO อยู่ภายใต้ข้อจำกัดขณะใช้งาน ด้วยเหตุนี้ คุณจึงสร้างmicrophoneบริการที่ทำงานอยู่เบื้องหน้าไม่ได้ขณะที่แอปอยู่ในเบื้องหลัง ยกเว้นบางกรณี ดูข้อมูลเพิ่มเติมได้ที่ข้อจำกัดในการเริ่มบริการที่ทำงานอยู่เบื้องหน้าซึ่งต้องใช้สิทธิ์ขณะใช้งาน

คำอธิบาย

ไมโครโฟนจะบันทึกเสียงจากเบื้องหลังอย่างต่อเนื่อง เช่น โปรแกรมอัดเสียงหรือแอปการสื่อสาร

การโทร

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.

การรับส่งข้อความระยะไกล

Foreground service type to declare in manifest under
android:foregroundServiceType
remoteMessaging
Permission to declare in your manifest
FOREGROUND_SERVICE_REMOTE_MESSAGING
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
Runtime prerequisites
None
Description
Transfer text messages from one device to another. Assists with continuity of a user's messaging tasks when they switch devices.

บริการสั้นๆ

ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ในส่วน
android:foregroundServiceType
shortService
สิทธิ์ในการประกาศในไฟล์ Manifest
ไม่มี
ค่าคงที่ที่จะส่งไปยัง 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 ข้อความ ANRP พูดถึง FOREGROUND_SERVICE_TYPE_SHORT_SERVICE ด้วยเหตุนี้ การใช้Service.onTimeout()การเรียกกลับจึงถือเป็นแนวทางปฏิบัติแนะนำ

ฟีเจอร์ติดต่อกลับ Service.onTimeout() ไม่มีให้บริการใน Android 13 และต่ำกว่า หากบริการเดียวกันทำงานในอุปกรณ์ดังกล่าว บริการจะไม่หมดเวลาและจะไม่เกิด ANR ตรวจสอบว่าบริการหยุดทำงานทันทีที่ประมวลผลงานเสร็จ แม้ว่าจะยังไม่ได้รับService.onTimeout() callback ก็ตาม

โปรดทราบว่าหากไม่ปฏิบัติตามการหมดเวลาของ shortService แอปจะแสดง ANR แม้ว่าจะมีบริการที่ทำงานอยู่เบื้องหน้าอื่นๆ ที่ถูกต้องหรือกระบวนการอื่นๆ ของวงจรชีวิตของแอปที่ทำงานอยู่ก็ตาม

หากผู้ใช้มองเห็นแอปหรือแอปเป็นไปตามข้อยกเว้นข้อใดข้อหนึ่งซึ่งอนุญาตให้เริ่มบริการที่ทำงานอยู่เบื้องหน้าจากเบื้องหลังได้ การเรียกใช้ Service.StartForeground() อีกครั้งด้วยพารามิเตอร์ FOREGROUND_SERVICE_TYPE_SHORT_SERVICE จะขยายเวลาหมดเวลาอีก 3 นาที หากผู้ใช้ไม่เห็นแอปและไม่เป็นไปตามข้อยกเว้นข้อใดข้อหนึ่ง การพยายามเริ่มบริการที่ทำงานอยู่เบื้องหน้าอีกรายการหนึ่งไม่ว่าประเภทใดก็ตามจะทำให้เกิดForegroundServiceStartNotAllowedException

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

หากคุณเริ่มบริการที่ทำงานอยู่เบื้องหน้าซึ่งมีประเภท shortService และประเภทบริการที่ทำงานอยู่เบื้องหน้าอื่น ระบบจะไม่สนใจการประกาศประเภท shortService อย่างไรก็ตาม บริการดังกล่าวยังคงต้องเป็นไปตามข้อกําหนดเบื้องต้นของประเภทอื่นๆ ที่ประกาศไว้ ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบเกี่ยวกับบริการที่ทำงานอยู่เบื้องหน้า

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

要在清单中声明的前台服务类型
android:foregroundServiceType
specialUse
在清单中声明的权限
FOREGROUND_SERVICE_SPECIAL_USE
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
运行时前提条件
说明

涵盖其他前台服务类型未涵盖的所有有效前台服务用例。

除了声明 FOREGROUND_SERVICE_TYPE_SPECIAL_USE 前台服务类型之外,开发者还应在清单中声明用例。为此,他们会在 <service> 元素内指定 <property> 元素。这些值和相应的应用场景 。用途 您提供的案例均为自由形式,因此,您应确保提供足够的 相关信息,让审核人员了解您为何需要使用 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 แบบเต็มหน้าจอ