Multiple Runtime Permissions in Android Without Any Third-Party Libraries
Tags: android, java, androiddev, todayilearned
Published: 2018-11-24 05:55:31

---
title: Multiple Runtime Permissions in Android Without Any Third-Party Libraries
published: true
cover_image: https://cdn-images-1.medium.com/max/720/0*-eNsGIwHpq_VrpUc.png
tags: android,java,androiddev, til
canonical_url: https://medium.com/mindorks/multiple-runtime-permissions-in-android-without-any-third-party-libraries-53ccf7550d0
---

#### Learn about adding multiple runtime permissions in this step-by-step tutorial

> This article is part of [Today I Learned](https://medium.com/p/65c64e1dcb6) series and was originally posted at my [TIL Github Repository](https://github.com/wajahatkarim3/Today-I-Learned/) and my website at [wajahatkarim.com](http://wajahatkarim.com)

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](https://github.com/ParkSangGwon/TedPermission) 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](https://medium.com/u/ecac7eac58cd).

[ParkSangGwon/TedPermission](https://github.com/ParkSangGwon/TedPermission)

Android introduced [Runtime Permissions in API Level 23 (Android Marshmallow 6.0) or later versions](https://developer.android.com/guide/topics/permissions/overview). The permissions has a very complicated flow to implement as shown in the below image:

![](https://cdn-images-1.medium.com/max/919/0*LuQugCqLUU26Vwp-.png)<figcaption>Image Credit:<a href="https://github.com/ParkSangGwon/TedPermission"> Ted Permissions</a></figcaption>

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:

![](https://cdn-images-1.medium.com/max/1024/1*Cr30LVf1II7JC0I5TPD3Iw.png)

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:

![](https://cdn-images-1.medium.com/max/1024/1*VAGMcWntbgrR85F8Olh-nw.png)

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.

![](https://cdn-images-1.medium.com/max/1024/1*ZrHn5aQ8hLx4nyp8_YGN6Q.png)

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

![](https://cdn-images-1.medium.com/max/276/0*eP-yGS-wucHZ9F3A.png)

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”.

![](https://cdn-images-1.medium.com/max/276/0*kjeyFWB2bErkbtVA.png)

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.

![](https://cdn-images-1.medium.com/max/320/0*fJa0II29-vyh8mAC.png)<figcaption>Permissions screen for the Chrome on Android</figcaption>

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.

![](https://cdn-images-1.medium.com/max/1024/1*mQ0JXufFma_b6Q7Br5GXZg.png)

And here’s the showDialog() method. Its just for creating AlertDialog.

![](https://cdn-images-1.medium.com/max/1024/1*BIs-o_7sy3xmShSfJ50vCA.png)

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](https://www.amazon.com/Learning-Android-Intents-Muhammad-Usama/dp/1783289635) and [Mastering Android Game Development with Unity](https://www.amazon.com/Mastering-Android-Game-Development-Unity/dp/1783550775/). 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](https://github.com/trending). [His libraries](http://github.com/wajahatkarim3) 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](https://medium.com/@wajahatkarim3) to get more updates about his work in Writing, Android and Open Source.

[Wajahat Karim (@WajahatKarim) | Twitter](https://twitter.com/WajahatKarim)

Also, if you have any questions you’d like him to answer, contact him through his website at [wajahatkarim.com](http://wajahatkarim.com) with DEAR WAJAHAT in the subject line.

* * *