Android location request PRIORITY_HIGH_ACCURACY not affected

My Android app requires precision location tracking. When the application starts, it automatically reads the location parameters and displays a screen if high accuracy is not selected.

I applied the official Google example ( https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient ) in Kotlin.

This works like on a Huawei phone, but it doesn’t work on Samsung S7 and S8: if the user selects Power balance, a dialog box will appear, and location tracking will be installed with high accuracy. However, if only GPS was previously selected, it is ApiExceptionnot thrown away and the setting remains unchanged.

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 tips would be much appreciated, thanks :)

+4

Source: https://habr.com/ru/post/1694587/


All Articles