I'm using EasyPermissions to check if certain permissions have been granted in my android and and requesting them if not. Cool library, works great but I've still not gotten to figuring out how to handle if some permissions where denied.
so basically you run a code like this on create 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 permissions exist, continue else request which is fine. My question is what if during request someone click on the never ask button. The guys at EasyPermissions have a function for that its
EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)
My dilemma is where to call this function as the request permissions method doesnt returns nothing (void). I tried something like
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 run the permissions denied one on start and not when a user actually click the never button in runtime. Any help is appreciated thanks..
link to EasyPermissions https://github.com/googlesamples/easypermissions
The method shouldShowRequestPermissionRationale() can be used to check whether the user selected the 'never asked again' option and denied the permission.
hasPermissions() to check if the permission is granted or not. If the permission is not granted then we request for permission by using EasyPermission. requestPermissions(). Run your app and try to apply various conditions as mentioned in the description image.
Complete explanation for every case of permission if you don't want to use Easy Permissions
/**
* 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.
}
}
}
Check this link.
Here you have to implement the EasyPermissions.PermissionCallbacks with this you will be provided to add methods which will be onRequestPermissionsResult, onPermissionsGranted, onPermissionsDenied. Then in onPermissionsDenied you can handle your Denial status. Try it and let me know if it worked for you.
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