我试图在Android 7.0上显示多个权限请求
此代码在Android sdk 23上没有问题。
SDK 24显示一次请求,如果我触摸允许或拒绝按钮,此代码工作一次并且不显示其他请求。
代码在这里;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, 1);
}
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
答案 0 :(得分:1)
试试这个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION};
boolean flag = false;
for (int i = 0; i < permissions.length; i++) {
if (checkSelfPermission(permissions[i]) == PackageManager.PERMISSION_DENIED) {
flag = true;
break;
}
}
if (flag) {
requestPermissions(permissions, 1);
}
}