Android Kotlin

An Easy Way Of Handling Multiple Android Runtime Permissions

In this quick article, I’ll try to show you an easy way of how you can ask Android runtime permissions using Kotlin extension functions. Almost in every android app, we need to ask some permission from the user. Asking runtime permissions is a common task in android and different developers use different approaches.

The traditional way of asking android runtime permission

There are developers who still use uses the old-traditional way of asking android runtime permissions. Now let’s say I’m asking storage for read and write permission.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && 
                     checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_STORAGE_REQUEST_CODE)
            } else {
                // handle granted permission
            }
        } // no need to ask permission

The above example asks successfully storage permission to the user but can we improve it? Because there is always a better way to solve the problem.

Creating activity class extension function for asking runtime Permission

In this method, we’ll try to write an extension function for an Activity class. I can show you how to ask for runtime permission with this approach.

fun Activity.isHasPermission(vararg permissions: String): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        permissions.all { singlePermission ->
            applicationContext.checkSelfPermission(singlePermission) == PackageManager.PERMISSION_GRANTED
        }
    else true
}

fun Activity.askPermission(vararg permissions: String, @IntRange(from = 0) requestCode: Int) =
        ActivityCompat.requestPermissions(this, permissions, requestCode)

After adding the extension functions, we can directly use the isHasPermission and askPermission into an Activity class and use it like this:

val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        if (!isHasPermission(*permissions))
            askPermission(permissions = *permissions, requestCode = STORAGE_STORAGE_REQUEST_CODE)
        else
            // handle already granted permission

Nailed it 🔨 right! We have a solution from which we can easily ask runtime permission. Wait! this solution only works for kotlin developers. Let us take this example a little further and try to write these utility methods for Java developers.

Write a Java base activity class for asking the runtime permission

Let us start this method by creating a new Java BaseActivity class. All of our activities which needs to ask permission must extend with this BaseActivity class.

public class BaseActivity extends AppCompatActivity {

    protected boolean isHasPermission(String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean permissionFlag = true;
            for (String singlePermission : permissions) {
                permissionFlag =
                        getApplicationContext().checkSelfPermission(singlePermission) == PackageManager.PERMISSION_GRANTED;
            }
            return permissionFlag;
        }
        return true;
    }

    protected void askPermission(@IntRange(from = 0) int requestCode, String... permissions) {
        ActivityCompat.requestPermissions(this, permissions, requestCode);
    }
}

Next, we want the MainActivity to extend with BaseActivity class. After extending with BaseActivity the MainActivity able to use all the protected methods inside the class.

public class MainActivity extends BaseActivity {

    private static final int STORAGE_STORAGE_REQUEST_CODE = 6116;

    private String[] permissions = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        if (!isHasPermission(permissions))
            askPermission(STORAGE_STORAGE_REQUEST_CODE, permissions);
        else
            // handle already granted permission
    }
}

So, what do you think about asking runtime permissions with this approach? I excited to get your feedback or tips to improve those methods even further. Please let me know via the comments section.

Thank you for being here and keep reading…

2 Comments

Write A Comment