Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to code depending on the version of the API?

In Android I get the version of the SDK easily (Build.VERSION.SDK) but I need to use LabeledIntent only if the platform is newer than 1.6 (>Build.VERSION_CODES.DONUT)

I suppose that Reflection is necessary (I have read this link but it is not clear for a class or to me).

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:

 Intent theIntent=....;
      if(Integer.parseInt(Build.VERSION.SDK) > 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);
  }

HOW I SOLVED IT, SOME NOTES TO THE RIGHT ANSWER

@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.

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<5) {
   return(new LabeledIntentOld());
  }

  return(new LabeledIntentNew());
 }
}

So in the LabeledIntentNew, I included all the code that refers to LabeledIntent only available in API LEVEL 5. In LabeledIntentOld, I can implement another kind of control, in my case I return the intent itself without doing nothing more.

The call to this class is done like this:

LabeledIntentBridge.INSTANCE.BuildLabeledIntent(URLtest,theIntent);
like image 712
netadictos Avatar asked Nov 25 '10 11:11

netadictos


People also ask

What is API level in choosing a version in targeting Android application?

API Level is an integer value that uniquely identifies the framework API revision offered by a version of the Android platform. The Android platform provides a framework API that applications can use to interact with the underlying Android system.

How do I know my Android API version?

Tap the "Software Information" option on the About Phone menu. The first entry on the page that loads will be your current Android software version.

What is API 19 in Android?

Android api level 19 means the android os version (kitkat). It contains the standard android packages(from Android Open Source Projects). But the google api 19 is the android api 19+ google api's like google settings and other packages provided by google.

What is API version in Android?

Each Android device runs at exactly one API level – this API level is guaranteed to be unique per Android platform version. The API level precisely identifies the version of the API set that your app can call into; it identifies the combination of manifest elements, permissions, etc.


2 Answers

Follow the wrapper class pattern documented in the page you linked to above.

like image 63
CommonsWare Avatar answered Oct 12 '22 02:10

CommonsWare


You have to use reflection... The idea is good, but in your code you refer to LabeledIntent which is not available in 1.6. So when your app runs against 1.6 devices, it cannot find the class and crashes.

So the idea is to write code where you don't refer to LabeledIntent when running in 1.6. To do this, you can write a wrapper class (LabeledIntentWrapper) which extends LabeledIntent and call it in your function. So, in 1.6, the device will see a reference to a known class: LabeledIntentWrapper.

like image 26
Cédric Coulon Avatar answered Oct 12 '22 01:10

Cédric Coulon