RxPermissions的使用

以请求拍照、读取位置权限为例

module的build.gradle:

    compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'  
    compile 'io.reactivex.rxjava2:rxjava:2.0.5'

AndroidManifest.xml:

    
    

源码:

    Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RxPermissions rxPermissions = new RxPermissions((Activity) mContext);
                    rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION,
                                    Manifest.permission.CAMERA)
                                    .subscribe(new Observer() {
                                @Override
                                public void onSubscribe(Disposable d) {
    
                                }
    
                                @Override
                                public void onNext(Boolean aBoolean) {
                                    if (aBoolean) {
                                        Toast.makeText(mContext, "已获取权限,可以干想干的咯", Toast.LENGTH_LONG)
                                                .show();
                                    } else {
                                        //只有用户拒绝开启权限,且选了不再提示时,才会走这里,否则会一直请求开启
                                        Toast.makeText(mContext, "主人,我被禁止啦,去设置权限设置那把我打开哟", Toast.LENGTH_LONG)
                                                .show();
                                    }
                                }
    
                                @Override
                                public void onError(Throwable e) {
    
                                }
    
                                @Override
                                public void onComplete() {
    
                                }
                });
            }
        });

注意:

必须加 compile 'io.reactivex.rxjava2:rxjava:2.0.5'

你可能感兴趣的:(RxPermissions的使用)