安卓集中营安卓开发

RxPermission动态申请权限

2019-01-28  本文已影响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

上一篇下一篇

猜你喜欢

热点阅读