如果用户选择“从不”选项,则为Android启用位置服务

时间:2015-07-20 19:37:20

标签: android location location-services

如果用户之前选择了“从不”选项,是否有任何方法可以再次为应用程序(或至少显示对话框)提供位置服务?

enter image description here

我在代码中找不到再次显示的方法,因为我总是在onResult(..)回调中得到LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

    @Override
    public void onResult(LocationSettingsResult locationSettingsResult) {
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
//                        startLocationUpdates();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                        "upgrade location settings ");

                try {
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(activity, REQUEST_CHECK_SETTINGS);

                } catch (IntentSender.SendIntentException e) {
                    Log.i(TAG, "PendingIntent unable to execute request.");
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                        "not created.");
                break;
        }
    }

我找到的唯一解决方案是重新安装应用或清除数据。

4 个答案:

答案 0 :(得分:27)

如果用户之前选择了“从不”选项,我没有找到任何解决方法来启用位置服务,因此我按照上面的@Lesvmac回答再次询问用户 删除并重新安装 应用,但我认为这不是解决此问题的正确方法。

所以目前最好的方法是不允许“Never”选项出现在请求对话框中。尝试添加

builder.setAlwaysShow(true);

LocationSettingsRequest.Builder 这将导致仅“”和“”选项而不是默认“从不”,“ “& “不现在”&你永远不会收到 SETTINGS_CHANGE_UNAVAILABLE

这是完整的方法:

 private void requestSettings() {
    LocationSettingsRequest.Builder builder =
            new LocationSettingsRequest.Builder()
                    .setAlwaysShow(true)
                    .addLocationRequest(request);
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                    builder.build());

    result.setResultCallback(this);
}

<强>之前
     image_before_set_always_show

<强>后

image_after_set_always_show

答案 1 :(得分:1)

对,绝不永远!如果用户之前已对应用程序的位置许可请求回答“不是现在”,则可以生成一个询问权限的对话框。但如果他们选择永远不会再让你问,那就是永久设置,你必须告诉他们删除并重新安装。 (如果他们的位置暂时关闭,但应用程序有权访问其位置,则可以生成一个对话框,要求更改设备的位置服务状态。) 如果location是应用程序的必要组件,您可以考虑在安装时要求访问。在即将推出的Android M中,可以为每个应用设置和重置个人权限,从而有助于避免此类问题。

答案 2 :(得分:0)

您需要手动允许该特定应用的权限才能重置请求权限对话框。

打开应用信息(通过从应用启动器拖动,或设置 - 应用 - [应用名称])

选择权限

通过“永不再问”

启用您拒绝的权限

(可选)从此处再次禁用它;请注意,到目前为止,您的应用会在需要时再次请求权限。

链接:https://android.stackexchange.com/questions/125939/reset-the-request-permission-dialog-on-marshmallow

答案 3 :(得分:0)

解决方案非常简单,很容易错过(至少在Android 5.1上)......

从顶部屏幕边缘向下滑动以获得下拉菜单。按“应用程序权限管理”。按应用程序和您要更改权限的应用程序。瞧!