android.permission.ACCESS_COARSE_LOCATION
if your app needs approximate location information.android.permission.ACCESS_FINE_LOCATION
if your app needs precise location information.<manifest ... >
<!-- Always include this permission -->
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<!--Include only if your app benefits from precise location access. -->
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
/>
</manifest>
ACCESS_FINE_LOCATION
runtime permission. To accommodate this user preference, avoid requesting the ACCESS_FINE_LOCATION
permission in isolation. Instead, make a single runtime request for the ACCESS_FINE_LOCATION
and ACCESS_COARSE_LOCATION
permissions.// Create a class-level field for the ActivityResultLauncher
private ActivityResultLauncher<String[]> locationPermissionLauncher;
// Initialize the ActivityResultLauncher in your onCreate or initialization method
locationPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(),
new ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> permissions) {
if (permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false)) {
// Precise location access granted.
} else if (permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false)) {
// Only approximate location access granted.
} else {
// No location access granted.
}
}
}
);
Permission | Precise | Approximate |
---|---|---|
While using the app | ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION | ACCESS_COARSE_LOCATION |
Only this time | ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION | ACCESS_COARSE_LOCATION |
Deny | No location permissions | No location permissions |
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Handle location updates here
}
};
public Location getLastKnownLocation (String provider)
public void getCurrentLocation (String provider,
LocationRequest locationRequest,
CancellationSignal cancellationSignal,
Executor executor,
Consumer<Location> consumer)
public void requestLocationUpdates (String provider,
long minTimeMs,
float minDistanceM,
LocationListener listener)
public void requestLocationUpdates (String provider,
long minTimeMs,
float minDistanceM,
PendingIntent pendingIntent)
public void requestLocationUpdates (String provider,
LocationRequest locationRequest,
PendingIntent pendingIntent)
public void requestLocationUpdates (String provider,
LocationRequest locationRequest,
Executor executor,
LocationListener listener)
public void removeUpdates (LocationListener listener)
getCurrentLocation()
, might differ from the AOSP.Public methods | |
---|---|
getAllProviders() returns a list of the names of all available location providers. | |
getCurrentLocation() asynchronously returns a single current location fix from the given provider based on the given LocationRequest. Note: Your Meta Quest headset may provide a cached location for this call. | |
getLastKnownLocation(String provider) Gets the last known location by provider from the cache. | |
getProvider(String provider) returns a list of the names of available location providers. Note: Android deprecated this method in version 31 of the API. | |
getProviderProperties(String provider) returns the properties of the given provider or null if the properties are currently unknown. | |
getProviders() returns a list of the names of available location providers. | |
hasProvider() returns true if the given location provider exists on this device, irrespective of whether it is currently enabled. | |
isLocationEnabled() returns the current enabled/disabled state of the location. To listen for changes, see MODE_CHANGED_ACTION. | |
isProvidedEnabled() returns the current enabled/disabled status of the given provider. To listen for changes, see PROVIDERS_CHANGED_ACTION. | |
requestFlush() requests that the given provider flush any batched locations to listeners. | |
requestLocationUpdates() registers for location updates from the given provider | |
requestSingleUpdate() registers for a single location update using a named provider and pending intent. Note: Android deprecated this method in version 30 of the API. |
Public methods | |
---|---|
addGpsStatusListener() Meta Quest headsets do not have a GPS chipset. | |
addNmeaListener() Meta Quest headsets do not have a GPS chipset. | |
addProximityAlert() ACCESS_BACKGROUND_LOCATION permission is prohibited | |
addTestProvider() Leads to SecurityException. | |
clearTestProvider() Lead to SecurityException | |
getGnssAntennaInfo() Meta Quest headsets do not have a GPS chipset. | |
getGnssCapabilitie() Meta Quest headsets do not have a GPS chipset. | |
getGnssHardwareModelName() Meta Quest headsets do not have a GPS chipset. | |
getGnssYearofHardware() Meta Quest headsets do not have a GPS chipset. | |
getGPSStatus() Meta Quest headsets do not have a GPS chipset. | |
registerAntennaInfoListener() Meta Quest headsets do not have a GPS chipset. | |
registerGnssMeasurementsCallback() Meta Quest headsets do not have a GPS chipset. | |
registerGnssNavigationCallback() Meta Quest headsets do not have a GPS chipset. | |
registerGnssStatusCallback() Meta Quest headsets do not have a GPS chipset. | |
removeGpsStatusListener() Meta Quest headsets do not have a GPS chipset. | |
removeNmeaListener() Meta Quest headsets do not have a GPS chipset. | |
removeProximityAlert() ACCESS_BACKGROUND_LOCATION permission is prohibited. | |
removeTestProvider() Leads to SecurityException. | |
sendExtraCommand() ACCESS_LOCATION_EXTRA_COMMANDS permission is prohibited. | |
setTestProviderEnabled() Leads to SecurityException. | |
setTestProviderLocation() Leads to SecurityException. | |
setTestProviderStatus() Leads to SecurityException. | |
unregisterAntennaInfoListener() Meta Quest headsets do not have a GPS chipset. | |
unregisterGnssNavigationMessageCallback() Meta Quest headsets do not have a GPS chipset. |