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:
camera
connectedDevice
dataSync
health
location
mediaPlayback
mediaProjection
microphone
phoneCall
remoteMessaging
shortService
specialUse
systemExempted
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.
הצהרת הרשאה חדשה לשימוש בסוגי שירותים שפועלים בחזית
If apps that target Android 14 use a foreground service, they must declare a specific permission, based on the foreground service type, that Android 14 introduces. These permissions appear in the sections labeled "permission that you must declare in your manifest file" in the intended use cases and enforcement for each foreground service type section on this page.
All of the permissions are defined as normal permissions and are granted by default. Users cannot revoke these permissions.
הכללת סוג השירות שפועל בחזית בזמן הריצה
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
.
בדיקות זמן ריצה של המערכת
The system checks for proper use of foreground service types and confirms that
the app has requested the proper runtime permissions or uses the required APIs.
For instance, the system expects apps that use the foreground service type
FOREGROUND_SERVICE_TYPE_LOCATION
type to request either
ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
.
This implies that apps must follow a very specific
order of operations when requesting permissions from the user and starting
foreground services. Permissions must be requested and granted before the
app attempts to call startForeground()
. Apps that request the appropriate
permissions after the foreground service has been started must change this order
of operations and request the permission before starting the foreground service.
The specifics of platform enforcement appear in the sections labeled "runtime requirements" in the intended use cases and enforcement for each foreground service type section on this page.
תרחישים לדוגמה ואכיפה שמיועדים לכל סוג של שירות שפועל בחזית
כדי להשתמש בסוג מסוים של שירות שפועל בחזית, צריך להצהיר על הרשאה מסוימת בקובץ המניפסט, לעמוד בדרישות ספציפיות בסביבת זמן הריצה, והאפליקציה צריכה לעמוד באחד מהתרחישי לדוגמה שמיועדים לסוג הזה. בקטעים הבאים מוסבר מהי ההרשאה שצריך להצהיר עליה, מהן הדרישות המוקדמות בסביבת זמן הריצה ומה הם תרחישים לדוגמה שבהם נעשה שימוש בכל סוג.
מצלמה
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
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:
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: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:
Declare the
HIGH_SAMPLING_RATE_SENSORS
permission in your manifest.Request and be granted at least one of the following runtime permissions:
BODY_SENSORS
on API level 35 and lowerREAD_HEART_RATE
READ_SKIN_TEMPERATURE
READ_OXYGEN_SATURATION
ACTIVITY_RECOGNITION
Note: The
BODY_SENSORS
and sensor-based READ runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create ahealth
foreground service that uses body sensors while your app is in the background unless you've been granted theBODY_SENSORS_BACKGROUND
(API level 33 to 35) orREAD_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()
הערה: ההרשאות של מיקום זמן ריצה כפופות להגבלות בזמן השימוש. לכן, אי אפשר ליצור שירות
location
בחזית בזמן שהאפליקציה נמצאת ברקע, אלא אם קיבלתם את ההרשאהACCESS_BACKGROUND_LOCATION
בסביבת זמן הריצה. מידע נוסף זמין במאמר הגבלות על הפעלת שירותים שפועלים בחזית שצריכים הרשאות לשימוש.- תיאור
תרחישים לדוגמה לטווח ארוך שדורשים גישה למיקום, כמו ניווט ושיתוף מיקום.
- אפשרויות אחרות
אם אתם רוצים שהאפליקציה תופעל כשהמשתמש מגיע למיקומים ספציפיים, מומלץ להשתמש ב-Geofence API במקום ב-Location API.
מדיה
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceType
mediaPlayback
- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_MEDIA_PLAYBACK
- קבוע להעברה אל
startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
- דרישות מוקדמות בסביבת זמן הריצה
- ללא
- תיאור
- המשך ההפעלה של האודיו או הווידאו מהרקע. תמיכה בפונקציונליות של הקלטת וידאו דיגיטלית (DVR) ב-Android TV.
- אפשרויות אחרות
- אם אתם מציגים סרטון ב'תמונה בתוך תמונה', תוכלו להשתמש במצב 'תמונה בתוך תמונה'.
הקרנת מדיה
- 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 amicrophone
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.
שיחת טלפון
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceType
phoneCall
- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_PHONE_CALL
- קבוע להעברה אל
startForeground()
FOREGROUND_SERVICE_TYPE_PHONE_CALL
- דרישות מוקדמות בסביבת זמן הריצה
לפחות אחד מהתנאים הבאים חייב להתקיים:
- האפליקציה הצהירה על ההרשאה
MANAGE_OWN_CALLS
בקובץ המניפסט שלה.
- האפליקציה הצהירה על ההרשאה
- האפליקציה היא אפליקציית החיוג שמוגדרת כברירת מחדל דרך התפקיד
ROLE_DIALER
.
- האפליקציה היא אפליקציית החיוג שמוגדרת כברירת מחדל דרך התפקיד
- תיאור
המשך שיחה פעילה באמצעות ממשקי ה-API של
ConnectionService
.- אפשרויות אחרות
אם אתם צריכים להתקשר בטלפון, בשיחות וידאו או ב-VoIP, כדאי להשתמש בספרייה
android.telecom
.כדאי להשתמש ב-
CallScreeningService
כדי לסנן שיחות.
העברת הודעות מרחוק
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
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
. לכן, מומלץ להטמיע את פונקציית ה-callbackService.onTimeout()
.קריאת החזרה (call back)
Service.onTimeout()
לא קיימת ב-Android מגרסה 13 ומטה. אם אותו שירות פועל במכשירים כאלה, הוא לא מקבל זמן קצוב לתפוגה ולא מתרחשת בו בעיה מסוג ANR. חשוב לוודא שהשירות יפסיק לפעול ברגע שהוא יסיים את משימת העיבוד, גם אם הוא עדיין לא קיבל את הקריאה החוזרת (callback) שלService.onTimeout()
.חשוב לציין שאם לא יתקיים הזמן הקצוב לתפוגה של
shortService
, תופיע באפליקציה שגיאת ANR גם אם פועלות בה שירותים חוקיים אחרים בחזית או תהליכים אחרים במחזור החיים של האפליקציה.אם אפליקציה גלויה למשתמש או עומדת באחד מההחרגות שמאפשרות להפעיל שירותים שפועלים בחזית מהרקע, קריאה חוזרת ל-
Service.StartForeground()
עם הפרמטרFOREGROUND_SERVICE_TYPE_SHORT_SERVICE
מאריכה את הזמן הקצוב לתפוגה ב-3 דקות נוספות. אם האפליקציה לא גלויה למשתמש ולא עומדת באחד מההחרגות, כל ניסיון להפעיל שירות נוסף שפועל בחזית, ללא קשר לסוג שלו, יגרום לForegroundServiceStartNotAllowedException
.אם משתמש משבית את אופטימיזציית הסוללה באפליקציה, היא עדיין מושפעת מזמן הקצאת הזמן הקצוב לתפוגה של shortService FGS.
אם מפעילים שירות שפועל בחזית שכולל את הסוג
shortService
וגם סוג אחר של שירות שפועל בחזית, המערכת מתעלמת מהצהרת הסוגshortService
. עם זאת, השירות עדיין צריך לעמוד בדרישות המוקדמות של הסוגים האחרים שצוינו. מידע נוסף זמין במסמכי העזרה של שירותי חזית.
שימוש מיוחד
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceType
specialUse
- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_SPECIAL_USE
- קבוע להעברה אל
startForeground()
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
- דרישות מוקדמות לסביבת זמן ריצה
- ללא
- תיאור
רלוונטי לתרחישים לדוגמה תקינים של שירות שפועל בחזית, שלא נכללים בפלטפורמה האחרת סוגי השירותים שפועלים בחזית.
בנוסף להצהרה על
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
בסוג השירות שפועל בחזית, מפתחים צריכים להצהיר על תרחישים לדוגמה . כדי לעשות זאת, הן מציינות את הרכיב<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:foregroundServiceType
systemExempted
- 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_EMERGENCY
role - Device Admin apps
- Apps holding
SCHEDULE_EXACT_ALARM
orUSE_EXACT_ALARM
permission and are using Foreground Service to continue alarms in the background, including haptics-only alarms. VPN apps (configured using Settings > Network & Internet > VPN)
Otherwise, declaring this type causes the system to throw a
ForegroundServiceTypeNotAllowedException
.
אכיפת המדיניות של Google Play בנושא שימוש בסוגי שירותים שפועלים בחזית
If your app targets Android 14 or higher, you'll need to declare your app's foreground service types in the Play Console's app content page (Policy > App content). For more information on how to declare your foreground service types in Play Console, see Understanding foreground service and full-screen intent requirements.