Android Runtime Permissions Example

Welcome to android runtime permissions example. With the introduction of Android 6.0 Marshmallow, Google has changed the way permissions are handled by the app. In this tutorial we’ll look into the new android runtime permissions that are introduced and how to handle them. If not handled properly, it can cause application crashes.

What are Android Runtime Permissions?

With the introduction of Android 6.0 (SDK 23), users are prompted for some specific permissions at runtime when they become necessary to use.

So the first question that comes to our mind is – Will the older apps run on Android Marshmallow? The answer is yes if the targetSdkVersion is 22 or less.

Thus android runtime permissions support backward compatibility. Now this doesn’t mean that we can work with old model of permissions by setting the sdk version to 22. A user using Marshmallow can revoke the dangerous permissions (we’ll discuss the dangerous and normal permissions later) from the Settings->Apps->Permissions. In the case we try to call some function that requires a permission which user has not granted yet, the function will suddenly throw an Exception(java.lang.SecurityException) that will lead to the application crashing. Hence we need to implement this new android permissions model in our application.

Dangerous and Normal android permissions

Android defines some permissions as dangerous and some as normal. The common thing in both the types is that they need to be defined in the Manifest file.

From Android 6.0 only dangerous permissions are checked at runtime, normal permissions are not. An example of a normal permission is android.permission.INTERNET.

Dangerous permissions are grouped into categories that make it easier for the user to understand what they are allowing the application to do. If the user accepts one permission in a group/category they accept the entire group. An example of dangerous permission is android.permission.FINE_LOCATION and android.permission.COARSE_LOCATION. Enabling anyone of the location permissions enables all.

Requesting Android Runtime Permissions

The method requestPermissions(String[] permissions, int requestCode); is a public method that is used to request dangerous permissions. We can ask for multiple dangerous permissions by passing a string array of permissions.

Note: Android Permissions belonging to two different groups would prompt the user with an individual dialog for each of them. If they belong to the same group, then only one dialog prompt would be displayed. The results of the requests will be passed into the method onRequestPermissionResult.

Example : Let’s say we want to access the camera and location in our app. Both are dangerous permissions. We’ll display a prompt requesting access to these permissions when the application is launched. Let’s add the permissions into a string array and call the requestPermissions as shown below:

Now we don’t want the user to keep accepting permissions that he’s already accepted. Even if the permission has been previously granted it is necessary to check again to be sure that the user did not later revoke that permission. For this the following method needs to be called on every permission.

It returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED.

Note: If a user declines a permission that is critical in the app, then shouldShowRequestPermissionRationale(String permission); is used to describe the user the need for the permission.

Let’s develop an application which checks if the permission is already present. If not, then it’s requested at runtime.

Android Runtime Permissions Project Structure


Android Runtime PermissionsCode

The content_main.xml contains the two buttons to check and request permissions.

The is defined as below.

Note: Add the permissions that are to be checked at runtime in the Manifest file above the application tag as;

In the above code the two permissions that are checked and requested are CAMERA and LOCATION.
Importing the static permission full class name allows us to write just the PERMISSION object instead of the fully qualified path.

checkPermission() calls the checkSelfPermission on each of the permissions.

requestPermission() calls
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION, CAMERA}, PERMISSION_REQUEST_CODE);.

onRequestPermissionsResult checks if the permissions are granted or not. In our code if both the permissions are not granted an alert dialog is popped showing the mandatory need to request the permissions. To do that shouldShowRequestPermissionRationale(String permission) is invoked which invokes an alert dialog showing the need for the permissions. You can revoke the permissions manually from Settings->Apps->Permissions.

Note: The runtime permission specific methods are available only since API 23. Hence the following condition is checked at each of the methods :

The output of the android runtime permissions example application in action is given below.
android runtime permissions example

This brings an end to this tutorial. You can download the final Android Runtime Permissions project from the link below.


By admin

Leave a Reply