Android: LabeledIntent, how to use it with Android 1.6 (API level 4) to include it in a createChooser function?
I am programming for Android 1.6, the following code works in 2.2 (API Level 5):
Intent intentChooser = Intent.createChooser(intent, "Choose between these programs");
Intent intentTest= new Intent(Intent.ACTION_VIEW);
....
Parcelable [] parcelable = new Parcelable [1];
parcelable[0] = new LabeledIntent(intentTest, "", "the text of the label", 0);
intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, parcelable);
activity.startActivity(intentChooser);
I don't know if the parcelable can include another type of data (not LabeledIntent). That would be a possible solution.
I have tried to include th开发者_如何学Ce code of LabeledIntent in my project, I have found here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/content/pm/LabeledIntent.java
but no success. That would be another question, how to include classes of later versions in older Android versions.
Any ideas?
As I understand it you can also specify regular Intent classes, have you tried that or do you need the custom label?
The user Commonsware gave me the solution: Android: how to code depending on the version of the API?
The solution was to create a proxy class, so that determining if the sdk was < than level 5, I instanced a class that did not include the LabeledIntent class, only available from 2.0 on.
The one thing one has to bear in mind is that you cannot instance any class that includes a reference to a package / class from a more modern sdk. So in fact, with the code below I never reference the class (LabeledIntentNew) that had the reference to LabeledIntent, if the sdk was < than 5.
That is the code:
public abstract class LabeledIntentBridge {
public abstract Intent BuildLabeledIntent(String URL, Intent intent, Activity activity);
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());
}
}
To instance this class you do:
activity.startActivity(LabeledIntentBridge.INSTANCE.BuildLabeledIntent(
URL,
theIntent,
activity
)
);
精彩评论