9

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.

Guido
  • 46,642
  • 28
  • 120
  • 174
  • Interesting question though I can't help much. I think it's a good idea to save battery. And I would tend to say that the solution you suggest is the only one but I'm no expert in the location API so I'll let others speak. – Sephy Aug 22 '10 at 12:12
  • [i have used the same code in my application but it does'nt returns any values , was wondering if any of yu guys could help me with ...][1] [1]: http://stackoverflow.com/questions/9514980/android-getting-speed-with-gps –  Mar 02 '12 at 04:33
  • remove and add, just like this: https://stackoverflow.com/questions/5385094/change-mintime-for-gps-locationlistener-dynamically – jp1017 Nov 01 '16 at 03:23

3 Answers3

4

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.

RAINA
  • 802
  • 11
  • 22
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 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 Sep 13 '10 at 06:49
1

I have done something like this:

I had a LocationService (extends Service and implements LocationListener). and I added

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 1, 1, this);

and in the onLocationChangedListener:

    public void onLocationChanged(Location location) {
        AlarmManager aleManager = (AlarmManager) LocationService.context.getSystemService(ALARM_SERVICE);
//Change the values of MINIMIUM_TRIGGER_TIME and INTERVAL_TIME
        Intent forceLoc = new Intent(LocationService.context,
                ForceLocationService.class);
        PendingIntent pendingForceLoc = PendingIntent.getService(
                LocationService.context, 0, forceLoc,
                PendingIntent.FLAG_ONE_SHOT);
        // CANCEL PREVIOUS alarm and set new one
        aleManager.cancel(pendingForceLoc);
        aleManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                MINIMIUM_TRIGGER_TIME, INTERVAL_TIME, pendingForceLoc);
    }

and In the ForceLocationService (extends IntentService) trigger the regular location listener like this:

locMan=(LocationManager)getSystemService(LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationService);

This let me do the following:

  1. If the system gets any location update before the alarmManager triggers the ForceLocationService, it automatically gets the update and resets the alarm.

  2. If it doesn't get the update till the alarm triggers, then it forces the alarm to trigger, thereby ensuring that we get the update when needed.

The only issue I have is that, it doesn't work if there is no change in location cause the resetting of alarm is done in onLocationChanged().

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Naresh
  • 3,174
  • 1
  • 17
  • 22
1

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
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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 Aug 22 '10 at 14:07
  • 1
    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 Aug 22 '10 at 18:39
  • 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 Aug 22 '10 at 18:40
  • 1
    @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 Aug 22 '10 at 21:16
  • @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 Aug 22 '10 at 21:18