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.
הצהרת הרשאה חדשה לשימוש בסוגי שירותים שפועלים בחזית
אם אפליקציות שמטרגטות את Android 14 נעזרות בשירות שפועל בחזית, הן צריכות להצהיר על הרשאה ספציפית, בהתאם לסוג השירות שפועל בחזית, שהוצגה ב-Android 14. ההרשאות האלה מופיעות בקטעים שמסומנים בתווית 'הרשאה שצריך להצהיר עליה בקובץ המניפסט' בקטע תרחישים לדוגמה ותהליך האכיפה לכל סוג של שירות שפועל בחזית בדף הזה.
כל ההרשאות מוגדרות כהרשאות רגילות ומוקצות כברירת מחדל. המשתמשים לא יכולים לבטל את ההרשאות האלה.
הכללת סוג השירות שפועל בחזית בזמן הריצה
השיטה המומלצת לאפליקציות שמפעילות שירותים שפועלים בחזית היא להשתמש בגרסה 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.
מיקום
- 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:
Note: In order to check that the user has enabled location services as well as granted access to the runtime permissions, use
PermissionChecker#checkSelfPermission()Note: The location runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a
locationforeground service while your app is in the background, unless you've been granted theACCESS_BACKGROUND_LOCATIONruntime permission. For more information, see Restrictions on starting foreground services that need while-in-use 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: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.
שיחת טלפון
- Foreground service type to declare in manifest under
android:foregroundServiceTypephoneCall- 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 has declared the
MANAGE_OWN_CALLSpermission in its manifest file.
- App has declared the
- App is the default dialer app through the
ROLE_DIALERrole.
- App is the default dialer app through the
- Description
Continue an ongoing call using the
ConnectionServiceAPIs.- Alternatives
If you need to make phone, video, or VoIP calls, consider using the
android.telecomlibrary.Consider using
CallScreeningServiceto screen calls.
העברת הודעות מרחוק
- 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.
שירות מקוצר
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
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. עם זאת, השירות עדיין צריך לעמוד בדרישות המוקדמות של הסוגים האחרים שצוינו. מידע נוסף זמין במסמכי העזרה של שירותי חזית.
שימוש מיוחד
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceTypespecialUse- הרשאה להצהרה במניפסט
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>
פטור מהמערכת
- סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceTypesystemExempted- הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_SYSTEM_EXEMPTED- קבוע להעברה אל
startForeground() FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED- דרישות מוקדמות בסביבת זמן הריצה
- ללא
- תיאור
הרשאה ששמורה לאפליקציות מערכת ולשילובים ספציפיים של מערכת, כדי להמשיך להשתמש בשירותים שפועלים בחזית.
כדי להשתמש בסוג הזה, האפליקציה צריכה לעמוד באחד לפחות מהקריטריונים הבאים:
- המכשיר נמצא במצב הדגמה
- האפליקציה היא בעלים של מכשיר
- האפליקציה היא בעלים של פרופיל
- אפליקציות בטיחות עם התפקיד
ROLE_EMERGENCY - אפליקציות של ניהול מכשירים
- אפליקציות עם ההרשאה
SCHEDULE_EXACT_ALARMאוUSE_EXACT_ALARMשמשתמשות בשירות שפועל בחזית כדי להמשיך להפעיל התראות ברקע, כולל התראות עם משוב מישוש בלבד. אפליקציות VPN (שמוגדרות באמצעות הגדרות > רשת ואינטרנט > VPN)
אחרת, ההצהרה על הסוג הזה תגרום למערכת להפעיל
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.