Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : LocationManager dynamically adjust minTime/minDistance thresholds

This is the way I listen for GPS location updates (using LocationManager and a LocationListener):

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new MyLocationistener(); // LocationListener
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        30000, // milliseconds (minTime)
        20, // meters (minDistance)
        listener );

But I would like to dynamically adjust the minTime and minDistance arguments used by LocationManager#requestLocationUpdates. My aim is to save battery, according to several usage policies, i.e.:

  • If the user is not moving, increase the minTime to get a location update
  • If the user is moving very fast, increase the minDistance
  • If the user is indoors (no GPS coverage), increase both
  • If the battery is too low, increase both
  • ... (any other heuristic)

I would like to know:

  1. Is this really a good idea to save battery life ?
  2. How could I do that kind of adjustments? I can call both LocationManager#removeUpdates and LocationManager#requestLocationUpdates again, if there is the only alternative.
  3. Any idea or sample code you know to implement this kind of adaptive algorithms ?

Edit: The application is a tracking system to know where people are, in order to assign tasks to the person nearest to a given poing. Actually battery hardly lasts 8 hours, so I'd like to increase it.

like image 231
Guido Avatar asked Aug 22 '10 11:08

Guido


2 Answers

AFAIK you need to call removeUpdates and requestLocationUpdates again.

Also, you can look into other ways to see if the phone is moving at all, like the accelerometer. Read about it here and see this other question's answers

But To give you more ideas, you need to present the problem itself. Don't try to optimize too early, until you don't have a problem. If you have you need to post details about it.

like image 153
Pentium10 Avatar answered Sep 30 '22 01:09

Pentium10


Is this really a good idea to save battery life ?

Probably not. AFAIK, the GPS radio will be on the whole time, and that is the biggest battery drain in what you're describing. The way to save battery is to remove updates, so the GPS radio shuts off. However, the next time you request location updates, it will take some time for GPS to acquire its initial fix.

If the user is indoors (no GPS coverage), increase both

If you cannot get a GPS fix, turn the radio off, and try again some number of minutes later (or based on a UI event or something). There is no sense leaving the GPS on if it is not doing you any good.

If the battery is too low, increase both

If the battery is too low, turn off GPS. Either switch to a low-power provider (use Criteria to find one) or go without location data. Let the user decide what is "too low" via a preference.

like image 43
CommonsWare Avatar answered Sep 30 '22 00:09

CommonsWare