views:

531

answers:

5

Hi,

Is there any way to specify the time intervals that the Location Manager broadcasts the current location?

I am using a method called startListening:

public void startListening() {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                0, 
                0, 
                this
        );
}

Thanks

+2  A: 

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)

Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.

It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.

In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.

The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended.

The supplied Looper is used to implement the callback mechanism.

Parameters

  • provider the name of the provider with which to register
  • minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
  • minDistance the minimum distance interval for notifications, in meters
  • listener a whose onLocationChanged(Location) method will be called for each location update
  • looper a Looper object whose message queue will be used to implement the callback mechanism.

Throws

IllegalArgumentException if provider is null or doesn't exist
IllegalArgumentException if listener is null
IllegalArgumentException if looper is null
SecurityException if no suitable permission is present for the provider.

Pentium10
but is it the lesser of the 2 that it picks? If minTime is 10000 and minDistance is 0, could it still potentially update every second?
Xster
The doc doesn't tell us how it's working exactly, it can be. Anyway don't rely on the interval value.
Pentium10
A: 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 9000, 500,locListener);

// 60000 Is 1 Minute and 100 is KM

Above you have 9000 (Nine Seconds) and 500 which is if the location has moved more than 500KM

David L. Williams
I think you mean metres (m) not kilometres (km).
bramp
A: 

minTime : the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.

minDistance: the minimum distance interval for notifications, in meters

Zeeshan Qamar
A: 

I tried using a large value, such as 60000 for minTime, and yet, the location service still was constantly updating (and quickly draining the batter). Some hint! Which is why I searched and found this example. Thanks! +1

wrongmissle
A: 

Same problem here, is someone has an awnser to this i really would like to know! Ive noticed that the minDistance does work, because when i put that higher it will not update constantly

Webbo