How to handle denied permissions Android M (EasyPermissions)
I’m using EasyPermissions to check if my android has granted certain permissions and request them if not. Cool library, works great, but I still haven’t figured out what to do if certain permissions are denied.
So basically you run code like this at creation time to check
if (EasyPermissions.hasPermissions(Splash.this, perms )) {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String IMEI = telephonyManager.getDeviceId();
String SimSimSerial = telephonyManager.getSimSerialNumber();
Toast.makeText(Splash.this, "IMEI: " + IMEI + " SimSerial: " + SimSimSerial, Toast.LENGTH_SHORT).show();
} else {
EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. " ,PERMS_REQUEST_CODE, perms );
}
Code breakdown: If there is a permission, go ahead, otherwise the request will do. My question is, what if someone clicks the Never Ask button during the request. The people at EasyPermissions have a feature
EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)
My dilemma is where to call this function because the Request Permission method returns nothing (invalid). I’ve tried something similar
if (EasyPermissions.hasPermissions(Splash.this, perms )) {...
} else if (EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)) {
} else {
EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. " ,PERMS_REQUEST_CODE, perms );
}
But it always runs at startup with denied permissions, not when the user actually clicks the Never button at runtime. Thank you for any help…
EasyPermissions link https://github.com/googlesamples/easypermissions
Solution
If you don’t want to use Easy Permissions, explain each permission case in full
/**
* Case 1: User doesn't have permission
* Case 2: User has permission
*
* Case 3: User has never seen the permission Dialog
* Case 4: User has denied permission once but he din't clicked on "Never Show again" check box
* Case 5: User denied the permission and also clicked on the "Never Show again" check box.
* Case 6: User has allowed the permission
*
*/
public void handlePermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
This is Case 1. Now we need to check further if permission was shown before or not
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
This is Case 4.
} else {
This is Case 3. Request for permission here
}
} else {
This is Case 2. You have permission now you can do anything related to it
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
This is Case 2 (Permission is now granted)
} else {
This is Case 1 again as Permission is not granted by user
Now further we check if used denied permanently or not
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
case 4 User has denied permission but not permanently
} else {
case 5. Permission denied permanently.
You can open Permission setting's page from here now.
}
}
}