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:
cameraconnectedDevicedataSynchealthlocationmediaPlaybackmediaProjectionmicrophonephoneCallremoteMessagingshortServicespecialUsesystemExempted
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.
הכללת סוג השירות שפועל בחזית בזמן הריצה
השיטה המומלצת לאפליקציות שמפעילות שירותים שפועלים בחזית היא להשתמש בגרסה ServiceCompat של startForeground() (זמינה ב-androidx-core 1.12 ואילך), שבה מעבירים מספר שלם בינארי של סוגי השירותים שפועלים בחזית. אפשר לבחור להעביר ערך טיפוס אחד או יותר.
בדרך כלל, צריך להצהיר רק על הסוגים הנדרשים לתרחיש לדוגמה מסוים. כך קל יותר לעמוד בציפיות של המערכת לגבי כל סוג של שירות שפועל בחזית. במקרים שבהם שירות שפועל בחזית מופעל עם כמה סוגים, השירות שפועל בחזית צריך לעמוד בדרישות האכיפה של הפלטפורמה של כל הסוגים.
ServiceCompat.startForeground(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION)
אם לא מציינים את סוג השירות שפועל בחזית בקריאה, הערך שמוגדר כברירת מחדל הוא הערכים שמוגדרים במניפסט. אם לא ציינתם את סוג השירות במניפסט, המערכת תשליך את השגיאה MissingForegroundServiceTypeException.
אם לשירות שפועל בחזית נדרשות הרשאות חדשות אחרי ההפעלה שלו, צריך להפעיל שוב את startForeground() ולהוסיף את סוגי השירות החדשים. לדוגמה, נניח שאפליקציית כושר מפעילה שירות למעקב אחר ריצה שתמיד צריך להשתמש בו במידע location, אבל יכול להיות שהוא לא יצטרך הרשאות media. חשוב להצהיר גם על location וגם על mediaPlayback במניפסט. אם משתמש מתחיל ריצה ורוצה לעקוב רק אחרי המיקום שלו, האפליקציה צריכה לקרוא ל-startForeground() ולהעביר רק את סוג השירות location. לאחר מכן, אם המשתמש רוצה להפעיל את האודיו, קוראים שוב startForeground() ומעבירים את 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.
תרחישים לדוגמה ואכיפה שמיועדים לכל סוג של שירות שפועל בחזית
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
CAMERAruntime permissionNote: The
CAMERAruntime permission is subject to while-in-use restrictions. For this reason, you cannot create acameraforeground 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.
המכשיר המחובר
- 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 level 35 and lowerREAD_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
Note: The
BODY_SENSORSand sensor-based READ runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create ahealthforeground 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:foregroundServiceTypelocation- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_LOCATION- קבוע להעברה אל
startForeground() FOREGROUND_SERVICE_TYPE_LOCATION- דרישות מוקדמות בסביבת זמן הריצה
המשתמש צריך להפעיל את שירותי המיקום, והאפליקציה צריכה לקבל לפחות אחת מההרשאות הבאות בסביבת זמן הריצה:
הערה: כדי לבדוק שהמשתמש הפעיל את שירותי המיקום וגם העניק גישה להרשאות בסביבת זמן הריצה, משתמשים ב-
PermissionChecker#checkSelfPermission()הערה: ההרשאות של מיקום זמן ריצה כפופות להגבלות בזמן השימוש. לכן, אי אפשר ליצור שירות
locationבחזית בזמן שהאפליקציה נמצאת ברקע, אלא אם קיבלתם את ההרשאהACCESS_BACKGROUND_LOCATIONבסביבת זמן הריצה. מידע נוסף זמין במאמר הגבלות על הפעלת שירותים שפועלים בחזית שצריכים הרשאות לשימוש.- תיאור
תרחישים לדוגמה לטווח ארוך שדורשים גישה למיקום, כמו ניווט ושיתוף מיקום.
- אפשרויות אחרות
אם אתם רוצים שהאפליקציה תופעל כשהמשתמש מגיע למיקומים ספציפיים, מומלץ להשתמש ב-Geofence API במקום ב-Location 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: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.Note: The
RECORD_AUDIOruntime permission is subject to while-in-use restrictions. For this reason, you cannot create amicrophoneforeground 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:foregroundServiceTypephoneCall- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_PHONE_CALL- קבוע להעברה אל
startForeground() FOREGROUND_SERVICE_TYPE_PHONE_CALL- דרישות מוקדמות בסביבת זמן הריצה
לפחות אחד מהתנאים הבאים חייב להתקיים:
- האפליקציה הצהירה על ההרשאה
MANAGE_OWN_CALLSבקובץ המניפסט שלה.
- האפליקציה הצהירה על ההרשאה
- האפליקציה היא אפליקציית החיוג שמוגדרת כברירת מחדל דרך התפקיד
ROLE_DIALER.
- האפליקציה היא אפליקציית החיוג שמוגדרת כברירת מחדל דרך התפקיד
- תיאור
המשך שיחה פעילה באמצעות ממשקי ה-API של
ConnectionService.- אפשרויות אחרות
אם אתם צריכים להתקשר בטלפון, בשיחות וידאו או ב-VoIP, כדאי להשתמש בספרייה
android.telecom.כדאי להשתמש ב-
CallScreeningServiceכדי לסנן שיחות.
העברת הודעות מרחוק
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceTyperemoteMessaging- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_REMOTE_MESSAGING- קבוע להעברה אל
startForeground() FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- דרישות מוקדמות בסביבת זמן הריצה
- ללא
- תיאור
- העברת הודעות טקסט ממכשיר אחד למכשיר אחר. מאפשרת למשתמשים להמשיך את משימות ההודעות שלהם כשהם עוברים בין מכשירים.
שירות מקוצר
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceTypeshortService- הרשאה להצהרה במניפסט
- ללא
- קבוע להעברה אל
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. עם זאת, השירות עדיין צריך לעמוד בדרישות המוקדמות של הסוגים האחרים שצוינו. מידע נוסף זמין במסמכי העזרה של שירותי חזית.
שימוש מיוחד
- 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 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 בנושא שימוש בסוגי שירותים שפועלים בחזית
אם האפליקציה שלכם מטרגטת את Android מגרסה 14 ואילך, תצטרכו להצהיר על סוגי השירותים של האפליקציה בחזית בדף 'תוכן האפליקציה' ב-Play Console (Policy (מדיניות) > App content (תוכן האפליקציה)). מידע נוסף על הצהרת סוגי השירותים שפועלים בחזית ב-Play Console זמין במאמר הסבר על שירותים שפועלים בחזית ועל הדרישות לגבי כוונות במסך מלא.