Android 动态权限申请封装

/**
 * 动态权限申请(单个)
 * @param activity
 * @param requestCodeOut                       申请码
 * @param permissionPrepare2Request            申请的权限
 * @return
 */
public boolean requestPermission(Activity activity, final int requestCodeOut, String permissionPrepare2Request){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        int permissionCheck = ContextCompat.checkSelfPermission(activity, permissionPrepare2Request);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            registTempOnPermissionRequestCallBack(new OnPermissionRequestCallBack() {
                @Override
                public void onGrantedSuccess(int requestCode) {
                    if(requestCodeOut == requestCode){
                        if(onPermissionRequestCallBack != null){
                            onPermissionRequestCallBack.onGrantedSuccess(requestCode);
                        }
                    }
                }

                @Override
                public void onGrantedFailure(int requestCode) {
                    if(requestCodeOut == requestCode){
                        if(onPermissionRequestCallBack != null){
                            onPermissionRequestCallBack.onGrantedFailure(requestCode);
                        }
                    }
                }
            });

            String[] deniedPermissions = new String[]{permissionPrepare2Request};
            ActivityCompat.requestPermissions(activity, deniedPermissions, requestCodeOut);
            return false;
        }
    }else{
        return true;
    }
}

/**
 * 动态权限申请(多个)
 * @param activity
 * @param requestCodeOut                         申请码
 * @param permissionPrepare2RequestList          申请权限(一个或者多个)
 * @return
 */
//
public boolean requestPermission(Activity activity, final int requestCodeOut, String[] permissionPrepare2RequestList){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        List permissionDeniedList = new ArrayList<>();
        for (String permission : permissionPrepare2RequestList) {
            int permissionCheck = ContextCompat.checkSelfPermission(activity, permission);
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {

            } else {
                permissionDeniedList.add(permission);
            }
        }

        int requestBaseCode = 1;
        requestCodeTotal1 = 0;
        requestCodeTotal2 = 0;

        if (!permissionDeniedList.isEmpty()) {
            registTempOnPermissionRequestCallBack(new OnPermissionRequestCallBack() {
                @Override
                public void onGrantedSuccess(int requestCode) {
                    requestCodeTotal2 += requestCode;
                    //说明多个动态权限申请全部成功了
                    if(requestCodeTotal1 == requestCodeTotal2){
                        if(onPermissionRequestCallBack != null){
                            onPermissionRequestCallBack.onGrantedSuccess(requestCodeOut);
                        }
                    }
                }

                @Override
                public void onGrantedFailure(int requestCode) {
                    if(requestCodeOut == requestCode){
                        if(onPermissionRequestCallBack != null){
                            onPermissionRequestCallBack.onGrantedFailure(requestCode);
                        }
                    }
                }
            });

            //将权限
            for (int i = 0; i < permissionDeniedList.size(); i++) {
                requestBaseCode = requestCodeOut + (i+1);
                requestCodeTotal1 += requestBaseCode;
                ActivityCompat.requestPermissions(activity, new String[]{permissionDeniedList.get(i)}, requestBaseCode);
            }
            return false;
        }else{
            return true;
        }
    }else{
        return true;
    }
}

 

代码的封装  https://download.csdn.net/download/u013441613/12058143

你可能感兴趣的:(Android 动态权限申请封装)