android 6.0 Permission Authorization Method
- 2021-09-24 23:49:02
- OfStack
Two methods are introduced here, as follows:
1. Successive
private static final int PERMISSION_READ_EXTERNAL_STORAGE = 101;
private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 102;
private static final int PERMISSION_CAMERA = 103;
private void requestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_READ_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_CAMERA);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int []grantResults) {
switch (requestCode) {
case PERMISSION_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, " I can't do anything without camera access !", Toast.LENGTH_LONG).show();
}
break;
}
case PERMISSION_READ_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, " Turn on store read and write permissions and make sure APP Normal operation ", Toast.LENGTH_LONG).show();;
}
break;
}
case PERMISSION_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, " Turn on store read and write permissions and make sure APP Normal operation ", Toast.LENGTH_LONG).show();;
}
break;
}
}
}
Step 2 All
private void requestPermission() {
List<String> permissionsNeeded = new ArrayList<String>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED) {
permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
permissionsNeeded.add(Manifest.permission.CAMERA);
}
if(permissionsNeeded.size()>0){
ActivityCompat.requestPermissions(this, permissionsNeeded.toArray(new String[permissionsNeeded.size()]), 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int []grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
if (grantResults.length > 0 && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
} else {
dialog();
}
} else {
dialog();
}
} else {
dialog();
}
break;
}
}
}