LocationManagerCompat
public final class LocationManagerCompat
Helper for accessing features in LocationManager.
Summary
Public methods |
|
|---|---|
static void |
@RequiresPermission(anyOf = )Asynchronously returns a single current location fix from the given provider. |
static void |
@RequiresPermission(anyOf = )This method is deprecated. |
static @Nullable String |
getGnssHardwareModelName(@NonNull LocationManager locationManager)Returns the model name (including vendor and hardware/software version) of the GNSS hardware driver, or null if this information is not available. |
static int |
getGnssYearOfHardware(@NonNull LocationManager locationManager)Returns the model year of the GNSS hardware and software build, or 0 if the model year is before 2016. |
static boolean |
hasProvider(Returns true if the given location provider exists on this device, irrespective of whether it is currently enabled or not. |
static boolean |
isLocationEnabled(@NonNull LocationManager locationManager)Returns the current enabled/disabled state of location. |
static boolean |
@RequiresApi(value = VERSION_CODES.N)Registers a GNSS measurement callback. |
static boolean |
@RequiresApi(value = VERSION_CODES.N)Registers a GNSS measurement callback. |
static boolean |
@RequiresPermission(value = "android.permission.ACCESS_FINE_LOCATION")Registers a platform agnostic |
static boolean |
@RequiresPermission(value = "android.permission.ACCESS_FINE_LOCATION")Registers a platform agnostic |
static void |
@RequiresPermission(anyOf = )Removes all location updates for the specified |
static void |
@RequiresPermission(anyOf = )Register for location updates from the specified provider, using a |
static void |
@RequiresPermission(anyOf = )Register for location updates from the specified provider, using a |
static void |
@RequiresApi(value = VERSION_CODES.N)Unregisters a GNSS measurement callback. |
static void |
unregisterGnssStatusCallback(Unregisters a platform agnostic |
Public methods
getCurrentLocation
@RequiresPermission(anyOf = )
public static void getCurrentLocation(
@NonNull LocationManager locationManager,
@NonNull String provider,
@Nullable CancellationSignal cancellationSignal,
@NonNull Executor executor,
@NonNull Consumer<Location> consumer
)
Asynchronously returns a single current location fix from the given provider. This may activate sensors in order to compute a new location. The given callback will be invoked once and only once, either with a valid location or with a null location if the provider was unable to generate a valid location.
A client may supply an optional CancellationSignal. If this is used to cancel the operation, no callback should be expected after the cancellation.
This method may return locations from the very recent past (on the order of several seconds), but will never return older locations (for example, several minutes old or older). Clients may rely upon the guarantee that if this method returns a location, it will represent the best estimation of the location of the device in the present moment.
Clients calling this method from the background may notice that the method fails to determine a valid location fix more often than while in the foreground. Background applications may be throttled in their location accesses to some degree.
@RequiresPermission(anyOf = )
public static voidgetCurrentLocation(
@NonNull LocationManager locationManager,
@NonNull String provider,
@Nullable CancellationSignal cancellationSignal,
@NonNull Executor executor,
@NonNull Consumer<Location> consumer
)
Asynchronously returns a single current location fix from the given provider. This may activate sensors in order to compute a new location. The given callback will be invoked once and only once, either with a valid location or with a null location if the provider was unable to generate a valid location.
A client may supply an optional CancellationSignal. If this is used to cancel the operation, no callback should be expected after the cancellation.
This method may return locations from the very recent past (on the order of several seconds), but will never return older locations (for example, several minutes old or older). Clients may rely upon the guarantee that if this method returns a location, it will represent the best estimation of the location of the device in the present moment.
Clients calling this method from the background may notice that the method fails to determine a valid location fix more often than while in the foreground. Background applications may be throttled in their location accesses to some degree.
getGnssHardwareModelName
public static @Nullable String getGnssHardwareModelName(@NonNull LocationManager locationManager)
Returns the model name (including vendor and hardware/software version) of the GNSS hardware driver, or null if this information is not available.
No device-specific serial number or ID is returned from this API.
getGnssYearOfHardware
public static int getGnssYearOfHardware(@NonNull LocationManager locationManager)
Returns the model year of the GNSS hardware and software build, or 0 if the model year is before 2016.
hasProvider
public static boolean hasProvider(
@NonNull LocationManager locationManager,
@NonNull String provider
)
Returns true if the given location provider exists on this device, irrespective of whether it is currently enabled or not. If called on Android Q and below for the FUSED_PROVIDER, this method may return incorrect results if the client does not hold at least the ACCESS_COARSE_LOCATION permission.
isLocationEnabled
public static boolean isLocationEnabled(@NonNull LocationManager locationManager)
Returns the current enabled/disabled state of location.
NOTE: Calling this method on API levels prior to 20 may require the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission if run on non-standard Android devices. The vast majority of devices should not require either permission to be present for this method.
| Returns | |
|---|---|
boolean |
|
registerGnssMeasurementsCallback
@RequiresApi(value = VERSION_CODES.N)
@RequiresPermission(value = "android.permission.ACCESS_FINE_LOCATION")
public static boolean registerGnssMeasurementsCallback(
@NonNull LocationManager locationManager,
@NonNull GnssMeasurementsEvent.Callback callback,
@NonNull Handler handler
)
Registers a GNSS measurement callback. See registerGnssMeasurementsCallback.
The primary purpose for this compatibility method is to help avoid crashes when delivering GNSS measurements to client on Android R. This bug was fixed in Android R QPR1, but since it's possible not all Android R devices have upgraded to QPR1, this compatibility method is provided to ensure GNSS measurements can be delivered successfully on all platforms.
registerGnssMeasurementsCallback
@RequiresApi(value = VERSION_CODES.N)
@RequiresPermission(value = "android.permission.ACCESS_FINE_LOCATION")
public static boolean registerGnssMeasurementsCallback(
@NonNull LocationManager locationManager,
@NonNull Executor executor,
@NonNull GnssMeasurementsEvent.Callback callback
)
Registers a GNSS measurement callback. See registerGnssMeasurementsCallback.
In addition to allowing the use of Executors on older platforms, this compatibility method also helps avoid crashes when delivering GNSS measurements to clients on Android R. This bug was fixed in Android R QPR1, but since it's possible not all Android R devices have upgraded to QPR1, this compatibility method is provided to ensure GNSS measurements can be delivered successfully on all platforms.
registerGnssStatusCallback
@RequiresPermission(value = "android.permission.ACCESS_FINE_LOCATION")
public static boolean registerGnssStatusCallback(
@NonNull LocationManager locationManager,
@NonNull GnssStatusCompat.Callback callback,
@NonNull Handler handler
)
Registers a platform agnostic GnssStatusCompat.Callback. See addGpsStatusListener and registerGnssStatusCallback.
| See also | |
|---|---|
registerGnssStatusCallback |
registerGnssStatusCallback
@RequiresPermission(value = "android.permission.ACCESS_FINE_LOCATION")
public static boolean registerGnssStatusCallback(
@NonNull LocationManager locationManager,
@NonNull Executor executor,
@NonNull GnssStatusCompat.Callback callback
)
Registers a platform agnostic GnssStatusCompat.Callback. See addGpsStatusListener and registerGnssStatusCallback.
Internally, this API will always utilize GnssStatus APIs and instances on Android N and above, and will always utilize GpsStatus APIs and instances below Android N. Callbacks will always occur on the given executor.
If invoked on Android M or below, this will result in GpsStatus registration being run on either the current Looper or main Looper. If the thread this function is invoked on is different from that Looper, the caller must ensure that the Looper thread cannot be blocked by the thread this function is invoked on. The easiest way to avoid this is to ensure this function is invoked on a Looper thread.
| Throws | |
|---|---|
java.lang.IllegalStateException |
on Android M or below, if the current Looper or main Looper is blocked by the thread this function is invoked on |
removeUpdates
@RequiresPermission(anyOf = )
public static void removeUpdates(
@NonNull LocationManager locationManager,
@NonNull LocationListenerCompat listener
)
Removes all location updates for the specified LocationListener.
See removeUpdates for more information.
requestLocationUpdates
@RequiresPermission(anyOf = )
public static void requestLocationUpdates(
@NonNull LocationManager locationManager,
@NonNull String provider,
@NonNull LocationRequestCompat locationRequest,
@NonNull Executor executor,
@NonNull LocationListenerCompat listener
)
Register for location updates from the specified provider, using a LocationRequestCompat, and a callback on the specified Executor.
See requestLocationUpdates for more information.
requestLocationUpdates
@RequiresPermission(anyOf = )
public static void requestLocationUpdates(
@NonNull LocationManager locationManager,
@NonNull String provider,
@NonNull LocationRequestCompat locationRequest,
@NonNull LocationListenerCompat listener,
@NonNull Looper looper
)
Register for location updates from the specified provider, using a LocationRequestCompat, and a callback on the specified Looper.
See requestLocationUpdates for more information.
unregisterGnssMeasurementsCallback
@RequiresApi(value = VERSION_CODES.N)
public static void unregisterGnssMeasurementsCallback(
@NonNull LocationManager locationManager,
@NonNull GnssMeasurementsEvent.Callback callback
)
Unregisters a GNSS measurement callback. See unregisterGnssMeasurementsCallback.
unregisterGnssStatusCallback
public static void unregisterGnssStatusCallback(
@NonNull LocationManager locationManager,
@NonNull GnssStatusCompat.Callback callback
)
Unregisters a platform agnostic GnssStatusCompat.Callback. See removeGpsStatusListener and unregisterGnssStatusCallback.