Talk about the pitfalls of Android 6.0 dynamic permission application

During the day, I was doing the adaptation of the sdk23 version and encountered many pits. Now I take the time to write it down and take this as a warning.

First of all, to know which pits, you have to understand some definitions and basic usage.

Let's first introduce the permission grouping of dynamic application.

The following permission group is officially defined by Google. The purpose is that when applying for permission, as long as the user allows any permission of the same permission group, other permissions of the group are allowed by default. However, according to the expert, it is best to specifically request the permission according to the permission used, because Google will change or even delete the permission group as soon as it is happy

In fact, the definition of permission group is very simple. The following is a brief introduction to the steps of dynamic permission application.

The first step is to check the permissions of the app.  

Step 2: apply for permission.

The third step is the callback method of permission application.

Such three steps seem very simple, but it's not so simple to use. Let's talk about the pit here.

1. Permission application can only be in the context of activity or fragment, and getapplicationcontext () cannot be used.

Because our project is to get memory paths and create a series of file caches when initializing applications, these operations are written in different forms of Util tools in Application (onCreate ()), so writing above Android6.0 is a bit less reliable. At present, my solution is to judge the SDK version first during application initialization, and only create cache files for apps with version number less than 23. For apps with version number higher than 23, initialize after entering activity.

2. The request code used in permission application must be less than 16.

As for what reason is not clear, Google may think that the permission is not much, so there is no need to make the request code large and occupy excess memory. When it comes to the request code, that is, the constant value request not defined in the above code_ CODE_ PERMISSION_ Contacts, if the value you define exceeds 15, a security exception will be reported at runtime, and the prompt request code must be less than 16.

At present, these two pits are enough for me to work all day. It seems that I still lack experience. I need to learn more experience in the future.

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