sodcalculator

10 Tech How-To

Android Location Updates: Pick Per Feature

Guide 10-53510 · Updated · Step by step

Choosing the right location update strategy for each feature in your Android app is essential for balancing battery life, accuracy, and responsiveness. This guide breaks down the main options—from the high-level FusedLocationProviderClient to raw GPS—and shows you how to match them to your specific use cases, such as navigation, fitness tracking, or geofencing.

What You’ll Need

  • Android Studio with a project that already has the play-services-location dependency added.
  • Basic knowledge of Kotlin or Java and Android’s permission system.
  • A physical Android device (emulators often simulate location poorly).
  • Access to the Google Play services SDK documentation for reference.

Step-by-Step

  1. Identify the location needs of each feature.
    For every feature that uses location, note: How often do you need updates? (e.g., every second for turn-by-turn navigation, every minute for weather updates). How accurate must they be? (e.g., within 10 meters for a running app, within 100 meters for city-level weather). How long will the feature run? (e.g., a few seconds for a check-in, hours for a workout).
  2. Choose the priority for continuous updates.
    For most apps, use the FusedLocationProviderClient with a LocationRequest. Set the priority to one of these:
    • PRIORITY_HIGH_ACCURACY – use for navigation, running, or any feature where the user is actively watching the screen and needs precise location.
    • PRIORITY_BALANCED_POWER_ACCURACY – use for features that need city-block accuracy but not meter-level precision, like showing nearby places or weather.
    • PRIORITY_LOW_POWER – use for features that only need a coarse location, like showing the current city or timezone.
    • PRIORITY_NO_POWER – use when you only want to receive updates if another app has already requested them, to avoid extra battery drain.
  3. Set the update interval and fastest interval.
    In your LocationRequest, set setInterval() to the desired update frequency (e.g., 5000 ms for 5 seconds). Also set setFastestInterval() to the fastest your app can handle (e.g., 2000 ms) to prevent the system from sending updates faster than you can process them. For background updates, consider using setMaxWaitTime() to batch updates and save battery.
  4. Implement the location callback.
    Use a LocationCallback to receive updates. Override onLocationResult() to handle each new location. Remember to remove the callback in onStop() or when the feature is no longer active to avoid draining the battery.
  5. For one-time location requests, use getCurrentLocation().
    If a feature only needs the current location once (e.g., a “check-in” button), use getCurrentLocation() with a Priority enum (like Priority.PRIORITY_HIGH_ACCURACY). This is more efficient than starting continuous updates and then stopping them.
  6. For geofencing, use the Geofencing API.
    If a feature needs to trigger when the user enters or exits a specific area, use GeofencingClient. Define a Geofence with a radius (e.g., 100 meters) and set the transition types (enter, exit, dwell). Choose the appropriate GeofenceRequest initial trigger and expiration duration. The system handles the location updates efficiently, so you don’t need to manage them yourself.
  7. For background location, request the appropriate permission.
    If your feature needs location updates while the app is in the background (e.g., a fitness tracker), you must declare the ACCESS_BACKGROUND_LOCATION permission in your manifest and request it at runtime. Be aware that this is a sensitive permission and may require user review.
  8. Test on a real device with different scenarios.
    Run your app while walking, driving, and indoors. Monitor battery usage via Android Studio’s Profiler or the device’s battery settings. Adjust the priority and intervals based on your observations—if the battery drains too fast, lower the priority or increase the interval.

Common Mistakes

  • Using high accuracy for everything. This drains battery unnecessarily. Only use PRIORITY_HIGH_ACCURACY when you truly need meter-level precision.
  • Not removing the location callback. Forgetting to call removeLocationUpdates() in onStop() or onPause() keeps the GPS active and drains the battery.
  • Setting too-frequent updates in the background. Background updates should be less frequent (e.g., every 5-10 minutes) to conserve battery.
  • Ignoring the fastestInterval. If you don’t set it, the system may send updates faster than your app can handle, causing performance issues.
  • Requesting background permission unnecessarily. If your feature only needs foreground updates, don’t request background permission—it may scare users and is subject to stricter Play Store policies.

Tips

  • Use the LocationRequest.Builder in newer versions of the API for cleaner code and better defaults.
  • Consider using setWaitForAccurateLocation() to delay the first update until a more accurate location is available, which is useful for navigation.
  • For battery-sensitive features, use PRIORITY_BALANCED_POWER_ACCURACY and rely on Wi-Fi and cell tower triangulation. This is often sufficient for weather or news apps.
  • If you need location updates only while the screen is on, check the screen state and stop updates when the screen turns off.
  • Use the SettingsClient to check if the device’s location settings meet your requirements (e.g., GPS enabled) and prompt the user to change them if not.

Troubleshooting

  • No location updates received. Check that you have the correct permissions (ACCESS_FINE_LOCATION and/or ACCESS_COARSE_LOCATION) and that they are granted at runtime. Also verify that location services are enabled on the device.
  • Updates are too slow or inaccurate. Ensure you’ve set the priority to PRIORITY_HIGH_ACCURACY and that the device has a clear view of the sky (GPS). Indoors, GPS may not work well; consider using Wi-Fi.
  • Battery drains too fast. Reduce the update frequency, lower the priority, or stop updates when not needed. Use the battery profiler to identify the cause.
  • Background updates stop. On Android 8+ (API 26+), the system restricts background location updates. Use a foreground service with a notification for continuous background updates.
  • Geofence doesn’t trigger. Make sure the geofence radius is large enough (at least 100 meters) and that the device has location enabled. Also check that you’ve set the correct transition types and that the geofence isn’t expired.

Frequently Asked Questions

What is the difference between getCurrentLocation() and requestLocationUpdates()?

getCurrentLocation() returns a single location quickly, while requestLocationUpdates() provides a continuous stream. Use the former for one-time needs and the latter for ongoing tracking.

How do I choose between PRIORITY_HIGH_ACCURACY and PRIORITY_BALANCED_POWER_ACCURACY?

Use high accuracy when you need precise location for navigation or fitness tracking. Use balanced power when city-block accuracy is enough, like for weather or local news.

Can I use location updates in the background without a foreground service?

On Android 8+ (API 26+), background location updates are limited. You must use a foreground service with a visible notification for continuous background updates. Alternatively, use geofencing or periodic updates with WorkManager.

What permissions do I need for location updates?

You need ACCESS_FINE_LOCATION for GPS-level accuracy, and optionally ACCESS_COARSE_LOCATION for network-based location. For background updates, you also need ACCESS_BACKGROUND_LOCATION.

How can I save battery when using location updates?

Use the lowest priority that meets your needs, increase the update interval, and stop updates when they’re not needed. Also consider using setMaxWaitTime() to batch updates.

Why does my location update stop when the app goes to the background?

Android restricts background location updates to save battery. To keep receiving updates, you need to run a foreground service with a notification, or use a geofence or WorkManager for periodic updates.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →