از اندروید ۱۴ (سطح 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.
سلامت
- نوع سرویس پیش زمینه برای اعلام در مانیفست در زیر
-
android:foregroundServiceType -
health - اجازه اعلام در مانیفست شما
-
FOREGROUND_SERVICE_HEALTH - ثابت برای عبور به
startForeground() -
FOREGROUND_SERVICE_TYPE_HEALTH - پیش نیازهای زمان اجرا
حداقل یکی از شرایط زیر باید درست باشد:
مجوز
HIGH_SAMPLING_RATE_SENSORSدر مانیفست خود اعلام کنید.حداقل یکی از مجوزهای زمان اجرا زیر را درخواست کنید و به آنها اعطا شود:
-
BODY_SENSORSدر API 35 و پایینتر -
READ_HEART_RATE -
READ_SKIN_TEMPERATURE -
READ_OXYGEN_SATURATION -
ACTIVITY_RECOGNITION
-
- توضیحات
هر گونه موارد استفاده طولانی مدت برای پشتیبانی از برنامه های دسته تناسب اندام مانند ردیاب های ورزشی.
مکان
- نوع سرویس پیش زمینه برای اعلام در مانیفست در زیر
-
android:foregroundServiceType -
location - اجازه اعلام در مانیفست شما
-
FOREGROUND_SERVICE_LOCATION - ثابت برای عبور به
startForeground() -
FOREGROUND_SERVICE_TYPE_LOCATION - پیش نیازهای زمان اجرا
کاربر باید خدمات موقعیت مکانی را فعال کرده باشد و به برنامه باید حداقل یکی از مجوزهای زمان اجرا زیر اعطا شود:
- توضیحات
موارد استفاده طولانی مدت که نیاز به دسترسی به موقعیت مکانی دارند، مانند ناوبری و اشتراک گذاری مکان.
- جایگزین ها
اگر برنامه شما باید زمانی که کاربر به مکان های خاصی می رسد فعال شود، به جای آن از geofence API استفاده کنید.
رسانه
- 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.
طرحریزی رسانهای
- 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.- 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.
پیامرسانی از راه دور
- نوع سرویس پیش زمینه برای اعلام در مانیفست در زیر
-
android:foregroundServiceType -
remoteMessaging - اجازه اعلام در مانیفست شما
-
FOREGROUND_SERVICE_REMOTE_MESSAGING - ثابت برای عبور به
startForeground() -
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING - پیش نیازهای زمان اجرا
- هیچ کدام
- توضیحات
- پیام های متنی را از یک دستگاه به دستگاه دیگر منتقل کنید. به تداوم وظایف پیام رسانی کاربر هنگام تعویض دستگاه کمک می کند.
خدمات کوتاه
- نوع سرویس پیش زمینه برای اعلام در مانیفست در زیر
-
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>
سیستم معاف است
- نوع سرویس پیش زمینه برای اعلام در مانیفست در زیر
-
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 برای استفاده از انواع سرویسهای پیشزمینه
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.