美文网首页Android 进阶技术篇专题我爱编程
Android 简洁易用andrioid6.0+权限请求组件

Android 简洁易用andrioid6.0+权限请求组件

作者: SwitchLife | 来源:发表于2018-04-19 09:25 被阅读4次

开篇

  android 6.0+要求我们动态请求权限。网上针对于请求权限的封装很多,你是否用着还爽呢?不管如何,你且尝试下我给你写的组件——CustomPermissionChecker。(不喜勿喷,谢谢哈!)

屏幕截图

立即体验

扫描以下二维码下载体验App(从0.2.3版本开始,体验App内嵌版本更新检测功能):


JSCKit库传送门:https://github.com/JustinRoom/JSCKit

源码简析

1、CustomPermissionChecker.java
就两个主要的方法:

  • public boolean checkPermissions(Activity activity, @IntRange(from = 0) int requestCode, OnCheckListener onCheckListener, String... permissions)
    用途:请求权限。(请参阅方法里的注释)
    public boolean checkPermissions(Activity activity, @IntRange(from = 0) int requestCode, OnCheckListener onCheckListener, String... permissions) {
        if (permissions == null || permissions.length == 0)
            throw new NullPointerException("The parameter permissions can't be null.");

        this.activity = activity;
        this.onCheckListener = onCheckListener;
        this.permissions = Arrays.asList(permissions);
        boolean isAllGranted = true;
        List<String> unGrantedPermissions = new ArrayList<>();
        for (String permission : permissions) {
            boolean isGranted = ActivityCompat.checkSelfPermission(activity.getApplicationContext(), permission) == PackageManager.PERMISSION_GRANTED;
            isAllGranted = isAllGranted && isGranted;
            if (!isGranted)
                unGrantedPermissions.add(permission);
        }

        //所请求的permissions都已授权通过
        if (isAllGranted) {
            if (onCheckListener != null)
                onCheckListener.onResult(
                        requestCode,
                        true,
                        this.permissions,
                        null,
                        null
                );
            return true;
        }

        //请求授权
        ActivityCompat.requestPermissions(activity, unGrantedPermissions.toArray(new String[unGrantedPermissions.size()]), 0);
        return false;
    }
  • public void onPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    用途:处理权限请求结果。(请参阅方法里的注释)
    public void onPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (onCheckListener == null)
            return;

        List<String> deniedPermissions = new ArrayList<>();
        List<String> shouldShowPermissions = new ArrayList<>();
        for (int i = 0; i < grantResults.length; i++) {
            String permission = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_DENIED){
                deniedPermissions.add(permissions[i]);
                boolean shouldShow = ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
                if (!shouldShow)
                    shouldShowPermissions.add(permission);
            }
        }

        //移除掉已拒绝的之后就是授权通过的
        this.permissions.removeAll(deniedPermissions);
        onCheckListener.onResult(
                requestCode,
                deniedPermissions.size() == 0,
                this.permissions,
                deniedPermissions,
                shouldShowPermissions
        );
        //处理收尾工作
        onCheckListener.onFinally(requestCode);
    }

2、CustomPermissionChecker定义好了,接下来我们把它封装到框架里面去,复写一个abstractactivity,其他的Activity继承它:

public abstract class APermissionCheckActivity extends AppCompatActivity {
     private CustomPermissionChecker customPermissionChecker = null;

    public final boolean checkPermissions(String... permissions) {
        return checkPermissions(0, null, permissions);
    }

    public final boolean checkPermissions(@IntRange(from = 0) int requestCode, CustomPermissionChecker.OnCheckListener checkListener, String... permissions) {
        if (customPermissionChecker == null) {
            customPermissionChecker = new CustomPermissionChecker();
        }
        return customPermissionChecker.checkPermissions(this, requestCode, checkListener, permissions);
    }

    /**
     * Release resource. I suggest that you should call this method in {@link CustomPermissionChecker.OnCheckListener#onFinally(int)}.
     */
    public final void recyclePermissionChecker() {
        if (customPermissionChecker == null)
            return;
        customPermissionChecker = null;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        customPermissionChecker.onPermissionsResult(requestCode, permissions, grantResults);
    }
}

公共方法

用法

不是我吹,用起来真是简单:

    public void widgetClick(View v) {
        //请求相机权限、SD卡权限
        //注意:requestCode >= 0
        checkPermissions(0x100, new CustomPermissionChecker.OnCheckListener() {
            @Override
            public void onResult(int requestCode, boolean isAllGranted, @NonNull List<String> grantedPermissions, @Nullable List<String> deniedPermissions, @Nullable List<String> shouldShowPermissions) {
                if (isAllGranted){
                    toScannerActivity();
                    return;
                }

                if (shouldShowPermissions != null && shouldShowPermissions.size() > 0){
                    String message = "当前应用需要以下权限:\n\n" + getAllPermissionDes(shouldShowPermissions);
                    showPermissionRationaleDialog("温馨提示", message, "设置", "知道了");
                }
            }

            @Override
            public void onFinally(int requestCode) {
                removePermissionChecker();
            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
    }

篇尾

  就是这么简单!如果您觉得还不错的话,给个star(或加个关注)啰!我是JustinEoyQQ:1006368252

所有伟大的事迹和伟大的思想都有荒谬的开头。 —— 加缪

相关文章

  • Android 简洁易用andrioid6.0+权限请求组件

    开篇   android 6.0+要求我们动态请求权限。网上针对于请求权限的封装很多,你是否用着还爽呢?不管如何,...

  • 开源库推荐文章

    FcPermissions:也许是目前最好的动态权限请求库 一个简洁方便的处理 Android M 上动态权限的库...

  • Permission组件解析

    Android 6.0之后,对于一些敏感的权限,需要我们手动请求,本文介绍一个权限组件Permisssion,主要...

  • Android Q 发现

    android Q : 权限请求必须:android:requestLegacyExternalStorage="...

  • 权限请求框架

    getActivity/XXPermissions 简洁易用,采用链式调用的方式,使用只需一句代码支持单个权限、多...

  • Android权限请求

    权限请求 Android6.0 权限的动态请求 工作原理 usage 步骤一:检测系统版本private void...

  • Android授权app源码函数微解读二

    参考:Android (6.0) M 请求权限 (5)简单说下请求权限的流程App-->Activity.requ...

  • Android 权限说明大全

    Android程序往往都需要在androidmanifest.xml文件中来声明许多的相关权限请求, 而权限请求也...

  • android 8.0安装apk

    android 8.0安装apk需要请求未知来源权限

  • 网络框架 - 收藏集 - 掘金

    浅谈 Retrofit 封装 - 让框架更加简洁易用 - Android - 掘金 尊重他人的劳动成果,转载请标明...

本文标题:Android 简洁易用andrioid6.0+权限请求组件

本文链接:https://www.haomeiwen.com/subject/xbbbkftx.html