美文网首页Android 基础知识
Android 请求权限链式工具类

Android 请求权限链式工具类

作者: 圣光啊那个敌人值得一战 | 来源:发表于2018-07-19 18:07 被阅读184次

    老是用RxPermission请求权限,总觉得麻烦,所以索性花了半小时自己写了一个工具类
    不支持Permission_group请求
    在Rxjava上又封了一层强行链式结构enmmmm

    使用:

    PermissionUtils.requestPermission(BaseInfoActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, Config.PERMISSIONS_REQUEST_PHOTO)
                        .subscribe(new PermissionUtils.RequestPermissionObserver() {
    
                            @Override
                            public void shouldShowRequestPermissionRationale(final List<String> shouldAskPermissions) {
                                new AlertDialog.Builder(BaseInfoActivity.this).setTitle("不给相册相关的权限真的无法录头像哦~").setPositiveButton("还是给你吧", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        PermissionUtils.requestPersimsion(BaseInfoActivity.this, shouldAskPermissions.toArray(new String[]{}), Config.PERMISSIONS_REQUEST_PHOTO);
                                    }
                                }).setNegativeButton("我不爱放头像", null).show();
                            }
    
                            @Override
                            public void hadPermission(List<String> hadPermissions) {
    
                            }
    
                            @Override
                            public void allPermissionGranter() {
                                selectAvatarImage();
                            }
                        });
    

    以下是整体代码

    /**
     * Created by lip on 2018/5/7.
     * e:chenkenLip@163.com
     */
    
    public class PermissionUtils {
    
        public static boolean checkPermission(Context context, String permission) {
        boolean ret = false;
        int checkSelfPermission = PermissionChecker.checkSelfPermission(context, permission);
        if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {
            ret = true;
        }
        return ret;
    }
    
    public static void requestPersimsion(Activity activity, String permission, int requestCode) {
        ActivityCompat.requestPermissions(activity,
                new String[]{permission},
                requestCode);
    }
    
    public static void requestPersimsion(Activity activity, String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(activity,
                permissions,
                requestCode);
    }
    
    public static Observable<PermissionData> requestPermission(final Activity activity, final String[] permissions, final int requestCode) {
        return Observable.create(new ObservableOnSubscribe<PermissionData>() {
            @Override
            public void subscribe(ObservableEmitter<PermissionData> observableEmitter) throws Exception {
                List<String> couldReuqestPermissions = new ArrayList<>();
                List<String> shouldAskPermissions = new ArrayList<>();
                List<String> hadPermissions = new ArrayList<>();
                for (String permission : permissions) {
                    if (!checkPermission(activity, permission)) {
                        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                                permission)) {
                            shouldAskPermissions.add(permission);
                        } else {
                            couldReuqestPermissions.add(permission);
                        }
                    } else {
                        hadPermissions.add(permission);
                    }
                }
                if (couldReuqestPermissions.size() != 0) {
                    requestPersimsion(activity, couldReuqestPermissions.toArray(new String[]{}), requestCode);
                }
                PermissionData permissionData = new PermissionData();
                permissionData.setHadPermissions(hadPermissions);
                permissionData.setShouldAskPermissions(shouldAskPermissions);
                permissionData.setPermissionsCount(permissions.length);
                observableEmitter.onNext(permissionData);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(AndroidSchedulers.mainThread());
    }
    
    public abstract static class RequestPermissionObserver implements Observer<PermissionData> {
    
        @Override
        public void onSubscribe(Disposable disposable) {
    
        }
    
        @Override
        public void onNext(PermissionData data) {
            if (data.getShouldAskPermissions().size() != 0) {
                shouldShowRequestPermissionRationale(data.getShouldAskPermissions());
            }
            if (data.getHadPermissions().size() != 0) {
                hadPermission(data.getHadPermissions());
            }
            if (data.hadPermissions.size() == data.getPermissionsCount()) {
                allPermissionGranter();
            }
        }
    
        @Override
        public void onError(Throwable throwable) {
            Logger.i("lip--" + throwable.toString());
        }
    
        @Override
        public void onComplete() {
    
        }
    
        public abstract void shouldShowRequestPermissionRationale(List<String> shouldAskPermissions);
    
        public abstract void hadPermission(List<String> hadPermissions);
    
        public abstract void allPermissionGranter();
    }
    
    private static class PermissionData {
    
        private int permissionsCount;
    
        private List<String> shouldAskPermissions;
    
        private List<String> hadPermissions;
    
        public int getPermissionsCount() {
            return permissionsCount;
        }
    
        public void setPermissionsCount(int permissionsCount) {
            this.permissionsCount = permissionsCount;
        }
    
        public List<String> getShouldAskPermissions() {
            return shouldAskPermissions;
        }
    
        public void setShouldAskPermissions(List<String> shouldAskPermissions) {
            this.shouldAskPermissions = shouldAskPermissions;
        }
    
        public List<String> getHadPermissions() {
            return hadPermissions;
        }
    
        public void setHadPermissions(List<String> hadPermissions) {
            this.hadPermissions = hadPermissions;
        }
    }
    }
    

    相关文章

      网友评论

      本文标题:Android 请求权限链式工具类

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