Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Android build project target API level at runtime [duplicate]

I have an application, and it behaves differently when I run it on API 18 or 19. This is not a problem, and I know why does it happen.

However, I want to write one code that will deal with both the versions.

Is there any way to get in runtime which API my application was built with? Specifically, I would like to get 18 or 19 if I build my application with these APIs.

EDIT

It seems to be a duplicate question. I thought that the BUILD_VERSION is something else, because, when I compiled both the versions to API 18 and 19, and print the version, I receive 18. It looks like another problem (although I specified API 19, it is compiled according to 18).

I found that the problem was in the emulator configurations.

like image 550
Gari BN Avatar asked Jan 07 '14 13:01

Gari BN


2 Answers

I didn't understand your problem completely. But if you want to check which Build Version your app is working on and then act accordingly the you can use the following.

if(Build.VERSION.SDK_INT == 18 ){
    // Do some stuff
}
else if (Build.VERSION.SDK_INT == 19) {
    // Do some stuff
}
else{
    // Do some stuff
}
like image 106
Rohan Kandwal Avatar answered Oct 05 '22 23:10

Rohan Kandwal


The Android docs provides some sample code of how to load bitmaps effectively that handles this problem.

The code defines static methods in a class Utils that it references when it needs to know what platform the app is running on. The benefit of doing this is that you can reuse the function calls rather than rewriting long conditional statements over and over. The code looks like this:

public static boolean hasFroyo() {
    // Can use static final constants like FROYO, declared in later versions
    // of the OS since they are inlined at compile time. This is guaranteed behavior.
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
}

public static boolean hasGingerbread() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
}

public static boolean hasHoneycomb() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}

public static boolean hasHoneycombMR1() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
}

public static boolean hasJellyBean() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
}

public static boolean hasKitKat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}
like image 43
taylorstine Avatar answered Oct 05 '22 23:10

taylorstine