开发者预览2对应用程序请求权限的方式带来了一些变化(参见
http://developer.android.com/preview/support.html#preview2-notes)。
第一个对话框现在看起来像这样:
没有“不再显示”复选框(与开发者预览1不同)。如果用户拒绝了权限,并且如果权限对于应用程序是必要的,则它可以呈现另一个对话框来解释应用程序请求该权限的原因。喜欢这个:
如果用户再次拒绝应用程序应该关闭,如果它绝对需要该权限或继续运行有限的功能。如果用户重新考虑(并选择重试),则再次请求许可。这一次的提示如下:
第二次显示“不再询问”复选框。如果用户再次拒绝并且复选框被勾选,则不应再发生。
可以通过使用Activity.shouldShowRequestPermissionRationale(String)来确定是否勾选复选框。喜欢这个:
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) {...
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);
没有办法将用户直接发送到授权页面。