Reputation: 22173
I've got a foreground service (with START_STICKY so no problem for the aspect "killed by OS")that receive a location (GPS) update every 2 seconds for navigation purpose. I don't take any wakelock. My question is: have I to take a wakelock to avoid a deep sleep? Or the location updates is enough to be "running"?
Upvotes: 1
Views: 2136
Reputation: 22173
After of bit of digging in Android code, the response is: no, you don't need it. Android will grab a wakelock for you (LocationManagerService) until onLocationChange ends or your broadcast receiver receives the intent. If you do some async work (start an intent service, post some code in onLocationChange and so on) then you need to create your own partial wakelock.
Upvotes: 3
Reputation: 34026
I am doing the same in an app (but I am using a receiver to receive the locations updates which I recommend - there are many bug reports with the listeners). Your way has 2 problems:
The correct way is to register an alarm with the AlarmManager - the alarm will wake a receiver. From onReceive()
you must start a WakefulIntentService
which will manage the locks for you.
See:
EDIT: 2 seconds is really very often. This will kill the battery - expect very low ratings - or bump the interval up.
Upvotes: 0