RxPermissions使用总结
2019-07-10 本文已影响0人
崔兴旺
先上结论
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, | |
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);
}
});
}