RxPermissions使用总结

先上结论

API备注回调结果
request不支持返回权限名;
返回的权限结果:全部同意时值true,否则值为false
申请结果:false
requestEach把每一个权限的名称和申请结果都列出来权限名称:android.permission.READ_CONTACTS,申请结果:false
权限名称:android.permission.WRITE_CONTACTS,申请结果:false
权限名称:android.permission.READ_SMS,申请结果:false
权限名称:android.permission.ACCESS_FINE_LOCATION,申请结果:true
权限名称:android.permission.ACCESS_COARSE_LOCATION,申请结果:true
requestEachCombined返回的权限名称:将多个权限名合并成一个;
返回的权限结果:全部同意时值true,否则值为false
权限名称:android.permission.READ_CONTACTS, android.permission.WRITE_CONTACTS, android.permission.READ_SMS, android.permission.ACCESS_FINE_LOCATION, android.permission.ACCESS_COARSE_LOCATION,申请结果:false
ensure必须配合rxjava回调结果与request一样
ensureEach必须配合rxjava回调结果跟requestEach一样
ensureEachCombined必须配合rxjava回调结果跟requestEachCombined一样

完整代码

依赖库版本
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
java代码
/**
 * 权限组
 */
private static final String[] permissionsGroup=new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,Manifest.permission.READ_SMS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};

/**
 * request例子:
 * 不支持返回权限名;
 * 返回的权限结果:全部同意时值true,否则值为false
 */
public void testRequest(View view) {
    RxPermissions rxPermissions = new RxPermissions(this);
    rxPermissions.request(permissionsGroup)
            .subscribe(granted -> {
                Log.i("cxw","申请结果:"+granted);
            });

}

/**
 * requestEach例子:
 * 把每一个权限的名称和申请结果都列出来
 */
public void testRequestEach(View view) {
    RxPermissions rxPermissions = new RxPermissions(this);
    rxPermissions.requestEach(permissionsGroup)
            .subscribe(permission -> {
                Log.i("cxw","权限名称:"+permission.name+",申请结果:"+permission.granted);
            });
}

/**
 * requestEachCombined例子:
 * 返回的权限名称:将多个权限名合并成一个
 * 返回的权限结果:全部同意时值true,否则值为false
 */
public void testRequestEachCombined(View view) {
    RxPermissions rxPermissions = new RxPermissions(this);
    rxPermissions.requestEachCombined(permissionsGroup)
            .subscribe(permission -> {
                Log.i("cxw","权限名称:"+permission.name+",申请结果:"+permission.granted);
            });
}

/**
 * ensure例子:
 * 必须配合rxjava,回调结果与request一样
 */
public void testEnsure(View view) {
    RxPermissions rxPermissions = new RxPermissions(this);
    Observable.timer(10, TimeUnit.MILLISECONDS)
            .compose(rxPermissions.ensure(permissionsGroup))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    Log.i("cxw","申请结果:"+aBoolean);
                }
            });
}

/**
 * ensureEach例子:
 * 必须配合rxjava,回调结果跟requestEach一样
 */
public void testEnsureEach(View view) {
    RxPermissions rxPermissions = new RxPermissions(this);
    //
    Observable.timer(10, TimeUnit.MILLISECONDS)
            .compose(rxPermissions.ensureEach(permissionsGroup))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Permission>() {
                @Override
                public void accept(Permission permission) throws Exception {
                    Log.i("cxw","权限名称:"+permission.name+",申请结果:"+permission.granted);
                }
            });
}

/**
 * ensureEachCombined例子:
 * 必须配合rxjava,回调结果跟requestEachCombined一样
 */
public void testEnsureEachCombined(View view) {
    RxPermissions rxPermissions = new RxPermissions(this);
    Observable.timer(10, TimeUnit.MILLISECONDS)
            .compose(rxPermissions.ensureEachCombined(permissionsGroup))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Permission>() {
                @Override
                public void accept(Permission permission) throws Exception {
                    Log.i("cxw","权限名称:"+permission.name+",申请结果:"+permission.granted);
                }
            });
}

RxPermissions库地址

https://github.com/tbruyelle/RxPermissions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值