title: Android: how to code depending on the version of the API?
body: <p>In Android I get the version of the SDK easily (<code>Build.VERSION.SDK</code>) but I need to use LabeledIntent only if the platform is newer than 1.6 (<code>>Build.VERSION_CODES.DONUT</code>)</p>
  
  <p>I suppose that Reflection is necessary (I have read <a href="http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html" rel="noreferrer">this link</a> but it is not clear for a class or to me).</p>
  
  <p>This is the code but it gives me an exception because in my Android 1.6, the compiler verifies if the package exists even if the condition is not applied:</p>
  
  <pre><code> Intent theIntent=....;
        if(Integer.parseInt(Build.VERSION.SDK) &gt; Build.VERSION_CODES.DONUT)
     {    
   try{
               Intent intentChooser = Intent.createChooser(intent,"Choose between these programs");
                Parcelable[] parcelable = new Parcelable[1];
                parcelable[0] = new android.content.pm.LabeledIntent(theIntent, "", "Texto plano", 0);
                 intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, parcelable); 
    activity.startActivity(intentChooser);
     }
     catch(Exception e)
     {
      activity.startActivity(theIntent);
     }
  
    } else
    {
     activity.startActivity(intentMedicamento);
    }
  </code></pre>
  
  <p><strong>HOW I SOLVED IT, SOME NOTES TO THE RIGHT ANSWER</strong></p>
  
  <p>@Commonsware show me the way to do it. We create a bridge class so that depending on the API LEVEL, you instance one class that uses an API LEVEL or another class that uses another API LEVEL.
  The only detail one beginner could forget is that you have to compile your app with the newest SDK you are goint to make reference.</p>
  
  <pre><code>public abstract class LabeledIntentBridge {
   public abstract Intent BuildLabeledIntent(String URL, Intent theintent);
  
   public static final LabeledIntentBridge INSTANCE=buildBridge();
  
   private static LabeledIntentBridge buildBridge() {
    int sdk=new Integer(Build.VERSION.SDK).intValue();
  
    if (sdk&lt;5) {
     return(new LabeledIntentOld());
    }
  
    return(new LabeledIntentNew());
   }
  }
  </code></pre>
  
  <p>So in the <code>LabeledIntentNew</code>, I included all the code that refers to <code>LabeledIntent</code> only available in API LEVEL 5. In <code>LabeledIntentOld</code>, I can implement another kind of control, in my case I return the intent itself without doing nothing more.</p>
  
  <p>The call to this class is done like this:</p>
  
  <pre><code>LabeledIntentBridge.INSTANCE.BuildLabeledIntent(URLtest,theIntent);
  </code></pre>