美文网首页安卓集中营安卓开发
RxPermission动态申请权限

RxPermission动态申请权限

作者: 蓝不蓝编程 | 来源:发表于2019-01-28 17:39 被阅读2次

下载本文完整demo

添加依赖下载源

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

添加依赖

    implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.github.tbruyelle:rxpermissions:0.10.2'

Manifest添加权限申请(应根据实际情况修改)

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

样例

@SuppressLint("CheckResult")
    public void checkPermissionAndGetDeviceId() {
        RxPermissions permissions = new RxPermissions(this);
        permissions.setLogging(true);
        permissions.requestEach(Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Consumer<Permission>() {
                    @SuppressLint("MissingPermission")
                    @Override
                    public void accept(Permission permission) {
                        textView.setText("deviceId: " + PhoneUtils.getDeviceId());
                    }
                });
    }

参考资料:

https://www.jianshu.com/p/734ad53eb7b7

安卓开发技术分享: https://www.jianshu.com/p/442339952f26

相关文章

网友评论

    本文标题:RxPermission动态申请权限

    本文链接:https://www.haomeiwen.com/subject/potzjqtx.html