Android location request PRIORITY_HIGH_ACCURACY has no effect

China☆狼群 提交于 2019-12-23 12:33:03

问题


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.

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

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


回答1:


i have the same problem with s8, so I check device and if it s8 i make request with PRIORITY_BALANCED_POWER_ACCURACY and it works, but has some delay.




回答2:


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

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



来源:https://stackoverflow.com/questions/49150328/android-location-request-priority-high-accuracy-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!