views:

152

answers:

2

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.

A: 

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.

CommonsWare
Thank you. When do you think it is better to keep the GPS radio on (better than switch it off and on again). Seconds, minutes ? I mean, maybe if I need a fix every 30 seconds, it is better to keep it on, but it would not be true if you need a fix every 5 minutes.
Guido
You can't switch GPS on programatically, your user needs to switch it on for you. You can just present them the settings screen.
Pentium10
In order to answer how frequently you need to poll data, give us more details. Such as what for are you using the Location data?
Pentium10
@Pentium10: To reduce confusion, and to match the Android docs, I use "enabled" and "disabled" for what the user controls via settings, and "on" and "off" to refer to whether the GPS radio is consuming power and obtaining fixes.
CommonsWare
@Guido: As Pentium10 indicated, it is difficult to answer you in the abstract. Bear in mind that it takes GPS a while to obtain fixes after having been just turned on. So, if you need fixes every 5 minutes, but you have to have those fixes then right away, you probably cannot turn off GPS. Personally, I'd let the user make as many decisions here as possible, via preferences.
CommonsWare
+1  A: 

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 the give you more idea, you need to present the problem itself. Don't try to optimize to early, until you don't have a problem. If you have you need to post details about it.

Pentium10
Thank you. It is a internal application, where users have few technical skills, so they don't know what is a GPS and forcing them to change settings or switch GPA radio on and off has usability problems. Battery is now lasting 8-10 hours, and we need to extend it to at least 12 hours.
Guido