问题
im trying to show multiple permission request on Android 7.0
This code working with no problem on Android sdk 23.
SDK 24 showing one time the request, if i touch allow or denied button, this code working one time and Don't show other request.
Code is here;
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);
}
}
回答1:
try this
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);
}
}
回答2:
First of all check if permission is already granted and if not granted then ask for permission using below code.
String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_CALL_LOG, Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
Once permissions are granted, you can execute below code to check whether a particular permission was granted or denied
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_ALL: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[2] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[3] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[4] == PackageManager.PERMISSION_GRANTED) {
} else {
}
} else {
//All permission denied
}
runAfterPermissionGranted();
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
来源:https://stackoverflow.com/questions/43803825/android-7-0-dont-show-multiple-permission-request