Android: How to periodically check current location without draining the battery

Posted by uyahalom on Stack Overflow See other posts from Stack Overflow or by uyahalom
Published on 2012-06-03T09:43:59Z Indexed on 2012/06/03 10:40 UTC
Read the original article Hit count: 169

Filed under:
|
|

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:

  1. Is there any way to optimize this process?
  2. Will upgrading to API level 9 (and use locationManager.requestSingleUpdate) improve the process significantly? I mean, does it worth upgrading?

© Stack Overflow or respective owner

Related posts about android

Related posts about location