LocationManager

2017-7-31 6:35:59 来源: Gnsser 发布人:

LocationManager

public class LocationManager 
extends Object 

java.lang.Object
   ↳android.location.LocationManager



This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

Unless noted, all Location API methods require the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions. If your application only has the coarse permission then it will not have access to the GPS or passive location providers. Other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument LocationManager.class orContext.getSystemService(String) with the argument Context.LOCATION_SERVICE.

Summary


Constants

StringGPS_PROVIDER

Name of the GPS location provider.

StringKEY_LOCATION_CHANGED

Key used for a Bundle extra holding a Location value when a location change is broadcast using a PendingIntent.

StringKEY_PROVIDER_ENABLED

Key used for a Bundle extra holding an Boolean status value when a provider enabled/disabled event is broadcast using a PendingIntent.

StringKEY_PROXIMITY_ENTERING

Key used for the Bundle extra holding a boolean indicating whether a proximity alert is entering (true) or exiting (false)..

StringKEY_STATUS_CHANGED

Key used for a Bundle extra holding an Integer status value when a status change is broadcast using a PendingIntent.

StringMODE_CHANGED_ACTION

Broadcast intent action when LOCATION_MODE changes.

StringNETWORK_PROVIDER

Name of the network location provider.

StringPASSIVE_PROVIDER

A special location provider for receiving locations without actually initiating a location fix.

StringPROVIDERS_CHANGED_ACTION

Broadcast intent action when the configured location providers change.

Public methods

booleanaddGpsStatusListener(GpsStatus.Listener listener)

This method was deprecated in API level 24. use registerGnssStatusCallback(GnssStatus.Callback) instead.

booleanaddNmeaListener(OnNmeaMessageListener listener, Handler handler)

Adds an NMEA listener.

booleanaddNmeaListener(OnNmeaMessageListener listener)

Adds an NMEA listener.

booleanaddNmeaListener(GpsStatus.NmeaListener listener)

This method was deprecated in API level 24. use addNmeaListener(OnNmeaMessageListener) instead.

voidaddProximityAlert(double latitude, double longitude, float radius, long expiration, PendingIntent intent)

Set a proximity alert for the location given by the position (latitude, longitude) and the given radius.

voidaddTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite, boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude, boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy)

Creates a mock location provider and adds it to the set of active providers.

voidclearTestProviderEnabled(String provider)

Removes any mock enabled value associated with the given provider.

voidclearTestProviderLocation(String provider)

Removes any mock location associated with the given provider.

voidclearTestProviderStatus(String provider)

Removes any mock status values associated with the given provider.

List<String>getAllProviders()

Returns a list of the names of all known location providers.

StringgetBestProvider(Criteria criteria, boolean enabledOnly)

Returns the name of the provider that best meets the given criteria.

GpsStatusgetGpsStatus(GpsStatus status)

Retrieves information about the current status of the GPS engine.

LocationgetLastKnownLocation(String provider)

Returns a Location indicating the data from the last known location fix obtained from the given provider.

LocationProvidergetProvider(String name)

Returns the information associated with the location provider of the given name, or null if no provider exists by that name.

List<String>getProviders(Criteria criteria, boolean enabledOnly)

Returns a list of the names of LocationProviders that satisfy the given criteria, or null if none do.

List<String>getProviders(boolean enabledOnly)

Returns a list of the names of location providers.

booleanisProviderEnabled(String provider)

Returns the current enabled/disabled status of the given provider.

booleanregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback callback, Handler handler)

Registers a GPS Measurement callback.

booleanregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback callback)

Registers a GPS Measurement callback.

booleanregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback, Handler handler)

Registers a GNSS Navigation Message callback.

booleanregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback)

Registers a GNSS Navigation Message callback.

booleanregisterGnssStatusCallback(GnssStatus.Callback callback)

Registers a GNSS status callback.

booleanregisterGnssStatusCallback(GnssStatus.Callback callback, Handler handler)

Registers a GNSS status callback.

voidremoveGpsStatusListener(GpsStatus.Listener listener)

This method was deprecated in API level 24. use unregisterGnssStatusCallback(GnssStatus.Callback) instead.

voidremoveNmeaListener(OnNmeaMessageListener listener)

Removes an NMEA listener.

voidremoveNmeaListener(GpsStatus.NmeaListener listener)

This method was deprecated in API level 24. use removeNmeaListener(OnNmeaMessageListener) instead.

voidremoveProximityAlert(PendingIntent intent)

Removes the proximity alert with the given PendingIntent.

voidremoveTestProvider(String provider)

Removes the mock location provider with the given name.

voidremoveUpdates(LocationListener listener)

Removes all location updates for the specified LocationListener.

voidremoveUpdates(PendingIntent intent)

Removes all location updates for the specified pending intent.

voidrequestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

Register for location updates using the named provider, and a pending intent.

voidrequestLocationUpdates(long minTime, float minDistance, Criteria criteria, LocationListener listener, Looperlooper)

Register for location updates using a Criteria, and a callback on the specified looper thread.

voidrequestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)

Register for location updates using a Criteria and pending intent.

voidrequestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looperlooper)

Register for location updates using the named provider, and a callback on the specified looper thread.

voidrequestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)

Register for location updates using the named provider, and a pending intent.

voidrequestSingleUpdate(String provider, PendingIntent intent)

Register for a single location update using a named provider and pending intent.

voidrequestSingleUpdate(String provider, LocationListener listener, Looper looper)

Register for a single location update using the named provider and a callback.

voidrequestSingleUpdate(Criteria criteria, LocationListener listener, Looper looper)

Register for a single location update using a Criteria and a callback.

voidrequestSingleUpdate(Criteria criteria, PendingIntent intent)

Register for a single location update using a Criteria and pending intent.

booleansendExtraCommand(String provider, String command, Bundle extras)

Sends additional commands to a location provider.

voidsetTestProviderEnabled(String provider, boolean enabled)

Sets a mock enabled value for the given provider.

voidsetTestProviderLocation(String provider, Location loc)

Sets a mock location for the given provider.

voidsetTestProviderStatus(String provider, int status, Bundle extras, long updateTime)

Sets mock status values for the given provider.

voidunregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback callback)

Unregisters a GPS Measurement callback.

voidunregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback)

Unregisters a GNSS Navigation Message callback.

voidunregisterGnssStatusCallback(GnssStatus.Callback callback)

Removes a GNSS status callback.

Inherited methods


 From class java.lang.Object

Constants


GPS_PROVIDER

added in API level 1

String GPS_PROVIDER

Name of the GPS location provider.

This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.

The extras Bundle for the GPS location provider can contain the following key/value pairs:

  • satellites - the number of satellites used to derive the fix



Constant Value: "gps"

KEY_LOCATION_CHANGED

added in API level 3

String KEY_LOCATION_CHANGED

Key used for a Bundle extra holding a Location value when a location change is broadcast using a PendingIntent.


Constant Value: "location"

KEY_PROVIDER_ENABLED

added in API level 3

String KEY_PROVIDER_ENABLED

Key used for a Bundle extra holding an Boolean status value when a provider enabled/disabled event is broadcast using a PendingIntent.


Constant Value: "providerEnabled"

KEY_PROXIMITY_ENTERING

added in API level 1

String KEY_PROXIMITY_ENTERING

Key used for the Bundle extra holding a boolean indicating whether a proximity alert is entering (true) or exiting (false)..


Constant Value: "entering"

KEY_STATUS_CHANGED

added in API level 3

String KEY_STATUS_CHANGED

Key used for a Bundle extra holding an Integer status value when a status change is broadcast using a PendingIntent.


Constant Value: "status"

MODE_CHANGED_ACTION

added in API level 19

String MODE_CHANGED_ACTION

Broadcast intent action when LOCATION_MODE changes. For use with the LOCATION_MODE API. If you're interacting with isProviderEnabled(String), usePROVIDERS_CHANGED_ACTION instead. In the future, there may be mode changes that do not result in PROVIDERS_CHANGED_ACTION broadcasts.


Constant Value: "android.location.MODE_CHANGED"

NETWORK_PROVIDER

added in API level 1

String NETWORK_PROVIDER

Name of the network location provider.

This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup.


Constant Value: "network"

PASSIVE_PROVIDER

added in API level 8

String PASSIVE_PROVIDER

A special location provider for receiving locations without actually initiating a location fix.

This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. You can query the getProvider() method to determine the origin of the location update. Requires the permission ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes.


Constant Value: "passive"

PROVIDERS_CHANGED_ACTION

added in API level 9

String PROVIDERS_CHANGED_ACTION

Broadcast intent action when the configured location providers change. For use with isProviderEnabled(String). If you're interacting with theLOCATION_MODE API, use MODE_CHANGED_ACTION instead.


Constant Value: "android.location.PROVIDERS_CHANGED"

Public methods


addGpsStatusListener

added in API level 3

boolean addGpsStatusListener (GpsStatus.Listener listener)


This method was deprecated in API level 24.
use registerGnssStatusCallback(GnssStatus.Callback) instead.

Adds a GPS status listener.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
listenerGpsStatus.Listener: GPS status listener object to register


Returns
booleantrue if the listener was successfully added


Throws
SecurityExceptionif the ACCESS_FINE_LOCATION permission is not present

addNmeaListener

added in API level 24

boolean addNmeaListener (OnNmeaMessageListener listener, 
                Handler handler)

Adds an NMEA listener.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
listenerOnNmeaMessageListener: a OnNmeaMessageListener object to register


handlerHandler: the handler that the listener runs on.


Returns
booleantrue if the listener was successfully added


Throws
SecurityExceptionif the ACCESS_FINE_LOCATION permission is not present

addNmeaListener

added in API level 24

boolean addNmeaListener (OnNmeaMessageListener listener)

Adds an NMEA listener.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
listenerOnNmeaMessageListener: a OnNmeaMessageListener object to register


Returns
booleantrue if the listener was successfully added


Throws
SecurityExceptionif the ACCESS_FINE_LOCATION permission is not present

addNmeaListener

added in API level 5

boolean addNmeaListener (GpsStatus.NmeaListener listener)


This method was deprecated in API level 24.
use addNmeaListener(OnNmeaMessageListener) instead.

Adds an NMEA listener.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
listenerGpsStatus.NmeaListener: a GpsStatus.NmeaListener object to register


Returns
booleantrue if the listener was successfully added


Throws
SecurityExceptionif the ACCESS_FINE_LOCATION permission is not present

addProximityAlert

added in API level 1

void addProximityAlert (double latitude, 
                double longitude, 
                float radius, 
                long expiration, 
                PendingIntent intent)

Set a proximity alert for the location given by the position (latitude, longitude) and the given radius.

When the device detects that it has entered or exited the area surrounding the location, the given PendingIntent will be used to create an Intent to be fired.

The fired Intent will have a boolean extra added with key KEY_PROXIMITY_ENTERING. If the value is true, the device is entering the proximity region; if false, it is exiting.

Due to the approximate nature of position estimation, if the device passes through the given area briefly, it is possible that no Intent will be fired. Similarly, an Intent could be fired if the device passes very close to the given area but does not actually enter it.

After the number of milliseconds given by the expiration parameter, the location manager will delete this proximity alert and no longer monitor it. A value of -1 indicates that there should be no expiration time.

Internally, this method uses both NETWORK_PROVIDER and GPS_PROVIDER.

Before API version 17, this method could be used with ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. From API version 17 and onwards, this method requires ACCESS_FINE_LOCATION permission.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
latitudedouble: the latitude of the central point of the alert region


longitudedouble: the longitude of the central point of the alert region


radiusfloat: the radius of the central point of the alert region, in meters


expirationlong: time for this proximity alert, in milliseconds, or -1 to indicate no expiration


intentPendingIntent: a PendingIntent that will be used to generate an Intent to fire when entry to or exit from the alert region is detected


Throws
SecurityExceptionif ACCESS_FINE_LOCATION permission is not present

addTestProvider

added in API level 3

void addTestProvider (String name, 
                boolean requiresNetwork, 
                boolean requiresSatellite, 
                boolean requiresCell, 
                boolean hasMonetaryCost, 
                boolean supportsAltitude, 
                boolean supportsSpeed, 
                boolean supportsBearing, 
                int powerRequirement, 
                int accuracy)

Creates a mock location provider and adds it to the set of active providers.


Parameters
nameString: the provider name


requiresNetworkboolean


requiresSatelliteboolean


requiresCellboolean


hasMonetaryCostboolean


supportsAltitudeboolean


supportsSpeedboolean


supportsBearingboolean


powerRequirementint


accuracyint


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif a provider with the given name already exists

clearTestProviderEnabled

added in API level 3

void clearTestProviderEnabled (String provider)

Removes any mock enabled value associated with the given provider.


Parameters
providerString: the provider name


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists

clearTestProviderLocation

added in API level 3

void clearTestProviderLocation (String provider)

Removes any mock location associated with the given provider.


Parameters
providerString: the provider name


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists

clearTestProviderStatus

added in API level 3

void clearTestProviderStatus (String provider)

Removes any mock status values associated with the given provider.


Parameters
providerString: the provider name


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists

getAllProviders

added in API level 1

List<String> getAllProviders ()

Returns a list of the names of all known location providers.

All providers are returned, including ones that are not permitted to be accessed by the calling activity or are currently disabled.


Returns
List<String>list of Strings containing names of the provider


getBestProvider

added in API level 1

String getBestProvider (Criteria criteria, 
                boolean enabledOnly)

Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed by the calling activity will be returned. If several providers meet the criteria, the one with the best accuracy is returned. If no provider meets the criteria, the criteria are loosened in the following sequence:

  • power requirement

  • accuracy

  • bearing

  • speed

  • altitude

Note that the requirement on monetary cost is not removed in this process.


Parameters
criteriaCriteria: the criteria that need to be matched


enabledOnlyboolean: if true then only a provider that is currently enabled is returned


Returns
Stringname of the provider that best matches the requirements


getGpsStatus

added in API level 3

GpsStatus getGpsStatus (GpsStatus status)

Retrieves information about the current status of the GPS engine. This should only be called from the onGpsStatusChanged(int) callback to ensure that the data is copied atomically. The caller may either pass in a GpsStatus object to set with the latest status information, or pass null to create a newGpsStatus object.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
statusGpsStatus: object containing GPS status details, or null.


Returns
GpsStatusstatus object containing updated GPS status.


getLastKnownLocation

added in API level 1

Location getLastKnownLocation (String provider)

Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
providerString: the name of the provider


Returns
Locationthe last known location for the provider, or null


Throws
SecurityExceptionif no suitable permission is present
IllegalArgumentExceptionif provider is null or doesn't exist

getProvider

added in API level 1

LocationProvider getProvider (String name)

Returns the information associated with the location provider of the given name, or null if no provider exists by that name.


Parameters
nameString: the provider name


Returns
LocationProvidera LocationProvider, or null


Throws
IllegalArgumentExceptionif name is null or does not exist
SecurityExceptionif the caller is not permitted to access the given provider.

getProviders

added in API level 1

List<String> getProviders (Criteria criteria, 
                boolean enabledOnly)

Returns a list of the names of LocationProviders that satisfy the given criteria, or null if none do. Only providers that are permitted to be accessed by the calling activity will be returned.


Parameters
criteriaCriteria: the criteria that the returned providers must match


enabledOnlyboolean: if true then only the providers which are currently enabled are returned.


Returns
List<String>list of Strings containing names of the providers


getProviders

added in API level 1

List<String> getProviders (boolean enabledOnly)

Returns a list of the names of location providers.


Parameters
enabledOnlyboolean: if true then only the providers which are currently enabled are returned.


Returns
List<String>list of Strings containing names of the providers


isProviderEnabled

added in API level 1

boolean isProviderEnabled (String provider)

Returns the current enabled/disabled status of the given provider.

If the user has enabled this provider in the Settings menu, true is returned otherwise false is returned

Callers should instead use LOCATION_MODE unless they depend on provider-specific APIs such as requestLocationUpdates(String, long, float, LocationListener).

Before API version LOLLIPOP, this method would throw SecurityException if the location permissions were not sufficient to use the specified provider.


Parameters
providerString: the name of the provider


Returns
booleantrue if the provider exists and is enabled


Throws
IllegalArgumentExceptionif provider is null

registerGnssMeasurementsCallback

added in API level 24

boolean registerGnssMeasurementsCallback (GnssMeasurementsEvent.Callback callback, 
                Handler handler)

Registers a GPS Measurement callback.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
callbackGnssMeasurementsEvent.Callback: a GnssMeasurementsEvent.Callback object to register.


handlerHandler: the handler that the callback runs on.


Returns
booleantrue if the callback was added successfully, false otherwise.


registerGnssMeasurementsCallback

added in API level 24

boolean registerGnssMeasurementsCallback (GnssMeasurementsEvent.Callback callback)

Registers a GPS Measurement callback.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
callbackGnssMeasurementsEvent.Callback: a GnssMeasurementsEvent.Callback object to register.


Returns
booleantrue if the callback was added successfully, false otherwise.


registerGnssNavigationMessageCallback

added in API level 24

boolean registerGnssNavigationMessageCallback (GnssNavigationMessage.Callback callback, 
                Handler handler)

Registers a GNSS Navigation Message callback.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
callbackGnssNavigationMessage.Callback: a GnssNavigationMessage.Callback object to register.


handlerHandler: the handler that the callback runs on.


Returns
booleantrue if the callback was added successfully, false otherwise.


registerGnssNavigationMessageCallback

added in API level 24

boolean registerGnssNavigationMessageCallback (GnssNavigationMessage.Callback callback)

Registers a GNSS Navigation Message callback.


Parameters
callbackGnssNavigationMessage.Callback: a GnssNavigationMessage.Callback object to register.


Returns
booleantrue if the callback was added successfully, false otherwise.


registerGnssStatusCallback

added in API level 24

boolean registerGnssStatusCallback (GnssStatus.Callback callback)

Registers a GNSS status callback.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
callbackGnssStatus.Callback: GNSS status callback object to register


Returns
booleantrue if the listener was successfully added


Throws
SecurityExceptionif the ACCESS_FINE_LOCATION permission is not present

registerGnssStatusCallback

added in API level 24

boolean registerGnssStatusCallback (GnssStatus.Callback callback, 
                Handler handler)

Registers a GNSS status callback.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
callbackGnssStatus.Callback: GNSS status callback object to register


handlerHandler: the handler that the callback runs on.


Returns
booleantrue if the listener was successfully added


Throws
SecurityExceptionif the ACCESS_FINE_LOCATION permission is not present

removeGpsStatusListener

added in API level 3

void removeGpsStatusListener (GpsStatus.Listener listener)


This method was deprecated in API level 24.
use unregisterGnssStatusCallback(GnssStatus.Callback) instead.

Removes a GPS status listener.


Parameters
listenerGpsStatus.Listener: GPS status listener object to remove


removeNmeaListener

added in API level 24

void removeNmeaListener (OnNmeaMessageListener listener)

Removes an NMEA listener.


Parameters
listenerOnNmeaMessageListener: a OnNmeaMessageListener object to remove


removeNmeaListener

added in API level 5

void removeNmeaListener (GpsStatus.NmeaListener listener)


This method was deprecated in API level 24.
use removeNmeaListener(OnNmeaMessageListener) instead.

Removes an NMEA listener.


Parameters
listenerGpsStatus.NmeaListener: a GpsStatus.NmeaListener object to remove


removeProximityAlert

added in API level 1

void removeProximityAlert (PendingIntent intent)

Removes the proximity alert with the given PendingIntent.

Before API version 17, this method could be used with ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. From API version 17 and onwards, this method requires ACCESS_FINE_LOCATION permission.


Parameters
intentPendingIntent: the PendingIntent that no longer needs to be notified of proximity alerts


Throws
IllegalArgumentExceptionif intent is null
SecurityExceptionif ACCESS_FINE_LOCATION permission is not present

removeTestProvider

added in API level 3

void removeTestProvider (String provider)

Removes the mock location provider with the given name.


Parameters
providerString: the provider name


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists

removeUpdates

added in API level 1

void removeUpdates (LocationListener listener)

Removes all location updates for the specified LocationListener.

Following this call, updates will no longer occur for this listener.


Parameters
listenerLocationListener: listener object that no longer needs location updates


Throws
IllegalArgumentExceptionif listener is null

removeUpdates

added in API level 3

void removeUpdates (PendingIntent intent)

Removes all location updates for the specified pending intent.

Following this call, updates will no longer for this pending intent.


Parameters
intentPendingIntent: pending intent object that no longer needs location updates


Throws
IllegalArgumentExceptionif intent is null

requestLocationUpdates

added in API level 1

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

Register for location updates using the named provider, and a pending intent.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
providerString: the name of the provider with which to register


minTimelong: minimum time interval between location updates, in milliseconds


minDistancefloat: minimum distance between location updates, in meters


listenerLocationListener: a LocationListener whose onLocationChanged(Location) method will be called for each location update


Throws
IllegalArgumentExceptionif provider is null or doesn't exist on this device
IllegalArgumentExceptionif listener is null
RuntimeExceptionif the calling thread has no Looper
SecurityExceptionif no suitable permission is present

requestLocationUpdates

added in API level 9

void requestLocationUpdates (long minTime, 
                float minDistance, 
                Criteria criteria, 
                LocationListener listener, 
                Looper looper)

Register for location updates using a Criteria, and a callback on the specified looper thread.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
minTimelong: minimum time interval between location updates, in milliseconds


minDistancefloat: minimum distance between location updates, in meters


criteriaCriteria: contains parameters for the location manager to choose the appropriate provider and parameters to compute the location


listenerLocationListener: a LocationListener whose onLocationChanged(Location) method will be called for each location update


looperLooper: a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread


Throws
IllegalArgumentExceptionif criteria is null
IllegalArgumentExceptionif listener is null
SecurityExceptionif no suitable permission is present

requestLocationUpdates

added in API level 9

void requestLocationUpdates (long minTime, 
                float minDistance, 
                Criteria criteria, 
                PendingIntent intent)

Register for location updates using a Criteria and pending intent.

The requestLocationUpdates() and requestSingleUpdate() register the current activity to be updated periodically by the named provider, or by the provider matching the specified Criteria, with location and status updates.

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

Location updates are received either by LocationListener callbacks, or by broadcast intents to a supplied PendingIntent.

If the caller supplied a pending intent, then location updates are sent with a key of KEY_LOCATION_CHANGED and a Location value.

The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications.

Choosing a sensible value for minTime is important to conserve battery life. Each location update requires power from GPS, WIFI, Cell and other radios. Select a minTime value as high as possible while still providing a reasonable user experience. If your application is not in the foreground and showing location to the user then your application should avoid using an active provider (such as NETWORK_PROVIDER or GPS_PROVIDER), but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes) or greater. If your application is in the foreground and showing location to the user then it is appropriate to select a faster update interval.

The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.

If your application wants to passively observe location updates triggered by other applications, but not consume any additional power otherwise, then use the PASSIVE_PROVIDER This provider does not actively turn on or modify active location providers, so you do not need to be as careful about minTime and minDistance. However if your application performs heavy work on a location update (such as network activity) then you should select non-zero values for minTime and/or minDistance to rate-limit your update frequency in the case another application enables a location provider with extremely fast updates.

In case the provider is disabled by the user, updates will stop, and a provider availability update will be sent. As soon as the provider is enabled again, location updates will immediately resume and a provider availability update sent. Providers can also send status updates, at any time, with extra's specific to the provider. If a callback was supplied then status and availability updates are via onProviderDisabled(String),onProviderEnabled(String) or onStatusChanged(String, int, Bundle). Alternately, if a pending intent was supplied then status and availability updates are broadcast intents with extra keys of KEY_PROVIDER_ENABLED or KEY_STATUS_CHANGED.

If a LocationListener is used but with no Looper specified then the calling thread must already be a Looper thread such as the main thread of the calling Activity. If a Looper is specified with a LocationListener then callbacks are made on the supplied Looper thread.

Prior to Jellybean, the minTime parameter was only a hint, and some location provider implementations ignored it. From Jellybean and onwards it is mandatory for Android compatible devices to observe both the minTime and minDistance parameters.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
minTimelong: minimum time interval between location updates, in milliseconds


minDistancefloat: minimum distance between location updates, in meters


criteriaCriteria: contains parameters for the location manager to choose the appropriate provider and parameters to compute the location


intentPendingIntent: a PendingIntent to be sent for each location update


Throws
IllegalArgumentExceptionif criteria is null
IllegalArgumentExceptionif intent is null
SecurityExceptionif no suitable permission is present

requestLocationUpdates

added in API level 1

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

Register for location updates using the named provider, and a callback on the specified looper thread.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
providerString: the name of the provider with which to register


minTimelong: minimum time interval between location updates, in milliseconds


minDistancefloat: minimum distance between location updates, in meters


listenerLocationListener: a LocationListener whose onLocationChanged(Location) method will be called for each location update


looperLooper: a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread


Throws
IllegalArgumentExceptionif provider is null or doesn't exist
IllegalArgumentExceptionif listener is null
SecurityExceptionif no suitable permission is present

requestLocationUpdates

added in API level 3

void requestLocationUpdates (String provider, 
                long minTime, 
                float minDistance, 
                PendingIntent intent)

Register for location updates using the named provider, and a pending intent.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
providerString: the name of the provider with which to register


minTimelong: minimum time interval between location updates, in milliseconds


minDistancefloat: minimum distance between location updates, in meters


intentPendingIntent: a PendingIntent to be sent for each location update


Throws
IllegalArgumentExceptionif provider is null or doesn't exist on this device
IllegalArgumentExceptionif intent is null
SecurityExceptionif no suitable permission is present

requestSingleUpdate

added in API level 9

void requestSingleUpdate (String provider, 
                PendingIntent intent)

Register for a single location update using a named provider and pending intent.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
providerString: the name of the provider with which to register


intentPendingIntent: a PendingIntent to be sent for the location update


Throws
IllegalArgumentExceptionif provider is null or doesn't exist
IllegalArgumentExceptionif intent is null
SecurityExceptionif no suitable permission is present

requestSingleUpdate

added in API level 9

void requestSingleUpdate (String provider, 
                LocationListener listener, 
                Looper looper)

Register for a single location update using the named provider and a callback.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
providerString: the name of the provider with which to register


listenerLocationListener: a LocationListener whose onLocationChanged(Location) method will be called when the location update is available


looperLooper: a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread


Throws
IllegalArgumentExceptionif provider is null or doesn't exist
IllegalArgumentExceptionif listener is null
SecurityExceptionif no suitable permission is present

requestSingleUpdate

added in API level 9

void requestSingleUpdate (Criteria criteria, 
                LocationListener listener, 
                Looper looper)

Register for a single location update using a Criteria and a callback.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
criteriaCriteria: contains parameters for the location manager to choose the appropriate provider and parameters to compute the location


listenerLocationListener: a LocationListener whose onLocationChanged(Location) method will be called when the location update is available


looperLooper: a Looper object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread


Throws
IllegalArgumentExceptionif criteria is null
IllegalArgumentExceptionif listener is null
SecurityExceptionif no suitable permission is present

requestSingleUpdate

added in API level 9

void requestSingleUpdate (Criteria criteria, 
                PendingIntent intent)

Register for a single location update using a Criteria and pending intent.

See requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
criteriaCriteria: contains parameters for the location manager to choose the appropriate provider and parameters to compute the location


intentPendingIntent: a PendingIntent to be sent for the location update


Throws
IllegalArgumentExceptionif provider is null or doesn't exist
IllegalArgumentExceptionif intent is null
SecurityExceptionif no suitable permission is present

sendExtraCommand

added in API level 3

boolean sendExtraCommand (String provider, 
                String command, 
                Bundle extras)

Sends additional commands to a location provider. Can be used to support provider specific extensions to the Location Manager API


Parameters
providerString: name of the location provider.


commandString: name of the command to send to the provider.


extrasBundle: optional arguments for the command (or null). The provider may optionally fill the extras Bundle with results from the command.


Returns
booleantrue if the command succeeds.


setTestProviderEnabled

added in API level 3

void setTestProviderEnabled (String provider, 
                boolean enabled)

Sets a mock enabled value for the given provider. This value will be used in place of any actual value from the provider.


Parameters
providerString: the provider name


enabledboolean: the mock enabled value


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists

setTestProviderLocation

added in API level 3

void setTestProviderLocation (String provider, 
                Location loc)

Sets a mock location for the given provider.

This location will be used in place of any actual location from the provider. The location object must have a minimum number of fields set to be considered a valid LocationProvider Location, as per documentation on Location class.


Parameters
providerString: the provider name


locLocation: the mock location


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists
IllegalArgumentExceptionif the location is incomplete

setTestProviderStatus

added in API level 3

void setTestProviderStatus (String provider, 
                int status, 
                Bundle extras, 
                long updateTime)

Sets mock status values for the given provider. These values will be used in place of any actual values from the provider.


Parameters
providerString: the provider name


statusint: the mock status


extrasBundle: a Bundle containing mock extras


updateTimelong: the mock update time


Throws
SecurityExceptionif mock location app op is not set to allowed for your app.
IllegalArgumentExceptionif no provider with the given name exists

unregisterGnssMeasurementsCallback

added in API level 24

void unregisterGnssMeasurementsCallback (GnssMeasurementsEvent.Callback callback)

Unregisters a GPS Measurement callback.


Parameters
callbackGnssMeasurementsEvent.Callback: a GnssMeasurementsEvent.Callback object to remove.


unregisterGnssNavigationMessageCallback

added in API level 24

void unregisterGnssNavigationMessageCallback (GnssNavigationMessage.Callback callback)

Unregisters a GNSS Navigation Message callback.


Parameters
callbackGnssNavigationMessage.Callback: a GnssNavigationMessage.Callback object to remove.


unregisterGnssStatusCallback

added in API level 24

void unregisterGnssStatusCallback (GnssStatus.Callback callback)

Removes a GNSS status callback.


Parameters
callbackGnssStatus.Callback: GNSS status callback object to remove


阅读次数: 2632

下一篇: GnssStatus.Callback
上一篇: GnssMeasurement

尚无评论!

返回上一页面