Definition and application of application permission of Android system programming introduction series

When saving the data in the application as a file locally, it was mentioned that the application needs to apply for the read-write permission of the external storage device to access the files in the external storage. How should an application apply for a certain permission? This article will introduce it in detail.

Permissions in applications are mainly divided into two categories: normal permissions and dangerous permissions. Before Android 6.0, that is, API 23, these two permissions only need to be declared in the list file. Since Android 6.0, that is, API 23, dangerous permissions not only need to be declared in the list file, but also need to be dynamically applied in the activity interface where the code uses the permission. The permission application box pops up, and the user decides whether to authorize or not. The permission list and authorization results required by the application can be viewed from system settings - Application Management - permission management.

Most of the permission settings are configured in the list file. They need to be configured in the code only when dynamically applying for or adding additional permissions to interact with the four components.

If an application needs to use certain permissions, it must declare them in its manifest file.

Use the tag < uses permission / > in the manifest file and assign a value to its attribute Android: name. Different permissions define the corresponding string values. These different permissions can be obtained from @ L_ 403_ 1 @ view in permission class.

Starting from Android 6.0, API 23, dangerous permissions need to be dynamically applied and actively authorized by the user before continuing to perform the authorized operations. Otherwise, if relevant operations are performed without authorization, the program will throw a java.lang.securityexception exception when running.

For dynamically applied permissions, you also need to use the context environment object to complete the authorization related operations. At the same time, due to the upgrade of Android system library, the relevant classes involved below can be found in the old system support library android.support.v4, and the corresponding classes can also be found in the new version of android.core support library.

Dynamic permission application is mainly divided into three steps: check, request and result callback.

Check mainly aims at two directions. One is to check whether the application has obtained relevant permissions. Call the static method contextcompat.checkselfpermission (context context, string permission) and pass in the context environment object and the fixed string of related permissions as parameters respectively. Returns the result of type int, indicating whether it is authorized. Its value defines the authorized permission in the form of static constant in android.content.pm.packagemanager class_ Granted = 0 and unauthorized permission_ DENIED=-1。

If the check result of permission is authorized, you can perform subsequent operations to obtain the permission. When the result is unauthorized, you need to continue to check whether the current permission can show the request authorization interface to the user. Call the static method of activitycompat.shouldshowrequestpermissionrational e (android.app.activity, Java. Lang.string). The parameter activity is the current activity interface object, and the parameter permission is the constant of the related permission string. Returns a boolean result indicating whether the request authorization interface can be displayed normally.

If the result of checking the display request authorization interface fails, you need to prompt the user that the relevant permissions cannot be authorized normally. Usually, you will be prompted that you can open the relevant permissions of the application in system settings - permission management to normally perform the operations after the application is authorized. When the result returned from the inspection display interface is true, you can continue to request the permission. Call the activitycompat.requestpermissions (activity activity, int requestcode) static method. The parameter activity is the current activity interface object, the parameter permissions is an array composed of multiple permission strings, and the parameter requestcode is the current request value, which can be defined arbitrarily. At the same time, the value is consistent with the corresponding value when the request result is returned.

Finally, the callback of the request result. In the activity interface of requesting permission, rewrite the method public void onrequestpermissionsresult (int requestcode, int [] grantresults) {}. After the user chooses to approve or reject authorization, the system calls back this method. The parameter requestcode is the request value, which is consistent with the parameter when requesting permission; The parameter permissions is a related permission array, which is also consistent with the parameters when requesting permission; The parameter grantresults is the authorization result of the user. Its array index corresponds to the index in the parameter permissions one by one. The value also indicates the authorized permission_ Granted = 0 and unauthorized permission_ DENIED=-1。

After all permissions returned from the request result have been authorized, the edge can perform subsequent operations to obtain relevant permissions. If you have unauthorized permissions, you usually perform abnormal operations, such as giving the user a corresponding prompt and no longer performing normal subsequent operations.

In addition, if the user chooses to refuse authorization for a permission before the permission check operation returns, the user will not be prompted again,

Before Android 12, you can refer to the above methods for the use of permissions in the application. Then, the specific permissions provided by the system and what operations can be done after obtaining these permissions will be introduced in the following articles.

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
分享
二维码
< <上一篇
下一篇>>