Android 中RxPermissions 的使用方法详解
以请求拍照、读取位置权限为例
module的build.gradle:
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
compile 'io.reactivex.rxjava2:rxjava:2.0.5'
AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/
<uses-permission android:name="android.permission.CAMERA"/
源码:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RxPermissions rxPermissions = new RxPermissions((Activity) mContext);
rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CAMERA)
.subscribe(new Observer<Boolean () {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(mContext, "已获取权限,可以干想干的咯", Toast.LENGTH_LONG)
.show();
} else {
//只有用户拒绝开启权限,且选了不再提示时,才会走这里,否则会一直请求开启
Toast.makeText(mContext, "主人,我被禁止啦,去设置权限设置那把我打开哟", Toast.LENGTH_LONG)
.show();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
});
注意:
必须加 compile ‘io.reactivex.rxjava2:rxjava:2.0.5’
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!