Created
May 14, 2023 01:51
-
-
Save s0ubhik/eb96caee926b21d476c2997df72d7325 to your computer and use it in GitHub Desktop.
Request multiple permissions android java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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