RxPermissions使用详解

1、介绍:

This library allows the usage of RxJava with the new Android M permission model.
即: 这个库支持RxJava与新的Android M版本权限模型一起使用。

2、如何依赖

2.1 简单使用(不支持RxJava,订阅)
``` compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar' ```

2.2支持RxJava的使用(将rxpermissions包名换为rxpermissions2) 即:

compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'

3、迁移到0.9 后的说明

官网介绍:
Version 0.9 now uses a retained fragment to trigger the permission request from the framework. As a result, the RxPermissions class is no more a singleton. To migrate from 0.8 or earlier, just replace the following :
即:
版本0.9现在使用支持Fragment去触发框架的权限请求。 因此,RxPermissions类不再功能单一。 要从0.8或更低版本迁移,只需更换以下内容:

RxPermissions.getInstance(this) -> new RxPermissions(this) // where this is an Activity instance

4、如何在项目中使用

在初始化Activity中加入如下代码:

      RxPermissions rxPermissions=new RxPermissions(this);
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE,Manifest.permission.INTERNET).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean){
                    //申请的权限全部允许
                    Toast.makeText(MainActivity.this, "允许了权限!", Toast.LENGTH_SHORT).show();
                    initData();
                }else{
                    //只要有一个权限被拒绝,就会执行
                    Toast.makeText(MainActivity.this, "未授权权限,部分功能不能使用", Toast.LENGTH_SHORT).show();
                }
            }
        });

If you need to trigger the permission request from a specific event, you need to setup your event as an observable inside an initialization phase.
即:
如果您需要触发特定事件的权限请求,则需要在初始化阶段将事件设置为可观察的事件。

You can use JakeWharton/RxBinding to turn your view to an observable (not included in the library).
译:
您可以使用JakeWharton / RxBinding将您的视图转换为可观察对象(不包含在库中)。
例如:

// Must be done during an initialization phase like onCreate
RxView.clicks(findViewById(R.id.enableCamera))
    .compose(rxPermissions.ensure(Manifest.permission.CAMERA))
    .subscribe(granted -> {
        // R.id.enableCamera has been clicked
    });

同时申请多个权限:

rxPermissions
    .request(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(granted -> {
        if (granted) {
           // All requested permissions are granted
        } else {
           // At least one permission is denied
        }
    });

监听具体的某一些权限是否进行了授权

rxPermissions
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again
        } else {
           // Denied permission with ask never again
           // Need to go to the settings
        }
    });

5、状态

This library is still beta, so contributions are welcome. I'm currently using it in production since months without issue.
译:
这个库仍然是测试版,所以贡献是值得欢迎的。 目前我正在使用它在生产中没有问题。

6、优点:

避免担心框架版本。 如果sdk是前M,观察者将自动收到一个授予的结果。

防止您在权限请求和结果处理之间分割您的代码。 假如没有这个库,你必须在一个地方请求权限,并在Activity.onRequestPermissionsResult()中处理结果。

所有RX提供的有关转换,过滤,链接...

7、如果要使用订阅、观察,必须依赖rxjava库如:

    // RxJava
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
    //rxpermissions
    compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'

最后:附上RxPermissions的库github地址: https://github.com/tbruyelle/RxPermissions

猜你喜欢

转载自blog.csdn.net/qq_15988951/article/details/78826598