از اندروید ۱۴ (سطح 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
CAMERAruntime 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: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 35 and lowerREAD_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
- 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:
- 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:foregroundServiceTypemediaProcessing- 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
mediaProcessingforeground services.)Your app should manually stop the media processing service in the following scenario:
- When the transcoding operation finishes or reaches a failure state, have the
service call
Service.stopForeground()andService.stopSelf()to stop the service completely.
- When the transcoding operation finishes or reaches a failure state, have the
service call
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 callService.stopSelf(). If the service does not callService.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: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.- 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: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: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: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 VPN apps (configured using Settings > Network & Internet > VPN)
Otherwise, declaring this type causes the system to throw a
ForegroundServiceTypeNotAllowedException.
اجرای خطمشی Google Play برای استفاده از انواع سرویسهای پیشزمینه
اگر برنامه شما Android 14 یا بالاتر را هدف قرار می دهد، باید انواع خدمات پیش زمینه برنامه خود را در صفحه محتوای برنامه کنسول Play ( خط مشی > محتوای برنامه ) اعلام کنید. برای اطلاعات بیشتر در مورد نحوه اعلام انواع سرویس های پیش زمینه خود در Play Console، به درک خدمات پیش زمینه و الزامات هدف تمام صفحه مراجعه کنید.