Android Permissions — Customized and Simplified

Berk Özyurt
Huawei Developers
Published in
3 min readDec 26, 2021

Hello everyone,

In this article, I will talk about Android permissions and give an customized permission management example. I am sure this article will be helpful for all android developers about permissions management.

What is Permissions in Android?

Permissions is a kind of user consent, which must be obtained in order to use some features in an android application. The most common features are location and camera permissions.

Some scenarios doesn’t need any permissions in a android app, while some scenarios cannot be performed without user permission.

Frankly, there’s not much need to explain about permissions. Without further ado, I would like to talk about how we can customize the permissions.

You only need 2 classes to create your own customized permission structure. In the first class you will define the permissions, and in the second class you will manage your permissions. Thanks to these two classes, you can easily obtain the permissions you want anywhere in the app.

Development Process

  1. Add Permissions to AndroidManifest.xml

Firstly, you need to define the required permissions to the application. For this, define the permissions in the AndroidManifest.xml file as follows. Here you can add the permissions required for your application. I shared a few examples.

2. Create Permission Class

In this class you must define the permissions your application needs.

For example, if X activity requires WRITE_EXTERNAL_STORAGE and ACCESS_FINE_LOCATION permissions, you should create an object named X and give WRITE_EXTERNAL_STORAGE and ACCESS_FINE_LOCATION permissions as parameters.

object XPermissions : Permission(WRITE_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION)

Or, if you only need camera permission, you can create an object named Camera and set “CAMERA” as a parameter.

object Camera : Permission(CAMERA)

In another example, if you only need storage permissions, you can create a new object and set parameters as WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE.

object Storage : Permission(WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE)

Then, create a companion object and create a method in this object for obtain and define all of the permissions with parameters and names.

Finally, the Permission class should look like this:

3. Create PermissionManager Class

Thanks to PermissionManager class, you can check a permission status, request a permission, handle a permission request, clean all permissions, get permission list, set your permission request text and show your customized permission request window.

There is not much information to tell about this class. As I said we will only use this class to define above operations and call them inside activity/fragment.

Your PermissionManager class should look like this:

4. Get Permissions in Activity/Fragment

Firstly, create PermissionManager object.

private val permissionManager = PermissionManager.from(this)

Now, call the permissionManager class with your request parameter, description and check if permissions have been obtained. If the permission is granted, perform your operations. Else, notify to users.

The most important feature of this structure is that you can instantly see whether the permissions have been obtained or not. With traditional methods -especially for location permissions- this can be a problem. But thanks to the customized permissions, you will now be able to instantly control whether the permissions have been obtained.

You can see sample code on the below:

Result

Tips & Tricks

  • You can define permissions once and use them easily in many places.
  • Your user experience will improve as you design a more useful and clean screen to ask for permission.

--

--