Ideas are easy. Implementation is hard. Be Passionate.

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

android.permission.ACCESS_LOCATION_EXTRA_COMMANDS

android.permission.ACCESS_NETWORK_STATE

android.permission.ACCESS_NOTIFICATION_POLICY

android.permission.ACCESS_WIFI_STATE

android.permission.ACCESS_WIMAX_STATE

android.permission.BLUETOOTH

android.permission.BLUETOOTH_ADMIN

android.permission.BROADCAST_STICKY

android.permission.CHANGE_NETWORK_STATE

android.permission.CHANGE_WIFI_MULTICAST_STATE

android.permission.CHANGE_WIFI_STATE

android.permission.CHANGE_WIMAX_STATE

android.permission.DISABLE_KEYGUARD

android.permission.EXPAND_STATUS_BAR

android.permission.FLASHLIGHT

android.permission.GET_ACCOUNTS

android.permission.GET_PACKAGE_SIZE

android.permission.INTERNET

android.permission.KILL_BACKGROUND_PROCESSES

android.permission.MODIFY_AUDIO_SETTINGS

android.permission.NFC

android.permission.READ_SYNC_SETTINGS

android.permission.READ_SYNC_STATS

android.permission.RECEIVE_BOOT_COMPLETED

android.permission.REORDER_TASKS

android.permission.REQUEST_INSTALL_PACKAGES

android.permission.SET_TIME_ZONE

android.permission.SET_WALLPAPER

android.permission.SET_WALLPAPER_HINTS

android.permission.SUBSCRIBED_FEEDS_READ

android.permission.TRANSMIT_IR

android.permission.USE_FINGERPRINT

android.permission.VIBRATE

android.permission.WAKE_LOCK

android.permission.WRITE_SYNC_SETTINGS

com.android.alarm.permission.SET_ALARM

com.android.launcher.permission.INSTALL_SHORTCUT

com.android.launcher.permission.UNINSTALL_SHORTCUT

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.

1

2

3

4

5

privatestaticfinalSet<String>LOCATION_PERMISSIONS=newArraySet<>();

static{

LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_FINE_LOCATION);

LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_COARSE_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.

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.

1

compile'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar'

for RxJava2 it has modified

1

compile'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

@OnClick(R.id.RuntimeButton)

publicvoidRunTimeClick(){

RxPermissions rxPermissions=newRxPermissions(this);// where this is an Activity instance

// Must be done during an initialization phase like onCreate

rxPermissions

.requestEach(Manifest.permission.ACCESS_FINE_LOCATION,

Manifest.permission.ACCESS_COARSE_LOCATION)

.subscribe(permission->{// will emit 2 Permission objects

if(permission.granted){

// `permission.name` is granted !

tryToGetLocationValue();

}elseif(permission.shouldShowRequestPermissionRationale){

// Denied permission without ask never again

Toast.makeText(RunTimePermissionTestActivity.this,"Denied permission without ask never again",

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 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

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com