如何在用户选择“从不”允许后提示用户提供位置权限?

时间:2016-02-28 07:08:42

标签: android user-interface google-api location

编辑 Margaret Bloom's link正是我想要的。

我正在编写一个应用程序,根据您当时的位置推荐食物,所以没有位置信息我的应用程序基本上没用。以下是我以编程方式访问位置数据的方法。

public void onConnected(Bundle bundle) {
    //adds the location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocReq);

    //make sure the phone is giving us what we asked for
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                    builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:  //we have what we want
                    Log.v("Location settings", "Location settings are satisfied.");
                    startLocationUpdates();
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.v("Location Settings", "Location settings not satisfied. Starting REQUEST_CHECK_SETTINGS");
                    //we need to ask user before phone will give location information
                    try {
                        status.startResolutionForResult(LaunchActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // This phone can't give us what we need.
                    Log.e("Location Settings", "The user \"never\" wants to give us location info");
                    try {
                        status.startResolutionForResult(LaunchActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                    }
                    break;
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS: //if we got the request check settings callback
            switch (resultCode) {
                case Activity.RESULT_OK:
                    Log.i("Location Settings", "onActivityForResult returned RESUME_OK");
                    startLocationUpdates();
                    break;
                case Activity.RESULT_CANCELED:
                    Log.i("Location Settings", "user did not want to give us location permissions");
                    break;
            }
            break;
    }
}

如果用户在对话框中选择“从不”,则设置 LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:大小写并且手机不再允许我显示位置请求对话框。重新启动应用程序并手动启用位置数据也不起作用,我的应用程序无法发送位置更新。

因此,从编程方面和UX方面,我可以提示用户再次提供位置权限的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

如果他/她已选择从不再次提问,则无法提示用户。您可以做的最好的事情是您可以在设置中将用户登陆到应用页面。他/她必须手动允许的地方。

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                      Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

实施例: Example of the answer

图片来源:Google图片