Android开发权限请求的工具类封装

根据Blankj的utils工具 PermissionUtils简单封装的工具类:
有几个开源的Dialog可供选择:
//这是开源的Dialog,也可以使用自己的Dialog
selectorDialog:``空祖家的Dialog
这是CircleDialog
这是 Flyco的NormalDialog
简单封装

public class PermissionCheckUtil {
    private OnPermissionListener onPermissionListner;

    /**
     * 检查权限
     * @param activity 传的activity参数,为了创建对话框
     * @param permissionListener
     * @param permissions
     */
    public static void checkPermission(final Context activity,String title,String message,
                                       final OnPermissionListener permissionListener, String... permissions){
        if (PermissionUtils.isGranted(permissions)) {
            ToastUtils.showShort("检测权限是否已授权");
            permissionListener.onSuccess();
        }else {
            PermissionUtils.permission(permissions)
                    .callback(new PermissionUtils.FullCallback() {
                        @Override
                        public void onGranted(List permissionsGranted) {
                            permissionListener.onSuccess();
                            ToastUtils.showShort("允许了权限"+permissionsGranted.size());
                        }

                        @Override
                        public void onDenied(List permissionsDeniedForever, List permissionsDenied) {
                            if (permissionsDeniedForever.size()!=0) {
                                ToastUtils.showShort("拒绝了权限");
                                //这是开源的Dialog
                                //selectorDialog:``[空祖家的Dialog](https://github.com/kongzue/Dialog)
                                SelectDialog.show(activity, title,
                                        message,
                                        "设置",  new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        PermissionUtils.launchAppDetailsSettings();
                                    }
                                },
                                        "我拒绝",null );
//这是[CircleDialog](https://github.com/mylhyl/Android-CircleDialog)
//                                new CircleDialog.Builder()
//                                        .setTitle("提示")
//                                        .setText("您拒绝了这个权限,请打开手机设置界面去同意")
//                                        .setMaxHeight(0.5f)
//                                        .setWidth(0.6f)
//                                        .setPositive("设置", new View.OnClickListener() {
//                                            @Override
//                                            public void onClick(View v) {
//                                                PermissionUtils.launchAppDetailsSettings();
//                                            }
//                                        })
//                                        .setNegative("我拒绝",null)
//                                        .show(manager);
                            }
                        }
                    })
                    .rationale(new PermissionUtils.OnRationaleListener() {
                        @Override
                        public void rationale(final ShouldRequest shouldRequest) {
                       // [Flyco的Dialog](https://github.com/H07000223/FlycoDialog_Master)
                            NormalDialog normalDialog =new NormalDialog(activity);
                            normalDialog.title(title)
                                    .content(message)
                                    .btnText("取消", "确定")
                                    .show();
                            normalDialog.setOnBtnClickL(new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    normalDialog.dismiss();
                                }
                            }, new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    shouldRequest.again(true);
                                }
                            });
//                            SelectDialog.show(activity, "必要权限说明"
//                                    , "该应用需要这个权限才能正常使用某些功能",
//                                    new DialogInterface.OnClickListener() {
//                                        @Override
//                                        public void onClick(DialogInterface dialog, int which) {
//                                            shouldRequest.again(true);
//                                        }
//                                    });
//                            new CircleDialog.Builder()
//                                    .setTitle("必要权限说明")
//                                    .setText("需要这个权限")
//                                    .setWidth(0.6f)
//                                    .setPositive("确定", new View.OnClickListener() {
//                                        @Override
//                                        public void onClick(View v) {
//                                            shouldRequest.again(true);
//                                        }
//                                    })
//                                    .setCanceledOnTouchOutside(false)
//                                    .setNegative("取消",null)
//                                    .show(manager);
                        }
                    })
                    .request();
        }
    }

    /**
     *
     * @param manager 传 FragmentManager 为了创建DialogFragment对话框。
     * @param permissionListener
     * @param permissions
     */
    public static void checkPermission(final FragmentManager manager, final OnPermissionListener permissionListener, String... permissions){
        if (PermissionUtils.isGranted(permissions)) {
            ToastUtils.showShort("检测权限是否已授权");
            permissionListener.onSuccess();
        }else {
            PermissionUtils.permission(permissions)
                    .callback(new PermissionUtils.FullCallback() {
                        @Override
                        public void onGranted(List permissionsGranted) {
                            permissionListener.onSuccess();
                            ToastUtils.showShort("允许了权限"+permissionsGranted.size());
                        }

                        @Override
                        public void onDenied(List permissionsDeniedForever, List permissionsDenied) {
                            if (permissionsDeniedForever.size()!=0) {
                                ToastUtils.showShort("拒绝了");


                                new CircleDialog.Builder()
                                        .setTitle("提示")
                                        .setText("您拒绝了这个权限,请打开手机设置界面去同意")
                                      .setMaxHeight(0.5f)
                                        .setWidth(0.6f)
                                        .setPositive("设置", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                PermissionUtils.launchAppDetailsSettings();
                                            }
                                        })
                                        .setNegative("我拒绝",null)
                                        .show(manager)
                                ;
                            }
                        }
                    })
                    .rationale(new PermissionUtils.OnRationaleListener() {
                        @Override
                        public void rationale(final ShouldRequest shouldRequest) {
                            new CircleDialog.Builder()
                                    .setTitle("必要权限说明")
                                    .setText("需要这个权限")
                                    .setWidth(0.6f)
                                    .setPositive("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            shouldRequest.again(true);
                                        }
                                    })
                                    .setCanceledOnTouchOutside(false)
                                    .setNegative("取消",null)
                                    .show(manager);
                        }
                    })
                    .request();
        }
    }

//TODO  这个接口可以根据需要自定义一个失败的回调方法 onFailed
    public interface OnPermissionListener{
        void onSuccess();
        //  void onFailed();
    }
}

调用:

    PermissionCheckUtil.checkPermission(this,"提示","该应用需要这个权限", new PermissionCheckUtil.OnPermissionListener() {
                    @Override
                    public void onSuccess() {
                    //Todo 申请权限成功
                    }
                }, 
                //需要的权限
                Manifest.permission.SEND_SMS);

你可能感兴趣的:(Android)