Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Android version when requesting runtime permission

I have recently updated the code for an Android app to request permissions on Android 6.0+. However, I am now facing a dilemma on how I want to check for permissions.

I saw people online checking the OS version before checking for permissions, because versions before 23, permissions are unnecessary to check due to them being granted on installation.

Right now my checks look like this,

if(checkPermissions()){
    doThings();
} else {
    requestPermissions();
}

but should I take the effort to add this?

if (Build.VERSION.SDK_INT >= 23) {
    if(checkPermissions()){
        doThings();
    } else {
        requestPermissions();
    }
} else {
    doThings();
}

I don't see the point of adding the latter to the code as from my understanding, older versions of android could run into the first sample code just fine.

All this brings me to ask, is there a benefit to checking Android version in this case?

like image 862
M. Haché Avatar asked Dec 24 '22 18:12

M. Haché


1 Answers

but should I take the effort to add this?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(checkPermissions()){
        doThings();
    } else {
        requestPermissions();
    }
} else {
    doThings();
}

You should if checkPermissions(), and requestPermissions() invokes classes or methods strictly API 23 or later. Or else, you may do as you wish.

All this brings me to ask, is there a benefit to checking Android version in this case?

As far as I know, the verification by itself isn't necessary, given what I said above. So this check is just an expendable if clause cluttering your code.

like image 130
nandsito Avatar answered May 08 '23 17:05

nandsito