RxPermissions README

The project's README from github.com/tbruyelle/RxPermissions. As one markdown file: README.md.

RxPermissions

BuildVersion Build Status

This library allows the usage of RxJava with the new Android M permission model.

Setup

To use this library your minSdkVersion must be >= 14.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.12'
}

Usage

Create a RxPermissions instance :

final RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance

NOTE: new RxPermissions(this) the this parameter can be a FragmentActivity or a Fragment. If you are using RxPermissions inside of a fragment you should pass the fragment instance(new RxPermissions(this)) as constructor parameter rather than new RxPermissions(fragment.getActivity()) or you could face a java.lang.IllegalStateException: FragmentManager is already executing transactions.

Example : request the CAMERA permission (with Retrolambda for brevity, but not required)

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

If you need to trigger the permission request from a specific event, you need to setup your event as an observable inside an initialization phase.

You can use JakeWharton/RxBinding to turn your view to an observable (not included in the library).

Example :

// Must be done during an initialization phase like onCreate
RxView.clicks(findViewById(R.id.enableCamera))
    .compose(rxPermissions.ensure(Manifest.permission.CAMERA))
    .subscribe(granted -> {
        // R.id.enableCamera has been clicked
    });

If multiple permissions at the same time, the result is combined :

rxPermissions
    .request(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(granted -> {
        if (granted) {
           // All requested permissions are granted
        } else {
           // At least one permission is denied
        }
    });

You can also observe a detailed result with requestEach or ensureEach :

rxPermissions
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again
        } else {
           // Denied permission with ask never again
           // Need to go to the settings
        }
    });

You can also get combined detailed result with requestEachCombined or ensureEachCombined :

rxPermissions
    .requestEachCombined(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 1 Permission object
        if (permission.granted) {
           // All permissions are granted !
        } else if (permission.shouldShowRequestPermissionRationale)
           // At least one denied permission without ask never again
        } else {
           // At least one denied permission with ask never again
           // Need to go to the settings
        }
    });

Look at the sample app for more.

Important read

As mentioned above, because your app may be restarted during the permission request, the request must be done during an initialization phase. This may be Activity.onCreate, or View.onFinishInflate, but not pausing methods like onResume, because you'll potentially create an infinite request loop, as your requesting activity is paused by the framework during the permission request.

If not, and if your app is restarted during the permission request (because of a configuration change for instance), the user's answer will never be emitted to the subscriber.

You can find more details about that here.

Status

This library is still beta, so contributions are welcome. I'm currently using it in production since months without issue.

Benefits

License

Copyright (C) 2015 Thomas Bruyelle

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

RxPermissions (tbruyelle/RxPermissions)

Searching Database…

10.4K ·GitHub
archived
这是什么?一个开源的 Android 库,通过 RxJava2 处理运行时权限,把 Android M 的权限模型封装成了 observable。
这是添加到我应用里的库,还是一个独立工具?它是一个添加到 Android 应用中使用的 Java 库,采用 Apache-2.0 许可证发布。你的应用必须把 minSdkVersion 设为 14 或更高。
我怎么创建一个 RxPermissions 实例?在 FragmentActivity 或 Fragment 中用 new RxPermissions(this) 创建。在 Fragment 里要传 Fragment 自身,不要传 fragment.getActivity(),否则可能会抛出 IllegalStateException。
我的项目必须已经在用 RxJava2 吗?是的——整个 API 都建立在 RxJava2 的 observable 之上:你通过订阅来申请权限,而不是处理回调。
我可以在任何地方触发权限申请吗,比如 onResume?不行——申请必须在初始化阶段执行,比如 Activity.onCreate 或 View.onFinishInflate,不能放在 onResume 这类暂停相关的方法里。否则可能陷入无限申请循环,而且如果应用在申请过程中重启,用户的选择结果就永远不会返回。
我可以一次申请多个权限,还能看到每个权限各自的结果吗?可以。多个权限可以一起申请并得到合并的结果;requestEach 和 ensureEach 会为每个权限单独发出一条详细结果,requestEachCombined 或 ensureEachCombined 则给出合并后的详细结果。
我能在按钮点击时发起申请吗?可以,但要在初始化阶段把点击事件设置成 observable——JakeWharton 的 RxBinding 可以把视图变成 observable——然后把权限申请链接到它上面。
在 Marshmallow 之前的 Android 版本上它有特殊处理吗?有:在 Android M 之前的系统上,observer 会自动收到已授权的结果,所以你的代码里不用再做系统版本判断。
它可以直接用在正式项目里吗?README 里仍标注为 beta,但作者已经在生产环境用了好几个月,没有出问题。不过要注意,GitHub 仓库已经归档,后续不会再维护了。
Creator
tbruyelle
Size
0.6 MB
Stars
10.4K
Age
11y
Commits · 90 Days