RxPermission uses

Keywords: Mobile Android Maven github xml

I. RxPermission use

1. Reference dependency:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
}

2. Android Manifest. XML Declares Permissions

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />

3. Use cases

(1) Apply for a single privilege

    public void checkPermissionRequest(FragmentActivity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        permissions.request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        LogUtils.error(TAG, "checkPermission22--:" + aBoolean);
                    }
                });
    }

summary

1. Return true: successful application; Return false: failed application
2. After consent, the prompt box will no longer pop up after applying for this permission.
3. Do not use compose methods, such as RxLifeCycle

(2) Apply for multiple privileges

    public void checkPermissionRequest(FragmentActivity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        permissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CALENDAR)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        LogUtils.error(TAG, "checkPermission22--:" + aBoolean);
                    }
                });
    }

summary

1. If there is a prohibition, return false; if all agree, return true.
2. After a permission has been granted, the prompt box will no longer pop up after applying for this permission. Others will continue to pop up.
3. This application has three permissions, but only two bullet windows. READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE belong to one group.

(3) Apply for a single authority and obtain detailed information.

    public void checkPermissionRequestEach(FragmentActivity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        permissions.requestEach(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-permission-:" + permission.name + "---------------");
                        if (permission.name.equalsIgnoreCase(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                            if (permission.granted) {//Call after consent
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + true);
                            } else if (permission.shouldShowRequestPermissionRationale){//Prohibited, but did not choose "no further inquiries", later application permission, will continue to pop up prompts.
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                            }else {//Forbidden, but choose "no more inquiries later", later apply for permission, will not continue to pop up prompts.
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + false);
                            }
                        }
                    }
                });
    }

summary

1. Return only one Permission object

(4) Apply for multiple privileges and obtain detailed information

 public void checkPermissionRequestEach(FragmentActivity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        permissions.requestEach(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CALENDAR)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-permission-:" + permission.name + "---------------");
                        if (permission.name.equalsIgnoreCase(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                            if (permission.granted) {//Call after consent
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + true);
                            } else if (permission.shouldShowRequestPermissionRationale){//Prohibited, but did not choose "no further inquiries", later application permission, will continue to pop up prompts.
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                            }else {//Forbidden, but choose "no more inquiries later", later apply for permission, will not continue to pop up prompts.
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + false);
                            }
                        }
                        if (permission.name.equalsIgnoreCase(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            if (permission.granted) {
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-WRITE_EXTERNAL_STORAGE-:" + true);
                            }else if (permission.shouldShowRequestPermissionRationale){
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                            } else {
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-WRITE_EXTERNAL_STORAGE-:" + false);
                            }
                        }
                        if (permission.name.equalsIgnoreCase(Manifest.permission.READ_CALENDAR)) {
                            if (permission.granted) {
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_CALENDAR-:" + true);
                            }else if (permission.shouldShowRequestPermissionRationale){
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                            } else {
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_CALENDAR-:" + false);
                            }
                        }
                    }
                });
    }

summary

1. Return three permission objects

_. Apply for multiple privileges to obtain the merged details

   public void checkPermissionRequestEachCombined(FragmentActivity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        permissions.requestEachCombined(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CALENDAR)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-permission-:" + permission.name + "---------------");
                        if (permission.granted) {//Call after full consent
                            LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-READ_EXTERNAL_STORAGE-:" + true);
                        } else if (permission.shouldShowRequestPermissionRationale) {//As long as there is a choice: prohibit, but do not choose "no further inquiries", later application permission, will continue to pop up prompts.
                            LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                        } else {//As long as there is a choice: prohibit, but choose "no further inquiries", later application permission, will not continue to pop up prompts.
                            LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-READ_EXTERNAL_STORAGE-:" + false);
                        }
                    }
                });
    }

4. Use cases combined with RxJava

 public void checkPermissionsEnsure(FragmentActivity activity) {
        final RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        Observable.timer(100, TimeUnit.MILLISECONDS)
                .compose(permissions.ensure(Manifest.permission.READ_EXTERNAL_STORAGE))
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        LogUtils.error(TAG, "checkPermissionsEnsure--:" + "-READ_EXTERNAL_STORAGE-:" + aBoolean);
                    }
                });
    }

summary

1. The usage of ensure, ensure Each, ensure EachCombine is similar to that of before.

5. Judging whether a privilege has been applied for

    /**
     * Check whether a permission has been applied for
     *
     * @param activity
     */
    public void checkPermissionsIsGranted(FragmentActivity activity) {
        RxPermissions permissions = new RxPermissions(activity);
        permissions.setLogging(true);
        LogUtils.error(TAG, "checkPermissionsIsGranted--:" + "-WRITE_EXTERNAL_STORAGE-:" + permissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE));
    }
    


Reprinted: https://www.jianshu.com/p/c1219d1d2401

Posted by j007w on Sat, 26 Jan 2019 11:45:14 -0800