Skip to content

Instantly share code, notes, and snippets.

@s0ubhik
Created May 14, 2023 01:51
Show Gist options
  • Save s0ubhik/eb96caee926b21d476c2997df72d7325 to your computer and use it in GitHub Desktop.
Save s0ubhik/eb96caee926b21d476c2997df72d7325 to your computer and use it in GitHub Desktop.
Request multiple permissions android java
String[] perms = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public void check_perms(){
for (String perm: perms) {
if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
// request permission
if (ActivityCompat.shouldShowRequestPermissionRationale(this, perm)){
new AlertDialog.Builder(this)
.setTitle("Permission Needed")
.setMessage("This app needs a certain permission to work properly")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(Base.this, perms, 1);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create()
.show();
return;
} else {
ActivityCompat.requestPermissions(this, perms, 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment