背景位置資訊限制
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
為降低耗電量,Android 8.0 (API 級別 26) 會限制應用程式在背景執行時,擷取使用者目前位置的頻率。在這些情況下,應用程式每小時只能接收數次的位置更新。
注意: 這些限制適用於在搭載 Android 8.0 (API 級別 26) 以上版本的裝置上使用的所有應用程式,無論應用程式的目標 SDK 版本為何。
如果應用程式在背景執行時需要即時快訊或動作偵測功能,請務必留意這項位置擷取行為。
保留前景應用程式行為
如果應用程式在搭載 Android 8.0 (API 級別 26) 的裝置上處於前景,位置更新行為就會與 Android 7.1.1 (API 級別 25) 以下版本相同。
警告:如果應用程式長時間擷取近乎即時的位置更新資訊,裝置的電池續航力會大幅縮短。
調整應用程式的位置資訊行為
請考量如果應用程式收到的位置更新頻率太低,是否會導致應用程式無法在背景執行。如果是這種情況,您可以執行下列其中一項操作,以便更頻繁地擷取位置更新:
注意:如果應用程式需要存取位置記錄 (包含經常更新的時間),請使用 Fused Location Provider API 元素的批次版本,例如 FusedLocationProviderApi
介面。當應用程式在背景執行時,這個 API 會比非批次 API 更頻繁地接收使用者的所在位置。不過,請注意,應用程式每小時仍只能接收數次的批次更新。
受影響的 API
背景應用程式中位置擷取行為的變更會影響下列 API:
- 整合式位置預測提供工具 (FLP)
-
- 地理圍欄
-
- 背景應用程式可比 Fused Location Provider 更頻繁地接收地理圍欄轉換事件。
- 地理圍欄事件的平均回應時間約為每隔幾分鐘。
- GNSS 測量資料和 GNSS 導航訊息
-
- 位置管理工具
-
- Wi-Fi 管理員
-
startScan()
方法只會每小時對背景應用程式執行幾次完整掃描。如果背景應用程式在之後不久再次呼叫該方法,WifiManager
類別會提供先前掃描作業的快取結果。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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."]]