使用RxPermission框架对android6.0权限进行检测

可前往Github上进行查看相关DEMO

https://github.com/tbruyelle/RxPermissions

如果想用RxPermissionminSdkVersion 必须大于等于9.

在gradle中添加如下代码

repositories {
    jcenter() // If not already there
}

dependencies {
    compile 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
}
举个例子

// 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
        }
    });
如果你想要在一个特殊事件中触发权限申请,你需要在你的初始化段落里,将这个事件订阅。

你可以使用 JakeWharton/RxBinding 把你的view订阅为一个观察者。

举例

// Must be done during an initialization phase like onCreate
RxView.clicks(findViewById(R.id.enableCamera))
    .compose(RxPermissions.getInstance(this).ensure(Manifest.permission.CAMERA))
    .subscribe(granted -> {
        // R.id.enableCamera has been clicked
    });
如果同时有多个权限请求,结果如下

RxPermissions.getInstance(this)
    .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
        }
    });
你也可以用requestEach和 ensureEach 订阅具体的结果。

RxPermissions.getInstance(this)
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        }
    });


阅读更多
文章标签: Android6.0 RxPermission
个人分类: Android6.0 Android开发
上一篇让你的程序实现MaterialDesign风格
下一篇ContentProvider 和ContentResolver内容提供者和内容解析者
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭