In -depth research on the technical principles of the EasyPermissions framework in the Java class library
EasyPermissions is an open source framework for simplifying the right to apply for Android.It provides a simple and easy -to -use way to request and handle permissions, avoid tedious permissions applications, and provide developers with a better user experience.
The technical principles of the EasyPermissions framework are as follows:
1. Permanent check: Before using EasyPerMissions, you need to check whether the current application has obtained the required permissions.By using the method provided by EasyPerMissions, you can quickly check whether one or more permissions have been authorized.
2. Permanent application: If certain permissions have not been authorized, EasyPerMissions provides developers with convenient ways to request these permissions.By using the application permission method of EasyPerMissions, you can display a permissions application dialog box to request the required permissions.
3. Permanent processing: Once the user responds to the permission request, EasyPerMissions will automatically pass the results to the developer.Developers can handle the results of the permissions request by rewrite the callback method.If the permissions have been granted, the developer can continue to perform the operation required; if the permissions are rejected, the developer can take appropriate measures according to the need, such as displaying a dialog box or guiding the user to the application setting page.
EasyPermissions has achieved simple and easy -to -use permissions management by encapsulating the Android permission framework.Here are some examples of examples that use the EasyPermissions framework:
1. Check whether the permission is authorized for example code:
if (EasyPermissions.hasPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Already authorized
} else {
// Unauthorized, required application permissions
}
2. Example code for request permissions:
EasyPermissions.requestPerMissions (Activity, "Need to read storage permissions", rc_storage, manifest.permission.read_external_storage);
3. Example code of processing permissions request results:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
// permissions have been granted
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
// Performance is rejected
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
// If the authority is permanently rejected, the jump to the application settings page
EasyPermissions.goSettings(this);
} else {
// If the authority is temporarily rejected, you can display an explanation dialog box, etc.
}
}
Through the above example code, we can see the simple and easy use of the EasyPermissions framework.It provides developers with a fast and convenient way to process Android operating permissions, making permission management easier and efficient.