Android runtime permissions with example

In this article i am going to explain about Android runtime permissions with example.  In my previous article i have explained about Show confirmation dialog on back pressed event in android,Android contact picker exampleAndroid Shared Preference ExampleConvert speech to text in android with example code and Android Signature Pad Example.

Need for runtime permissions:

Requesting runtime permissions from app is introduced in Android 6.0 -Marshmallow (API level 23). It means users grant permissions to apps while the app is running, not when they install the app. For users it gives more control by providing option to grant or deny permissions.

For example the user may decide to grant permission to internet and deny permission for camera. And also if the user wish either to revoke any of the granted permission or grant permission to any of the pending permission he can do so by navigating to apps setting screen.

Implementing runtime permissions in android:

To implement runtime permissions in android i have created a new android project and named it as Runtime-Permissions. Refer Create a new project in android studio for creating new project.

Now in the onCreate method of MainActivity.java i am creating the reference to the xml widget. For demonstration purpose i have two buttons. One button is used to request the runtime permissions and another button is used to open the apps setting page from where the user can manually enable or disable the permission.

After that on buttonRequestPermission onclick listener i am checking for the android os version. If the version is less than 6.0 that is less than marshmallow then i am displaying the toast that Runtime permissions are required only in Marshmallow and above. If not i will check whether all the permissions are granted or not. If any of the permission is not granted then i will prompt the user to grant the permission.

Below is the complete java code used to implement runtime permissions in android.

In the above code hasPermissions method will return true if all permissions are granted. If not it will return false even anyone permission was denied. Based on that either i will ask for the missing permissions or  i will show Permissions are already received toast message.

Now after receiving permissions from the user onRequestPermissionsResult method will be called. Here i will check for number of granted permissions against number of requested permissions and then i will show the toast message.

Below is the xml file used with MainActivity.java

Below is the complete AndroidManifest.xml file code.

Below is the complete strings.xml file content.

Demo of Android runtime permissions:

Now if you run the app you can see the below output.

Output 1 of Android runtime permissions with example

While clicking on the Request Permissions button we will see the below output.

Output 2 of Android runtime permissions with example

After granting/denying the permission you will get the toast message of number of granted/denied permissions.

Output 3 of Android runtime permissions with example

Now clicking on Enable Permissions button will open the below app’s permission page where you can manage all the permissions.

Output 4 of Android runtime permissions with example

Now after granting all the permissions if you click on Request Permission button it will show the toast as Permissions are already granted like in the below screenshot.

Output 5 of Android runtime permissions with example

I tried to run the app in the device with lollipop os. And when i tried to click the Request Permission button it showed the toast message as Runtime permissions are required only in Marshmallow and above.

Output 6 of Android runtime permissions with example

Source code of Android Runtime Permissions:

Source code of Android Runtime Permissions with example

If you like this article please share your feedback in the comments section.