Android: How to periodically check current location without draining the battery
- by uyahalom
I have a background service which works periodically by timer.scheduleAtFixedRate.
It wakes up every amount of time (let's say 60 seconds for example) and checks for the location.
The location is checked by locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 5, listener); and the actual location is collected from the listener's onLocationChanged.
Now, when the phone is outside and GPS reception is good, this works fine.
But, if the phone is inside, the GPS is almost always active - looking for a signal, and the battery is drained rapidly.
I created another thread using a Handler and a Runnable in order to conrol the GPS active time accurately:
I used locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); and locManager.removeUpdates(listener); so I can open and close the GPS as I want.
In this case, I can open the GPS for the exact amount of time, but found out that it doesn't lock in an area with good reception even after 10 seconds. So here I'm draining the battery again...
I'm using API level 7, hence I cannot use locationManager.requestSingleUpdate.
I have two questions:
Is there any way to optimize this process?
Will upgrading to API level 9 (and use locationManager.requestSingleUpdate) improve the process significantly? I mean, does it worth upgrading?