即使点击了“确定”,LocationSettingsRequest在onActivityResult中仍然返回0

26

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

        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。这是在更新Play服务到16.0.0之后发生的。已在Google issuetracker上报告了此错误,以下是更多详细信息的链接。

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


请发布您正在使用的代码以接收弹出窗口结果。 - BLuFeNiX
你在 onActivityResult 中调用了 super 吗?如果是,请尝试将其删除,然后告诉我是否解决了你的问题。 - Vilen
我有同样的问题,我发现有时它会进入onActivityResult并使用NETWORK_PROVIDER && GPS_PROVIDER以OK的结果启动,而有时它会取消结果并仅启动设备位置模式。这是我的问题 https://stackoverflow.com/questions/53387741/strange-behavior-of-locationrequest-priorities - Vadim Eksler
1个回答

0

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

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

https://github.com/gavingt/basic_location_improved


使用支持库版本16.0.0,问题仅出现在Android N及以下操作系统版本。 - amodkanthe
你的应用程序的targetSDKVersion为什么这么低?显然小于23,因为它没有请求运行时权限。这可能与此有关。无论如何,你应该以我的代码为基准开始,因为你所做的某些事情不起作用,而我的代码运行良好。 - Gavin Wright
这只是演示应用程序,因为我没有足够的时间处理权限,而且它只是一个样品,保留目标23。但我尝试使用sdk 27并从设置中启用了权限,问题仍然存在。 - amodkanthe
没有完整的项目文件,很难确定问题所在。但我猜测可能与你的Gradle依赖有关。 - Gavin Wright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接