一、简介
This library allows the usage of RxJava with the new Android M permission model.
即: 这个库支持RxJava与新的Android M版本权限模型一起使用。
GitHub 地址:RxPermissions
二、使用
1. 权限申请
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 在 app/build.gradle 中添加:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.12'
}
3. 逻辑代码
public class MainActivity extends BaseActivity {
private static final String[] permissions = new String[]{
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
protected int getLayoutId() {
return R.layout.activity_main;
}
protected void initView() {
checkPermissionRequest();
}
public void checkPermissionRequest() {
RxPermissions rxPermissions = new RxPermissions(activity);
rxPermissions.request(permissions)
.subscribe(new Observer<Boolean>() {
public void onSubscribe( Disposable d) {
Log.d(TAG, "enter onSubscribe");
}
public void onNext( Boolean aBoolean) {
Log.d(TAG, "enter onNext");
if (aBoolean) {
if (listener != null) {
listener.onPermissionGranted();
} else {
}
} else {
Toast.makeText(activity, "Permission not granted.", Toast.LENGTH_SHORT).show();
}
}
public void onError( Throwable e) {
Log.d(TAG, "enter onError");
}
public void onComplete() {
Log.d(TAG, "enter onComplete");
}
});
}
protected void onDestroy() {
super.onDestroy();
}
}