即使单击确定,LocationSettingsRequest也会向onActivityResult返回0

amo*_*the 17 android android-location android-support-library google-play-services onactivityresult

我正在使用以下代码来显示弹出窗口以打开位置

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    SettingsClient client = LocationServices.getSettingsClient(getActivity());
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

    task.addOnSuccessListener(getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
            getUserLocation();
        }
    });

    task.addOnFailureListener(getActivity(), new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            int statusCode = ((ApiException) e).getStatusCode();
            Log.d("AMOD", "onFailure " + statusCode);
            switch (statusCode) {
                case CommonStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {

                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(getActivity(),
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way
                    // to fix the settings so we won't show the dialog.
                    break;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

以下是显示如下弹出窗口的代码 在此输入图像描述

但有时即使用户点击确定我得到了resposne 0即RESULT_CANCEL 在将播放服务更新为16.0.0后发生这种情况

在google issuetracker上报告了这个错误,以下是有关它的更多详细信息的链接

https://issuetracker.google.com/issues/118347902

Gav*_*ght 0

我刚刚尝试了大约 50 次来重现此问题,但总是得到代码 -1(运行 Android Pie 的 Pixel 3)。但在运行 Oreo 的 Nexus 5x 上,大约一半的时间我得到 0。

我无法准确地找出你做错了什么,但这是我制作的一个示例项目,它做同样的事情并且每次都能正确工作:

https://github.com/gavingt/basic_location_improved