Rxpermissions

該庫允許將 RxJava 與新的 Android M 許可權模型一起使用。

將庫新增到依賴項:

Rxjava

dependencies {
    compile 'com.tbruyelle.rxpermissions:rxpermissions:0.8.0@aar'
}

Rxjava2

dependencies {
    compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.8.1@aar'
}

用法

示例(為了簡潔起見,使用 Retrolambda,但不是必需的):

// Must be done during an initialization phase like onCreate
RxPermissions.getInstance(this)
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

閱讀更多: https//github.com/tbruyelle/RxPermissions