How to apply various permissions gracefully after Android 6.0

Foreword:

Recently, I made an APP about maps in the company, so I needed to apply for the location permission of the map. I started to write the application permission in the AndroidManifest.xml file like crazy. After a series of error reports and Baidu, I found the problem, so I wrote it down. This blog also gives a reminder to myself and my friends who are preparing to mine the pit. (For those who are not familiar with the Android permission mechanism (yes, including me), the big ones can be ignored).

Introduction:

Android's permission system requires developers to apply manually since 6.0. These permissions generally involve the user's privacy, so user authorization is required, such as reading internal and external memory cards, cameras, photo albums, recordings, positioning, etc., although The amount of code is not much, but it is also troublesome, so I wrote a tool class, please see the code below. (Because I don’t need much code, I only wrote the permission application for positioning. I hope everyone understands. I believe that if you read my blog carefully, it will not be difficult to obtain other permission applications).


Apply for location permission code block

write picture description here
If you need to replace or apply for another permission, you only need to replace the ACCESS_COARSE_LOCATION location permission with another permission, (there is no other example here).


If the current user does not authorize the permission or the user refuses to authorize, a dialog box will pop up to let the user know that this permission is a necessary permission, and the user needs to manually open the system application. The code is as follows
write picture description here

I believe that these codes do not need to be explained too much, just copy them!


At this time, there is a lack of a method for authorization authorization callback processing. The code is as follows. Please note
write picture description here
this code, this callback method can only be called if it is written in the MainActivity class. If it is to be written in the tool class, then the MainActivity class must be If the utility class is inherited, it must not be called. Because this method is an overridden method of Activity, if it is written in a tool class and MainActivity does not inherit the tool class, it will be invalid if you force a new object in MainActivity to call this method.


Summarize

The above is all my code for how to dynamically apply for permissions for Android 6.0. If you don’t understand and have questions, you can leave a message and send a private message. In addition, I would like to recommend a wave of Daniel https://my.csdn.net/qq_34379015 A very awesome Android , Java God, he is also writing a blog like crazy recently, everyone may wish to pay attention to it. (By the way, if you think my blog is good, remember to like it!!)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763761&siteId=291194637