天天看点

Android 权限申请工具类

Android 权限申请工具类

本篇文章用于记录在项目中用到的权限申请工具类。我们在项目中都遇到过权限申请问题,每一次的权限申请都是繁琐的,并且在权限的各个状态都需要我们去判断,让每个开发人员都无不烦躁。在这里我要十分感谢郭霖大神的博客PermissionX现在支持Java了!还有Android 11权限变更讲解这篇文章为我们封装了很好的权限申请功能。

集成:

使用:

PermissionX.init(fragmentActivity)
                .permissions(Manifest.permission.CAMERA)
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                    //这里是选择禁止后的提示
                        scope.showRequestReasonDialog(deniedList, "相机权限是必须依赖的权限", "去开启","关闭");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                    //这里是选择禁止且不再提示后的提示
                        scope.showForwardToSettingsDialog(deniedList, "程序需要您去应用程序设置当中手动开启相机权限", "去开启","关闭");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                           //这里是申请权限同意后的操作
                        }else {
                           //不同意时的操作
                        }

                    }
                });
           

上面是郭霖大神封装后的使用方法,很简便。在实际项目使用中,我发现光是这样还不够,我根据自己项目的业务逻辑在上面的基础上封装成了工具类,直接调用即可。

使用情景1、申请的权限不是必须权限,不管是否同意都能继续往下走

工具类实现:

/**
     * 此方法申请的权限不是必须的,就是不管是否同意权限都能继续往下走
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                    	//不管同不同意权限,都继续往下执行
                        if (afterPermission!=null){
                            afterPermission.doNext();
                        }
                    }
                });
    }
           

外部调用:

PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermission(this, new PermissionUtils.AfterPermission() {
            @Override
            public void doNext() {
                //此处调用继续往下执行的代码
            }
        },Manifest.permission.WRITE_EXTERNAL_STORAGE);
           
使用情景2、此方法申请的权限不是必须的,但在不同意权限的时候会走另外一条路

工具类中方法实现:

/**
     * 此方法申请的权限不是必须的,但在不同意权限的时候会走另外一条路
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,boolean other,AfterPermissionTwo afterPermissionTwo,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                        	//同意权限
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.doNext();
                            }
                        }else {
                        	//不同意权限
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.toOther();
                            }
                        }

                    }
                });
    }
           

实际使用:

PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermission(getActivity(),true,new PermissionUtils.AfterPermissionTwo() {
            @Override
            public void doNext() {
                //同意权限
            }
            @Override
            public void toOther() {
                //不同意权限
            }
        },Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
           
使用情景3、此方法申请的权限是必须的,必须同意权限都能继续往下走

工具类实现:

/**
     * 此方法申请的权限是必须的,必须同意权限都能继续往下走
     * @param fragmentActivity
     * @param msg1 申请的权限名称
     * @param msg2  哪个功能需要申请权限
     * @param afterPermission   监听权限设置接口
     * @param permissions   权限列表
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"权限是"+msg2+"必须依赖的权限", "去开启","关闭");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去应用程序设置当中手动开启"+msg1+"权限", "去开启","关闭");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                        	//同意权限后才能继续走
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }

                    }
                });
    }
           

实际调用:

PermissionUtils permissionUtils = new PermissionUtils();
                permissionUtils.setPermission(this, "存储", "保存图片", new PermissionUtils.AfterPermission() {
                    @Override
                    public void doNext() {
                        //同意权限后往下执行
                    }
                },Manifest.permission.WRITE_EXTERNAL_STORAGE);
           
使用情景4、此方法申请的权限是必须的,必须同意权限都能继续往下走,不同意会走另外一条路

工具类方法实现:

/**
     * 此方法申请的权限是必须的,必须同意权限都能继续往下走,不同意会走另外一条路
     * @param fragmentActivity
     * @param msg1 申请的权限名称
     * @param msg2  哪个功能需要申请权限
     * @param afterPermission   监听权限设置接口
     * @param permissions   权限列表
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermissionTwo afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"权限是"+msg2+"必须依赖的权限", "去开启","关闭");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去应用程序设置当中手动开启"+msg1+"权限", "去开启","关闭");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                        //同意权限
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }else {
                        //不同意权限
                            if (afterPermission!=null){
                                afterPermission.toOther();
                            }
                        }

                    }
                });
    }
           

实际使用:

PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermission(this,"定位","选择位置",new PermissionUtils.AfterPermissionTwo() {
            @Override
            public void doNext() {
                //同意权限继续走
            }
            @Override
            public void toOther() {
                //不同意走另一条路
            }
        },Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
           

最后给大家复制一下总的工具类:

/**
 * created by DELL
 * on 2020/7/29
 * 权限申请工具类
 */
public class PermissionUtils {
    /**
     * 此方法申请的权限不是必须的,就是不管是否同意权限都能继续往下走
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (afterPermission!=null){
                            afterPermission.doNext();
                        }
                    }
                });
    }
    /**
     * 此方法申请的权限不是必须的,但在不同意权限的时候会走另外一条路
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,boolean other,AfterPermissionTwo afterPermissionTwo,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.doNext();
                            }
                        }else {
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.toOther();
                            }
                        }

                    }
                });
    }

    /**
     * 此方法申请的权限是必须的,必须同意权限都能继续往下走
     * @param fragmentActivity
     * @param msg1 申请的权限名称
     * @param msg2  哪个功能需要申请权限
     * @param afterPermission   监听权限设置接口
     * @param permissions   权限列表
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"权限是"+msg2+"必须依赖的权限", "去开启","关闭");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去应用程序设置当中手动开启"+msg1+"权限", "去开启","关闭");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }else {

                        }

                    }
                });
    }
    /**
     * 此方法申请的权限是必须的,必须同意权限都能继续往下走,不同意会走另外一条路
     * @param fragmentActivity
     * @param msg1 申请的权限名称
     * @param msg2  哪个功能需要申请权限
     * @param afterPermission   监听权限设置接口
     * @param permissions   权限列表
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermissionTwo afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"权限是"+msg2+"必须依赖的权限", "去开启","关闭");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去应用程序设置当中手动开启"+msg1+"权限", "去开启","关闭");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }else {
                            if (afterPermission!=null){
                                afterPermission.toOther();
                            }
                        }

                    }
                });
    }

    /**
     * 定义申请权后下一步操作接口
     */
    public interface AfterPermission{
        void doNext();
    }
    public interface AfterPermissionTwo{
        void doNext();  //权限同意
        void toOther();  //权限不同意
    }
           

本工具类已经运用到项目中,本篇文章用作记录,也欢迎大家纠错。