انواع خدمات پیش زمینه

از اندروید ۱۴ (سطح API ۳۴)، شما باید برای هر سرویس پیش‌زمینه، یک نوع سرویس مناسب تعریف کنید. این یعنی باید نوع سرویس را در مانیفست برنامه خود تعریف کنید و همچنین مجوز سرویس پیش‌زمینه مناسب را برای آن نوع درخواست کنید (علاوه بر درخواست مجوز FOREGROUND_SERVICE ). علاوه بر این، بسته به نوع سرویس پیش‌زمینه، ممکن است قبل از راه‌اندازی سرویس، مجبور شوید مجوزهای زمان اجرا را نیز درخواست کنید.

دوربین

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

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: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:

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:

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: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
mediaProcessing
Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PROCESSING
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
Runtime prerequisites
None
Description

Service for performing time-consuming operations on media assets, like converting media to different formats. The system allows this service a limited time to run; under normal circumstances, this time limit would be 6 hours out of every 24. (This limit is shared by all of an app's mediaProcessing foreground services.)

Your app should manually stop the media processing service in the following scenario:

If the timeout period is reached, the system calls the service's Service.onTimeout(int, int) method. At this time, the service has a few seconds to call Service.stopSelf(). If the service does not call Service.stopSelf(), an ANR will occur with this error message: "A foreground service of <fgs_type> did not stop within its timeout: <component_name>".

Note: Service.onTimeout(int, int) is not available on Android 14 or lower. On devices running those versions, if a media processing service reaches the timeout period, the system immediately caches the app. For this reason, your app shouldn't wait to get a timeout notification. Instead, it should terminate the foreground service or change it to a background service as soon as appropriate.

طرح‌ریزی رسانه‌ای

نوع سرویس پیش‌زمینه برای اعلام در مانیفست زیر
android:foregroundServiceType
mediaProjection
اجازه اعلان در مانیفست شما
FOREGROUND_SERVICE_MEDIA_PROJECTION
ثابتی که باید به startForeground() ارسال شود
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
پیش‌نیازهای زمان اجرا

قبل از شروع سرویس پیش‌زمینه، متد createScreenCaptureIntent() را فراخوانی کنید. انجام این کار یک اعلان مجوز به کاربر نشان می‌دهد؛ کاربر باید قبل از ایجاد سرویس، مجوز را اعطا کند.

پس از ایجاد سرویس پیش‌زمینه، می‌توانید MediaProjectionManager.getMediaProjection() را فراخوانی کنید.

توضیحات

با استفاده از APIهای MediaProjection محتوا را به نمایشگر غیر اصلی یا دستگاه خارجی منتقل کنید. این محتوا لزوماً نباید منحصراً محتوای رسانه‌ای باشد.

جایگزین‌ها

برای پخش رسانه به دستگاه دیگر، از 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.

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 را برای غربالگری تماس‌ها در نظر بگیرید.

پیام‌رسانی از راه دور

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 اشاره می کند. به این دلایل، اجرای فراخوان Service.onTimeout() بهترین روش در نظر گرفته می شود.

فراخوانی Service.onTimeout() در اندروید 13 و پایین‌تر وجود ندارد. اگر همان سرویس در چنین دستگاه‌هایی اجرا شود، نه مهلت زمانی دریافت می‌کند و نه ANR. مطمئن شوید که سرویس شما به محض اتمام کار پردازش متوقف می شود، حتی اگر هنوز فراخوانی Service.onTimeout() دریافت نکرده باشد.

توجه به این نکته مهم است که اگر مهلت زمانی shortService رعایت نشود، برنامه ANR می‌کند حتی اگر سایر خدمات پیش‌زمینه معتبر یا سایر فرآیندهای چرخه عمر برنامه در حال اجرا باشد.

اگر برنامه‌ای برای کاربر قابل مشاهده است یا یکی از معافیت‌هایی را برآورده می‌کند که اجازه می‌دهد خدمات پیش‌زمینه از پس‌زمینه شروع شود، با فراخوانی مجدد Service.StartForeground() با پارامتر FOREGROUND_SERVICE_TYPE_SHORT_SERVICE ، مدت زمان 3 دقیقه دیگر تمدید می‌شود. اگر برنامه برای کاربر قابل مشاهده نباشد و یکی از معافیت ها را برآورده نکند، هرگونه تلاش برای راه اندازی یک سرویس پیش زمینه دیگر، صرف نظر از نوع، باعث ایجاد یک ForegroundServiceStartNotAllowedException می شود.

اگر کاربری بهینه سازی باتری را برای برنامه شما غیرفعال کند، باز هم تحت تأثیر مهلت زمانی کوتاه سرویس 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:

اجرای خط‌مشی Google Play برای استفاده از انواع سرویس‌های پیش‌زمینه

اگر برنامه شما Android 14 یا بالاتر را هدف قرار می دهد، باید انواع خدمات پیش زمینه برنامه خود را در صفحه محتوای برنامه کنسول Play ( خط مشی > محتوای برنامه ) اعلام کنید. برای اطلاعات بیشتر در مورد نحوه اعلام انواع سرویس های پیش زمینه خود در Play Console، به درک خدمات پیش زمینه و الزامات هدف تمام صفحه مراجعه کنید.