1

为了使我们的应用程序在室内工作以获取位置,我们需要打开网络位置服务开关

在此处输入图像描述

我们正在使用这个功能来检测任何仍然关闭的设置 在此处输入图像描述

我们注意到响应是LocationSettingsStates,当开关始终为真时

在此处输入图像描述

我是否使用错误的功能来检测它?

4

2 回答 2

1

原始帖子中提到的类和方法是用于检查网络位置服务可用性的正确方法。

请参考从Github获取的华为示例代码中提取的部分代码

public void checkSettings(View view) {
    new Thread() {
        @Override
        public void run() {
            try {
                CheckSettingsRequest checkSettingsRequest = new CheckSettingsRequest();
                LocationRequest locationRequest = new LocationRequest();
                checkSettingsRequest.setLocationRequest(locationRequest);
                checkSettingsRequest.setAlwaysShow(false);
                checkSettingsRequest.setNeedBle(false);
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(checkSettingsRequest.getLocationRequest())
                    .setAlwaysShow(checkSettingsRequest.isAlwaysShow())
                    .setNeedBle(checkSettingsRequest.isNeedBle());
                settingsClient.checkLocationSettings(builder.build())
                    .addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
                        @Override
                        public void onComplete(Task<LocationSettingsResponse> task) {
                            if (task != null && task.isSuccessful()) {
                                LocationSettingsResponse response = task.getResult();
                                if (response == null) {
                                    return;
                                }
                                LocationSettingsStates locationSettingsStates =
                                    response.getLocationSettingsStates();

                                stringBuilder.append(",\nisLocationPresent=")
                                    .append(locationSettingsStates.isLocationPresent());
                                stringBuilder.append(",\nisLocationUsable=")
                                    .append(locationSettingsStates.isLocationUsable());
                                stringBuilder.append(",\nisNetworkLocationUsable=")
                                    .append(locationSettingsStates.isNetworkLocationUsable());
                                stringBuilder.append(",\nisNetworkLocationPresent=")
                                    .append(locationSettingsStates.isNetworkLocationPresent());
                                stringBuilder.append(",\nisHMSLocationUsable=")
                                    .append(locationSettingsStates.isHMSLocationUsable());
                                stringBuilder.append(",\nisHMSLocationPresent=")
                                    .append(locationSettingsStates.isHMSLocationPresent());
                                LocationLog.i(TAG, "checkLocationSetting onComplete:" + stringBuilder.toString());
                            }
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(Exception e) {
                            LocationLog.i(TAG, "checkLocationSetting onFailure:" + e.getMessage());
                            int statusCode = 0;
                            if (e instanceof ApiException) {
                                statusCode = ((ApiException) e).getStatusCode();
                            }
                            switch (statusCode) {
                                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                    android.util.Log.i(TAG,
                                        "Location settings are not satisfied. Attempting to upgrade "
                                            + "location settings ");
                                    try {
                                        // Show the dialog by calling startResolutionForResult(), and check the
                                        // result in onActivityResult().
                                        if (e instanceof ResolvableApiException) {
                                            ResolvableApiException rae = (ResolvableApiException) e;
                                            rae.startResolutionForResult(CheckSettingActivity.this, 0);
                                        }
                                    } catch (IntentSender.SendIntentException sie) {
                                        android.util.Log.i(TAG, "PendingIntent unable to execute request.");
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    });
            } catch (Exception e) {
                LocationLog.i(TAG, "checkLocationSetting exception:" + e.getMessage());
            }
        }
    }.start();
}

开启和关闭“网络定位服务”时的执行结果如下图所示。它分别以真假表示状态。

在此处输入图像描述

于 2021-12-16T04:33:29.887 回答
1

在某些手机中,LocationSettings 界面可能无法获得准确的状态。

您可以将优先级设置为 PRIORITY_BALANCED_POWER_ACCURACY 并使用 requestLocationUpdatesWithCallback 接口获取位置更新。

如果未启用网络位置,您将收到错误代码 NETWORK_LOCATION_SERVICES_DISABLED 10105。

则表示该开关未启用。

于 2021-12-16T06:34:59.560 回答