0

I am using FusedLocationProviderClient with pending intent. I am getting the location when the app is active. But while the app goes into background or app is killed, I am getting the location in irregular intervals say 2mins, 3 mins, 10 mins sometimes. Need help with this?

Note : Also I am targeting Oreo.

my code

Milad Bahmanabadi
  • 946
  • 11
  • 27
Harsh Masand
  • 121
  • 1
  • 4

1 Answers1

0

If you want to get location data while your app is in the background on Oreo or higher, you should run a foreground service (or something similar):

https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService

With Oreo, Google started limiting what apps can do in the background:

https://developer.android.com/about/versions/oreo/background

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
  • Thanks @Gavin Wright but then, I don't want notifications as well. And my minimum sdk version is 15. Please suggest something. – Harsh Masand Oct 31 '18 at 11:49
  • This could possibly be done with JobScheduler, but you may run into problems because getting location every couple minutes in the background uses a lot of battery. That's why the notification is there, to inform the user about something that's draining lots of battery. Anyways, give it a try I guess: https://stackoverflow.com/questions/47224219/how-to-run-a-jobscheduler-or-a-service-every-minute-wihout-stopping – Gavin Wright Oct 31 '18 at 12:27
  • JobScheduler is for api level greater than 21. I suppose I have to use Firebase Job Dispatcher then. – Harsh Masand Oct 31 '18 at 12:42