Analyze the technical principles of the EasyPerMissions framework in the Java class library
EasyPerMissions is a framework for simplifying the right to simplify the time of Android. It is developed based on Android's PermissINDISPATCHER library.In the Java class library, EasyPerMissions has implemented a simple and flexible mechanism that enables developers to ask more easily to request and handle operating permissions.
The technical principles of the EasyPermissions framework mainly include the following aspects:
1. Running permissions request: EasyPermissions framework uses Android native running permissions mechanism.When the application requires request permissions, developers can use the annotations provided by EasyPerMissions to mark the required permissions.For example, the method of using @AterPerMissionGrantedes will be automatically executed after the permission request is successful.
2. permissions application process: EasyPermissions encapsulates the process of permissions application in the Java class library, so that developers can trigger permissions requests through a line of code.Specifically, EasyPerMissions first checks the Android version of the device. If the version of the device is lower than the Android 6.0, that is, no operating permissions are required, then the permission request will be considered success.If the version of the device is higher than or equal to Android 6.0, EasyPerMissions will use the PermissionDispatcher library to handle the permission request.
3. Permanent callback processing: When the authority request is successful or failed, the EasyPerMissions frame will call the corresponding callback method.Developers can rewrite the results of the permission request by rewriting these callback methods.In addition, EasyPerMissions also provides some convenient methods, such as ShouldshowRequestPerMissionrationAle method to determine whether the permission is required.
Below is a Java sample code using the EasyPerMissions framework:
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;
public class MainActivity extends AppCompatActivity
implements EasyPermissions.PermissionCallbacks {
private static final int REQUEST_CODE_CAMERA = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Request camera permissions
requestCameraPermission();
}
@AfterPermissionGranted(REQUEST_CODE_CAMERA)
private void requestCameraPermission() {
String[] permissions = {Manifest.permission.CAMERA};
if (EasyPermissions.hasPermissions(this, permissions)) {
// Get the camera permission, you can perform related operations
openCamera();
} else {
// Request camera permissions
EasyPermissions.requestPermissions(
new PermissionRequest.Builder(this, REQUEST_CODE_CAMERA, permissions)
.setrationale ("You need camera permissions to take pictures")
.setpositivebuttontext ("OK")
.setnegativebuttontext ("Cancel")
.setTheme(R.style.AppTheme)
.build());
}
}
private void openCamera() {
// Turn on the camera
// ...
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Transmit permissions Request results to EasyPermissions framework
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
// Performance requests successfully recovered
if (requestCode == REQUEST_CODE_CAMERA) {
openCamera();
}
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
// Permit request failure recovers
if (requestCode == REQUEST_CODE_CAMERA) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
// The user checked the "No Inquiry" option to display a dialog box to guide the user to apply the setting page manual grant permissions
showPermissionDialog();
} else {
// User rejection of permission request
Toast.maketext (this, "need camera permissions to take pictures", toast.length_short) .show ();
}
}
}
private void showPermissionDialog() {
// Display permissions dialog box
// ...
}
}
The above is the technical principles of the EasyPerMissions framework in the Java class library and a simple example code.Through EasyPerMissions, we can simplify the processing process of Android running permissions, thereby making more convenient permission requests and processing.