[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Optimize location use for real-world scenarios\n\nThis section describes some typical location-gathering scenarios, along with\nrecommendations for optimal use of the geofencing and fused location provider\nAPIs.\n\nUser visible or foreground updates\n----------------------------------\n\nExample: A mapping app that needs frequent, accurate updates with very low\nlatency. All updates happen in the foreground: the user starts an activity,\nconsumes location data, and then stops the activity after a short time.\n\nUse the [`setPriority()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setPriority(int)) method with a value of\n[`PRIORITY_HIGH_ACCURACY`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest#PRIORITY_HIGH_ACCURACY) or [`PRIORITY_BALANCED_POWER_ACCURACY`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_BALANCED_POWER_ACCURACY).\n\nThe interval specified in the [`setInterval()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setInterval(long)) method depends on the use\ncase: for real time scenarios, set the value to few seconds; otherwise, limit to\na few minutes (approximately two minutes or greater is recommended to minimize\nbattery usage).\n\nKnow the location of the device\n-------------------------------\n\nExample: A weather app wants to know the device's location.\n\nUse the [`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation()) method, which returns the most recently\navailable location (which in rare cases may be null). This method provides a\nstraightforward way of getting location and doesn't incur costs associated with\nactively requesting location updates. Use in conjunction with the\n[`isLocationAvailable()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationAvailability.html#isLocationAvailable()) method, which returns `true` when the location\nreturned by [`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation()) is reasonably up-to-date.\n\nStart updates when a user is at a specific location\n---------------------------------------------------\n\nExample: Requesting updates when a user is within a certain distance of work,\nhome, or another location.\n\nUse [geofencing](/training/location/geofencing) in conjunction with fused location provider updates.\nRequest updates when the app receives a geofence entrance trigger, and remove\nupdates when the app receives a geofence exit trigger. This ensures that the app\ngets more granular location updates only when the user has entered a defined\narea.\n\nThe typical workflow for this scenario could involve surfacing a notification\nupon the geofence enter transition, and launching an activity which contains\ncode to request updates when the user taps the notification.\n\nStart updates based on the user's activity state\n------------------------------------------------\n\nExample: Requesting updates only when the user is driving or riding a bike.\n\nUse the [Activity Recognition API](https://developers.google.com/location-context/activity-recognition/) in conjunction with fused location\nprovider updates. Request updates when the targeted activity is detected, and\nremove updates when the user stops performing that activity.\n\nThe typical workflow for this use case could involve surfacing a notification\nfor the detected activity, and launching an activity which contains code to\nrequest updates when the user taps the notification.\n\nLong running background location updates tied to geographical areas\n-------------------------------------------------------------------\n\nExample: The user wants to be notified when the device is within proximity of a\nretailer.\n\nThis is an excellent use case for geofencing. Because the use case almost\ncertainly involves background location, use the\n[`addGeofences(GeofencingRequest, PendingIntent)`](https://developers.google.com/android/reference/com/google/android/gms/location/GeofencingClient#addGeofences(com.google.android.gms.location.GeofencingRequest,%20android.app.PendingIntent)) method.\n\nYou should set the following configuration options:\n\n- If you're tracking dwell transitions, use the [`setLoiteringDelay()`](https://developers.google.com/android/reference/com/google/android/gms/location/Geofence.Builder.html#setLoiteringDelay(int))\n method passing a value of approximately five minutes or less.\n\n- Use the [`setNotificationResponsiveness()`](https://developers.google.com/android/reference/com/google/android/gms/location/Geofence.Builder.html#setNotificationResponsiveness(int)), passing a value of\n approximately five minutes. However, consider using a value of approximately\n ten minutes if your app can manage the extra delay in responsiveness.\n\nAn app may only register a maximum of 100 geofences at a time. In a use case\nwhere an app wants to track a large number of retailer options, the app may want\nto register large geofence (at the city level) and dynamically register smaller\ngeofences (for locations within the city) for stores within the larger geofence.\nWhen a user enters a large geofence, add smaller geofences; when the user exits\nthe larger geofence, remove the smaller geofences and re-register geofences for\na new area.\n\nLong running background location updates without a visible app component\n------------------------------------------------------------------------\n\nExample: An app that passively tracks location\n| **Note:** Consider if you really need to collect location in the background, since this can lead to undesirable battery drain. Also, consider geofencing as an option, since geofencing APIs are optimized for performance.\n\nUse the [`setPriority()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setPriority(int)) method with the [`PRIORITY_NO_POWER`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_NO_POWER) option if\npossible because it incurs almost no battery drain. If using `PRIORITY_NO_POWER`\nisn't possible, use [`PRIORITY_BALANCED_POWER_ACCURACY`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_BALANCED_POWER_ACCURACY) or\n[`PRIORITY_LOW_POWER`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_LOW_POWER), but avoid using [`PRIORITY_HIGH_ACCURACY`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_HIGH_ACCURACY) for\nsustained background work because this option substantially drains battery.\n\nIf you need more location data, use passive location by calling the\n[`setFastestInterval()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setFastestInterval(long)) method passing a smaller value than what you pass\nto [`setInterval()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setInterval(long)). When combined with the [`PRIORITY_NO_POWER`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#PRIORITY_NO_POWER)\noption, passive location can opportunistically deliver location computed by\nother apps at no extra cost.\n\nModerate frequency by adding some latency, using the [`setMaxWaitTime()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setMaxWaitTime(long))\nmethod. For example, if you use the `setinterval()` method with a value of\napproximately 10 minutes, you should consider calling `setMaxWaitTime()` with a\nvalue between 30 to 60 minutes. Using these options, location is computed for\nyour app approximately every 10 minutes, but the app is only woken up every 30\nto 60 minutes with some location data available as a batch update. This approach\ntrades latency for more data available and better battery performance.\n\nFrequent high accuracy updates while the user interacts with other apps\n-----------------------------------------------------------------------\n\nExample: A navigation or fitness app that continues to work when the user either\nturns off the screen or opens a different app.\n\nUse a foreground service. If expensive work is potentially going to be done by\nyour app on behalf of the user, making the user aware of that work is a\nrecommended best practice. A foreground service requires a persistent\nnotification. For more information, see [Notifications Overview](/develop/ui/views/notifications)."]]