2

Possible Duplicate:
Retrieving Android API version programmatically

I need if the phone running the app api level is 14 which is android 4.0 or more ( example api levcel 15 ) then startActivity ... else if the api level is lower than 14 ( example 13 ), then startActivity ...

                String AndroidVersion = android.os.Build.VERSION.RELEASE;
                if ( AndroidVersion == 4.0 ) {
                    Intent start = new Intent(S.this, Menu.class);
                    startActivity(start);                       
                }
                else {
                    Intent startt = new Intent(S.this, Menu2.class);
                    startActivity(startt);
                }

whats the wrong ?

Community
  • 1
  • 1
John Smith
  • 33
  • 1
  • 3
  • 1
    [possible duplicate](http://stackoverflow.com/q/3423754/1465828) – Aprian Aug 31 '12 at 09:21
  • In your question you state "(...)which is android 4.0 or more(...)" but you are using ==. This is only true if the version is exactly 4.0, but not if it is 4.0.3, 4.1 or something else. – Søren Lorentzen Aug 31 '12 at 09:27

3 Answers3

4

Use SDK_INT instead of RELEASE. So your code would look like:

Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  intent = new Intent(S.this, Menu.class);
} else {
  intent = new Intent(S.this, Menu2.class);
}

try {
  startActivity( intent );
} catch( Exception e ) {
  e.printStackTrace();
}

all SDK codes are listed here. Mind to always catch possible exception from startActivity(); It won't hurt (even within your app - you can always forget to add activity to Manifest while developing), is a good habit and prevent your app from crashing while providing something useful in the logs

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I want if the api level is 14+ so if it's 14 or 15 or 16 starts Menu else if it's less than 14 ( example 13,12,11, .... ) start Menu2 .. this will work right ? – John Smith Aug 31 '12 at 09:36
  • Yes, see the condition: ">=" it means that `Menu.class` is used for all the ICS and newer versions, while `Menu2.class` for any other case (which in this case means anything lower than ICS) – Marcin Orlowski Aug 31 '12 at 09:37
  • btw Im using int AndroidVersion = android.os.Build.VERSION.SDK_INT; if ( AndroidVersion >= 14 ) { .. thats correct right ? – John Smith Aug 31 '12 at 09:38
  • Technically yes, but I'd replace `14` with `Build.VERSION_CODES.ICE_CREAM_SANDWICH` anyway, because that makes your code more readable (you do not have to wonder "what the heck is API 14"). It still be 14, as this is constant defined in SDK (and you can hover your mouse in Eclipse to see the exact value) but you look and you instantly see "ok, this is of ICS and up". It's clear even on worst hangover :) – Marcin Orlowski Aug 31 '12 at 09:39
0

It's cleaner to compare the integer version, and to use >= for future compatibility

if (android.os.Build.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
yoah
  • 7,180
  • 2
  • 30
  • 30
0

This will do!

     if ( Build.VERSION.SDK_INT == 4 ) {
            Intent start = new Intent(S.this, Menu.class);
            startActivity(start);                       
        }
        else {
            Intent startt = new Intent(S.this, Menu2.class);
            startActivity(startt);
            }
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • 1
    This will check if you run on android 1.6. instead of ICS due to the wrong int used. Rather use fixed constants from Build.VERSION_CODES. that increases readability and avoids mistakes like this. –  Aug 31 '12 at 09:34