Permissions Dispatcher, an Android permission application framework, uses

Permissions Dispatcher, an Android dynamic permission application library, uses

Dynamic permission application library https://github.com/permissions-dispatcher/PermissionsDispatcher

Before Android 6.0, the application permissions will be listed in the form of a list when the application is installed. Users can see that those permissions have been applied for or selectively shut down. After 6.0, when the application is installed, the system does not apply for permissions in this way. For general permissions which are not dangerous, the application automatically applies for permissions when it is installed, and the permissions cannot be closed. Hazardous permissions require users to choose whether to open or not. The description of permissions can refer to official documents. https://developer.android.google.cn/guide/topics/permissions/overview It introduces several types of permissions in the latest version of Android P, which can be viewed by oneself as normal permissions, dangerous permissions, and signature permissions.

Having said so much, let's get to the point below. The dynamic permission application library, Permissions Dispatcher, has been used, because the AS I used is the latest version 3.1.4, which is the reference operation for the following.

Install plug-ins in AS
Open AS's setting s - > Plugins - > Browse repositories, then enter Permissions in the search box, find Permissions Dispatcher plugin in the results, click install, display Restart Android Studio after installation, click and restart studio, and then enter studio. First, declare in Android Manifest. XML the permissions you need to apply for, such as:

//Write storage permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//Read Storage Permissions
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
//Camera permissions
<uses-permission android:name="android.permission.CAMERA" />

After the declaration in the manifest file, right mouse button - > Generate - > Generate Runtime Permissions (or use the shortcut Alt+Insert - > Generate Runtime Permissions) where permission is required

Pop-up plug-in selection permission window, select permissions in the window (corresponding to the permissions applied in the Android Manifest file, if there is no permission to apply, even if the permission application is checked in the plug-in window, the actual runtime will not apply for permissions)

After the permission is selected, the necessary method Needs Permission enters the method name of the permission application, clicks the bottom button Generate, and prompts Re build Project to confirm. After the construction is completed, the code for the permission application will be automatically generated, and a registration annotation will be added to the class.

@RuntimePermissions
public class MainActivity extends BaseActivity {

    ......

    @NeedsPermission({Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
        void applayPermissions() {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}

Permissions Dispatcher dependencies will also be added to app moudle s (if no library dependencies have been added before)

implementation "com.github.hotchemi:permissionsdispatcher:3.1.0"
annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:3.1.0"

@ The method under the Needs Permission annotation is the method of the name just entered, which is called where permission is required.

//The method here is called applayPermissions (the method name under the @Needs Permission annotation)+WithPermission Check, or it can be viewed directly in the Generation MainActivity Permission Dispatcher class.
MainActivityPermissionsDispatcher.applayPermissionsWithPermissionCheck(this);

At this point, the application permission has been completed, and the running program will pop up the window to apply for permission.

Because only the first necessary application method is processed in the demo program, the other three are not processed. After refusing the permission, the next entry will still apply for permission, and the window will not pop up after no further inquiry. The introduction of the use of this permission application library is completed. Welcome to point out the shortcomings, thank you!

Keywords: Android github Google xml

Added by eektech909 on Sun, 19 May 2019 18:52:28 +0300