محدودیت های موقعیت مکانی پس زمینه
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
در تلاش برای کاهش مصرف انرژی، Android 8.0 (سطح API 26) تعداد دفعات بازیابی مکان فعلی کاربر را در زمانی که برنامه در پسزمینه اجرا میشود، محدود میکند. تحت این شرایط، برنامهها میتوانند هر ساعت فقط چند بار بهروزرسانی موقعیت مکانی را دریافت کنند.
توجه: این محدودیتها برای همه برنامههای مورد استفاده در دستگاههای دارای Android 8.0 (سطح API 26) یا بالاتر، صرفنظر از نسخه SDK هدف برنامه اعمال میشود.
اگر برنامه شما در هنگام اجرا در پسزمینه به هشدارهای بیدرنگ یا تشخیص حرکت تکیه میکند، این رفتار بازیابی مکان بسیار مهم است که به خاطر داشته باشید.
رفتار برنامه پیش زمینه حفظ می شود
اگر برنامهای در دستگاه دارای Android 8.0 (سطح API 26) در پیشزمینه باشد، رفتار بهروزرسانی مکان مانند Android 7.1.1 (سطح API 25) و پایینتر است.
هشدار: اگر برنامه شما بهروزرسانیهای موقعیت مکانی نزدیک به زمان واقعی را در مدت زمان طولانی بازیابی کند، عمر باتری دستگاه به طور قابل توجهی کوتاهتر میشود.
تنظیم رفتار مکان برنامه شما
در نظر بگیرید که اگر برنامه شما بهروزرسانیهای مکان را به ندرت دریافت کند، موارد استفاده برنامه شما برای اجرای در پسزمینه اصلاً موفق نمیشود یا خیر. در این صورت، میتوانید با انجام یکی از اقدامات زیر، بهروزرسانیهای مکان را بیشتر بازیابی کنید:
- برنامه خود را در پیش زمینه قرار دهید.
با فراخوانی startForegroundService()
یک سرویس پیش زمینه را در برنامه خود راه اندازی کنید. هنگامی که چنین سرویس پیش زمینه فعال است، به عنوان یک اعلان در حال انجام در ناحیه اعلان ظاهر می شود.
احتیاط: اگر برنامه شما در حال اجرا در پسزمینه در دستگاهی که دارای Android 11 (سطح API 30) یا بالاتر است، سرویس پیشزمینه را راهاندازی کند، برنامه شما نمیتواند به اطلاعات مکان دسترسی داشته باشد مگر اینکه کاربر مجوز ACCESS_BACKGROUND_LOCATION
را به برنامه شما اعطا کرده باشد. برای اطلاعات بیشتر، راهنمای محدودیتهای حین استفاده مرتبط با خدمات پیشزمینه را مشاهده کنید.
- از عناصر Geofencing API مانند
GeofencingClient
استفاده کنید که برای به حداقل رساندن مصرف انرژی بهینه شده است. - از شنونده موقعیت مکانی غیرفعال استفاده کنید، که اگر برنامههای پیشزمینه درخواست بهروزرسانی مکان با سرعت بیشتری داشته باشند، ممکن است بهروزرسانیهای مکان سریعتر را دریافت کنند.
توجه: اگر برنامه شما نیاز به دسترسی به سابقه موقعیت مکانی دارد که حاوی بهروزرسانیهای مکرر است، از نسخه دستهبندیشده عناصر Fused Location Provider API مانند رابط FusedLocationProviderApi
استفاده کنید. وقتی برنامه شما در پسزمینه اجرا میشود، این API مکان کاربر را بیشتر از API غیر دستهای دریافت میکند. با این حال، به خاطر داشته باشید که برنامه شما همچنان بهصورت دستهای بهروزرسانیها را هر ساعت چند بار دریافت میکند.
APIهای تحت تأثیر
تغییرات رفتار بازیابی مکان در برنامههای پسزمینه بر APIهای زیر تأثیر میگذارد:
- ارائه دهنده مکان فیوز شده (FLP)
اگر برنامه شما در پسزمینه اجرا میشود، سرویس سیستم موقعیت مکانی یک مکان جدید را برای برنامه شما فقط چند بار در هر ساعت محاسبه میکند. این مورد حتی زمانی که برنامه شما درخواست بهروزرسانی مکان مکرر میکند نیز وجود دارد.
با این حال، با استفاده از نسخه دستهای FLP، پس از دریافت بهروزرسانی دستهای برنامه، به تاریخچه مکان مکرر بیشتری دسترسی خواهید داشت، که این نیز فقط چند بار در هر ساعت انجام میشود.
- اگر برنامه شما در پیشزمینه اجرا میشود، در مقایسه با Android 7.1.1 (سطح API 25) تغییری در نرخهای نمونهگیری مکان وجود ندارد.
- ژئوفنسینگ
- برنامههای پسزمینه میتوانند رویدادهای انتقال جغرافیایی را بیشتر از بهروزرسانیهای Fused Location Provider دریافت کنند.
- میانگین پاسخگویی برای یک رویداد geofencing هر چند دقیقه یا بیشتر است.
- اندازهگیریهای GNSS و پیامهای ناوبری GNSS
- مدیر موقعیت مکانی
به روز رسانی مکان برای برنامه های پس زمینه فقط چند بار در هر ساعت ارائه می شود.
توجه: اگر برنامه شما روی دستگاهی با سرویسهای Google Play نصب شده اجرا میشود، توصیه میشود به جای آن از Fused Location Provider (FLP) استفاده کنید.
- مدیر وای فای
- متد
startScan()
یک اسکن کامل برای برنامه های پس زمینه فقط چند بار در هر ساعت انجام می دهد. اگر یک برنامه پسزمینه دوباره این روش را بلافاصله بعد از آن فراخوانی کند، کلاس WifiManager
نتایج ذخیرهشده را از اسکن قبلی ارائه میکند.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Background Location Limits\n\nIn an effort to reduce power consumption, Android 8.0 (API level 26) limits\nhow frequently an app can retrieve the user's current location while the app is\n[running in the background](/guide/background). Under these\nconditions, apps can receive location updates only a few times each hour.\n\n**Note:** These limitations apply to all apps used on devices\nrunning Android 8.0 (API level 26) or higher, **regardless of an app's\ntarget SDK version**.\n\nThis location retrieval behavior is particularly\nimportant to keep in mind if your app relies on real-time alerts or motion\ndetection while running in the background.\n\nForeground app behavior is preserved\n------------------------------------\n\n\nIf an app is in the foreground on a device running Android 8.0 (API level 26),\nthe location update behavior is the same as on Android 7.1.1 (API level\n25) and lower.\n\n**Warning:** If your app retrieves near real-time location\nupdates over a long period of time, the device's battery life becomes\nsignificantly shorter.\n\nTuning your app's location behavior\n-----------------------------------\n\nConsider whether your app's use cases for running in the background cannot\nsucceed at all if your app receives infrequent location updates. If this is the\ncase, you can retrieve location updates more frequently by performing one of the\nfollowing actions:\n\n- Bring your app to the foreground.\n- Start a [foreground\n service](/guide/components/foreground-services) in your app by calling\n [startForegroundService()](/reference/android/content/Context#startForegroundService(android.content.Intent)). When such a foreground service is active, it\n appears as an ongoing notification in the\n [notification\n area](/guide/topics/ui/notifiers/notifications).\n\n **Caution:** If your app starts a foreground service\n while running in the background on a device that runs Android 11 (API\n level 30) or higher, your app cannot access location information unless the\n user has granted the\n [`ACCESS_BACKGROUND_LOCATION`](/reference/android/Manifest.permission#ACCESS_BACKGROUND_LOCATION)\n permission to your app. For more information, view the guidance about the\n [while-in-use\n restrictions](/guide/components/foreground-services#while-in-use-restrictions) that are associated with foreground services.\n- Use elements of the Geofencing API, such as the [`GeofencingClient`](https://developers.google.com/android/reference/com/google/android/gms/location/GeofencingClient), which are optimized for minimizing power use.\n- Use a passive location listener, which may receive faster location updates if there are foreground apps requesting location updates at a faster rate.\n\n**Note:** If your app needs access to location history that\ncontains time-frequent updates, use the batched version of the Fused Location\nProvider API elements, such as the\n[`FusedLocationProviderApi`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi)\ninterface. When your app is running in the background, this API receives the\nuser's location more frequently than the non-batched API. Keep in mind, however,\nthat your app still receives updates in batches only a few times each hour.\n\nAffected APIs\n-------------\n\n\nThe changes to location retrieval behavior in background apps affect the\nfollowing APIs:\n\n[Fused\nLocation Provider (FLP)](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi)\n:\n - If your app is running in the background, the location system service\n computes a new location for your app only a few times each hour. This\n is the case even when your app is requesting more frequent location\n updates.\n\n By using the\n [batched version](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest#setMaxWaitTime(long)) of FLP, however, you have access to more\n time-frequent location history after your app receives a batch update,\n which also occurs only a few times each hour.\n - If your app is running in the foreground, there is no change in location sampling rates compared to Android 7.1.1 (API level 25).\n\nGeofencing\n:\n - Background apps can receive geofencing transition events more frequently than updates from the Fused Location Provider.\n - The average responsiveness for a geofencing event is every couple of minutes or so.\n\nGNSS Measurements and GNSS Navigation Messages\n:\n - When your app is in the background, callbacks that are registered to receive outputs from [GnssMeasurement](/reference/android/location/GnssMeasurement) and [GnssNavigationMessage](/reference/android/location/GnssNavigationMessage) stop executing.\n\nLocation Manager\n:\n - Location updates are provided to background apps only a few times\n each hour.\n\n\n **Note:** If your app is running on a device with\n Google Play services installed, it is highly recommended that you use\n the [Fused\n Location Provider (FLP)](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi) instead.\n\nWi-Fi Manager\n:\n The [startScan()](/reference/android/net/wifi/WifiManager#startScan()) method\n performs a full scan for background apps only a few times each hour. If a\n background app calls the method again soon afterward, the\n [WifiManager](/reference/android/net/wifi/WifiManager) class provides cached results from the\n previous scan."]]