7

How can I update time and distance intervals, or call requestLocationUpdates again with different intervals?

My code:

LocationManager locationManager = (LocationManager) activityObject.getSystemService(Context.LOCATION_SERVICE);

    cll = new CheckinLocationListener();

    LocationProvider gps = locationManager.getProvider(LocationManager.GPS_PROVIDER);

    // GPS

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeDelay, distanceDelay, cll);

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

onLocationChanged:

if(location.getSpeed()>=0 && location.getSpeed()<= 3)
            {
                LocationsModel.this.distanceDelay = 25;
                LocationsModel.this.timeDelay = 10000;
            }
            else if(location.getSpeed()>=3 && location.getSpeed()<= 17)
            {
                LocationsModel.this.distanceDelay = 150;
                LocationsModel.this.timeDelay = 60000;
            }
            else if(location.getSpeed()>=17)
            {
                LocationsModel.this.distanceDelay = 300;
                LocationsModel.this.timeDelay = 240000;
            }

I would like to use the new intervals at the next measure.

McDermott
  • 1,025
  • 2
  • 16
  • 28

1 Answers1

7

You'll need to first call locationManager.removeUpdates( cll ); and then re-register for updates using your new interval.

Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • 1
    It works, I've put it in onLocationChanged after the checks, but it loops infinitely, it starts gps and turns it off after every second. So the gps icon blinks continuously, and I get a coordinate for every second. – McDermott Jun 14 '11 at 08:28
  • Maybe you should change your "am I moving" conditional because this will return true even when you're stationary or if the LocationProvider does not support speed (because of the `location.getSpeed()>=0`). Perhaps this should be `location.getSpeed()>=0.5` so that it's not constantly jabbering because of small movements. Maybe you should also be checking `location.hasSpeed()` first, as well. – Mark Allison Jun 14 '11 at 08:36
  • I've tried location.hasSpeed() and location.getSpeed()>=1, but i still get a coordinate each second. I think when i call a new requestLocationUpdates, that calls onLocationChanged immediately it connects to a gps provider, so thats why it loops. – McDermott Jun 14 '11 at 08:54
  • That's possible. Maybe you should try storing the timstamp (`System.currentTimeMillis();`) of the last time `onLocationChanged()` was called, and only re-register the location updates if a minimum time period has elapsed (i.e. your current timeDelay value). – Mark Allison Jun 14 '11 at 08:57
  • I've made something similar, and it works well (I think, need testing). Thank You all for your quick help. – McDermott Jun 14 '11 at 09:03
  • after the checks i made another: `` if(LocationsModel.this.LASTdistanceDelay != LocationsModel.this.distanceDelay && LocationsModel.this.LASTtimeDelay != LocationsModel.this.timeDelay) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); locationManager.removeUpdates( cll ); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeDelay, distanceDelay, cll); } `` – McDermott Jun 14 '11 at 09:05
  • I have the same trouble... and whats about with this posting http://stackoverflow.com/questions/18914316/setfastestintervallong-milis-not-working Is this wrong? – Martin Pfeffer Oct 07 '14 at 01:27