Android--Permission declaration third-party library

1. It is more troublesome for Android to apply for permissions now. I found a relatively easy-to-use library on github: AndPermission

rely:

implementation 'com.yanzhenjie:permission:2.0.0-rc4'

The following statement should be added in Fragment, but it can be omitted in Activity, but I have added it all in the way of processing. It is an overriding method of the parent class, and it is not necessary to use it in activity.

// This sentence cannot be commented in Fragment, otherwise Fragment will not receive notification of obtaining permission.
super.onRequestPermissionsResult(requestCode,permissions,grantResults);

Specific implementation reference: https://blog.csdn.net/yanzhenjie1003/article/details/52503533

二、RxPermissions

rely:

//RxPermissions
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
//RxJava2
compile "io.reactivex.rxjava2:rxjava:2.0.0"

To apply for a single permission:

RxPermissions rxPermissions = new RxPermissions(MainActivity.this);
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Toast.makeText(MainActivity.this, "应用需要读写权限", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(Boolean value) {
                        if(value){
                            Toast.makeText(MainActivity.this, "同意权限", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this, "拒绝权限", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

同时申请多个权限并且分别判断权限是否授予:

RxPermissions rxPermission = new RxPermissions(getActivity());
        rxPermission
                .requestEach(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_CALENDAR,
                        Manifest.permission.READ_CALL_LOG,
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.READ_SMS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.SEND_SMS)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // The user has agreed to the permission
                            Log.d(TAG, permission.name + " is granted.");
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // If the user refuses the permission and does not select "Never ask again", then the next time the user starts again, a dialog box for requesting permission will also be prompted
                            Log.d(TAG, permission.name + " is denied. More info should be provided.");
                        } else {
                            // The user denied the permission and checked "Don't ask again"
                            Log.d(TAG, permission.name + " is denied.");
                        }
                    }
                });

In general, the second library is more convenient and concise, and it is recommended to use it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325887044&siteId=291194637