codepath / codepath/android_guides

SettingsClient returns failure when Location is enabled on device

Open
#398 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
28.3k
Forks
6.2k
PR merge metrics
No merged PRs in 30d

Description

Facing issue with SettingsClient.
Due to https://developer.android.com/training/location/change-location-settings i did following request to ask user enable location when its disabled:
```
settingsClient.checkLocationSettings(locationSettingsRequest.build())
.addOnSuccessListener {
viewModel.locationPermissionGranted()
}
.addOnFailureListener {
if (it is ResolvableApiException) {
try {
it.startResolutionForResult(
fragment.activity,
RESULT_CODE_LOCATION
)
} catch (sendEx: IntentSender.SendIntentException) {
...
}
}
}
```
**First issue** here that onFailure() is called when location is enabled on device(but in this case onSuccessListener should be called).

Bad, but then user still will see dialog to enable location and after he taps "OK", i am trying to request location updates :
```
**// ITS ALWAYS TRUE**
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
) {
val callback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationClient.removeLocationUpdates(this) ....
}

override fun onLocationAvailability(p0: LocationAvailability?) {
super.onLocationAvailability(p0)
**// BUT ALWAYS APPEAR HERE with p0.isLocationAvailable==false**
}
}

locationClient.requestLocationUpdates(
locationRequest,
callback,
Looper.myLooper()
)
}
```

So **second issue** that even locationManager.isProviderEnabled(GPS||NETWORK) always returns true (so location enabled as it is), but just onLocationAvailability(p0: LocationAvailability?) is called (where p0.isLocationAvailable==false)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.