我想知道如何在单个请求中添加多个权限。这是关于Android的Marshmallow版本。
答案 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);