Android easypermissions official library efficient processing of permissions related tutorials

Introduction:

Easypermission library is a library that simplifies basic system permission logic and can be used on Android m or later.

1 relevant documents

Official documents:

https://github.com/googlesamples/easypermissions

Explanation of official documents of runtime permissions:

https://developer.android.com/training/permissions/requesting.html

2 why use easypermissions

Android m has changed the permission system. The permissions required before Android m only need to be applied in Android manifest. However, after updating the Android M system, some "dangerous permissions" need to ask for applications at runtime.

Import easypermissions dependency Library:

Add the following code in the build.gradle of the module.

After the dependency library configuration is completed, it can be used.

Use steps

1. Use easypermissions to check permissions:

Permissions can be individual or columns. In the easypermission library, use easypermissions #haspermissions (...) to check several permissions.

The first parameter: context parameter. For example, activity object.

The second parameter: some series of permissions. For example, public final static string [] perms_ WRITE ={Manifest.permission.WRITE_EXTERNAL_STORAGE};

2. Use easypermissions to request permission:

After checking, it is found that the user has not given permission. At this time, the code needs to request permission for the user's consent.

In the easypermission library, use easypermissions #requestpermissions to request permissions.

The first parameter is the context object, and the second parameter is the text prompt on the permission pop-up window. Tell the user the purpose of this permission. The third parameter: the unique identifier of the requested permission, code. The fourth parameter: some series of permissions.

3. Easypermissions response request result:

After the request, the system permission pop-up window will pop up, and the rest is whether the user authorizes the operation. The permission result is called back to the overridden onrequestpermissionsresult() method in the activity or fragment.

Pass the permission result to easypermissions. Onrequestpermissionsresult() for processing

Easypermissions. Onrequestpermissionsresult() method:

The first parameter: the code of the request, the second parameter: the request permission of some columns, the third parameter: the result of user authorization, and the fourth parameter: the permission callback listener. There is no need to manually judge whether the permission is successful, but will respond in the easypermissions.permissioncallbacks listener.

Easypermissions.permissioncallbacks interface:

onPermissionsGranted():

The user authorization is successful. Next, perform the required logical operations

onPermissionsDenied():

User authorization failed, processing failed logic.

Note: in the permission pop-up window, the user checks' never ask again. 'or' no prompt ', and rejects the permission. At this time, you need to jump to the setting interface and let the user start it manually.

You may not be satisfied and need to call logical processing methods manually. The easypermissions library has been considered for you and is powerful. When the user agrees to the permission, it can directly call the methods of relevant logic code without using the listener. You only need to annotate the method of logical processing with the @ afterpermissiongranted annotation.

4. @ afterpermissiongranted annotation:

@The afterpermissiongranted annotation can be added or not for convenience. It is optional.

Benefits:

With this annotation, when the permission request is approved by the user, it will be executed according to the request code, and the corresponding method with @ afterpermissiongranted annotation.

It simplifies the operation process of successful request. It is not necessary to manually call the method of successful request in the callback listener of easypermissions. Onrequestpermissionsresult(), and the logic code to be operated after obtaining the permission.

The following code requests the permission to write to the disk. When the user agrees to the permission, a toast pop-up window will pop up for logical processing.

When the user agrees to the permission, this method does not need to be called manually and will match the constant.write_ PERMISSION_ Code request code, automatically executed.

5. Use easypermissions to handle permission denial:

In the permission pop-up window, the user may directly refuse permission. The permission pop-up window will still pop up next time the permission request is made. In addition, you can also check 'never ask again.' or 'no prompt' and refuse permission. The pop-up window cannot pop up and allow the user to authorize the next request for permission. At this time, you need to jump to the setting interface and let the user start it manually.

In the easypermission library, use easypermissions. Somepermissionpermanentlydenied() to process whether to check the option that will not be prompted again.

The above code is that when the permission pop-up box cannot be popped up, you can directly jump to the setting interface and let the user manually open the permission.

When returning from the setting interface, the result will respond with onactivityresult() in the activity or fragment.

The operation effect is as follows:

summary

The above is a tutorial about the efficient processing of permissions in the official Android easypermissions library introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>