0

I implemented getting location updates using FusedLocationProviderApi. I need to track device location in background, when app is not running.

I followed the tutorials, I have a Service where I call requestLocationUpdates with an intent which starts a service.

I've purposely NOT implemented keeping a wake lock just to test and see that when the screen goes off, the device stops getting location updates. Once I verified that I could then go and implement the wake lock and expect the opposite, which is to see it working.

I used two devices: a Google Nexus 10 with KitKat and a Samsung Galaxy A5 (2016) with Lollipop.

On both devices, it keep sending updates (I'm sending the updates to a website). I tested with screen off, after leaving device on a desk for a minute, and then walking again around the house.

I know Android 6 and Doze mode is more restrictive, but I want to nail and understand how it works on Android 4.4 and Android 5.

So frustrating! Any ideas how to actually make device go in sleep mode so I can see getting location updates stop working?

Funny, everything I've read is about making it work :)

EDIT: I'm confused why getting location updates is still working when screen is off even with an aggressive location update setup like the following:

locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setFastestInterval(2);
locationRequest.setInterval(1000);
Don Box
  • 3,166
  • 3
  • 26
  • 55
  • As long as the service is running you will keep getting location updates – tyczj Feb 14 '17 at 19:40
  • @tyczj thanks but how does that help? Could you please share more thoughts? – Don Box Feb 14 '17 at 19:41
  • Well what I am getting at is you need to get your service to stop either by the OS stopping it or you manually going and stopping the service, wake lock or not wont make a difference. – tyczj Feb 14 '17 at 19:44
  • I see what you mean. You think I should be more interested about how to be sure service stays running. Because this what I need in the end for the app I write. Maybe I am on approaching this in the wrong way. – Don Box Feb 14 '17 at 20:05
  • Yes, I have never used a wake lock on a location service and the only time it stopped working is because the OS killed the service. If you need to keep your service running then you need to make it a foreground service – tyczj Feb 14 '17 at 20:07
  • Thanks. But is making it a foreground service sufficient? I'm thinking about Android 4.4 and up. All the places I read talk about keeping CPU awake with a wake lock. – Don Box Feb 14 '17 at 20:17
  • Yes it is, foreground service only get killed under extreme circumstances and they are excluded from Doze mode. Google maps uses it, media players use it – tyczj Feb 14 '17 at 20:19
  • Right, I read that in official documentation about Doze. Then is everyone using wake locks for location updates? makes no sense – Don Box Feb 14 '17 at 20:22
  • Just take a look at google's example https://developer.android.com/guide/topics/location/strategies.html no where do they mention a wake lock – tyczj Feb 14 '17 at 20:22
  • Sorry. Maybe I wasn't clear. I need to track device location in background. I'm updating the post now. – Don Box Feb 14 '17 at 20:50
  • No I got you and thats what the service is for, a foreground service does not mean it only runs in the foreground. `A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory` maybe you should do a little more reading on how a service works. https://developer.android.com/reference/android/app/Service.html – tyczj Feb 14 '17 at 20:52
  • but isn't killing the service and sleeping two different things? OS might want to set CPU into sleep after user inactivity. I know that in Android 6+ App Standby is not happening for foreground service, but I don't know how it works for Android 4.4 and Android 5. – Don Box Feb 14 '17 at 21:31
  • It doesn't seem having a foreground service is enough. I found in this book(albeit a Xamarin book) saying that both a wakelock and a foreground service are required (see 2nd paragraph) https://books.google.com/books?id=3fSoCwAAQBAJ&pg=PA255&redir_esc=y#v=onepage&q&f=false – Don Box Feb 15 '17 at 19:27
  • holding long running wake locks are a very very bad idea, in the example of that book they are playing a song. songs typically are 3-4 min long, you want your gps running longer than that. If you are concerned about the points stopping I suggest using an alarm manager and doing a task every so often. A long running wake lock is going to kill the battery heavily making it very likely a user will uninstall your app. the choice is yours though – tyczj Feb 15 '17 at 21:51
  • Thanks. I think you need to have both foreground process and wakelock when you need to run something which you don't want to be killed (use foreground process) or put to sleep (use a wakelock). In my case, getting location updates requires them in different phases. For example, one phase is when setting up getting location updates. Another phase is when receiving the location update and process it though an IntentService. And I figured out that I don't actually need a long running service, I can just use PendinIntent which starts IntentService when calling `requestLocationUpdates`. – Don Box Feb 16 '17 at 11:41
  • What do you think? – Don Box Feb 16 '17 at 11:44

1 Answers1

2

Thing about requesting location updates is that underneath it all, the LocationManagerService, which is what LocationManager uses, actually creates and acquires it's own wakelock. So technically, your device will never go to sleep as long as you have a location listener registered to actively receive GPS. Now, I know you are using Google's FusedLocationApi, but underneath it all, it does use LocationManager. If you are interested in understanding the location code a little better, here is the source code for LocationManagerService.

Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36
  • Thanks. When you say "technically, your device will never go to sleep as long as you have a location listener registered to actively receive GPS". Not sure, but isn't the device going to sleep and wakes itself up to execute the `Intent` I provided in `requestLocationUpdate` ? – Don Box Feb 21 '17 at 21:46
  • 1
    It does look like it uses an AlarmManager as well, so if you set the interval to be greater than 0, it releases the wakelock after GPS has turned off, to let the device go to sleep. – Pablo Baxter Feb 22 '17 at 01:22
  • Since it's using wakelocks, does this mean in Android 6+ Doze mode location updates are not sent? Because in Doze mode wake locks are ignored. Unless app is Doze mode white-listed.... What a mess – Don Box Feb 25 '17 at 09:33
  • 1
    That may not be true. The wakelocks that are ignored are those originating from the apps. The OS also holds wakelocks, and it seems like LocationManagerService holds an OS wakelock, which might not be ignored. That's just my hypothesis on the new Doze mode features. However, in Android 7, looks like GPS does get shut down when it goes into the deep Doze mode. https://developer.android.com/about/versions/nougat/android-7.0-changes.html#doze – Pablo Baxter Mar 09 '17 at 01:38
  • Thanks for sharing the link to Nougat changes! I'm confused why they mention GPS. If device is stationary, you don't get updates anyway, even when Doze mode is off. So why they are mentioning it? Even in deep Doze mode, I expect GPS to wake up as soon as device starts moving. I feel like there's different nuances and aspects which I can't understand from those few phrases they wrote there. – Don Box Mar 16 '17 at 09:05
  • Location updates can still be requested or polling even if the device is stationary. Probably to detect movement or leaving an area for some apps. Doze mode just shuts down different things based on how long it's been still. If the screen is off and device is in your pocket, you typically don't need internet or any jobs to be run that are typically done when the device is in use. When the phone is on your desk for a while without moving, no point using GPS, AlarmManager, WakeLocks, etc, since your phone is literally just a paper weight at that moment. – Pablo Baxter Mar 16 '17 at 17:34