5

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.

Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76
  • This new feature is so bad that even Google Map's location sharing feature become useless, I can no longer get friend's current location – user7180 Mar 09 '18 at 07:04

1 Answers1

3

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.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Nikunj Peerbits
  • 785
  • 4
  • 12
  • as you suggest in your answer , one should have to implement firebase job scheduler , so its kind a push to phone to start service again . am i right ? is there any other way at android side to trigger it without push from server ? – Tejas Pandya Feb 22 '18 at 05:55
  • 1
    Can't believe that this could be the intended solution - what's the point of providing the background location listener if it doesn't work... – Arturs Vancans Feb 22 '18 at 06:25