On Pixel 4a with Android 12 (SPB5.210812.002), when approximate location permission is given by the user, no location is returned from FusedLocationProviderClient. When I change permission to exact location permission, then I'm able to get location.
I have both coarse and fine location permissions in Manifest, as well as requesting both at runtime.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Once either permission is given, I'm requesting lastKnownLocation, as well as requesting for location updates.
With precise location permission, Im getting location shortly, but not when user gives approximate location permission.
For location request priority, I've tried both LocationRequest.PRIORITY_HIGH_ACCURACY and LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY.
On Android 9 everything works as expected, so I guess this is related to precise/approximate location permission, introduced in Android 12.
Here is part of my code:
private val fusedLocationClient by lazy {
LocationServices.getFusedLocationProviderClient(requireContext())
}
private val cts: CancellationTokenSource = CancellationTokenSource()
private val locationRequest = LocationRequest()
.setPriority(LOCATION_REQUEST_PRIORITY)
.setFastestInterval(MIN_TIME_BETWEEN_STAMPS_IN_MILLIS) // 1000
.setInterval(TIME_BETWEEN_STAMPS_IN_MILLIS) // 10000
private val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult?.locations?.firstOrNull()?.let {
userLocation = it
onUserLocationUpdated()
}
}
}
private fun onLocationPermissionGranted() {
if (!requireContext().isLocationEnabled()) {
requireContext().showLocationPermissionRequiredDialog {
onBackPressed()
}
} else {
try {
getCurrentLocation()
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
} catch (t: Throwable) {
onBackPressed()
}
}
}
private fun getCurrentLocation() {
fusedLocationClient.getCurrentLocation(
LOCATION_REQUEST_PRIORITY,
cts.token
).addOnSuccessListener { location: Location? ->
if (location != null) {
userLocation = location
onUserLocationUpdated()
}
}
}