Multiple Runtime Permissions in Android Without Any Third-Party Libraries

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.

ParkSangGwon/TedPermission

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:

Image Credit: [Ted Permissions](https://github.com/ParkSangGwon/TedPermission)

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.

Permissions screen for the Chrome on Android

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.


If you liked this article, you can read my new articles below:


profile card
Wajahat Karim
🌍 Making the world a better place, one app at a time.
🔥 Google Developer Expert (GDE) in Android . 📱 Professional Android Developer with ~10 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.
Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan