Android permission第三方rxpermission的正确使用姿势

之前的permission申请都是用的原生封装,现在接触下第三方的权限请求框架.
使用姿势:
1.Gradle引入

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

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

2.在AndroidManifest中声明权限:

    
    

    
    
    
    
    
    
    

3.在activity(fragment)的oncreate方法中使用,申请权限,注意的是申请的权限需要在androidmanifest中有对应的声明:

 RxPermissions rxPermissions = new RxPermissions(this);
        Disposable subscribe = rxPermissions
                .requestEachCombined(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(permission -> {
                    if (permission.granted) {
                        // All requested permissions are granted
                        showToast("权限获取成功");
                        Log.d("lwp","success permission:"+permission.name);
                        // AppSp appSp = new AppSp(this, "", 1);
                        String string = SPUtils.getInstance().getString(Constant.TOKEN);
                        //testApkUpdate();
                        try {
                            if (isStringEmpty(string)) {
                                Observable.timer(1000, TimeUnit.MILLISECONDS).subscribe(new Consumer() {
                                    @Override
                                    public void accept(Long aLong) throws Exception {
                                        toLogin();
                                    }
                                });

                            } else {
                                Observable.timer(1000, TimeUnit.MILLISECONDS).subscribe(new Consumer() {
                                    @Override
                                    public void accept(Long aLong) throws Exception {
                                        toMain();
                                    }
                                });
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } 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
                      
        }
                });


你可能感兴趣的:(Android)