gpt4 book ai didi

Android M - 检查运行时权限 - 如何确定用户是否检查了 "Never ask again"?

转载 作者:IT老高 更新时间:2023-10-28 12:50:36 26 4
gpt4 key购买 nike

据此:http://developer.android.com/preview/features/runtime-permissions.html#coding应用程序可以检查运行时权限并请求权限(如果尚未授予)。然后将显示以下对话框:

enter image description here

如果用户拒绝了重要权限,imo 应用程序应显示为什么需要该权限以及拒绝有什么影响的解释。该对话框有两个选项:

  1. 重试(再次请求权限)
  2. 拒绝(应用将在没有该权限的情况下运行)。

如果用户选中Never ask again,则不应显示第二个带有解释的对话框,特别是如果用户之前已经拒绝过一次。现在的问题是:我的应用如何知道用户是否勾选了Never ask again? IMO onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 没有给我这些信息。

第二个问题是:Google 是否有计划在权限对话框中加入自定义消息来解释应用为什么需要权限?这样一来,就永远不会有第二个对话,这肯定会带来更好的用户体验。

最佳答案

Developer Preview 2 对应用程序请求权限的方式进行了一些更改(另请参阅 http://developer.android.com/preview/support.html#preview2-notes)。

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

enter image description here

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

enter image description here

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

enter image description here

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

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

Android 文档是这么说的 (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.

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

@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...*/ ) {
}
}
}
}
}

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

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

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

关于Android M - 检查运行时权限 - 如何确定用户是否检查了 "Never ask again"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30719047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com