【发布时间】:2018-06-26 08:11:41
【问题描述】:
我的应用要求用户授予相机权限。如果用户拒绝该权限,则在重新启动时再次请求他的权限。这一次,向用户显示“不再询问”复选框。如果用户选择不再询问复选框,则应用程序将永远无法请求许可。没关系。问题是我的手机永远不会忘记这个选择,它会记住这个选择。我删除了应用程序,清除了所有数据,但没有任何效果。
当我删除应用程序并重新安装它时,应用程序仍然无法请求许可。 我打开了 Settings-> Application Settings -> 然后我手动给了我的应用程序必要的权限。没关系。然后,我删除并重新安装它。但是应用程序仍然不能请求许可。我不想在每次安装时手动授予我的应用权限。如何重置“不再询问”复选框的选择。
if (Build.VERSION.SDK_INT >= 23)
{
int hasPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
if (hasPermission != PackageManager.PERMISSION_GRANTED)
{
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
{
getErrorDialog("You need to allow Camera permission." +
"\nIf you disable this permission, You will not able to add attachment.", MainActivity.this, true).show();
}
return;
}
}
public AlertDialog.Builder getErrorDialog(String message, Context context, final boolean isFromCamera) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(getString(R.string.app_name)).setMessage(message);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (Build.VERSION.SDK_INT >= 23) {
if(isFromCamera){
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
}else {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
}
}
}
});
return alertDialog;
}
【问题讨论】:
-
使用我的库:github.com/nabinbhandari/Android-Permissions 它还处理“不再询问”的情况。
-
您必须使用 shouldShowRequestPermissionRationale() 处理权限
-
使用我的库:(为了用户而删除了链接) @NabinBhandari 太糟糕了......像
static PermissionHandler permissionHandler;这样的代码permissionHandler保留了我们显然拥有的活动参考活动泄漏... 这就是糟糕的应用程序的制作方式 ...您还应该测试它是否与“不保留活动”一起使用...我敢打赌它不会 -
“然后,我删除并重新安装它”——在一些 Android 6.0 开发者预览版中存在与此相关的错误,但我记得它已修复。您在什么设备上进行测试,它使用的是什么版本的 Android?
-
@CommonsWare ,我在 else 语句中添加了 requestPermissions() 方法,它可以按我的意愿工作。当我删除并重新安装该应用程序时,该应用程序现在要求请求。
标签: android permissions