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

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.

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

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.

מצלמה

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

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:

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.

מיקום

Foreground service type to declare in manifest under
android:foregroundServiceType
location
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 location foreground service while your app is in the background, unless you've been granted the ACCESS_BACKGROUND_LOCATION runtime 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.

מדיה

סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
android:foregroundServiceType
mediaPlayback
הרשאה להצהרה במניפסט
FOREGROUND_SERVICE_MEDIA_PLAYBACK
קבוע להעברה אל startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
דרישות מוקדמות בסביבת זמן הריצה
ללא
תיאור
המשך ההפעלה של האודיו או הווידאו מהרקע. תמיכה בפונקציונליות של הקלטת וידאו דיגיטלית (DVR) ב-Android TV.
אפשרויות אחרות
אם אתם מציגים סרטון ב'תמונה בתוך תמונה', תוכלו להשתמש במצב 'תמונה בתוך תמונה'.

הקרנת מדיה

要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
mediaProjection
在清单中声明的权限
FOREGROUND_SERVICE_MEDIA_PROJECTION
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
运行时前提条件

在启动前台服务之前调用 createScreenCaptureIntent() 方法。这样做会向用户显示权限通知;用户必须先授予权限,您才能创建服务。

创建前台服务后,您可以调用 MediaProjectionManager.getMediaProjection()

说明

使用 MediaProjection API 将内容投影到非主要显示屏或外部设备。这些内容不必全都为媒体内容。

替代方案

如需将媒体流式传输到其他设备,请使用 Google Cast SDK

מיקרופון

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

מבקשים את ההרשאה RECORD_AUDIO בזמן הריצה ומקבלים אותה.

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

תיאור

המשך הקלטה מהמיקרופון ברקע, כמו מכשירי הקלטה קולית או אפליקציות תקשורת.

שיחת טלפון

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

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

  • האפליקציה הצהירה על ההרשאה MANAGE_OWN_CALLS בקובץ המניפסט שלה.
  • האפליקציה היא אפליקציית החיוג שמוגדרת כברירת מחדל דרך התפקיד ROLE_DIALER.
תיאור

המשך שיחה פעילה באמצעות ממשקי ה-API של ConnectionService.

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

אם אתם צריכים להתקשר בטלפון, בשיחות וידאו או ב-VoIP, כדאי להשתמש בספרייה android.telecom.

כדאי להשתמש ב-CallScreeningService כדי לסנן שיחות.

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

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.

שירות מקוצר

סוג השירות שפועל בחזית שצריך להצהיר עליו במניפסט בקטע
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. לכן, מומלץ להטמיע את פונקציית ה-callback‏ Service.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:foregroundServiceType
specialUse
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_USE foreground 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 the specialUse type.

<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 or USE_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 בנושא שימוש בסוגי שירותים שפועלים בחזית

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