My Android app needs high accuracy location tracking. On app start, it reads the location settings programmatically and presents a screen if high accuracy is not selected.
I adapted Google's official example (https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient) to Kotlin.
This works as intended on a Huawei phone, but fails on Samsung S7 and S8: If the user has selected Power balanced, a dialog appears and the location tracking is set to high accuracy. However, if GPS only was selected previously, the ApiException
is not thrown and the setting stays the same.
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val result = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build())
result.addOnCompleteListener(OnCompleteListener {
try {
it.getResult(ApiException::class.java)
// on Samsung + GPS only setting, execution passes here, no ApiException thrown
} catch(e: ApiException) {
if(e is ResolvableApiException) {
// ...show resolution dialog...
}
}
}
Any hints would be much appreciated, thanks :)