Understanding Runtime permission and RxPermission of RxJava in Android part -3

As We have the basic understanding of RxJava features and function from the last post of basic of RxJava and RxJava2 Part-1 and basic of RxJava and RxBinding part-2. Rx is getting popular in Android application development because it gives the best result of reactive nature. In this series, we will learn some of new Rx feature is called RxPermission.

Earlier we have practiced that the user can be seen the permission detail at while installing the app. But in marshmallow device version 6.0 it has changed. Now application will not be granted the permission while installing the application. It will ask users at runtime. Why is this required?

In the previous below the marshmallow versions like the lollipop and other lower version devices, User was felt like obvious to see the permission details while installing that, They might be thinking that why can I shared my confidential information without asking that permission. The application should be asked me, hey if you want to access those then please grant me permission to access. For example, if any other app wants to get access the phone contacts then user feel obvious to give access those. That why it comes runtime permission. If the other application wants to access phone contacts of users then its all depend on the user if they granted the permission or not. If the user has not granted then other application will not be allowed to access that information.

One common question of every developer that how Android take care our old application launch on marshmallow device. So I would say do not panic be relaxed android take care that issue very smartly. If your application has built on targeted SDK version is less than 23 then it should not be crashed on Android M. It will work fine without any issue. Runtime permission will only ask users if you build the app on target SDK version 23 or higher.

This really sounds good for the users to feel confident that my phone is secure if I used to install any other application without any issue. Android take care security point of view at the top priority. Awesome:)

Okay, Let’s see what are those permissions are granted while installing the application. These permissions called protection-level permission. It will not ask the user at runtime.

Great. This permission is required to add in Android manifest file then it works fine without any crash. Now let’s check what is required to check the permission of my target SDK version and compile SDK version is 23 or higher.

A developer needs to follow three basic methods.
1. checkSelfPermission(Permission) – to check the permission is already granted or not.
2. requestPermissions(String [] permissions, int requestCode) – if not then request for permission
3. onRequestPermissionsResult(int premsRequestCode, String [] permissions, int [] grantResults) – to check the result whether permission is granted or not.

Let’s see one example I am building an app which targets SDK version is 23 or higher. I want location access in one of my application. First of all, you need to know what are permission is required for access the Location. Okay, How do I make sure? Let’s check in framework level what Gooogle has defined for location access?
When I look this file DefaultPermissionGrantPolicy.java in framework module.
I found this permission is required to access the location.

So basically these two permission is required to access Location. Ok, Let’s check while clicking on a button I want to access the location.

Here we are checking the build SDK version is marshmallow then request for permission else not required permission.

To access the runtime permission it is mandatory that permission also registered in AndroidManifest.xml file.

Once the user will be allowed to granting this permission it will trigger the result in onRequestPermissionResult ().

So this is the basic Runtime permission in Android application development. But now the time is the Reactive, So we should be thinking about the reactive that removed all the boilerplate code. RxPermissions is the library allows the usage of RxJava with the new Android M permission model.

To access the reactive permission very first thing we need to add the relevant dependency in android gradle file.

for RxJava2 it has modified

It has removed all the boilerplate code and make our life pretty easy and simple. It is not required to check the permission result that particular permission is granted or not. Every thing is handling at single place.

RxPermission is awesome for runtime permission prompt. In my next tutorial of this Rx series, we will learn new reactive feature and functions. If you wondering Kotlin for android then I would be recommended to check all this post of Kotlin Category.

Please do subscribe your email to get every newsletter from this blog and if you feel that this post helps you then do not forget to share and comment below.

I used my old reference to this post which is written here.

Happy coding 🙂

Sunil Gupta

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

You may also like...

1 Response

  1. Mattia says:

    Thank you for the article, really nice 😉

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.