Android M - 检查运行时权限 - 如何确定用户是否选中了“不再询问"? [英] Android M - check runtime permission - how to determine if the user checked "Never ask again"?

查看:37
本文介绍了Android M - 检查运行时权限 - 如何确定用户是否选中了“不再询问"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个:).

第一个对话框现在看起来像这样:

没有不再显示"复选框(与开发者预览版 1 不同).如果用户拒绝该权限并且该权限对应用程序至关重要,则它可能会显示另一个对话框来解释应用程序请求该权限的原因,例如像这样:

如果用户再次拒绝,则应用应该在绝对需要该权限的情况下关闭,或者以有限的功能继续运行.如果用户重新考虑(并选择重试),则再次请求权限.这次的提示是这样的:

第二次显示不再询问"复选框.如果用户再次拒绝并且勾选了复选框,则不会再发生任何事情.是否勾选复选框可以通过使用 Activity.shouldShowRequestPermissionRationale(String) 来确定,例如像这样:

if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) {...

Android 文档就是这么说的(https://developer.android.com/training/permissions/requesting.html):

<块引用>

帮助找到您需要提供额外服务的情况说明,系统提供Activity.shouldShowRequestPermissionRationale(String) 方法.这个如果应用已请求此权限,则方法返回 true之前,用户拒绝了该请求.那说明你可能应该向用户解释为什么您需要权限.

如果用户过去拒绝了权限请求并选择了权限请求系统对话框中的不再询问选项,此方法返回 false.如果设备政策禁止应用程序拥有该权限.

要知道用户是否以不再询问"拒绝,您可以在用户未授予权限时再次检查您的 onRequestPermissionsResult 中的 shouldShowRequestPermissionRationale 方法.

@Overridepublic void onRequestPermissionsResult(int requestCode, String[] 权限, int[] grantResults) {如果(请求代码 == REQUEST_PERMISSION){//对于每个权限检查用户是否授予/拒绝它们//您可能希望在单个对话框中对基本原理进行分组,//这只是一个例子for (int i = 0, len = permissions.length; i < len; i++) {字符串权限 = 权限[i];如果(grantResults[i] == PackageManager.PERMISSION_DENIED){//用户拒绝权限boolean showRationale = shouldShowRequestPermissionRationale(权限);如果(!showRationale){//用户还检查了不再询问"//您可以启用一些回退,//禁用您的应用程序的功能//或打开另一个对话框解释//再次获得许可并指向//应用设置} else if (Manifest.permission.WRITE_CONTACTS.equals(permission)) {显示原理(权限,R.string.permission_denied_contacts);//用户没有勾选不再询问"//这是解释用户的好地方//为什么你需要权限并询问他是否想要//接受它(理由)} else if (/* 可能检查更多权限...*/) {}}}}}

您可以使用以下代码打开您的应用设置:

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);Uri uri = Uri.fromParts("package", getPackageName(), null);意图.setData(uri);startActivityForResult(意图,REQUEST_PERMISSION_SETTING);

无法将用户直接发送到授权页面.

According to this: http://developer.android.com/preview/features/runtime-permissions.html#coding an app can check for runtime permissions and request permissions if it hasn't been granted already. The following dialog will be displayed then:

In case the user declines an important permission, imo an app should display an explanation why the permission is needed and what impact declining has. That dialog has two options:

  1. re-try again (permission is requested again)
  2. deny (app will work without that permission).

If the user checks Never ask again however, the second dialog with the explanation shouldn't be shown, especially if the user already declined once before. Now the question is: how does my app know whether the user has checked the Never ask again? IMO the onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) doesn't give me that information.

A second question would be: does Google have plans to incorporate a custom message in the permission dialog that would explain why the app needs the permission? That way there would never be a second dialog which would certainly make for a better ux.

解决方案

Developer Preview 2 brings some changes to how permissions are requested by the app (see also http://developer.android.com/preview/support.html#preview2-notes).

The first dialog now looks like this:

There's no "Never show again" check-box (unlike developer preview 1). If the user denies the permission and if the permission is essential for the app it could present another dialog to explain the reason the app asks for that permission, e.g. like this:

If the user declines again the app should either shut down if it absolutely needs that permission or keep running with limited functionality. If the user reconsiders (and selects re-try), the permission is requested again. This time the prompt looks like this:

The second time the "Never ask again" check-box is shown. If the user denies again and the check-box is ticked nothing more should happen. Whether or not the check-box is ticked can be determined by using Activity.shouldShowRequestPermissionRationale(String), e.g. like this:

if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) {...

That's what the Android documentation says (https://developer.android.com/training/permissions/requesting.html):

To help find the situations where you need to provide extra explanation, the system provides the Activity.shouldShowRequestPermissionRationale(String) method. This method returns true if the app has requested this permission previously and the user denied the request. That indicates that you should probably explain to the user why you need the permission.

If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if the device policy prohibits the app from having that permission.

To know if the user denied with "never ask again" you can check again the shouldShowRequestPermissionRationale method in your onRequestPermissionsResult when the user did not grant the permission.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_PERMISSION) {
        // for each permission check if the user granted/denied them
        // you may want to group the rationale in a single dialog,
        // this is just an example
        for (int i = 0, len = permissions.length; i < len; i++) {
            String permission = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
            // user rejected the permission
                boolean showRationale = shouldShowRequestPermissionRationale( permission );
                if (! showRationale) {
                    // user also CHECKED "never ask again"
                    // you can either enable some fall back,
                    // disable features of your app
                    // or open another dialog explaining
                    // again the permission and directing to
                    // the app setting
                } else if (Manifest.permission.WRITE_CONTACTS.equals(permission)) {
                    showRationale(permission, R.string.permission_denied_contacts);
                    // user did NOT check "never ask again"
                    // this is a good place to explain the user
                    // why you need the permission and ask if he wants
                    // to accept it (the rationale)
                } else if ( /* possibly check more permissions...*/ ) {
                }
            }
        }
    }
}

You can open your app setting with this code:

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);

There is no way of sending the user directly to the Authorization page.

这篇关于Android M - 检查运行时权限 - 如何确定用户是否选中了“不再询问"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆