13

My app periodically checks the location using an alarm to a BroastcastReceiver that starts a service. I know I should acquire a wakelock before I start the service but my question is when can I release it?

If I release the wakelock after calling requestLocationUpdates, can the device go to sleep and not call my LocationListener or will the device always wake to deliver updates and stay awake until the callback has returned? I'm reluctant to hold onto the wakelock because that would prevent the device from sleeping between updates until I'm done with the location.

noah
  • 21,289
  • 17
  • 64
  • 88
  • My slightly educated guess is that requesting location updates will hold a wakelock so you can release yours. However, what are you doing that you need location updates when the phone is asleep? – Falmarri Sep 28 '10 at 19:25
  • 2
    I think there probably lots of use cases for location updates while the device is asleep, e.g. recording GPS tracks of a journey. – Christopher Orr Sep 29 '10 at 08:49
  • You could take a look at [CommonsGuy's `WakefulService`][1] which, according to the documentation can handle the partial wakelock stuff for you. [1]:http://github.com/commonsguy/cwac-wakeful#readme – Christopher Orr Sep 28 '10 at 18:05
  • 1
    The question is not "How to use a WakeLock" but "Is a WakeLock necessary?" – noah Sep 28 '10 at 18:23
  • The answer is yes, because you're starting a `Service`, as I believe the SDK documentation mentions. – Christopher Orr Sep 28 '10 at 18:30
  • 1
    The complete question is: Is a WakeLock necessary when calling requestLocationUpdates in order to guarantee updates are received when they are available? – noah Sep 28 '10 at 18:34
  • 1
    I believe you just have to hold a wakelock until the `Service` has started up and you have called `requestLocationUpdates`. At that point, the service is running and can receive the location updates; the wakelock can be released, the `onReceive` method will have completed and the device can potentially go back to sleep. – Christopher Orr Sep 28 '10 at 19:25

2 Answers2

2

So based on my experience (and confirmed in the comments here), releasing the wake lock after after calling requestLocationUpdates doesn't seem to be a problem. Unfortunately, the only way to get a definitive answer is to ask a platform developer, but I've received no response.

Oliv
  • 10,221
  • 3
  • 55
  • 76
noah
  • 21,289
  • 17
  • 64
  • 88
  • 2
    This is correct. I am not a platform dev, but have a working GPS tracking app that functions correctly when the device is "asleep"... Basically each later location update comes with its own `WakeLock`, allowing you to receive the `Intent`. In your receiver, if you want to process that event, you need to get (and later release) your own WakeLock. – Richard Le Mesurier Mar 13 '13 at 07:35
  • Would it make a difference which `requestLocationUpdates` method you call ? @RichardLeMesurier : you are using the `PendingIntent` version ? – Mr_and_Mrs_D May 09 '13 at 21:21
  • @Mr_and_Mrs_D I do not know and recommend you try it to make sure. I was using the `PendingIntent` version. – Richard Le Mesurier May 10 '13 at 09:26
  • @RichardLe : that's what I thought - seems to work with no lock held waiting for the receiver but I do hold a lock when requesting the updates (I do it with `FLAG_ONE_SHOT` in the pending intent to simulate `requestSingleUpdate` and use alarm manager to request the update - haven't tried relying completely on `minTime`) – Mr_and_Mrs_D May 10 '13 at 15:44
  • @RichardLeMesurier: I can finally confirm this myself - once one registers for updates the Receiver is triggered regularly - I guess the wake lock is valid while `onReceive()` runs – Mr_and_Mrs_D Jul 18 '13 at 00:35
  • @Mr_and_Mrs_D Wow, nice to have that confirmed by someone. Thanks for following up on this thread. And good luck with your locator app too. – Richard Le Mesurier Jul 18 '13 at 12:28
  • @RichardLeMesurier: real nice would be to be confirmed by a dev - maybe [bug them a bit more](https://groups.google.com/group/android-developers/browse_thread/thread/facf17b6b392657a/991b6ecfca1918c8?lnk=gst) ? - app is [here](https://github.com/Utumno/monitoring/tree/master/src/gr/uoa/di/monitoring/android) and the commit I disable the alarm manager who used to wake me up is [here](http://tinyurl.com/kvve7av). I only checked the _network provider_ - but I believe gps won't differ. Pardon the mess - it's a squashed tree and it's pre alpha. Btw I left the alarm - min time is reported unreliable – Mr_and_Mrs_D Jul 18 '13 at 13:23
0

check for power saving mode in Android system settings: it must be disable to permit location manager to generate update location when the screen is off; tested on Samsung A5; I took two days to find this solution :-)

Jason Roman
  • 8,146
  • 10
  • 35
  • 40
rico
  • 1