runtime_permissions_blog

Runtime Permissions in Android


In this tutorial, you will learn how to use Runtime Permissions in your Android project. This article has information about permission system in different Android versions, Group permissions, etc.

Beginning from Android Marshmallow was added Runtime permissions. It’s mean that application will not granted any permission during installation step. Instead it, application has to ask user for a permission one-by-one at runtime.

runtime_permission3

Note: that permission request dialog shown above will not launch automatically. Developer has to call for it manually.

Besides, user can also manage the permission from setting. For it need to choose Permissions item from Application Settings (Settings / Apps / SOME_APPLICATION / Permissions).

runtime_permission5

Anyway this new Runtime Permission will work like described only when we set the application’s targetSdkVersion to 23 and this feature will work only on Android 6.0 Marshmallow. If the application’s targetSdkVersion is set to less than 23, It will be assumed that application is not tested with new permission system yet and will switch to the same old behavior: user has to accept every single permission at install time and they will be all granted once installed !

rp_compilesdkversion23_2rp_compilesdkversion22_3

As a result, application will run perfectly like previous. Anyway please note that user still can revoke a permission after that ! Although Android 6.0 warn the user when they try to do that but they can revoke anyway.

rp_compilesdkversion22_2

When we call a function that requires a permission user revoked on application with targetSdkVersion less than 23, no any Exception will be thrown. Instead it will just simply do nothing. For the function that return value, it will return either null or 0 depends on the case.

rp_compilesdkversion23_1 rp_compilesdkversion22_1

Automatically granted permissions

Some Android permission granted automatically. We call it Normal Permission (PROTECTION_NORMAL).

  • 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

However you must declare this permission in AndroidManifest.xml file.

Android Permission Groups

Permissions are grouped into Permission Group like table below.

Permission Group Permissions
CALENDAR
CAMERA
CONTACTS
LOCATION
MICROPHONE
PHONE
SENSORS
SMS
STORAGE

If any permission in a Permission Group is granted. Another permission in the same group will be automatically granted as well. In this case, once WRITE_CONTACTS is granted, application will also grant READ_CONTACTS and GET_ACCOUNTS.

 

Make your application support new Runtime Permission

Now it’s time to make our application support new Runtime Permission perfectly. Start with setting compileSdkVersion and targetSdkVersion to 23.

 

Requesting for Permission

When the user tries to do something that requires a permission, the app checks to see if it currently has permission to perform this operation (by calling Context.checkSelfPermission(Context context, String permission)).

If permission has already been granted, then the respect function will execute, else we request the user that certain permission is needed to perform this operation (by calling Context.requestPermissions (Activity activity, String[] permissions, int requestCode)).

Further, granting some permissions may require a restart of you application. In such a case, the system will recreate the activity stack before delivering the result to your onRequestPermissionsResult( int, String[], int[]). Callback for the result from requesting permissions. This method is invoked for every call on requestPermissions(String[], int).
Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.

runtime_permission3 runtime_permission4 runtime_permission5

Handle “Never Ask Again”

If user denied a permission. In the second launch, user will get a “Never ask again” option to prevent application from asking this permission in the future.

If this option is checked before denying. Next time we call requestPermissions, this dialog will not be appeared for this kind of permission anymore. Instead, it just does nothing. So before calling requestPermissions, we need to check that should we show a rationale about why application needs the being-requested permission through

shouldShowRequestPermissionRationale(Activity activity, String permission)

runtime_permission6

Complete Source code available here.

References:
  1. Requesting Permissions at Run Time
  2. Android Developer must know about new Runtime Permission

 


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.