9

I am trying the 'LocationUpdates' sample from http://developer.android.com/training/location/receive-location-updates.html . This application gets and prints location notifications.

I am trying to change the interval of the location updates according to my latest location.

So - I had added mLocationRequest.setInterval() into onLocationChanged

The result is very wrong. My application is bombarded with many location updates (few a second!!!!)

My only change to the sample is this:

private int x=0;
@Override
public void onLocationChanged(Location location) {
    // Report to the UI that the location was updated
    mConnectionStatus.setText(R.string.location_updated);

    // In the UI, set the latitude and longitude to the value received
    mLatLng.setText(String.valueOf(x++));

    mLocationRequest.setInterval(1000);          // Change 1
    mLocationClient.requestLocationUpdates(mLocationRequest, this); // Change 2
}

How can I change the interval inside onLocationChanged ?

I think that the problem is that requestLocationUpdates resets the last request, and then immediately sends another notification. so a loop is created. (faster than the fastest interval). so I need a reliable way to change the interval of a 'live' LocationRequest

DuduArbel
  • 1,100
  • 1
  • 12
  • 25

3 Answers3

1

You are not supposed to call mLocationClient.requestLocationUpdates(mLocationRequest, this); inside onLocationChanged(Location location)

since you are registering the listener again, and you will get the first call immediately.

so what i would do would be:

  1. dont call mLocationClient.requestLocationUpdates(mLocationRequest, this); and see if anyways mLocationRequest.setInterval(1000); is taking effect
  2. if this doesnt work, try to unregister the listener, and then use a trick to wait before registering it again with the new settings, something like:

     Handler h = new Handler();
     @Override
       public void onLocationChanged(Location location) {
        //... all your code
    
    
         mLocationRequest.setInterval(1000);          
         mLocationClient.removeLocationUpdates(LocationListener listener)
    
         h.postDelayed (new Runnable(){
    
           public void run(){
             mLocationClient.requestLocationUpdates(mLocationRequest, YOUROUTTERCLASS.this); 
           }
    
         }, 1000);
    
        }
    

    So during one second there is not registered listener, so you wont get any updated, and after that, the listener is registerered with that interval.

Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
0

Try using mLocationRequest.setFastestInterval(long millis)
As mentioned in developer.android.com :

This allows your application to passively acquire locations at a rate faster than it actively acquires locations, saving power. Unlike setInterval(long), this parameter is exact. Your application will never receive updates faster than this value.

Ankit Bansal
  • 1,801
  • 17
  • 34
  • I did it, and set the fastest rate to 1000. Nevertheless I still get many notifications a second !? – DuduArbel May 01 '14 at 08:57
  • Then maybe removing previous updates and then starting a new LocationRequest might help, although android says "mLocationClient.requestLocationUpdates(mLocationRequest, this);" replace old ones but there are many bugs yet to be resolved ;) – Ankit Bansal May 01 '14 at 09:01
  • I think that the problem is that requestLocationUpdates resets the last request, and then immediately sends another notification. so a loop is created. (faster than the fastest interval). so I need a reliable way to change the interval – DuduArbel May 01 '14 at 09:17
0

Try to use :

mLocationRequest.requestLocationUpdates("gps", 1000, 0, this);

However I don't agree to do a requestLocationUpdates in onLocationChanged event; In my opinion should be setted outside onLocationChanged Event...

Ciro Rizzo
  • 492
  • 4
  • 8
  • It does not matter where I call it from. The point is that when you reset the updates, you might receive the next notification very fast, even faster that the fastest rate. – DuduArbel May 07 '14 at 06:20
  • It is probably not possible to change the interval at all. only restart the request, which is unreliable. – DuduArbel May 07 '14 at 06:22
  • 1
    My solution is to compare the current notification time to the previous notification time, and ignore it if they are too close - absurd. – DuduArbel May 07 '14 at 06:22