RxPermission使用

TTMMJJ99發表於2018-10-23

一、RxPermission使用

1、引用依賴:

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

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

2、AndroidManifest.xml宣告許可權

    <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、使用用例

①.申請單個許可權

    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);
                    }
                });
    }

總結

1、返回true:申請成功 ;返回false:申請失敗
2、同意後,之後再申請此許可權則不再彈出提示框
3、不要使用compose方法,如RxLifeCycle

②.申請多個許可權

    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);
                    }
                });
    }

總結

1、只要有一個禁止,則返回false;全部同意,則返回true。
2、某個許可權同意後,之後再申請此許可權則不再彈出提示框,其他的會繼續彈出
3、此申請三個許可權,但只會有兩個彈窗,READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE 是屬於一組,

③.申請單個許可權,獲得詳細資訊

    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) {//同意後呼叫
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + true);
                            } else if (permission.shouldShowRequestPermissionRationale){//禁止,但沒有選擇“以後不再詢問”,以後申請許可權,會繼續彈出提示
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                            }else {//禁止,但選擇“以後不再詢問”,以後申請許可權,不會繼續彈出提示
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + false);
                            }
                        }
                    }
                });
    }

總結

1、只返回一個Permission物件

④.申請多個許可權,獲得詳細資訊

 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) {//同意後呼叫
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-:" + true);
                            } else if (permission.shouldShowRequestPermissionRationale){//禁止,但沒有選擇“以後不再詢問”,以後申請許可權,會繼續彈出提示
                                LogUtils.error(TAG, "checkPermissionRequestEach--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                            }else {//禁止,但選擇“以後不再詢問”,以後申請許可權,不會繼續彈出提示
                                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);
                            }
                        }
                    }
                });
    }

總結

1、返回三個permission物件

⑤.申請多個許可權,獲取合併後的詳細資訊

   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) {//全部同意後呼叫
                            LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-READ_EXTERNAL_STORAGE-:" + true);
                        } else if (permission.shouldShowRequestPermissionRationale) {//只要有一個選擇:禁止,但沒有選擇“以後不再詢問”,以後申請許可權,會繼續彈出提示
                            LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-READ_EXTERNAL_STORAGE-shouldShowRequestPermissionRationale:" + false);
                        } else {//只要有一個選擇:禁止,但選擇“以後不再詢問”,以後申請許可權,不會繼續彈出提示
                            LogUtils.error(TAG, "checkPermissionRequestEachCombined--:" + "-READ_EXTERNAL_STORAGE-:" + false);
                        }
                    }
                });
    }

4、使用用例結合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);
                    }
                });
    }

總結

1、ensure、ensureEach、ensureEachCombined用法跟之前類似

5、判斷某個許可權是否被申請

    /**
     * 檢查某個許可權是否被申請
     *
     * @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));
    }
    


轉載:https://www.jianshu.com/p/c1219d1d2401

相關文章