A translated foreign article. The name of Android M was officially announced not long ago, and the final official version is coming soon! Android is constantly evolving, the latest update M is very different, some major changes like runtime permissions will have disruptive impact. Surprisingly, the Android community doesn't talk much about this, even though it's important and may cause serious problems in the near future. That’s why I’m writing this blog post today. Here’s everything you need to know about Android runtime permissions, including how to implement them in your code. It’s not too late to fix it now. New runtime permissions Android's permission system has always been a security-first concept, because these permissions are only asked once during installation. Once installed, the app can access everything within the permission without the user knowing. No wonder some bad guys exploit this flaw to maliciously collect user data and do bad things! The Android team knows this, too. After 7 years, the permissions system has finally been redesigned. In Android 6.0 Marshmallow, apps will no longer be granted permissions when they are installed. Instead, apps will have to ask the user for permissions one by one at runtime. Paste_Image.pngNote that the permission inquiry dialog box will not pop up automatically. The developer has to call it by himself. If some functions that the developer wants to call require certain permissions and the user refuses to grant them, the function will throw an exception and directly cause the program to crash. Paste_Image.pngIn addition, users can also cancel authorized permissions in the settings at any time. Paste_Image.pngYou may have felt a chill in your back... If you are an Android developer, this means you have to completely change your program logic. You can no longer call methods directly like before, you have to check permissions for each place you need, otherwise the app will crash! Yes. I can't fool you into thinking it's easy. Although it's great for users, it's a nightmare for developers. We have to change the code or there will be potential problems in both the short and long term. This new runtime permission only works when we set targetSdkVersion to 23 (which means you have tested it on 23), and of course it has to be an M phone. Apps on devices before 6.0 still use the old permission system. What happens to apps that have already been released? The new runtime permissions may have made you panic. "Hey, man! What will happen to my app that I released three years ago? If it is installed on Android 6.0, will my app break?!?" Don't panic, just relax. The Android team is not stupid, they must have considered this situation. If the targetSdkVersion of the app is lower than 23, it will be considered that the app has not been tested with the new permissions of 23, and the old rules will continue to apply: the user has to accept all permissions when installing, and the app will have those permissions after installation! Paste_Image.pngThen the app runs as before! Note that the user can still cancel the authorization they have agreed to! When the user cancels the authorization, the Android 6.0 system will warn, but this does not prevent the user from canceling the authorization. Paste_Image.pngThe question is, does your app crash at this time? #p# The well-meaning owner also told the Android team about this. When we call a function that requires permission in an app with a targetSdkVersion lower than 23, if the permission is revoked by the user, no exception will be thrown. However, it will do nothing, resulting in the function return value being null or 0. Paste_Image.pngDon’t get too excited. Even though your app won’t crash when calling this function, a return value of null or 0 may still cause a crash later. The good news (at least for now) is that this kind of revocation of permissions is rare, and I believe that few users would do this. If they do, it is at their own risk. But in the long run, I believe there will still be a large number of users who will turn off some permissions. It is unacceptable that our app can no longer run perfectly on new devices. How to make it work perfectly? You'd better modify the code to support the latest permission system, and I suggest you start doing it right away! If your code has not been successfully modified to support the latest runtime permissions, do not set targetSdkVersion 23 when releasing the app, otherwise you will be in trouble. Only change targetSdkVersion 23 after you have tested it. Warning: When you create a new project in Android Studio, targetSdkVersion is automatically set to 23. If you don't support the new runtime permissions yet, I recommend you first downgrade your targetSdkVersion to 22. PROTECTION_NORMAL class permissions When a user installs or updates an app, the system grants the app all permissions requested that are PROTECTION_NORMAL (a basic set of permissions granted at install time). These permissions include:
Just declare these permissions in AndroidManifest.xml and authorize them during installation. No need to check permissions every time you use the app, and users cannot revoke the above authorizations. Make your app support new runtime permissions It’s time to make our app support the new permission model, starting with setting compileSdkVersion and targetSdkVersion to 23.
For example, I want to add a contact using the following method.
The above code requires the WRITE_CONTACTS permission. If you don't ask for permission, the app will crash. Next, add the permissions declaration in AndroidManifest.xml as before.
Next, you have to write a method to check if there is permission. If not, a dialog box will pop up to ask the user for permission. Then you can create a contact in the next step. Permissions are grouped as shown in the following table: Paste_Image.png Once any permission in the same group is granted, the other permissions are automatically granted as well. For example, once WRITE_CONTACTS is granted, the app also has READ_CONTACTS and GET_ACCOUNTS.
If the permission is already granted, insertDummyContact() will be executed. Otherwise, requestPermissions will be executed to pop up the authorization request dialog box, as follows: Paste_Image.png#p# Regardless of whether the user agrees or refuses, the activity's onRequestPermissionsResult will be called back to notify the result (via the third parameter), grantResults, as follows:
This is how the new permissions model works. The code is really complicated but you just have to get used to it. . . In order to make your app compatible with the new permissions model, you have to use similar methods to handle all the necessary cases. Handling "Don't remind me" If the user refuses an authorization, the next time a pop-up window appears, the user will have an option to "not remind me again" to prevent the app from requesting authorization in the future. Paste_Image.pngIf this option is checked by the user before denying authorization, the next time you requestPermissions for this permission, the dialog box will not pop up, and the result is that the app will do nothing. This will be a very bad user experience, as the user performs an operation but does not get a response. This situation needs to be handled properly. Before requesting requestPermissions, we need to check whether we need to display a prompt for requesting permissions through the activity's shouldShowRequestPermissionRationale. The code is as follows:
The dialog we wrote is shown when a permission is first requested and the user has marked not to be asked again. In the latter case, onRequestPermissionsResult will receive PERMISSION_DENIED and the system query dialog box will not be displayed. Paste_Image.pngDone! Requesting multiple permissions at once Of course, sometimes you need multiple permissions, you can use the above method to request multiple permissions at once. Don't forget to check the "Don't remind me again" setting for each permission.
If all permissions are granted, onRequestPermissionsResult is still called back. I use hashmap to make the code neat and easy to read.
The conditions are flexible, you can set them yourself. In some cases, if a permission is not granted, it will not be available; but in other cases, it can work, but the performance is limited. I will not comment on this, you can design it yourself. #p# Use compatibility libraries to make your code compatible with older versions The above code runs fine on Android 6.0 and above, but it doesn't work before 23 API because there are no such methods. The crude way is to check the version
But it is too complicated. I suggest using the v4 compatible library, which has been compatible with this. Use this method instead:
The last two methods can also be used in Fragment, using the v13 compatibility package: FragmentCompat.requestPermissions() and FragmentCompat.shouldShowRequestPermissionRationale(). The effect is the same as activity. Third-party libraries simplify code The above code is really complicated. To solve this problem, many third-party libraries have been released, which are really awesome and fast. I tried many and finally found a satisfactory one, hotchemi's PermissionsDispatcher. What will happen if my app is still open and the permissions are revoked? Permissions can be revoked at any time. Paste_Image.pngWhat happens when the app is open and is terminated? I tried this and found that the app is terminated. Everything in the app is simply stopped because it is terminated! This makes sense to me because if the system allows it to continue running (without certain permissions), it will summon Freddy into my nightmares. Or worse... Conclusion and Recommendations I believe you have a clear understanding of the new permission model. I believe you also realize the seriousness of the problem. But you have no choice. The new runtime permissions have already been used in Marshmallow. We have no way back. The only thing we can do now is to make sure that the app adapts to the new permission model. Fortunately, only a few permissions require a runtime permission model. Most commonly used permissions, such as network access, belong to Normal Permission and are automatically granted during installation. Of course, you have to declare them and no need to check them later. Therefore, only a small part of the code you need to modify. Two suggestions: 1. Take the new permissions model seriously 2. If your code does not support the new permissions, do not set targetSdkVersion 23. Especially when you create a new project in Studio, don't forget to modify it! Let’s talk about code modification. This is a big deal. If the code structure is not designed well enough, you need some painful refactoring. Every app needs to be corrected. As mentioned above, we have no choice. . . List all the permissions you need to request, and what happens if A is granted and B is denied. blah, blah. Good luck with the refactoring. Make it a priority that you need to do, and start working on it now to ensure that there are no issues when M is officially released. I hope you found this article useful, and happy coding! |
>>: Facebook Internal Efficient Work PPT Guide
Compared to the 1950s and 1960s, when there were ...
Learn Bazi from scratch, learn Bazi Jiugongge fro...
As the undisputed top player in China's onlin...
The 400 telephone number is a relatively popular ...
I found that being in love makes people ugly. Tak...
one. Product Information 1. Product Name: WeChat ...
5G messaging, which is regarded by the industry a...
What is a hot product? What are the factors that ...
Zhu Guomiao's pelvic and sacroiliac joint fun...
How much is the quotation for automatic ordering ...
Last December, I went to Suzhou with a friend. Wh...
Can an e-commerce business license be used to ope...
In the first half of 2014, domestic sales of flat...
Recently, the business direction of the docking h...
In my sleep, I was awakened by the pain of calf c...