Android 权限 - 永远记住“不再询问”选择

met*_*tis 5 permissions android

我的应用程序要求用户提供相机权限。如果用户拒绝该权限,则在重新启动时再次请求他的权限。这一次,向用户显示“不再询问”复选框。如果用户选择从不再次询问复选框,则应用程序永远不会请求许可。没关系。问题是我的手机永远不会忘记这个选择,它会记住这个选择。我删除了应用程序,清除了所有数据,但没有任何效果。

当我删除应用程序并重新安装它时,该应用程序仍然无法请求许可。我打开设置-> 应用程序设置->然后我手动为我的应用程序授予了必要的权限。没关系。然后,我删除并重新安装它。但是应用程序仍然不能请求许可。我不想在每次安装时手动授予我的应用程序权限。如何重置“不再询问”复选框的选择。

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;
}
Run Code Online (Sandbox Code Playgroud)

Kse*_*nia 8

你不能改变这种行为(来自马克墨菲的书):

如果用户检查并单击拒绝按钮,您现在不仅不会获得运行时权限,而且所有未来的请求都会立即调用 onRequestPermissionsResult() 表明您的权限请求被拒绝。用户现在可以授予您此权限的唯一方法是通过“设置”应用程序。

在这种情况下,您可以执行以下操作:

对于在拒绝时使您的应用程序处于完全无用状态的权限,您可能只会在应用程序启动时显示一个屏幕,上面写着“抱歉,此应用程序对您没用”,并提供用户卸载应用程序的选项或授予您所需的权限。请注意,shouldShowRequestPermissionRationale()如果用户拒绝许可并选中复选框以要​​求您停止纠缠用户,则返回 false。

因此,正如 Jaymin 正确指出的那样,您最好的选择是使用shouldShowRequestPermissionRationale().


met*_*tis 5

添加下面的 else 块解决了我的问题。

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();
         }
         else
         { 
             requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS_CAMERA);

         }
         return;
     }
 }
Run Code Online (Sandbox Code Playgroud)