EasyPermissions framework technical analysis and application cases
EasyPermissions framework technical analysis and application cases
EasyPermissions is an Android permission management framework, which aims to simplify permissions requesting processes in Android applications.Through EasyPerMissions, developers can easily request permissions and handle user authorization results.This article will introduce the technical principles of EasyPerMissions and provide a application case to demonstrate its usage.
Technical principle:
EasyPermissions uses the operating permissions request mechanism provided by the Android system to implement permissions management.In the Android 6.0 (API 23) and above versions, the system has introduced the operating permissions model, which requires the application of dynamic request permissions during runtime.EasyPermissions simplified the process of permission request based on this mechanism.
The core principles of EasyPermissions are as follows:
1. Examination permissions: First check whether there is corresponding permissions where the application permissions are required.If you already have it, perform the corresponding operation directly; if not, continue the next step.
2. Request permissions: Use the getRequestPerMissions () method provided by EasyPermissions to pass the required permissions array and initiate permissions requests.
3. Processing results: Rewilling the onrequestPerMissionsRet () method in Activity or Fragment, EasyPermissions will automatically pass the result to the method.In this method, the corresponding treatment can be performed according to the results.
Applications:
Suppose our application needs to access the user's camera and storage permissions. The following is an example of using EasyPerMissions:
1. Add dependencies:
First, add EasyPerMissions to the project's Build.gradle file:
implementation 'pub.devrel:easypermissions:3.0.0'
2. Check permissions:
Where to use cameras and storage permissions, first check whether these two permissions already have these two permissions:
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// I have obtained permissions and perform the corresponding operation
// ...
} else {
// No permission, initiated permission request
EasyPermissions.requestPerMissions (this, "Requirement of camera and storage permissions", request_code_permissions,
Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
3. Processing results:
Rewrite the onrequestPerMissionsResult () method in the Activity or Fragment, and the result of the request for processing permissions:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// Pass the result of the permission request to EasyPermissions for processing
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@AfterPermissionGranted(REQUEST_CODE_PERMISSIONS)
public void onPermissionsGranted(int requestCode, List<String> grantedPermissions) {
// The permissions have been authorized, and the corresponding operation is performed
// ...
}
@AfterPermissionDenied(REQUEST_CODE_PERMISSIONS)
public void onPermissionsDenied(int requestCode, List<String> deniedPermissions) {
// The permissions are rejected, prompting the user and handling it accordingly
// ...
}
In this example, after the request permissions, if the user agrees with authorization, EasyPerMissions will automatically call the onPerMissionsgranted () method and pass the authorized authority list to the method.If the user refuses to authorize, EasyPerMissions will automatically call the onPerMissionsons method and pass the rejected authority list to the method.
Through EasyPerMissions, we can simply and easily manage the permissions request process of Android applications, improve user experience, and simplify development work.
The above is the technical principles of the EasyPerMissions framework and a simple application case. I hope it will help you understand and use EasyPermissions.