Android位置请求PRIORITY_HIGH_ACCURACY无效

时间:2018-03-07 11:02:48

标签: android request location setting

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

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

这在华为手机上有效,但在三星S7和S8上失败:如果用户选择了电源平衡,则会出现一个对话框,并且位置跟踪设置为高精度。但是,如果先前选择了仅限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...
        }
    }
}

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

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我找到了解决方案。

    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
            val locationManager = application.getSystemService(Context.LOCATION_SERVICE) as LocationManager
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                AlertDialog.Builder(this@YourActivity)
                        .setMessage("message for user")
                        .setPositiveButton(R.string.ok) { _, _ ->
                            val intent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                            this@YourActivity.startActivityForResult(intent, RC_GPS_LOCATION)
                        }
                        .setNegativeButton(R.string.no_thanks, null)
                        .create()
                        .show()
            } else {
                // your code for High accuracy mode
            }
        } catch (ApiException exception) {
            switch (exception.getStatusCode()) {
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    try {
                        ResolvableApiException resolvable = (ResolvableApiException) exception;
                        resolvable.startResolutionForResult(getActivity(), RC_RESOLVE_LOCATION);
                    } catch (IntentSender.SendIntentException ex) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    showToast(R.string.sorry_your_device_is_not_supported);
                    break;
            }
        }
    }

请不要忘记处理活动结果。

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            RC_GPS_LOCATION -> {
                // do action after user closed GPS settings screen
            }
        }
    }

注意:在某些设备上,会启动一个对话框,使您可以一键启用GPS。在其他用户上,用户将被重定向到设置屏幕(但这总比没有好。)

答案 1 :(得分:0)

我与s8存在相同的问题,因此我检查了设备以及它是否与s8一起使用PRIORITY_BALANCED_POWER_ACCURACY进行了请求,并且可以正常工作,但是存在一些延迟。