Android Runtime Permissions Tutorial With Example Programmatically

Android Runtime Permissions Tutorial With Example Programmatically is today’s topic.

We will learn to check/request/handle runtime permissions with easy and simple example source code.

From Android Version M (marshmallow), developer needs to ask for runtime permissions to the user.

Main purpose of runtime permission is to enhance the privacy of the user.

First let us understand some brief information about runtime permissions, it’s lifecycle and then we will develop example in android studio.

Android Runtime Permissions Concept

Earlier user used to grant these permissions at the time of the installation of the app.

This concept was less secure because user did not pay enough attention to the security purpose at the installation time.

Thus, google introduced the concept of the runtime permissions.

Runtime permissions means that developer will ask for every single permission hence, it will enhance the privacy as user will have to play with pop up dialog for each permission.

Pop up dialog will have two options, allow or deny. There is a one checkbox (never ask again) which allows user to prevent app for asking any particular permission in future.

Developer need to educate and inform his users about the need for permission requests properly.

Inappropriate permission requests, those that the user doesn’t fully understand, can result in users failing to grant a permission.

As a result, your app may be unable to deliver the user the features intended.

In the worst case, poorly requested permissions could result in users losing trust in your app and uninstalling it.

Two Types

There are mainly two types of permissions regarding to it’s risk level.

  1. Normal Permissions
  2. Dangerous Permissions

Normal Permissions

Normal permissions are considered as a low risky permissions. These permissions are less risky for user’s privacy or for other app’s operations.

For example INTERNET permission. Accessing internet is now a days very common concept. Almost every app need internet for smooth functioning.

Thus INTERNET is normal permission and developer do not need to ask it to the user at runtime. Just define it in Manifest file and you are good to go.

Some normal permissions :

  • ACCESS_NETWORK_STATE
  • ACCESS_WIFI_STATE
  • BLUETOOTH
  • CHANGE_NETWORK_STATE
  • SET_ALARM

Dangerous Permissions

These are the permissions for which you are reading this tutorial. These permissions are dangerous to user’s security, his device’s data and to the functioning of the other apps.

For example, CONTACT permission comes under the dangerous concept. With this permission, app can read the contacts of the user’s device and can also send them to server.

Here, you need to ask for runtime permissions, because accessing the sensitive information like contact require user’s attention.

Several Dangerous Permissions:

  • READ_CALENDAR
  • ACCESS_FINE_LOCATION
  • RECORD_AUDIO
  • USE_SIP
  • SEND_SMS

LifeCycle Of Runtime Permissions

This is the key part of the tutorial. I suggest you to read this below steps carefully. You can create more user friendly runtime structure if you know it’s entire lifecycle deeply.

For example let’s assume, we want to request camera permission from user.

  • Step 1 -> First of all, we will ask it with pop up dialog. If user grants permissions then you can implement camera feature.
  • Step 2 -> Other than granting the permission, user have other option to deny it. When user denies the permission we have to repeat the step 1 and this process continues.
  • Step 3 -> Last case is that user first checks the “Never Ask Again” checkbox and then denies the permission. Here you can not repeat the step 1. In this scenario, you need to create custom pop up dialog which will educate user about the importance of the permission. In this dialog, give user a button which will led him to the app settings, from where he can grant the camera or other permissions.

For the practical example of lifecycle, watch the below video which is also a output of our android studio project.

Developer needs to use this feature carefully, otherwise it may cause app crashes or it can led to bad user experience.

You can read google’s official documentation for proper usage of runtime permissions.

Now let us create an example in android studio to learn how to request runtime permissions.

Step 1. Adding in Manifest

First, create a fresh new project in the android studio. Choose empty activity while you are opening a new project.

Now, add the camera permission in the AndoridManifest.xml file using below line

Step 2. Changing Main Files

Copy the following source code in activity_main.xml file

I have taken only one button in the above source code.

Write down the following coding lines in MainActivity.java file

After adding above code, our example is over. Now let us see how compiler will execute this coding lines.

Diving In above code snippet

Consider the following snippet

  • Compiler will execute the above code when the user clicks the button.
  • Compiler will first check if the camera permission is already granted by the user or not.
  • If user has granted the permission, then a toast will be displayed. You can write your code for the app feature that is using camera here.
  • For checking if the user has granted the camera permissions or not, compiler will use permissionAlreadyGranted() method.

Code for permissionAlreadyGranted() method is as following

  • permissionAlreadyGranted() method will return true if permission is already allowed otherwise it will return false.
  • Now if the user have not granted camera permission yet, then compiler will request for runtime permission by using the requestPermission() method.

Below is the code for requestPermission() method

  1. When the compiler will execute the requestPermission() method, a pop up dialog will be shown to the user with allow and deny options.
  2. When the user selects any of the option, compiler will execute the onRequestPermissionsResult() method.

onRequestPermissionsResult() method is written as below

  1. In this method, compiler will decide whether user have clicked allow button or deny button.
  2. If the user have clicked allow button then it will create a toast saying that “Permission granted successfully.”
  3. Otherwise it will make toast saying “permission is denied!”. After this, compiler will check if the user have checked mark the checkbox saying “Never Ask Again” or not.
  4. If no, then again compiler will show the pop up dialog to request camera permission.
  5. If yes, then compiler will not be able to generate a pop up dialog again.

Here, we will call the openSettingsDialo() method.

openSettingsDialo() method is as following

  • We will create one alert dialog with message that tells the user about the importance of the permission.
  • This alert dialog has two options : TAKE ME TO SETTINGS and CANCEL
  • When user clicks the TAKE ME TO SETTINGS button, it will take him to the app settings where he can turn on the camera permission.

We have ask for single permission in this example but there will be times where you need to request multiple permissions.

For this, read Android Multiple runtime permission example

You will be able to ask for single as well multiple permissions with same source code. You just need to change permission array only.

There is one github library dexter which can simplify this process.

Read android Easy Runtime permission with dexter example

Thus, we have successfully request the runtime permissions. If you have any query then ask them in comments.

I will try my best to help you out in every possible way.

Download Source Code For Android Runtime Permissions Example

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *