RxPermisson2 用法 翻译

To use this library your minSdkVersion must be >= 11. (最低版本>=11)

dependencies {
    compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar'
}

Usage

Create a RxPermissions instance : (创建一个PxPermissions 实体)

RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance (this 是一个activity实体)

Example : request the CAMERA permission (with Retrolambda for brevity, but not required)

// Must be done during an initialization phase like onCreate 
rxPermissions
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

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).

Example :

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

If multiple permissions at the same time, the result is combined : (如果多个权限,得到的结果是合并的)

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 (至少存在一个拒绝)
        }
    });

You can also observe a detailed result with requestEach or ensureEach : (可以使用requestEach或者ensureEach  得到详细结果

rxPermissions
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects (会发射两个结果)
        if (permission.granted) {
           // `permission.name` is granted ! (.name 得到权限名称, 得到授权)
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again (拒绝授权,未勾选不再询问)
        } else {
           // Denied permission with ask never again (拒绝授权, 勾选不再询问)
           // Need to go to the settings (需求去设置开启)
        }
    });

You can also get combined detailed result with requestEachCombined or ensureEachCombined :  (可以使用requestEachCombined 或者 ensureEachCombined 得到合并后的详细结果

rxPermissions
    .requestEachCombined(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 1 Permission object (仅仅发送一个结果)
        if (permission.granted) {
           // All permissions are granted ! (都得到授权)
        } else if (permission.shouldShowRequestPermissionRationale)
           // At least one denied permission without ask never again (至少一个未得到授权 并 都未勾选不再询问)
        } else {
           // At least one denied permission with ask never again (至少存在一个 未得到授权,并勾选不再询问)
           // Need to go to the settings (需要去设置开启)
        }
    });
todo : 如果requestEachCombined  拒绝,permission是否可以明确指出是具体哪个permission,待求证
 

求证结果,多个权限请求时,合并一个结果时源码

 public Permission(List<Permission> permissions) { permission 是请求权限集,仅仅检查后更新其granted,showldShowRequestPermissonR..属性
        this.name = this.combineName(permissions);
        this.granted = this.combineGranted(permissions).booleanValue();
        this.shouldShowRequestPermissionRationale = this.combineShouldShowRequestPermissionRationale(permissions).booleanValue();
    }

private String combineName(List<Permission> permissions) {
        return ((StringBuilder)Observable.fromIterable(permissions).map(new Function<Permission, String>() {
            public String apply(Permission permission) throws Exception {
                return permission.name;
            }
        }).collectInto(new StringBuilder(), new BiConsumer<StringBuilder, String>() {
            public void accept(StringBuilder s, String s2) throws Exception {
                if(s.length() == 0) {
                    s.append(s2);
                } else {
                    s.append(", ").append(s2);
                }

            }
        }).blockingGet()).toString();
    }

    private Boolean combineGranted(List<Permission> permissions) {
        return (Boolean)Observable.fromIterable(permissions).all(new Predicate<Permission>() {
            public boolean test(Permission permission) throws Exception {
                return permission.granted;
            }
        }).blockingGet();
    }

    private Boolean combineShouldShowRequestPermissionRationale(List<Permission> permissions) {
        return (Boolean)Observable.fromIterable(permissions).any(new Predicate<Permission>() {
            public boolean test(Permission permission) throws Exception {
                return permission.shouldShowRequestPermissionRationale;
            }
        }).blockingGet();
    }
操作符注释:
all(Predicate)Observable<Boolean>Single<Boolean>如果所有的元素都匹配,则发射true

any(Predicate)Observable<Boolean>Single<Boolean>如果所有的元素都匹配,则发射true
map 转换类型

结论:多个权限使用 requestEachCombined, 得到的结果 name为 以多个权限名以,拼接而成,granted为全部同意才返回true,shouldShowRequestPermissionRationale为有任何一个为true 就返回true(有一个点了不再提醒,就需要提示的)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值