חובה לבחור סוגים של שירותים שפועלים בחזית

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:

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.

בדיקות זמן ריצה של המערכת

המערכת בודקת אם נעשה שימוש תקין בסוגי שירותים שפועלים בחזית, ומאשרת שהאפליקציה ביקשה את הרשאות זמן הריצה המתאימות או משתמשת בממשקי ה-API הנדרשים. לדוגמה, המערכת מצפה שאפליקציות שמשתמשות בשירות שפועל בחזית מסוג FOREGROUND_SERVICE_TYPE_LOCATION יבקשו את ההרשאה ACCESS_COARSE_LOCATION או ACCESS_FINE_LOCATION.

המשמעות היא שאפליקציות צריכות לפעול לפי סדר פעולות ספציפי מאוד כשהן מבקשות הרשאות מהמשתמשים ומפעילות שירותים שפועלים בחזית. צריך לבקש את ההרשאות ולהעניק אותן לפני שהאפליקציה תנסה לבצע קריאה ל-startForeground(). אפליקציות שמבקשות את ההרשאות המתאימות אחרי שהשירות שפועל בחזית הופעל צריכות לשנות את סדר הפעולות ולבקש את ההרשאה לפני הפעלת השירות שפועל בחזית.

הפרטים של אכיפת הפלטפורמה מופיעים בקטעים שמסומנים בתווית 'דרישות בסביבת זמן הריצה' בקטע תרחישים לדוגמה ותהליך האכיפה של כל סוג של שירות שפועל בחזית בדף הזה.

תרחישים לדוגמה ואכיפה שמיועדים לכל סוג של שירות שפועל בחזית

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 CAMERA runtime permission

Note: The CAMERA runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a camera 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 to access the camera from the background, such as video chat apps that allow for multitasking.

המכשיר המחובר

סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceType
connectedDevice
הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_CONNECTED_DEVICE
קבוע להעברה אל startForeground()
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
דרישות מוקדמות בסביבת זמן הריצה

לפחות אחד מהתנאים הבאים חייב להתקיים:

תיאור

אינטראקציות עם מכשירים חיצוניים שדורשים חיבור Bluetooth,‏ NFC,‏ IR,‏ USB או חיבור לרשת.

אפשרויות אחרות

אם באפליקציה שלכם יש צורך לבצע העברה רציפה של נתונים למכשיר חיצוני, כדאי להשתמש במקום זאת במנהל המכשיר הנלווה. אתם יכולים להשתמש ב-companion device presence API כדי לעזור לאפליקציה להמשיך לפעול בזמן שהמכשיר הנלווה נמצא בטווח.

אם האפליקציה שלכם צריכה לסרוק מכשירי Bluetooth, כדאי להשתמש ב-Bluetooth scan API במקום זאת.

סנכרון נתונים

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:

Note: The BODY_SENSORS and sensor-based READ runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a health foreground service that uses body sensors while your app is in the background unless you've been granted the BODY_SENSORS_BACKGROUND (API level 33 to 35) or READ_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.

מדיה

Foreground service type to declare in manifest under
android:foregroundServiceType
mediaPlayback
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: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 a microphone 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.

שיחת טלפון

Foreground service type to declare in manifest under
android:foregroundServiceType
phoneCall
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 is the default dialer app through the ROLE_DIALER role.
Description

Continue an ongoing call using the ConnectionService APIs.

Alternatives

If you need to make phone, video, or VoIP calls, consider using the android.telecom library.

Consider using CallScreeningService to screen calls.

העברת הודעות מרחוק

Foreground service type to declare in manifest under
android:foregroundServiceType
remoteMessaging
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.

שירות מקוצר

Foreground service type to declare in manifest under
android:foregroundServiceType
shortService
Permission to declare in your manifest
None
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
Runtime prerequisites
None
Description

Quickly finish critical work that cannot be interrupted or postponed.

This type has some unique characteristics:

  • Can only run for a short period of time (about 3 minutes).
  • No support for sticky foreground services.
  • Cannot start other foreground services.
  • Doesn't require a type-specific permission, though it still requires the FOREGROUND_SERVICE permission.
  • A shortService can only change to another service type if the app is currently eligible to start a new foreground service.
  • A foreground service can change its type to shortService at any time, at which point the timeout period begins.

The timeout for shortService begins from the moment that Service.startForeground() is called. The app is expected to call Service.stopSelf() or Service.stopForeground() before the timeout occurs. Otherwise, the new Service.onTimeout() is called, giving apps a brief opportunity to call stopSelf() or stopForeground() to stop their service.

A short time after Service.onTimeout() is called, the app enters a cached state and is no longer considered to be in the foreground, unless the user is actively interacting with the app. A short time after the app is cached and the service has not stopped, the app receives an ANR. The ANR message mentions FOREGROUND_SERVICE_TYPE_SHORT_SERVICE. For these reasons, it's considered best practice to implement the Service.onTimeout() callback.

The Service.onTimeout() callback doesn't exist on Android 13 and lower. If the same service runs on such devices, it doesn't receive a timeout, nor does it ANR. Make sure that your service stops as soon as it finishes the processing task, even if it hasn't received the Service.onTimeout() callback yet.

It's important to note that if the timeout of the shortService is not respected, the app will ANR even if it has other valid foreground services or other app lifecycle processes running.

If an app is visible to the user or satisfies one of the exemptions that allow foreground services to be started from the background, calling Service.StartForeground() again with the FOREGROUND_SERVICE_TYPE_SHORT_SERVICE parameter extends the timeout by another 3 minutes. If the app isn't visible to the user and doesn't satisfy one of the exemptions, any attempt to start another foreground service, regardless of type, causes a ForegroundServiceStartNotAllowedException.

If a user disables battery optimization for your app, it's still affected by the timeout of shortService FGS.

If you start a foreground service that includes the shortService type and another foreground service type, the system ignores the shortService type declaration. However, the service must still adhere to the prerequisites of the other declared types. For more information, see the Foreground services documentation.

שימוש מיוחד

סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
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>

פטור מהמערכת

סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceType
systemExempted
הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_SYSTEM_EXEMPTED
קבוע להעברה אל startForeground()
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
דרישות מוקדמות בסביבת זמן הריצה
ללא
תיאור

הרשאה ששמורה לאפליקציות מערכת ולשילובים ספציפיים של מערכת, כדי להמשיך להשתמש בשירותים שפועלים בחזית.

כדי להשתמש בסוג הזה, האפליקציה צריכה לעמוד באחד לפחות מהקריטריונים הבאים:

  • המכשיר נמצא במצב הדגמה
  • האפליקציה היא בעלים של מכשיר
  • האפליקציה היא בעלים של פרופיל
  • אפליקציות בטיחות עם התפקיד ROLE_EMERGENCY
  • אפליקציות של ניהול מכשירים
  • אפליקציות עם ההרשאה SCHEDULE_EXACT_ALARM או USE_EXACT_ALARM שמשתמשות בשירות שפועל בחזית כדי להמשיך להפעיל התראות ברקע, כולל התראות עם משוב מישוש בלבד.
  • אפליקציות VPN (שמוגדרות באמצעות הגדרות > רשת ואינטרנט > VPN)

    אחרת, ההצהרה על הסוג הזה תגרום למערכת להפעיל ForegroundServiceTypeNotAllowedException.

אכיפת המדיניות של Google Play בנושא שימוש בסוגי שירותים שפועלים בחזית

אם האפליקציה שלכם מטרגטת את Android מגרסה 14 ואילך, תצטרכו להצהיר על סוגי השירותים של האפליקציה בחזית בדף 'תוכן האפליקציה' ב-Play Console (Policy (מדיניות) > App content (תוכן האפליקציה)). מידע נוסף על הצהרת סוגי השירותים שפועלים בחזית ב-Play Console זמין במאמר הסבר על שירותים שפועלים בחזית ועל הדרישות לגבי כוונות במסך מלא.