Android Runtime Permissions Request Tutorial

Android Runtime Permissions - Featured

Android M introduced a new concept of permissions model with its release. According to this new concept now apps would have to request permissions from the users at run time. In a way this is a very useful feature for the end users. Making their user experience very secure on an Android device. But inversely this feature imposes a great deal of effort on development. As the new permissions model also allows the user to deny the permission at runtime. Therefore as developers, we would have to handle all the use cases. Thankfully, requesting Android runtime permissions, is not that difficult, with a few lines of code we can build a full fledged permission handling mechanism. But before doing so lets understand the permission model first.

Understanding Android Run Time Permissions Model

Android system permissions are divided into many categories. But when it comes to permission request at runtime from Android 6.0 onward, system permissions are categorized into two categories. Normal and Dangerous, to access either of these two permissions, the first step is to declare them in the manifest, as shown below. The difference between these two is that, dangerous permissions are now granted by the user at run time. Otherwise your app would crash with security exception. On the other hand we don’t need to request permissions falling under the normal category.

In the above android manifest, first permission android.permission.INTERNET is a normal permission and other two android.permission.READ_CONTACTS, android.permission.WRITE_EXTERNAL_STORAGE are dangerous permissions. Therefore in the following example, we would request the user to grant the later two Android permissions at runtime.

Android Runtime Permissions

There is another special category of permissions having permissions like SYSTEM_ALERT_WINDOW and WRITE_SETTINGS. Although these permissions are neither categorized in normal or dangerous section, but still it is required that these are requested by user at runtime by firing an intent (not discussed in this article).


Requesting Android Runtime Permissions

From Android 6.0 onward, declaring a permission in manifest will not enable an app to use that permission. In a way its just a way to notify the system that your app can use this permission. But to actually use the permission you would have to make an Android runtime permission request. As mentioned above, in this example we would be requesting two permissions android.permission.READ_CONTACTS, and android.permission.WRITE_EXTERNAL_STORAGE. Usually a permission should be requested only when it is required. Hence when a permission is required in your code, please initiate the permission request process by using the following code:

This would initiate an Android runtime permissions request. If you have the permission, myMethod() would be called. If you have been denied or granted the permission by user action, then the onRequestPermissionResult() method would be called:

This is an additional method which you need to implement in your activity, to receive the Android runtime permissions request callback. Once the callback is received in the above method, and if the permission is granted you can call the same myMethod() to invoke the desired functionality. Also here in the above method we have taken care of the situation in which the permission is not granted. We would ask the user to launch the settings screen and enable permissions manually.


Android M Runtime Permissions – Design Pattern

By going through the above code you must have understood, requesting Android runtime permissions is very easy. But the implementation of this methodology in your existing code base could be a very time taking activity. Therefore a design pattern is required, where we can write the permission request code once and use it again and again.

To do so lets design an Abstract activity class, where the onRequestPermissionResult() method can be implemented. This would enable us to deliver permission granted callbacks to the child activity by using an abstract method, resulting minimal code change.

To request Android runtime permissions we would have to extend the above activity and call its requestAppPermissions() method, as shown below:

In the above piece of code, we took the full advantage of an abstract class. Where we made all Android run time permission requests in the parent class, and called the abstract method onPermissionsGranted() of that class, to return callbacks to the child activity. Full source code available here:

Source Code


This way, when implementing Android runtime permissions model, to support Android M and above, minimal code needs to be written in the existing activities. As the permission request would be made through the parent class method requestAppPermissions(), and results would be propagated back by the onPermissionsGranted() method. Although if you have already implemented a base activity pattern in your app, implementing this pattern would be very easy, as the same class would be reused to implement this pattern. Hope this helped. For more updates please connect with us on Facebook, Twitter and Google+.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

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

5 thoughts on “Android Runtime Permissions Request Tutorial