AndPermission权限库

我觉得最好用的权限询问框架,是我觉得
官方地址:https://github.com/yanzhenjie/AndPermission

配置

1.权限不管什么版本都得在AndroidManifest中配置,我也掉过这个坑。共勉
2.implementation 'com.yanzhenjie:permission:2.0.3'

使用

完整示例代码:

AndPermission.with(context)
                    .permission(PermissionAppUtils.Group.CAMERA_STORAGE)
                    .onGranted(new Action() {
                        @TargetApi(Build.VERSION_CODES.M)
                        @Override
                        public void onAction(List<String> permissions) {
                            //权限允许做的操作
                        }
                    })
                    .onDenied(new Action() {
                        @Override
                        public void onAction(@NonNull List<String> permissions) {
                            //权限禁止
                            if (PermissionAppUtils.hasAlwaysDeniedPermission(context, PermissionAppUtils.Group.CAMERA_STORAGE)) {
                                //是否永久拒绝了权限
                                
                            }
                        }
                    }).start();

永久禁止权限代码

/**
 * Show permissions rationale?
 */
public static final boolean isShowRationalePermission(Context context,String permission) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false;

    PackageManager packageManager = context.getPackageManager();
    Class<?> pkManagerClass = packageManager.getClass();
    try {
        Method method = pkManagerClass.getMethod("shouldShowRequestPermissionRationale", String.class);
        if (!method.isAccessible()) method.setAccessible(true);
        return (boolean) method.invoke(packageManager, permission);
    } catch (Exception ignored) {
        return false;
    }
}

/**
 * Has always been denied permission.
 */
public static boolean hasAlwaysDeniedPermission(Context context,
        @NonNull String... deniedPermissions) {
    for (String permission : deniedPermissions) {
        if (!isShowRationalePermission(context,permission)) {
            return true;
        }
    }
    return false;
}

觉得有用的点个赞。

推荐阅读更多精彩内容