Android位置请求PRIORITY_HIGH_ACCURACY不起作用 [英] Android location request PRIORITY_HIGH_ACCURACY has no effect

查看:234
本文介绍了Android位置请求PRIORITY_HIGH_ACCURACY不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I adapted Google's official example (https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient) to Kotlin.

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

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 :)

推荐答案

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

I encountered the same problem. And I found a solution.

    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;
            }
        }
    }

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

Please do not forget to handle an activity result.

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.在其他用户上,用户将被重定向到设置屏幕(但这总比没有好.)

Note: On some devices, a dialog is launched that allows you to enable GPS in one click. On others, the user will be redirected to the settings screen (But this is better than nothing).

这篇关于Android位置请求PRIORITY_HIGH_ACCURACY不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆