For handling permissions in android M+, I want to write a single class, namely PermissionHandler class, to handle all the permission-related work so that I can easily use the same class in any project without making changes to the calling activity by calling only the constructor:
new PermissionHandler(CallingActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE, new PermissionHandler.PermissionGranted() {
@Override
public void onPermissionGranted() {
doWhatever();
}
});
My PermissionHandler is:
public class PermissionHandler implements ActivityCompat.OnRequestPermissionsResultCallback{
.
.
.
public PermissionHandler(AppCompatActivity callingActivity, String permission, PermissionGranted permissionGranted) {
this.permission = permission;
this.permissionGranted = permissionGranted;
this.callingActivity= callingActivity;
askForPermission();
}
private void askForPermission() {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
showAlertDialog();
} else {
ActivityCompat.requestPermissions(callingActivity,permissionsArray, PERMISSION_REQUEST);
}
} else {
permissionGranted.onPermissionGranted();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionGranted.onPermissionGranted();
} else {
onPermissionIsNotGranted();
}
break;
}
}
}
My problem here is that onRequestPermissionsResult
which is supposed to be invoked when ActivityCompat.requestPermissions(callingActivity,permissionsArray, PERMISSION_REQUEST)
is called is never invoked.
What I found out is that this is due to android calling callingActivity.onRequestPermissionsResult
which does not exist in the callingActivity and is passed to PermissionHandler.
I also considered using Reflection and Proxies to resolve this issue at runtime, but no success.
In case one or more permissions are not granted, ActivityCompat. requestPermissions() will request permissions and the control goes to onRequestPermissionsResult() callback method. You should check the value of shouldShowRequestPermissionRationale() flag in onRequestPermissionsResult() callback method.
Android categorizes permissions into different types, including install-time permissions, runtime permissions, and special permissions.
You need to declare the permissions in the manifest file first before you request for them programmatically. refer this for more information. declaring the permission in the manifest file is static type declaration by which your os know what kind of permission your app might require.
As far as i searched i found that its a bug in android. You can see the live issue here. onRequestPermissionsResult()
will not be called in any other class then the activity it is called by.
For more details refer this question : onRequestPermissionsResult not being called in dialog fragment. this user having the same problem as yours.
Creating a Common class for requesting permissions is a really good attempt but my friend, we need to find an alternative for handling
onRequestPermissionsResult()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With