如何在权限请求中添加多个权限

时间:2016-11-07 10:31:30

标签: android android-6.0-marshmallow android-permissions

我想知道如何在单个请求中添加多个权限。这是关于Android的Marshmallow版本。

1 个答案:

答案 0 :(得分:1)

您需要创建一个需要权限的arraylist

 List<String> permissionsNeeded = new ArrayList<String>();
    final List<String> permissionsList = new ArrayList<String>();
    if (!addPermission(permissionsList, Manifest.permission.READ_PHONE_STATE))
        permissionsNeeded.add("android.permission.READ_PHONE_STATE");
    if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
        permissionsNeeded.add("android.permission.WRITE_EXTERNAL_STORAGE");

// addPermission

private boolean addPermission(List<String> permissionsList, String permission) {
        if (ContextCompat.checkSelfPermission(LoginActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (!ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this, permission))
                return false;
        }
        return true;
    }

同样在数组列表中添加所有权限。

// now request permission
            ActivityCompat.requestPermissions(context,
                    permissionsList.toArray(new String[permissionsList.size()]),
                    PERMISSIONS_REQUEST_CODE);