Android 6.0 Quick Request for Rights (more elegant based on RxJava2)

1. Preface

Preface?Where is so much nonsense? Get straight to the point!

2. Rights to apply for

Cough and cough, before we get to the topic, let's first know which permissions need to be applied for manually after Android 6.0. If you take the right medicine, it's the kingdom!

group:android.permission-group.CONTACTS (Mobile Contact Related Permissions)
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS

group:android.permission-group.PHONE (Phone-related privileges)
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL

group:android.permission-group.CALENDAR (Mobile Time Related Permissions)
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR

group:android.permission-group.CAMERA (Camera-related permissions)
permission:android.permission.CAMERA

group:android.permission-group.SENSORS (Sensor-related permissions)
permission:android.permission.BODY_SENSORS

group:android.permission-group.LOCATION (Get geolocation-related permissions)
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION

group:android.permission-group.STORAGE (Permissions to read and write data)
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE

group:android.permission-group.MICROPHONE (Permission to microphone)
permission:android.permission.RECORD_AUDIO

group:android.permission-group.SMS (SMS related permissions)
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS

Whether it feels so intimate that it has been specially translated into Chinese, we can see that the permissions that need to be applied for are divided into groups. Permissions under the same group, as long as one of them is applied for, then other permissions do not need to be applied for manually anymore.Let's just raise a chestnut, like "Permission to read and write data". We applied for WRITE_EXTERNAL_STORAGE manually, so we don't have to apply for READ_EXTERNAL_STORAGE manually anymore. Okay, let's start the next performance.

3. Adding Dependencies

Add the following code to build.gradle:

dependencies {
    ยทยทยท
    compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
    compile "io.reactivex.rxjava2:rxjava:2.1.0"
}

4. Use

Call the following code initialization in Activity that needs to request permissions:

RxPermissions rxPermissions = new RxPermissions(this);

Apply for the permissions you need, old rules, and raise a chestnut:

rxPermissions
                        //Add the name of the permission you want to get to the request, for example, read and write permissions
                        .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(@NonNull Boolean aBoolean) throws Exception {
                                //Logical judgment based on Boolean to judge whether an application succeeds or fails
                                if (aBoolean) {
                                    Toast.makeText(MainActivity.this, "Application Successful", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(MainActivity.this, "withdraw", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

This completes the permission request.Of course, if you want to apply for multiple permissions at the same time, you just need to continue adding permissions in the request:

rxPermissions
                        //request for multiple permissions
                        .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, 
                                Manifest.permission.CALL_PHONE, 
                                Manifest.permission.CAMERA)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(@NonNull Boolean aBoolean) throws Exception {
                                //Logical judgment based on Boolean to judge whether an application succeeds or fails
                                if (aBoolean) {
                                    Toast.makeText(MainActivity.this, "Application Successful", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(MainActivity.this, "withdraw", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

Android 6.0 permission application was completed so easily by us, is it convenient to stir-fry chicken?Okay, this is the end of the tutorial. Thanks for your support.

Keywords: Android Mobile Gradle

Added by mm00 on Thu, 27 Jun 2019 19:48:03 +0300