Given the limits placed on location gathering on Android apps running in the background on O devices, what are good options for developers who want more frequent location updates?
I realize that O is still in developer preview, but there are significant changes to how location can be gathered in the background, and I want to project ahead to changes that might be needed in my apps.
Apps requesting location updates that are not in the foreground are subject to some limits when running on O devices. Location updates are still available in the background, but they may be happen less frequently than the interval specified in LocationRequest#setInterval. Developers have the following options for getting more frequent location updates:
Request updates in the foreground. This means requesting and removing updates as part of the activity lifecycle (request in onResume()
and remove in onPause()
, for example). Apps running in the foreground are not subject to any location limits on O devices .
Request updates using a foreground service. This involves displaying a persistent notification to users.
Use geofencing to trigger notifications based on the device’s location. If your use case relies on the device entering, dwelling, or exiting a particular area of interest, this API provides a performant way to get these notifications. See the GeofencingEvent#getTriggeringLocation[, which gets the location that triggered the geofence transition.
Use batched location updates using LocationRequest#setMaxWaitTime. With this API, locations may be provided more frequently than the non-batched API however, will be delivered in a batch after the interval specified in setMaxWaitTime
(also limited to a few times an hour)
Use passive location updates: While your app is in the background, it may continue to receive location updates passively if another app in the foreground requests location updates. You can receive some of these updates by using LocationRequest#setFastestInterval with a small interval, such as 5 min.