ตั้งแต่ Android 14 (API ระดับ 34) เป็นต้นไป คุณต้องประกาศ
ประเภทบริการที่เหมาะสมสำหรับบริการที่ทำงานอยู่เบื้องหน้าแต่ละรายการ ซึ่งหมายความว่าคุณต้องประกาศ
ประเภทบริการในไฟล์ Manifest ของแอป และขอสิทธิ์บริการที่ทำงานอยู่เบื้องหน้าที่เหมาะสมกับประเภทดังกล่าวด้วย (นอกเหนือจากการขอสิทธิ์
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.
การซิงค์ข้อมูล
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ภายใต้
android:foregroundServiceTypedataSync- สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_DATA_SYNC- ค่าคงที่ที่จะส่งไปยัง
startForeground() FOREGROUND_SERVICE_TYPE_DATA_SYNC- ข้อกำหนดเบื้องต้นของรันไทม์
- ไม่มี
- คำอธิบาย
การดำเนินการโอนข้อมูล เช่น การดำเนินการต่อไปนี้
- การอัปโหลดหรือดาวน์โหลดข้อมูล
- การดำเนินการสำรองและกู้คืนข้อมูล
- การดำเนินการนำเข้าหรือส่งออก
- ดึงข้อมูล
- การประมวลผลไฟล์ในเครื่อง
- โอนข้อมูลระหว่างอุปกรณ์กับระบบคลาวด์ผ่านเครือข่าย
- ทางเลือก
ดูข้อมูลโดยละเอียดได้ที่ทางเลือกแทนบริการที่ทำงานเบื้องหน้าสำหรับการซิงค์ข้อมูล
สุขภาพ
- Foreground service type to declare in manifest under
android:foregroundServiceTypehealth- 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:
Declare the
HIGH_SAMPLING_RATE_SENSORSpermission in your manifest.Request and be granted at least one of the following runtime permissions:
BODY_SENSORSon API 35 and lowerREAD_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
- Description
Any long-running use cases to support apps in the fitness category such as exercise trackers.
ตำแหน่ง
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ในส่วน
android:foregroundServiceTypelocation- สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_LOCATION- ค่าคงที่ที่จะส่งไปยัง
startForeground() FOREGROUND_SERVICE_TYPE_LOCATION- ข้อกําหนดเบื้องต้นของรันไทม์
ผู้ใช้ต้องเปิดใช้บริการตำแหน่งและแอปต้องได้รับสิทธิ์รันไทม์อย่างน้อย 1 รายการต่อไปนี้
- คำอธิบาย
กรณีการใช้งานที่ทำงานต่อเนื่องซึ่งต้องใช้สิทธิ์เข้าถึงตำแหน่ง เช่น การนำทางและการแชร์ตำแหน่ง
- ทางเลือก
หากต้องการให้แอปทริกเกอร์เมื่อผู้ใช้ไปถึงสถานที่ที่เฉพาะเจาะจง ให้ลองใช้ 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.
การโทร
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ภายใต้
android:foregroundServiceTypephoneCall- สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_PHONE_CALL- ค่าคงที่ที่จะส่งไปยัง
startForeground() FOREGROUND_SERVICE_TYPE_PHONE_CALL- ข้อกำหนดเบื้องต้นของรันไทม์
เงื่อนไขต่อไปนี้อย่างน้อย 1 รายการต้องเป็นจริง
- แอปได้ประกาศสิทธิ์
MANAGE_OWN_CALLSในไฟล์ Manifest
- แอปได้ประกาศสิทธิ์
- แอปเป็นแอปโทรศัพท์เริ่มต้นผ่านบทบาท
ROLE_DIALER
- แอปเป็นแอปโทรศัพท์เริ่มต้นผ่านบทบาท
- คำอธิบาย
ต่อสายการโทรที่กำลังดำเนินการอยู่โดยใช้ API ของ
ConnectionService- ทางเลือก
หากต้องการโทรศัพท์ วิดีโอ หรือ VoIP ให้ลองใช้ไลบรารี
android.telecomลองใช้
CallScreeningServiceเพื่อคัดกรองสายเรียกเข้า
การรับส่งข้อความระยะไกล
- Foreground service type to declare in manifest under
android:foregroundServiceTyperemoteMessaging- 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:foregroundServiceTypeshortService- สิทธิ์ในการประกาศในไฟล์ 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อย่างไรก็ตาม บริการดังกล่าวยังคงต้องเป็นไปตามข้อกําหนดเบื้องต้นของประเภทอื่นๆ ที่ประกาศไว้ ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบเกี่ยวกับบริการที่ทำงานอยู่เบื้องหน้า
การใช้งานพิเศษ
- Foreground service type to declare in manifest under
android:foregroundServiceTypespecialUse- 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_USEforeground 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 thespecialUsetype.<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 สำหรับการใช้ประเภทบริการที่ทำงานอยู่เบื้องหน้า
หากแอปกำหนดเป้าหมายเป็น Android 14 ขึ้นไป คุณจะต้องประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าของแอปในหน้าเนื้อหาแอปของ Play Console (นโยบาย > เนื้อหาแอป) ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าใน Play Console ได้ที่การทำความเข้าใจบริการที่ทำงานอยู่เบื้องหน้าและข้อกำหนดของ Intent แบบเต็มหน้าจอ