【问题标题】:Android location request PRIORITY_HIGH_ACCURACY has no effectAndroid位置请求PRIORITY_HIGH_ACCURACY无效
【发布时间】:2018-08-15 11:04:41
【问题描述】:

我的 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...
        }
    }
}

任何提示将不胜感激,谢谢:)

【问题讨论】:

  • 也有这个问题。

标签: android request location setting


【解决方案1】:

我对 s8 也有同样的问题,所以我检查了设备,如果是 s8,我向PRIORITY_BALANCED_POWER_ACCURACY 发出请求,它可以工作,但有一些延迟。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 2014-06-03
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2017-04-28
      相关资源
      最近更新 更多