public interface FusedLocationProviderClient implements HasApiKey<Api.ApiOptions.NoOptions>

The main entry point for interacting with the Fused Location Provider (FLP). In order to obtain an instance of this class, see LocationServices.

In order to use most location APIs, clients are required to hold either the Manifest.permission.ACCESS_COARSE_LOCATION permission or the Manifest.permission.ACCESS_FINE_LOCATION. Clients holding only the coarse permission will receive locations that have been obfuscated to hide the device's exact location, and only reveal the approximate area of the device. In addition, clients with only the coarse permission will receive location updates at a throttled rate. Applications which do not require an exact location to work (such as a weather app for instance) are encouraged to use only the coarse permission. From Android 12 onwards, the user may force any app to use coarse location, so apps should test carefully their behavior with only the coarse location permission to ensure everything works as expected.

If clients have only the coarse or fine location permission, they will not receive locations while they are in the background. Whether an app is in the background or foreground is normally determined by whether it is currently showing any UI to the user. Apps may also use a foreground location service to maintain their foreground status when they would normally be in the background.

If clients also hold the Manifest.permission.ACCESS_BACKGROUND_LOCATION permission, they may receive locations while in the background even if the above conditions are not met.

There are several types of use cases for location. One of the most common is simply obtaining a single location in order to determine where the device is now, and continue from there. The getCurrentLocation(CurrentLocationRequest, CancellationToken) API is designed with exactly this use case in mind. On the other hand, if repeated location updates are required, such as when tracking the user's location over time, requestLocationUpdates(LocationRequest, Executor, LocationListener) or one of its variants is better suited. Clients are encourage to familiarize themselves with the full range of APIs available in this class to understand which is best suited for their needs.

Constant Summary

String KEY_MOCK_LOCATION This constant is deprecated. Use Location.isMock() on Android S and above, otherwise use LocationCompat.isMock() from the compat libraries instead.
String KEY_VERTICAL_ACCURACY This constant is deprecated. Use Location.getVerticalAccuracyMeters() on Android O and above, otherwise use LocationCompat.getVerticalAccuracyMeters() from the compat libraries instead.

Public Method Summary

abstract Task<Void>
flushLocations()
Flushes any locations currently being batched and sends them to all registered LocationListeners, LocationCallbacks, and PendingIntents.
abstract Task<Location>
getCurrentLocation(int priority, CancellationToken cancellationToken)
Returns a single location fix representing the best estimate of the current location of the device.
abstract Task<Location>
getCurrentLocation(CurrentLocationRequest request, CancellationToken cancellationToken)
Returns a single location fix representing the best estimate of the current location of the device.
abstract Task<Location>
getLastLocation(LastLocationRequest request)
Returns the most recent historical location currently available according to the given request.
abstract Task<Location>
getLastLocation()
Returns the most recent historical location currently available.
abstract Task<LocationAvailability>
getLocationAvailability()
Returns the estimated availability of location data.
abstract Task<Void>
removeLocationUpdates(LocationListener listener)
Removes all location updates for the given listener.
abstract Task<Void>
removeLocationUpdates(LocationCallback callback)
Removes all location updates for the given callback.
abstract Task<Void>
removeLocationUpdates(PendingIntent pendingIntent)
Removes all location updates for the given pending intent.
abstract Task<Void>
requestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper)
Requests location updates with the given request and results delivered to the given listener on the specified Looper.
abstract Task<Void>
requestLocationUpdates(LocationRequest request, Executor executor, LocationCallback callback)
Requests location updates with the given request and results delivered to the given callback on the specified Executor.
abstract Task<Void>
requestLocationUpdates(LocationRequest request, Executor executor, LocationListener listener)
Requests location updates with the given request and results delivered to the given listener on the specified Executor.
abstract Task<Void>
requestLocationUpdates(LocationRequest request, LocationCallback callback, Looper looper)
Requests location updates with the given request and results delivered to the given callback on the specified Looper.
abstract Task<Void>
requestLocationUpdates(LocationRequest request, PendingIntent pendingIntent)
Requests location updates with the given request and results delivered via the specified PendingIntent.
abstract Task<Void>
setMockLocation(Location location)
Sets the mock location of the Fused Location Provider.
abstract Task<Void>
setMockMode(boolean mockMode)
Sets whether or not the Fused Location Provider is in mock mode.

Constants

public static final String KEY_MOCK_LOCATION

This constant is deprecated.
Use Location.isMock() on Android S and above, otherwise use LocationCompat.isMock() from the compat libraries instead.

Key used for the Bundle extra in Location object indicating whether this is a mock location.

Constant Value: "mockLocation"

public static final String KEY_VERTICAL_ACCURACY

This constant is deprecated.
Use Location.getVerticalAccuracyMeters() on Android O and above, otherwise use LocationCompat.getVerticalAccuracyMeters() from the compat libraries instead.

Key used for the Bundle extra in Location object holding a float indicating the estimated vertical accuracy of the location, in meters.

Constant Value: "verticalAccuracy"

Public Methods

public abstract Task<Void> flushLocations ()

Flushes any locations currently being batched and sends them to all registered LocationListeners, LocationCallbacks, and PendingIntents. This call is only useful when batching is specified using LocationRequest.setMaxWaitTime(long), otherwise locations are already delivered immediately when available.

When the returned Task is complete, then you can assume that any pending batched locations have already been delivered.

public abstract Task<Location> getCurrentLocation (int priority, CancellationToken cancellationToken)

Returns a single location fix representing the best estimate of the current location of the device. This may return a cached location if a recent enough location fix exists, or may compute a fresh location. If unable to retrieve a current location fix, null will be returned.

Clients may supply an optional CancellationToken which may be used to cancel the request.

Parameters
priority Priority used to obtain location
cancellationToken optional CancellationToken to cancel the request

public abstract Task<Location> getCurrentLocation (CurrentLocationRequest request, CancellationToken cancellationToken)

Returns a single location fix representing the best estimate of the current location of the device. This may return a historical location if a recent enough location fix exists, or may compute a fresh location. If unable to retrieve a current location fix, null will be returned.

Clients may supply an optional CancellationToken which may be used to cancel the request.

Parameters
request CurrentLocationRequest with parameters detailing how to obtain the current location
cancellationToken optional CancellationToken to cancel the request

public abstract Task<Location> getLastLocation (LastLocationRequest request)

Returns the most recent historical location currently available according to the given request. Will return null if no matching historical location is available.

public abstract Task<Location> getLastLocation ()

Returns the most recent historical location currently available. Will return null if no historical location is available. The historical location may be of an arbitrary age, so clients should check how old the location is to see if it suits their purposes.

public abstract Task<LocationAvailability> getLocationAvailability ()

Returns the estimated availability of location data. If LocationAvailability.isLocationAvailable() returns true then it is likely (but not guaranteed) that Fused Location Provider APIs will be able to derive and return fresh location updates. If LocationAvailability.isLocationAvailable() returns false, then it is likely (but not guaranteed) that Fused Location Provider APIs will be unable to derive and return fresh location updates, though there may be historical locations available.

public abstract Task<Void> removeLocationUpdates (LocationListener listener)

Removes all location updates for the given listener.

public abstract Task<Void> removeLocationUpdates (LocationCallback callback)

Removes all location updates for the given callback.

public abstract Task<Void> removeLocationUpdates (PendingIntent pendingIntent)

Removes all location updates for the given pending intent.

public abstract Task<Void> requestLocationUpdates (LocationRequest request, LocationListener listener, Looper looper)

Requests location updates with the given request and results delivered to the given listener on the specified Looper. A previous request for location updates for the same listener will be replaced by this request. If the location request has a priority higher than Priority.PRIORITY_PASSIVE, a wakelock may be held on the client's behalf while delivering locations.

Use removeLocationUpdates(LocationListener) to stop location updates once no longer needed.

Depending on the arguments passed in through the LocationRequest, locations from the past may be delivered when the listener is first registered. Clients should ensure they are checking location timestamps appropriately if necessary.

If the given Looper is null, the Looper associated with the calling thread will be used instead.

Throws
IllegalStateException if looper is null and the calling thread has not called Looper.prepare()

public abstract Task<Void> requestLocationUpdates (LocationRequest request, Executor executor, LocationCallback callback)

Requests location updates with the given request and results delivered to the given callback on the specified Executor. A previous request for location updates for the same callback will be replaced by this request. If the location request has a priority higher than Priority.PRIORITY_PASSIVE, a wakelock may be held on the client's behalf while delivering locations. A wakelock will not be held while delivering availability updates.

Use removeLocationUpdates(LocationCallback) to stop location updates once no longer needed.

Depending on the arguments passed in through the LocationRequest, locations from the past may be delivered when the callback is first registered. Clients should ensure they are checking location timestamps appropriately if necessary.

public abstract Task<Void> requestLocationUpdates (LocationRequest request, Executor executor, LocationListener listener)

Requests location updates with the given request and results delivered to the given listener on the specified Executor.

public abstract Task<Void> requestLocationUpdates (LocationRequest request, LocationCallback callback, Looper looper)

Requests location updates with the given request and results delivered to the given callback on the specified Looper. A previous request for location updates for the same callback will be replaced by this request. If the location request has a priority higher than Priority.PRIORITY_PASSIVE, a wakelock may be held on the client's behalf while delivering locations. A wakelock will not be held while delivering availability updates.

Use removeLocationUpdates(LocationCallback) to stop location updates once no longer needed.

Depending on the arguments passed in through the LocationRequest, locations from the past may be delivered when the callback is first registered. Clients should ensure they are checking location timestamps appropriately if necessary.

If the given Looper is null, the Looper associated with the calling thread will be used instead.

Throws
IllegalStateException if looper is null and the calling thread has not called Looper.prepare()

public abstract Task<Void> requestLocationUpdates (LocationRequest request, PendingIntent pendingIntent)

Requests location updates with the given request and results delivered via the specified PendingIntent. A previous request for location updates for the same pending intent will be replaced by this request. If the location request has a priority higher than Priority.PRIORITY_PASSIVE, a wakelock may be held on the client's behalf while delivering locations. A wakelock will not be held while delivering availability updates.

Location updates should be extracted from the received Intent via LocationResult.hasResult(Intent) and LocationResult.extractResult(Intent). Availability updates should be extracted from the Intent via LocationAvailability.hasLocationAvailability(Intent) and LocationAvailability.extractLocationAvailability(Intent).

This method is suited for receiving location updates in the background, even when the receiving app may have been killed by the system. Using a PendingIntent allows the target component to be started and receive location updates. For foreground use cases prefer to listen for location updates via a listener or callback instead of a pending intent.

PendingIntent location requests are automatically removed when the client application is reset (for example, when the client application is upgraded, restarted, removed, or force-quit), or if the pending intent is canceled.

Use removeLocationUpdates(PendingIntent) to stop location updates once no longer needed.

Depending on the arguments passed in through the LocationRequest, locations from the past may be delivered when the callback is first registered. Clients should ensure they are checking location timestamps appropriately if necessary.

This API will not work for instant apps, since they are intended to run only in the foreground and will be disconnected otherwise. Instant apps should prefer to use any of the overloads that accept LocationListener or LocationCallback instead.

public abstract Task<Void> setMockLocation (Location location)

Sets the mock location of the Fused Location Provider.

Delivers the given location to the FLP as if it was coming from an underlying location source. Normal FLP logic around receiving and delivering location will generally apply. For this reason the timestamps of the location should be set appropriately, as the FLP may expect monotonically increasing timestamps. When this location is reported to FLP clients it will be marked as a mock location (see Location.isMock() or LocationCompat.isMock() from the compat libraries).

This API can only be successfully used while the FLP is in mock mode. Clients must fulfill the same security requirements as for setMockMode(boolean) as well.

Parameters
location valid location to set as the next FLP location
Throws
SecurityException if security requirements are not met

public abstract Task<Void> setMockMode (boolean mockMode)

Sets whether or not the Fused Location Provider is in mock mode.

Entering mock mode clears the FLP's cached locations, and ensures that the FLP will only report locations set through setMockLocation(Location). Exiting mock mode will clear any mock locations set from the FLP's cache as well. Mock mode affects all location clients using the FLP, including location clients in other processes and derivative APIs such as geofencing and so forth. Because this affects all FLP usage, clients should always ensure they properly set the mock mode to false when finished.

Successfully using this API on devices running Android M+ requires the client to request the android.permission.ACCESS_MOCK_LOCATION permission and to be selected as the mock location app within the device developer settings. Using this API on pre-M devices requires the Settings.Secure.ALLOW_MOCK_LOCATION setting to be enabled.

Parameters
mockMode the mock mode state to set for the Fused Location Provider APIs
Throws
SecurityException if security requirements are not met