LocationManagerCompat

public final class LocationManagerCompat
extends Object

java.lang.Object
   ↳ androidx.core.location.LocationManagerCompat


Helper for accessing features in LocationManager.

Summary

Public methods

static void getCurrentLocation(LocationManager locationManager, String provider, CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer)

Asynchronously returns a single current location fix from the given provider.

static String getGnssHardwareModelName(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(LocationManager locationManager)

Returns the model year of the GNSS hardware and software build, or 0 if the model year is before 2016.

static boolean isLocationEnabled(LocationManager locationManager)

Returns the current enabled/disabled state of location.

static boolean registerGnssStatusCallback(LocationManager locationManager, GnssStatusCompat.Callback callback, Handler handler)

Registers a platform agnostic GnssStatusCompat.Callback.

static boolean registerGnssStatusCallback(LocationManager locationManager, Executor executor, GnssStatusCompat.Callback callback)

Registers a platform agnostic GnssStatusCompat.Callback.

static void unregisterGnssStatusCallback(LocationManager locationManager, GnssStatusCompat.Callback callback)

Unregisters a platform agnostic GnssStatusCompat.Callback.

Inherited methods

Public methods

getCurrentLocation

public static void getCurrentLocation (LocationManager locationManager, 
                String provider, 
                CancellationSignal cancellationSignal, 
                Executor executor, 
                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.

Requires the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions.

Parameters
locationManager LocationManager

provider String

cancellationSignal CancellationSignal

executor Executor

consumer Consumer

getGnssHardwareModelName

public static String getGnssHardwareModelName (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.

Parameters
locationManager LocationManager

Returns
String

getGnssYearOfHardware

public static int getGnssYearOfHardware (LocationManager locationManager)

Returns the model year of the GNSS hardware and software build, or 0 if the model year is before 2016.

Parameters
locationManager LocationManager

Returns
int

isLocationEnabled

public static boolean isLocationEnabled (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.

Parameters
locationManager LocationManager

Returns
boolean true if location is enabled or false if location is disabled

registerGnssStatusCallback

public static boolean registerGnssStatusCallback (LocationManager locationManager, 
                GnssStatusCompat.Callback callback, 
                Handler handler)

Registers a platform agnostic GnssStatusCompat.Callback. See LocationManager.addGpsStatusListener(GpsStatus.Listener) and LocationManager.registerGnssStatusCallback(GnssStatus.Callback, Handler).

Requires the ACCESS_FINE_LOCATION permission.

Parameters
locationManager LocationManager

callback GnssStatusCompat.Callback

handler Handler

Returns
boolean

registerGnssStatusCallback

public static boolean registerGnssStatusCallback (LocationManager locationManager, 
                Executor executor, 
                GnssStatusCompat.Callback callback)

Registers a platform agnostic GnssStatusCompat.Callback. See LocationManager.addGpsStatusListener(GpsStatus.Listener) and LocationManager.registerGnssStatusCallback(Executor, GnssStatus.Callback).

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.

Requires the ACCESS_FINE_LOCATION permission.

Parameters
locationManager LocationManager

executor Executor

callback GnssStatusCompat.Callback

Returns
boolean

Throws
IllegalStateException on Android M or below, if the current Looper or main Looper is blocked by the thread this function is invoked on

unregisterGnssStatusCallback

public static void unregisterGnssStatusCallback (LocationManager locationManager, 
                GnssStatusCompat.Callback callback)

Unregisters a platform agnostic GnssStatusCompat.Callback. See LocationManager.removeGpsStatusListener(GpsStatus.Listener) and LocationManager.unregisterGnssStatusCallback(GnssStatus.Callback).

Parameters
locationManager LocationManager

callback GnssStatusCompat.Callback