如何处理被拒绝的权限Android M(EasyPermissions)

时间:2016-10-20 18:08:21

标签: java android permissions android-permissions

我正在使用EasyPermissions来检查我的android中是否已授予某些权限,如果没有请求它们。很酷的库,效果很好,但我仍然没有弄清楚如果某些权限被拒绝将如何处理。

所以基本上你在create上运行这样的代码来检查

cond.get("icon_url").getAsString();

代码细分:如果存在权限,请继续其他请求,这很好。我的问题是如果在请求期间有人点击永不询问按钮。 EasyPermissions的工作人员有其功能

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 );
    }

我的困境是在哪里调用此函数,因为请求权限方法不会返回任何内容(void)。我试过像

这样的东西
EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)

但它始终在启动时运行被拒绝的权限,而不是在用户实际单击运行时的“从”按钮时运行。感谢任何帮助..

链接到EasyPermissions https://github.com/googlesamples/easypermissions

2 个答案:

答案 0 :(得分:3)

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.

答案 1 :(得分:3)

如果您不想使用简易权限,请填写每个许可案例的完整说明

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

    }
}