Pregunta

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.

¿Fue útil?

Solución

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
}

Otros consejos

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;
}
android.os.Build.VERSION.SDK_INT

Check this link out

Build Version Codes

So you can use as follows...

int thisDevicesApi =Build.VERSION.SDK_INT;

if (thisDevicesApi <=Build.VERSION_CODES.GINGERBREAD) { 
     //for example
    //do something
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top