Android rxpermissions二次封装,打造高大上的权限请求

 

一、RxPermissions使用。

 rxpermissions主要是创建了一个空的fragment,跟activity绑定生命周期,然后触发申请权限成功失败的回调给我们。

RxPermissions rxPermissions = new RxPermissions(activity);
rxPermissions
                .requestEach(permissions)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(permission1 -> {
                    
                });

二、RxPermissions二次封装。

兄弟们重头戏来了,rxpermissions的二次封装,使用简单便捷,而且分为三种情况回调供自己处理(用户同意权限、拒绝权限、用户拒绝并且勾选不再提醒)。

直接上代码不多BB。

package com.pxwx.common.rxpermission;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.FragmentActivity;

import com.pxwx.dialog.common.CustomDialog;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

import common.utiles_lib.AppUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 描述: 权限管理类
 * Created By liweidong on 2020/7/18
 */
public class RxPermissionManager {

    //存储不再询问权限
    private static ArrayList noAskPermission = new ArrayList<>();
    //权限申请次数
    private static int requestCount;
    //拒绝权限的次数
    private static int refuseCount;
    //去往设置弹窗
    private static CustomDialog customDialog;

    /**
     * 申请多个权限,带拒绝权限并选择不再询问处理
     * @param activity
     * @param permissions
     */
    public static void requestPermissions(FragmentActivity activity, RxPermissionListener listener, String... permissions){
        //每次清空一下
        noAskPermission.clear();
        requestCount = 0;
        refuseCount = 0;
        int length = permissions.length;
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions
                .requestEach(permissions)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(permission1 -> {
                    if (permission1.granted){
                        requestCount++;
                        //用户同意
                        handlePermission(activity, length, listener);
                    }else if (permission1.shouldShowRequestPermissionRationale){
                        requestCount++;
                        refuseCount++;
                        //用户拒绝
                        handlePermission(activity, length, listener);
                    }else{
                        requestCount++;
                        //用户拒绝了权限,并选中了不再询问
                        noAskPermission.add(permission1);
                        handlePermission(activity, length, listener);
                    }
                });
    }

    /**
     * 处理多组权限问题
     * @param activity
     * @param length
     * @param listener
     */
    private static void handlePermission(FragmentActivity activity, int length, RxPermissionListener listener){
        //当权限个数等于申请次数时,证明所有权限都已申请完毕
        if (length == requestCount){
            if (refuseCount == 0 && noAskPermission.size() == 0){
                //权限全部申请通过
                listener.accept();
            }else if (noAskPermission.size() == 0){
                //拒绝权限
                listener.refuse();
            }else{
                //拒绝权限,并勾选了不再提醒,弹窗提示去设置页
                listener.noAsk();
                String permissionName = getPermissionName();
                CustomDialog.Builder builder = new CustomDialog.Builder(activity).setMessage("请前往设置->应用->朴新助教->权限中打开" + permissionName + "权限,否则功能无法正常运行!")
                        .setLeftBtnTitle("取消")
                        .setLeftButtonListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //取消
                                customDialog.dismiss();
                            }
                        })
                        .setRightBtnTitle("确定")
                        .setRightBtnBlue()
                        .setRightButtonListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", AppUtil.getLocalPackageInfo(activity).packageName, null);
                                intent.setData(uri);
                                activity.startActivity(intent);
                                customDialog.dismiss();
                            }
                        });
                customDialog = builder.createTwoButtonAndNoTitleDialog();
                customDialog.show();
            }
        }
    }

    /**
     * 获取权限的名字
     * @return
     */
    private static String getPermissionName(){
        if (noAskPermission != null && noAskPermission.size() != 0){
            StringBuilder sb = new StringBuilder();
            for (Permission permission : noAskPermission){
                Log.e("lwd",permission.name);
                switch (permission.name){
                    case  Manifest.permission.CAMERA://相机权限
                        sb.append("相机");
                        sb.append("、");
                        break;
                    case Manifest.permission.WRITE_EXTERNAL_STORAGE://读写
                        sb.append("存储");
                        sb.append("、");
                        break;
                    case Manifest.permission.RECORD_AUDIO://录音
                        sb.append("麦克风");
                        sb.append("、");
                        break;
                    case Manifest.permission.REQUEST_INSTALL_PACKAGES://安装应用权限
                        sb.append("应用内安装其他应用");
                        sb.append("、");
                        break;
                    case Manifest.permission.CALL_PHONE://打电话
                        sb.append("电话");
                        sb.append("、");
                        break;
                }
            }
            return TextUtils.isEmpty(sb.toString()) ? "相关" : sb.toString().substring(0, sb.toString().length() - 1);
        }
        return "相关";
    }

}

上面是工具类,对外暴露申请权限。

 

package com.pxwx.common.rxpermission;

/**
 * 描述: 权限申请回调
 * Created By liweidong on 2020/7/18
 */
public abstract class RxPermissionListener {

    public abstract void accept();

    public abstract void refuse();

    public void noAsk(){

    }
}

上面是回调类。

 

dialog的话就不提供了,大家脑补,自己自定义一个即可。

 

如果帮到你了,请给作者一个关注,谢谢老铁!!!

你可能感兴趣的:(android,android,java)