In my current project at work, I had a task to add run time permissions in an android app whose code is very old and using legacy methods and frameworks/tools. Normally, I use Ted Permissions in all my apps for the runtime permissions and I must say that it’s one hell of an amazing library I ever saw and given the complex scenario and flow of runtime permissions in Android (thanks to Google who always makes sure to make every thing more complicated than ever), this library makes the runtime permissions like a breeze. Wonderful job Ted Park.
Android introduced Runtime Permissions in API Level 23 (Android Marshmallow 6.0) or later versions. The permissions has a very complicated flow to implement as shown in the below image:
Today, when I was looking for very simple approach to do this, I was shocked to see that how Google has made this simple thing so much complicated and confusing to implement just like how they have done with Camera APIs. It took me a whole day just to add two simple runtime permissions at the start of my code. Here’s how I have done it.
IMPORTANT: You should not request all permissions at once. Rather you should request the concerning permission only at the time when its supposed to be used. I am requesting all the permissions at start because our code base is very old and complicated.
My app needed Storage and Location permissions. So, add these permissions in the Android Manifest file like this:
Now, as soon the app starts, it asks for both (Location and Storage) permissions. If both permissions are granted, then the app proceeds normally with the usual functionality. We can do it in onCreate() method of the our first Activity like the below:
Please note in the above code that I have created a String array which contains all the permissions we will request here. Now, let's implement the checkAndRequestPermissions() method.
As we can see in the code above, we can check status of any permission with using ContextCompat.checkSelfPermission() method. And we can request any permission with ActivityCompat.requestPermissions() method. These methods are already included in support library.
Now, if the listPermissionsNeeded is not empty, that means that at least one or more permissions have been denied. So, requestPermissions() method will show the permission dialog and will invoke onRequestPermissionsResult() callback method
Now, here’s how the whole case goes after requesting permissions. First time, android will show the permissions normally. If all are accepted, then everything goes normally. If any one or all are denied, then it shows the explanation dialog (created by developer) to tell the user why permissions are important. And this dialog will allow the user to grant permissions again. Now, android will show the permission dialog again but this time with an extra checkbox “Never ask again”.
If any of the permission has been denied with “Never ask again” check, then that permission can only be allowed from the Settings of the app. Since my app cannot work without these permissions, so I will show another dialog which will allow the user to go to Settings screen of the app and allow to grant permissions manually.
We can do so with this another method ActivityCompat.shouldShowRequestPermissionRationale(context, permission), which keeps the record for "Never ask again" check. If this method returns true, that means that Android will show the request permission dialog. And if false, Android will not show the dialog as user has checked "Never ask again". So this is where you will have to redirect the user to the Settings -> Permissions screen to allow the user to grant permissions manually.
Now, Let’s see the code in onRequestPermissionsResult() method. I have put some comments as well, so this should be self-explanatory.
And here’s the showDialog() method. Its just for creating AlertDialog.
I hope this helps in your app development and solves the problem of runtime permissions.
Wajahat Karim is a graduate from NUST, Islamabad, an experienced mobile developer, an active open source contributor, and co-author of two books Learning Android Intents and Mastering Android Game Development with Unity. In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on blog and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on Github Trending. His libraries have about 2000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and Medium to get more updates about his work in Writing, Android and Open Source.
Also, if you have any questions you’d like him to answer, contact him through his website at wajahatkarim.com with DEAR WAJAHAT in the subject line.