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

时间:2018-10-24 09:46:51

标签: android google-play-services android-support-library android-location 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;
            }
        }
    });

以下是代码 它显示如下弹出窗口 enter image description here

但是有时候即使用户单击确定,我也会收到0,即RESULT_CANCEL ,这是在将播放服务更新为16.0.0之后发生的情况

在Google issuetracker上报告了此错误,以下是有关该错误的更多链接

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

1 个答案:

答案 0 :(得分:-1)

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

我无法完全找出您在做什么错,但这是我制作的一个示例项目,该项目执行相同的操作并每次都能正常工作:

https://github.com/gavingt/basic_location_improved