【问题标题】:SettingsClient's request for location is always getting RESULT_CANCELEDSettingsClient 的位置请求总是得到 RESULT_CANCELED
【发布时间】:2019-08-28 05:57:41
【问题描述】:

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

        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() 给它监听器时,

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

它属于onFailure(),google official samples on github在这种情况下采取以下方法;

检查onFailure()中收到的异常状态码,如果是LocationSettingsStatusCodes.RESOLUTION_REQUIRED, 然后调用 startResolutionForResult(),它允许我们启用 GPS,这使用 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在几秒钟内就打开了。

@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,结果始终为 Activity.RESULT_CANCELED,尽管 GPS 已打开。

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

还有其他人使用过 SettingsClient 的这种方法并面临任何类似问题吗?

Here is the official sample I am following

This bug is also reported on official GitHub issues page

【问题讨论】:

  • 最近几天我也面临同样的问题?还是想不通。
  • 我在小米和金立设备上也面临这个问题...我猜这是上次播放服务更新中的错误
  • 在三星设备上也存在此问题。任何人都知道上一个可用的 Play Services 版本吗?
  • 如何处理?

标签: android location google-play-services android-gps


【解决方案1】:

我发现在向请求中添加 PRIORITY_HIGH_ACCURACY 时会发生这种情况,因为定位模式设置为省电模式,因此即使肯定回答打开定位服务(确实已打开),模式仍然是无法处理 PRIORITY_HIGH_ACCURACY 请求的电池节省,因此您得到 RESULT_CANCELED 指示您的请求未得到满足。我不知道有什么方法可以请求同时打开位置服务并将模式更改为高精度,所以我只是取消了优先级,因为它对我的用例并不重要。

【讨论】:

    【解决方案2】:

    添加这一行 bulider.setAlwaysShow(true);

    【讨论】:

    • 您能提供任何有助于上述情况的理由吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多