Issue
In my Android app using Java, I define a checkForPermission()
method to check the grant status of a single permission. If the permission has not already been granted, it requests this permission from the user using ActivityCompat.requestPermissions()
.
Here is the method:
private boolean checkForPermission(String permission, int rationaleMessageId, int requestCode) {
// Check if permission is already granted.
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
// Permission has not been granted. Check if a rationale AlertDialog should be shown.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
// Show rationale AlertDialog explaining why this permission is needed.
new AlertDialog.Builder(this)
.setTitle(R.string.rationale_dialog_title)
.setMessage(rationaleMessageId)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Request permission again.
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
})
.create()
.show();
}
// Do not show rationale AlertDialog. Just request permission again.
else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
return false;
}
return true;
}
I've noticed that the method works perfectly fine for simple permissions such as android.permission.WRITE_EXTERNAL_STORAGE
, but does not work for more dangerous permissions such as android.permission.WRITE_SETTINGS
.
I'm wondering which types of permissions ActivityCompat.requestPermissions()
may actually request. My assumption is that dangerous permissions must be requested using other means, but I would like any feedback I can get on this. Thank you.
Solution
Specifically, requestPermissions()
is used for permissions with a protectionLevel
of dangerous
.
WRITE_EXTERNAL_STORAGE
is dangerous
. WRITE_SETTINGS
is not.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.