Doubts and Key Points of Run-time Permission in android

There are some key information related to permission architecture in android. Sometime it confuses developer on different API Versions.

  • There are two type of permissions 1) Normal 2) Dangerous .All permission should be declared in Android Manifest File. You can find detail about it.
  • Android introduced a Run-time Permission on device running API level 23 and above,whose targetSDKVersion is above or equal to 23.
  • Normal permission needs to declare in manifest only.
  • Dangerous or we can call them Privacy Permission also need to declare in manifest.
  •  We also need to request these(Dangerous) permission at run-time. We can request it when user perform some specific operation related to that like opens camera, contacts and so on.
  •  User can revoke run-time permission anytime from settings.
These are the few key points of Android Permission. In this tutorial, I am not going to explain about implementation of Runtime Permission, there are already lots of tutorial and developer docs available. 
Here I am going clarify some doubts of developer regarding implementation of Run-time Permission. 

Firstly we need to understand basic flow of permission implementation theoretically. Let's suppose your app running on targetSDK 23 on M device. App having camera feature where it has to ask permission. Let say A permission
  1. App checks the Permission A is granted or not.
  2. If no
    then it check is app requesting permission first time or not
    |  If Yes then you don't need to show addition information about that permission.
    |  If No it means you have to show additional information to user.
  3. If yes, Permissions are already granted. It means you can do what you need to do.
  4. You also need to handle these case when user allowed or denied a permission in Dialog.
  5. Also when user denied by checking Never Ask checkbox.
So by having following questions we get clarification on some of our doubts :-

Q 1. Do I need to register Run-time Permission in manifest file also?
 Yes, you have to register all the permission in the manifest even it is runtime or normal  permission.

Q 2. If our app is running targetSDKVersion 21 will it crash on android M devices?
No, android system is already designed to handle such scenario. But in android M user can revoke such permissions. So if user did this then if you app perform some operation related to that app will crash.

Q 3 What does shouldShowRequestPermissionRationale() method returns?
It returns boolean value which is used to handle two scenario
  • If true, means app had already asked user for this permission. But user denied it, so we need to show some extra information about that permission requirement in app and then ask permission again.
    That's why some app shows special information dialog for the requirement of that permission.
  • If false then it means you are requesting permission from user first time and there is no need for rationale or info dialog.
Q 4 How to handle the case when use denied a permission with Never ask again checked? or
 Here comes role of  onRequestPermissionResult. When user denied a permission by checking      Never ask Again, then each time System will automatically deny that permission when user performs  that action. So you have handle this case by checking shouldShowRequestPermissionRationale  again in onRequestPermissionResult if your grantresults permission not granted.

 So if shouldShowRequestPermissionRationale  returns true it means you permission is reject  normally by clicking on deny. 
 But if it return false then if meas user also checked Never ask again and you have to show user to  move on setting to allow that permission.

You can also use this gist for permission implementation on app. Here all the scenario as I know handled.
You simply need to pass permission array  and message to requestPermission method.

Also here are some links apart from android developer docs, that I found relevant for implementing run-time permission.
How can we miss stackoverflow, some worthy discussion on doubtful points
Though there are some other doubts also you can found on different portals like stackoverflow and github. I wrapping these, you can also comment your doubts for helping others.


Comments

Popular posts from this blog

Software Design Principles (SOLID)

Draw Over Screen App Example Android

Sync Adapter Overview