Runtime Permissions in Android app

Hey Technoz, hope you are doing well. In android, we usually need the permissions to perform some of the tasks. We need to list these permissions in our manifest file. However, from android 6.0 Marshmallow, we have to explicitly ask the user for permissions at runtime, as they are not given by default. So in this tutorial, we will see how to ask user for the permissions at runtime.

Understanding permissions

As you are here, probably you know what are permissions in android. However, android has divided the runtime permissions in two types namely normal permissions and dangerous permissions.  Normal permissions are the ones which does not deal with user’s personal data, while dangerous permissions are the ones which requires access to user’s sensitive data like read/write access of data, send sms, etc. The android OS grants the normal permissions the first time user install the app, while the dangerous permissions require to ask user explicitly, that’s what we are going to learn in this tutorial.

Declaring Permissions

First of all, start a new android studio project or if you wish, you can continue with your existing one. In this tutorial, we are asking the runtime permissions in android app – specifically the storage permission. So, open the AndroidManifest.xml and declare the following storage permission in it.

Now, come to your layout file in my case, activity_main.xml. we are simply putting simple Textviews and a Button to request permissions.  Lets put the following code in it.

As you can see above code, the layout is quite simple.

Building Logic for runtime permissions in android app

Lets move towards the MainActivity.java file where we will implement all the intended logic.

In the above file, at top, we have declared the static integer for the PERMISSION_REQUEST_CODE, which is same throughout the app for convenience. You can assign any value to it. Later, we are simply referring the button in layout and handling the onclick event so that it should call the method checkPermission(). We have some methods in above file, let me explain them one by one what they do in a task of getting runtime permissions in android app.

    • checkPermission(): This is a boolean method which checks whether the permissions are already granted or not and returns the relevant result. The checkSelfPermission() method is used to check for the permissions which takes two arguments as application context and second one is a permission. Note that, if you require user to grant more than one permission, then you have to apply this method for each permission individually.
    • requestPermission(): This method performs the task of getting permissions from the user by showing the appropriate message to user. You do not need to design the UI for asking permissions, android does that for you. This method takes  arguments as context, a string array of permissions, and the permission request code respectively. You can declare more than one permissions the the string array here. However, the PERMISSION_REQUEST_CODE remains the same.
    • onRequestPermissionsResult(): This method overrides from the superclass, and is called automatically after the requestPermission() call to know the result of permissions. This method accepts three parameters as request code, array of permissions and an array of results of respective permissions. We are checking if the user has granted the permissions. If yes, then we will show a Toast confirmation message and if not granted, then we are building an AlertDialog requesting to grant the permissions.

See the Results

Once the user grants permissions, then you can use that feature anytime in future. Android grouped the permissions together in such a way that similar tasks do not need permissions again and again. For ex. Once user grants the storage permission, then the app can read, write and modify contents in storage regardless of asking separately for reading, writing and modifying permissions.

Please note that the user can revoke the permission anytime for the app. So, it is good practice to always check for the runtime permissions in android app when performing the task that requires dangerous permissions.

That’s it! this simple code will do our intended work. Lets see the below screenshot of how the permission will be asked.

Runtime permissions in android

If you want to jump directly on the implementation, then feel free to download the source code from the below link.

Download Source Code

Hope you liked this simple and useful tutorial. If you have any queries or difficulties implementing this tutorial, then please let me know in the comment box below. I will try my best to answer you. If you find this tutorial useful, then please do share it with your friends and subscribe to our email newsletter to get notified about such useful tutorials in your inbox. Happy Coding. 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.