เริ่มตั้งแต่ Android 14 (API ระดับ 34) คุณต้องประกาศประเภทบริการที่เหมาะสมสำหรับบริการที่ทำงานอยู่เบื้องหน้าแต่ละรายการ ซึ่งหมายความว่าคุณต้องประกาศประเภทบริการในไฟล์ Manifest ของแอป และขอสิทธิ์บริการที่ทำงานอยู่เบื้องหน้าที่เหมาะสมกับประเภทดังกล่าวด้วย (นอกเหนือจากการขอสิทธิ์ FOREGROUND_SERVICE
) นอกจากนี้ คุณอาจต้องขอสิทธิ์รันไทม์ก่อนเปิดตัวบริการ ทั้งนี้ขึ้นอยู่กับประเภทบริการที่ทำงานอยู่เบื้องหน้า
กล้อง
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ภายใน
android:foregroundServiceType camera- สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_CAMERA- ค่าคงที่ที่จะส่งไปยัง
startForeground() FOREGROUND_SERVICE_TYPE_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.
สุขภาพ
- 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.
ตำแหน่ง
- 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:
- 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: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.
ไมโครโฟน
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่ต้องประกาศในไฟล์ Manifest ภายใต้
android:foregroundServiceTypemicrophone- สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_MICROPHONE- ค่าคงที่ที่จะส่งไปยัง
startForeground() FOREGROUND_SERVICE_TYPE_MICROPHONE- ข้อกำหนดเบื้องต้นของรันไทม์
ขอและรับสิทธิ์รันไทม์
RECORD_AUDIO- คำอธิบาย
บันทึกเสียงจากไมโครโฟนอย่างต่อเนื่องจากเบื้องหลัง เช่น เครื่องบันทึกเสียงหรือ แอปการสื่อสาร
การโทร
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่ต้องประกาศในไฟล์ 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เพื่อคัดกรองสายเรียกเข้า
การรับส่งข้อความระยะไกล
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ในส่วน
android:foregroundServiceTyperemoteMessaging- สิทธิ์ในการประกาศในไฟล์ Manifest
FOREGROUND_SERVICE_REMOTE_MESSAGING- ค่าคงที่ที่จะส่งไปยัง
startForeground() FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- ข้อกําหนดเบื้องต้นของรันไทม์
- ไม่มี
- คำอธิบาย
- โอน SMS จากอุปกรณ์เครื่องหนึ่งไปยังอีกเครื่อง ช่วยเหลืองานการรับส่งข้อความของผู้ใช้ให้มีความต่อเนื่องเมื่อเปลี่ยนอุปกรณ์
บริการระยะสั้น
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ 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อย่างไรก็ตาม บริการดังกล่าวยังคงต้องเป็นไปตามข้อกําหนดเบื้องต้นของประเภทอื่นๆ ที่ประกาศไว้ ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบเกี่ยวกับบริการที่ทำงานอยู่เบื้องหน้า
การใช้งานพิเศษ
- ประเภทบริการที่ทำงานอยู่เบื้องหน้าที่จะประกาศในไฟล์ Manifest ภายใต้
android:foregroundServiceTypespecialUse- สิทธิ์ในการประกาศในไฟล์ 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>
ได้รับการยกเว้นจากระบบ
- 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 แบบเต็มหน้าจอ