I need to do background GPS tracking on an Android phone, and I'm currently planning on having two scenarios:
- When the phone is connected to a (car) charger, get frequent GPS locations (every minute).
- When the phone is not connected to a charger, only poll for a location once every 5-30 minutes, depending on how much the battery is affected.
I know that I'll need to implement the tracking in a service, and probably show a notification to keep the service alive. However, I gather that the phone might go into sleep mode at any time, which would stop the GPS tracking.
For these scenarios, what is the best way to do continuous background GPS tracking, without the phone going to sleep, and without draining too much power?
So far I gathered the following:
For scenario 1, I'll probably need a acquire a WakeLock, and keep the CPU awake all the time (but the screen may sleep). The MyTracks application seems to be doing this in TrackRecordingService. OpenGPSTracking seems to do the same.
For scenario 2, I can use AlarmManager to schedule a check every x minutes. My service will need to acquire a temporary WakeLock, wait for a single GPS location, then stop and release the WakeLock again.
How would the power usage compare for these two methods (permanent WakeLock vs AlarmManager)? Can the GPS receive an accurate location in a reasonable amount of time when only checking every 30 minutes?
There are a few related questions on StackOverflow, but none with satisfying answers. Some semi-useful ones: