ANDROID RUNTIME PERMISSIONS
Today I am going to show you a simple and easy method for handling runtime permissions. But before that let's understand what runtime permission is and why it is required?
Permission means allowing officially to do a particular thing like accessing your data, location etc. Hence runtime permission means the same, In Android when we run/open any app it asks permission to access our photos/gallery, location, contacts etc and we got options to either allow or disallow such permission is known as runtime permission.
From API level 26 (Android 6.0), Users are asked to allow or deny different permissions requested by App. Previously it was done while installing the app. It gives the user more control over the app in order to preserve their privacy.
Complexity faced due to the same:
- It is difficult to manage permission callbacks from fragment.
- It requires proper checks everywhere in your code for same.
- Increases complexity in code.
So due to above riddles, I came up with creating a new library which can handle all runtime permissions without writing extra code for it and also without applying checks everywhere.
The runtimepermission library handles all your permissions by reading your manifest and processing each request that if it requires runtime permission or not.
Currently, this library is hosted on Maven central so you can get it through maven from following compile statement.
compile 'com.app.runtimepermission:runtimepermission:0.0.4'
Now call the constructor and add pass on activity context and listener as follows
// to get single permission
PermissionHandler.getPermission(getActivity(), Manifest.permission.READ_CONTACTS, new PermissionListener() {
@Override
public void onGranted(String message) {
Log.e(TAG, message);
}
@Override
public void onError(String errorMessage) {
Log.e(TAG, errorMessage);
}
@Override
public void onRejected(String message) {
Log.e(TAG, message);
}
});
// to check if permission is critical or not
boolean value = PermissionHandler.isCriticalPermission(getContext(),
Manifest.permission.INTERNET);
Log.e(TAG , Manifest.permission.INTERNET+" "+value);
// to get all permissions
PermissionHandler.getAllPermissions(getActivity(), new PermissionListener() {
@Override
public void onGranted(String message) {
Log.e(TAG, message);
}
@Override
public void onError(String errorMessage) {
Log.e(TAG, errorMessage);
}
@Override
public void onRejected(String message) {
Log.e(TAG, message);
}
});
That's it, You are good to go now, You will receive the messages once you get the permission granted by the user. Also, you will get the response at the same place where you are calling from.
For more info check out the detailed info from the following link.
https://github.com/amitrai98/MVVM
Well, this was fun. Isn’t it? Please feel free to share your thoughts and questions in the comment section below.
Happy Coding !!!
0 Comment(s)