一、简介

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};

@Override
protected int getLayoutId() {
return R.layout.activity_main;
}

@Override
protected void initView() {
checkPermissionRequest();

}



public void checkPermissionRequest() {
RxPermissions rxPermissions = new RxPermissions(activity);
rxPermissions.request(permissions)
.subscribe(new Observer<Boolean>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.d(TAG, "enter onSubscribe");
}

@Override
public void onNext(@NonNull 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();
}
}

@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "enter onError");
}

@Override
public void onComplete() {
Log.d(TAG, "enter onComplete");
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
}
}