Understand the technical principles of the EasyPermissions framework in the Java class library
EasyPermissions is an open source framework for handling runtime permissions in Android.It uses the authority management function provided by the Android system, and provides an easy -to -use interface and packaging, making the processing of runtime authority more convenient and flexible.
The technical principles of EasyPermissions mainly involve the following aspects:
1. Dynamic application permissions: EasyPermissions uses Android permission application framework. By calling the request permissions provided by the system to display the user permissions application dialog box and wait for the user's response.According to the user's response (consent or rejection), EasyPermissions will call the corresponding callback method to make follow -up processing.
2. Permissions inspection and processing: EasyPermissions provides a series of convenient methods for inspection and processing permissions.By calling these methods, developers can determine whether a certain permissions have been authorized and the result of processing permission authorization.EasyPermissions also provides some callback methods to notify the developers for corresponding treatment when the authority authorization results change.
3. Permission authorization: In order to better handle the authority authorization results, EasyPerMissions abstract the authority authorization results into three states: granted, rejected and rejected and no longer ask.Developers can judge the user's authorization of a certain authority through these states and perform corresponding treatment.
Below is an example code that uses EasyPerMissions for dynamic permissions:
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);
// Check the camera permissions and apply
if (hasCameraPermission()) {
// Get the camera permission, you can perform the corresponding operation
openCamera();
} else {
// Apply for camera permissions
requestCameraPermission();
}
}
// Determine whether the camera permissions have been obtained
private boolean hasCameraPermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA);
}
// Apply for camera permissions
private void requestCameraPermission() {
EasyPermissions.requestPermissions(
this,
"Need camera permissions for taking pictures",
RC_CAMERA_PERMISSION,
Manifest.permission.CAMERA);
}
/ Third
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
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 permissions have been granted, and the corresponding operation can be performed
openCamera();
}
}
// Performance is rejected
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
if (requestCode == RC_CAMERA_PERMISSION) {
// The camera permissions are rejected, and the corresponding treatment can be processed according to the needs
}
// Check whether the user has chosen "No longer asking"
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
// The user chooses "No longer asking", and the pop -up window prompts the user to apply the settings to the page manual opening permissions
openAppSettings();
}
}
// Turn on the camera
private void openCamera() {
// Open the logic of the camera
}
// Open the application settings page
private void openAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
}
In the above example, first check the camera permissions. If the permissions have been obtained, call the `OpenCamera ()` method to perform the corresponding operation.If no permissions are obtained, call the `RequestCameraperMission ()` to initiate permission applications.The permission application results will be processed in the method of `OnPermissionsgranted () and` OnPerMissionsDenied () `.While the permissions are rejected, if the user chooses "no more inquiries", you can call the method of `OpenAppSettings (), jump to the application settings page to prompt the user to manually open the permissions.
In summary, the technical principle of the EasyPermissions framework is to provide a set of simple and easy -to -use interfaces by packaging the operating authority management function of the Android system to facilitate developers to handle the operation of permissions in the application.By using EasyPermissions reasonably, developers can handle operating permissions more flexible and efficiently to improve the user experience.