Android 8 (Oreo) has introduced a few Background Execution and Location limits. There are 2 ways to request location updates - via a looper for foreground requests and via pending intent (ie BroadcastReceiver) for background requests.
If your app is running in the background, the location system service computes a new location for your app only a few times each hour.
The problem: When the app goes into background mode, I receive location updates for roughly 2 hours and then none until after I actually open the app again. My LocationRequest settings do not specify any expiry times, therefore, I expected to receive the location updates indefinitely.
val locationRequest = {
val mLocationRequest = LocationRequest()
mLocationRequest.interval = 4.minutes()
mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
mLocationRequest.maxWaitTime = 10.minutes()
mLocationRequest.fastestInterval = 30.seconds()
mLocationRequest
}()
val intent = Intent(this, BackgroundLocationUpdatesReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
LocationServices.getFusedLocationProviderClient(mContext).requestLocationUpdates(locationRequest, pendingIntent)
I was considering some battery optimizations to be responsible for that (ie. Android Doze or OS putting the app in standby mode) but other apps are sending notifications, therefore, it's not in Doze and the app being in standby doesn't matter as it is sent via PendingIntent to a BroadcastReceiver.
Possible Solution for this will be Firebase Job Dispatcher. You can schedule a job that will start the service after each 2 hours or 1 hours.
You logic for starting background service can vary. I am assuming this as I see whatsapp is running in background message on certain interval on my device which is having O update.
You can check out Firebase dispatcher here.
Other alternatives for background service in Oreo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With