SettingsClient的位置请求始终获得RESULT_CANCELED [英] SettingsClient's request for location is always getting RESULT_CANCELED

查看:363
本文介绍了SettingsClient的位置请求始终获得RESULT_CANCELED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在获取当前位置流时,我正在使用SettingsClient检查基于当前LocationRequest是否满足位置设置. 目前,我的优先级设置为HIGH_ACCURACY,这需要不惜一切代价启用GPS.

In getting current location flow, I am using SettingsClient to check if location settings are satisfied based on current LocationRequest. Currently, my priority is set to HIGH_ACCURACY, which needs GPS to be enabled at all costs.

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        settingsClient = LocationServices.getSettingsClient(this);
        locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(500)
                .setFastestInterval(500);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(locationRequest);
        locationSettingsRequest = builder.build();

现在,当我调用SettingsClient.checkLocationSettings()为其提供侦听器时,

Now, when I call SettingsClient.checkLocationSettings() giving it listeners,

settingsClient.checkLocationSettings(locationSettingsRequest)
                .addOnCompleteListener(this)
                .addOnFailureListener(this);

它属于onFailure(),

it falls into onFailure(), google official samples on github takes following approach in this case;

检查onFailure()中接收到的异常的状态码(如果它是LocationSettingsStatusCodes.RESOLUTION_REQUIRED, 然后调用startResolutionForResult(),它使我们能够启用GPS,这会等待使用onActivityResult的结果.

Check status code of exception that is received in onFailure(), if it is LocationSettingsStatusCodes.RESOLUTION_REQUIRED, then call startResolutionForResult(), which allows us to enable GPS, this waits for result using onActivityResult.

    @Override
    public void onFailure(@NonNull Exception e) {

        int statusCode = ((ApiException) e).getStatusCode();
        switch (statusCode) {
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

                try {
                    // Show the dialog by calling startResolutionForResult(), and check the
                    // result in onActivityResult().
                    ResolvableApiException rae = (ResolvableApiException) e;
                    rae.startResolutionForResult(LocationActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException sie) {
                    showLocationError();
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                showError();
                break;
        }
    }

问题是,每当调用设置客户端的onFailure并调用startResolution时,它始终属于Activity.RESULT_CANCELED情况.但这很奇怪,尽管这已取消,但GPS却在几秒钟内打开.

The problem is, whenever settings client's onFailure is called and startResolution is called, it always falls into Activity.RESULT_CANCELED case.But weird thing here is, although this says cancelled, GPS is turned on in a few seconds.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // fetch location here
                    break;
                case Activity.RESULT_CANCELED:
                    // this is called immediately first time 
                    // and second time even when GPS is on
                    break;
            }
            break;
    }
}

当我执行相同的操作(获取当前位置)时GPS开启后,SettingsClient仍会调用onFailure,尽管GPS开启了,但结果始终为Activity.RESULT_CANCELED.

After GPS is on when I perform same operation (getting current location) , SettingsClient still calls onFailure, and result is always Activity.RESULT_CANCELED despite GPS is on.

问题正在Redmi 4X和Motorola Droid Turbo上再现

Issue is reproducing on Redmi 4X and Motorola Droid Turbo

使用SettingsClient并遇到类似问题的其他人吗?

Anybody else who has used SettingsClient with this approach and facing any similar issues?

在GitHub官方问题页面上也报告了此错误

推荐答案

我发现,将PRIORITY_HIGH_ACCURACY添加到请求中时,由于位置模式已设置为省电,因此即使是肯定的回答也可以将其打开位置服务(确实已开启),其模式仍为省电模式,无法处理PRIORITY_HIGH_ACCURACY请求,因此您得到RESULT_CANCELED,表明您的请求未得到满足.我不知道要求同时打开两个定位服务并将模式更改为高精度"的方法,因此我只是取消了优先级,因为它对我的用例而言并不那么重要.

I found that when adding the PRIORITY_HIGH_ACCURACY to the request this was happening because the location mode was set to Battery Saving, so even though answering in the affirmative to turn on Location Services (which are indeed turned on), the mode is still Battery Saving which cannot handle the PRIORITY_HIGH_ACCURACY request so you get RESULT_CANCELED indicating that your request was not satisfied. I do not know of a way to request the both Location Services be turned on and the mode be changed to High Accuracy, so I just eliminated the priority as it wasn't that important to my use case.

这篇关于SettingsClient的位置请求始终获得RESULT_CANCELED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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