Android manually obtains judgment processing permission

Several methods are mainly used:

Permission constant ID:

Among the above four methods, the first three methods are available in activitycompat in support-v4. It is recommended to use the methods in the compatible library. The last method is that when a user authorizes or rejects a permission group, the system will call back the methods in activity or fragment.

1. checkSelfPermission(String)

Check permissions

1. Check the current status of a permission. You should check whether the permission has been authorized by the user when requesting a permission. Repeated applications for authorized permissions may annoy the user.

2. One parameter of this method is the permission name and has an int return value. Compare this value with the two constants mentioned above to judge the current status of the checked permission.

2. requestPermissions(int,String…)

Apply for permission

Request the user to authorize several permissions. After calling, the system will display a prompt dialog box requesting user authorization. App cannot configure and modify this dialog box. If you need to prompt the user for information or instructions related to this permission, you need to deal with it before calling requestpermissions()

The method has two parameters:

Int requestcode: will be returned when calling back onrequestpermissionsresult(). It is used to determine which authorization request callback it is.

String [] permissions: permission array, the array of permissions you need to apply for.

Because this method is asynchronous, it has no return value. When the user completes the authorization operation, it will call back the onrequestpermissionsresult() method of activity or fragment.

3. onRequestPermissionsResult(int,int[])

Processing permission result callback

1. This method should be overridden in activity / fragment. When the user completes the authorization operation, the system will automatically call back this method

2. The method has three parameters:

Int requestcode: the first parameter when calling requestpermissions().

String [] permissions: permission array, the second parameter when calling requestpermissions().

Int [] grantresults: authorization result array, corresponding to permissions. The specific values are compared with the two constants in packagemanager mentioned above.

4. shouldShowRequestPermissionRationale(String)

Should the request permission be displayed

1. When requesting permission for the first time, the user rejects it. After calling shouldshowrequestpermissionrational(), it returns true. It should show some instructions on why this permission is needed.

2. After the user rejects a permission for the first time, the "don't remind again" option will appear in the authorization dialog when he applies again. Once it is checked, the user will not be prompted for the next application.

3. When requesting permission for the second time, the user rejects it, selects the option of "no reminder", and returns false after calling shouldshowrequestpermissionrational().

4. The policy of the device prohibits the current application from obtaining authorization of this permission: shouldshowrequestpermissionrational() returns false.

5. The advantage of adding this reminder is that after the user has rejected the permission once, we can remind the importance of the permission when we apply again. When we have to apply again, the user checks "don't remind again" and refuses, resulting in the direct failure of the next application for permission.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

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