Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Handle Denied Permissions Android M (EasyPermissions)

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

like image 793
Mueyiwa Moses Ikomi Avatar asked Oct 20 '16 18:10

Mueyiwa Moses Ikomi


People also ask

How do I check if permission is permanently denied android?

The method shouldShowRequestPermissionRationale() can be used to check whether the user selected the 'never asked again' option and denied the permission.

How do you use Easypermissions on Android?

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.


2 Answers

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.
        }

    }
}
like image 170
saksham Avatar answered Sep 22 '22 19:09

saksham


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.

like image 36
Rakshit Nawani Avatar answered Sep 21 '22 19:09

Rakshit Nawani