Background location usage can significantly impact battery life. Android supports limits to background location gathering, prompting developers to optimize their applications for better battery efficiency. These optimizations are beneficial for all devices, regardless of the Android version they are running.
With Background Location Limits:
- Background location gathering is throttled and location is computed, and delivered only a few times an hour.
- Wi-Fi scans are more conservative, and location updates aren't computed when the device stays connected to the same static access point.
- Geofencing responsiveness changes from tens of seconds to approximately two minutes. This change improves battery performance—up to 10 times better on some devices.
Required knowledge
This document assumes familiarity with the following APIs:
- Google Location Services. These APIs offer higher accuracy and impose a lighter battery burden than the framework location APIs.
- Fused Location Provider. This API combines signals from GPS, Wi-Fi, and cell networks, as well as accelerometer, gyroscope, magnetometer and other sensors.
- Geofencing, This API is built on top of the Fused Location Provider API, and is optimized for battery performance.
Understand battery drain
Location gathering and battery drain are related as follows:
- Accuracy: The precision of the location data. In general, the higher the accuracy, the higher the battery drain.
- Frequency: How often location is computed. The more frequent location is computed, the more battery is used.
- Latency: How quickly location data is delivered. Less latency usually requires more battery.
Accuracy
You can specify location accuracy using the setPriority()
method, passing
one of the following values as the argument:
PRIORITY_HIGH_ACCURACY
provides the most accurate location possible, which is computed using as many inputs as necessary (it enables GPS, Wi-Fi, and cell, and uses a variety of Sensors), and may cause significant battery drain.PRIORITY_BALANCED_POWER_ACCURACY
provides accurate location while optimizing for power. Very rarely uses GPS. Typically uses a combination of Wi-Fi and cell information to compute device location.PRIORITY_LOW_POWER
largely relies on cell towers and avoids GPS and Wi-Fi inputs, providing coarse (city-level) accuracy with minimal battery drain.PRIORITY_NO_POWER
receives locations passively from other apps for which location has already been computed.
Use the balanced power or low power options to satisfy the location needs of most apps. Reserve high accuracy for apps that run in the foreground and require real time location updates (for example, a mapping app).
Frequency
You can specify location frequency using two methods:
- Use the
setinterval()
method to specify the interval for computing your app's location. - Use the
setFastestInterval()
method to specify the interval for receiving other apps' locations.
Pass the largest possible value when using setInterval()
. This is especially
true for background location gathering, which often drains the battery. Reserve
intervals of a few seconds for foreground use cases.
The background location limits introduced in Android 8.0 (API level 26) enforce these strategies, but your app should strive to enforce them on devices running Android 7.0 (API level 24) and lower.
Latency
You can specify latency using the setMaxWaitTime()
method, typically
passing a value that is several times larger than the interval specified in the
setInterval()
method. This setting delays location delivery, and
multiple location updates may be delivered in batches. These two changes help
minimize battery consumption.
If your app doesn't immediately need a location update, you should pass the
largest possible value to the setMaxWaitTime()
method, effectively trading
latency for more data and battery efficiency.
When using geofences, apps should pass a large value into the
setNotificationResponsiveness()
method to preserve power. A value of
five minutes or larger is recommended.
Further reading
For more information, see the following documents:
- Recommendations: This guide lays out some concrete recommended actions you can use to improve your app's impact on battery life.
- Use cases: This guide provides several common use cases where you might use location services and how you could optimise the impact on battery life for those cases.