Handle Android Permissions Easily Even User Denied it Permanently
Handling dynamic permissions in android in a single line code
Today I am posting this article as I have created android library to check and request any single or multiple permission to get exact status of permission e.g. Allowed, Not requested yet and to be requested/Denied temporarily (for once only) or Denied permanently.
I faced an issue in a project with dynamic permissions in Android when I was developing and my issue was to check if permission is denied temporarily or permanently. So as I researched a lot and found very small amount of solutions, hence me and my team thought, many of the developers are facing this issue and so let provide them a permanent solution.
First we’ll look into how the app permission works in a flow.
Flow the Android Permission Works
As we can see in the image above, there are 3 cases in which permission flow goes.
Case 1
Permission is never requested by app.
Case 2
Permission requested once or more times but user denied it temporarily (Without checking “Never Ask Again”)
Case 3
Permission requested and user denied it permanently (By checking “Never Ask Again” option)
Only one function can help us “shouldShowRequestPermissionRationale()”, which returns boolean value either true or false on the base of these cases.
Case returns TRUE:
- When app requested permission at least once and user denied it temporarily.
Case returns FALSE:
- App never requested required permission before.
- App requested permission and user denied permission
So It is easy to detect that user yet not given permission even asked before but it’s tricky to define whether user has denied permission permanently or app never requested permission to user.
Finding a solution for this, we stored a flag value for each requested permission in a shared preference so that we can get know if permission is requested or not and so can satisfy both the cases when function returns false.
Then thought if this solution will be needed to implement in each project which will waste the time. So I created a library to handle it very easily.
This library will return a response with status as,
ALLOWED
If you get this as final status for requested permission(s), it means all permission(s) are allowed by user, so can proceed further.
DENIED_PERMANENTLY
When user has already denied any/all of the requested permission(s), final status will be this as indicating that System will also not show a permission request dialog to the user so need to allow permission explicitly.
PermissionUtils.askUserToRequestPermissionExplicitly()
You can call this function to redirect user to allow permission explicitly if user has already denied requested permission permanently.
NOT_GIVEN
If user has not granted any/all of the requested permission(s), this status will be returned as final and also it will request permission which are needed. So no need to do any extra code to request permission again.
You can find full implementation guide on this library page which is too much easy for anyone. So Why to wait for anything? Implement it and enjoy.