Runtime permissions

786 views.

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

activity_main.xml

MainActivity.java

 

androidmanifest.xml

 

You may also like...

Leave a Reply