Detailed explanation of the technical principles of Java Library Library of EasyPermissions framework
EasyPermissions is an open source framework used in Android development to simplify permissions processing.The framework is based on Android's native authority and provides an easy -to -use API, enabling developers to more easily request and handle the operating permissions.
The technical principles of EasyPermissions mainly include three aspects: permissions requests, authority judgments and permissions processing.
1. Permanent request:
EasyPermissions encapsulates Android's native permissions request mechanism.Developers only need to call the static method of the EasyPerMissions class, RequestPerMissions () to request the authority permissions.This method will first determine whether there is currently required permissions, and if not, the permissions request dialog box will pop up.At the same time, EasyPermissions also supports multiple permissions and requests, simplifying the development process.
2. Permanent judgment:
When the application is running, EasyPerMissions will judge the existing permissions.It provides an annotation @AFTERPERMISSININGRANTED, which can be used to mark a method that automatically executes this method when the authority is granted.Developers can perform corresponding processing logic in this method to ensure operations without permissions.If the authority is not granted, the permission request dialog box will pop up.
3. Permission processing:
EasyPermissions provides a callback interface EasyPermissions.PermissionCallbacks for processing permissions request results.Developers need to implement the interface and processed the permissions in the corresponding method to be granted or rejected.For example, the logic that can be performed in the onPerMissionsGranted () method, and the logic of processing permissions in the onPerMissionsDenied () method.In addition, EasyPerMissions also provides a state of assistance to check the status and request results for checking permissions.
Below is an example code that uses the EasyPerMissions frame:
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int RC_CAMERA_PERMISSION = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Request camera permissions
requestCameraPermission();
}
// Request camera permissions
private void requestCameraPermission() {
// Check the camera permissions
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
// already have the right of the camera to perform the camera -related operation
openCamera();
} else {
// No camera permissions, request permissions
EasyPermissions.requestPerMissions (this, "requires camera permissions", RC_CAMERA_PERMISSION, Manifest.Permission.camera);
}
}
// Turn on the camera
private void openCamera() {
// Implement the camera operation logic
Toast.maketext (this, "Open the camera, toast.length_short) .show ();
}
// Performance Request Results Return
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Send the result of the permission request to EasyPermissions for processing
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
// permissions are granted
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
if (requestCode == RC_CAMERA_PERMISSION) {
// The camera authority is granted, and the camera -related operation
openCamera();
}
}
// Performance is rejected
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
if (requestCode == RC_CAMERA_PERMISSION) {
// The camera authority is rejected, and the corresponding prompt is given
Toast.makeText (this, "No camera permission", toast.length_short) .show ();
}
}
}
The above code demonstrates how to use the EasyPermissions framework to request the camera permissions and perform corresponding treatment when the authority is granted or rejected.