Reputation: 17915
Disclaimer: My app already working without any Wake Locks for 1+ year and all is well for most devices.
I'm tracking GPS and it works like this:
Due to bad connections and bad locations - whole thing take up to 2-3 minutes sometimes. And it works. No matter if phone is sleeping or not.
Now I'm reading about WakeLock and it doesn't make sense to me. How come my stuff is working? Is that coincidence?
Upvotes: 3
Views: 148
Reputation: 1007584
How come my stuff is working?
A combination of things, including a dollop of luck. :-)
First, as Joel noted, the device wakes up briefly courtesy of your alarm, but the OS is only guaranteed to hold a WakeLock
for the duration of onReceive()
of a BroadcastReceiver
.
It is possible that, at least on some versions of Android, requesting GPS updates causes the OS to acquire its own WakeLock
. This is undocumented behavior AFAIK, and I have never relied upon it personally. If it does, though, and you are doing the rest of your work ("Wrap up, send data to server and shut down service") before removing location updates, that would explain the behavior.
There are still potential gaps in your approach (e.g., if you delegate to a Service
to do the work and are not holding a WakeLock
as part of passing control to that service). Statistically speaking, it may fail occasionally but work a lot of the time.
Personally, I recommend using a WakeLock
, in case the undocumented behavior changes. That's what I do in LocationPoller
.
Upvotes: 1
Reputation: 4772
Well reading from the AlarmManager documentation..
The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing.
Further...
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
So based on that.. I think it makes sense that it currently works; correct me if I'm wrong.
Upvotes: 0