Specify intervals for Location Manager to broadcast current location in Android
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,
开发者_StackOverflow 0,
0,
this
);
}
Thanks
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.
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
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
精彩评论