代码之家  ›  专栏  ›  技术社区  ›  NullPointerException

如果用户选中“永不再次询问”,如何避免requestPermission()?

  •  1
  • NullPointerException  · 技术社区  · 8 年前

    我正在处理Android中的随需应变权限,我发现了一个令人困惑的情况。我的目标是,如果用户在权限中选择了“不再询问”,则显示一个带有“转到应用程序设置”按钮的对话框。

    onRequestPermissionsResult

    public boolean checkIfShouldShowGoToSettingsDialog(String permissions[], int[] grantResults){
        for (int i=0; i<permissions.length; i++){
            if (grantResults[i] == PackageManager.PERMISSION_DENIED && !ActivityCompat.shouldShowRequestPermissionRationale(SectionManager.getInstance().getCurrentActivity(), permissions[i])) {
                return true;
            }
        }
        return false;
    }
    

    问题是如果你这样做,每次你打电话 requestPermission() 然后 即使之前拒绝了权限,甚至选中了复选框,仍将再次调用。所以 checkIfShouldShowGoToSettingsDialog 他回来了 true 因为许可是无效的 DENIED 并且应该显示RequestPermission正在返回 false . 我不想那样。我只想在用户第一次选择“不再询问”时显示“转到应用程序设置”对话框。

    我在调用之前添加了此验证 请求权限() . 我尝试删除将返回false的权限 shouldShowRequestPermissionRationale() 因为不希望这些权限选中复选框:

    public String[] removePermanentlyDeniedPermissionsFromArray(String[] permissions){
        List<String> result = new ArrayList<>();
        for (String permission : permissions) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(SectionManager.getInstance().getCurrentActivity(), permission)) {
                result.add(permission);
            }
        }
        return result.toArray(new String[result.size()]);
    }
    

    那么,如果用户选中了“永不再次询问”,如何避免调用RequestPermission()?

    2 回复  |  直到 8 年前
        1
  •  0
  •   mxbhxx    8 年前

    我很高兴回答你们的问题。在我过去的项目中,我也遇到了类似的问题。以下是我的解决方案。 (1) 为了避免重复提示,我们必须在用户首次同意时保存一个成功的令牌。等待下一个操作,首先确定用户是否同意,如果同意,则不再提示。否则,再次提示用户。 https://github.com/yangfuhai/ASimpleCache

        2
  •  0
  •   Mohammed Atif    7 年前

    所以这里是错误的使用 shouldShowPermissionRationale . 如果先前拒绝了权限(未单击复选框),则为真。第一次,它总是错误的。检查文件

    /**
         * Gets whether you should show UI with rationale for requesting a permission.
         * You should do this only if you do not have the permission and the context in
         * which the permission is requested does not clearly communicate to the user
         * what would be the benefit from granting this permission.
         * <p>
         * For example, if you write a camera app, requesting the camera permission
         * would be expected by the user and no rationale for why it is requested is
         * needed. If however, the app needs location for tagging photos then a non-tech
         * savvy user may wonder how location is related to taking photos. In this case
         * you may choose to show UI with rationale of requesting this permission.
         * </p>
         *
         * @param activity The target activity.
         * @param permission A permission your app wants to request.
         * @return Whether you can show permission rationale UI.
         *
         * @see #checkSelfPermission(android.content.Context, String)
         * @see #requestPermissions(android.app.Activity, String[], int)
         */
    

    请求运行时权限的过程

    1.生成未授予的权限列表。

    List<String> permissionList = new ArrayList<>()
    if(somePermission == PackageManager.PERMISSION_DENIED){
       permissionList.add(somePermission);
    }
    //so on
    String[] permissionArray = permissionList.toArray(new String[permissionArray.size()]);
    

    2.如果同时请求多个权限,原理可能会变得复杂, 所以干脆忽略它 .

    最佳做法:

    始终对应用程序中的强制和可选权限进行分组,并忽略对可选权限的拒绝。