Android位置请求PRIORITY_HIGH_ACCURACY无效

jan*_*ann 5 android location request setting

我的Android应用需要高精度的位置跟踪.在应用程序启动时,它会以编程方式读取位置设置,并在未选择高精度时显示屏幕.

我将Google的官方示例(https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient)改编为Kotlin.

这在华为手机上有效,但在三星S7和S8上失败:如果用户选择了Power平衡,则会出现一个对话框,并且位置跟踪设置为高精度.但是,如果之前选择了GPS,ApiException则不会抛出,并且设置保持不变.

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...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何提示将非常感谢,谢谢:)