net*_*tos 9 reflection android
在Android中我很容易得到SDK的版本(Build.VERSION.SDK)但是只有当平台比1.6(>Build.VERSION_CODES.DONUT)更新时我才需要使用LabeledIntent
我认为反思是必要的(我已经阅读过这个链接,但对于一个班级或我来说,这是不明确的).
这是代码,但它给了我一个例外,因为在我的Android 1.6中,编译器会验证包是否存在,即使条件未应用:
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);
}
Run Code Online (Sandbox Code Playgroud)
我如何解决它,一些注意到正确的答案
@Commonsware告诉我如何做到这一点.我们创建一个桥类,以便根据API LEVEL实例化一个使用API LEVEL的类或另一个使用另一个API LEVEL的类.一个初学者可以忘记的唯一细节是你必须使用最新的SDK编译你的应用程序,你可以参考.
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());
}
}
Run Code Online (Sandbox Code Playgroud)
所以在这里LabeledIntentNew,我包含了所有LabeledIntent仅在API LEVEL 5中提供的代码.在LabeledIntentOld,我可以实现另一种控件,在我的情况下,我返回意图本身而不做更多.
对此类的调用如下所示:
LabeledIntentBridge.INSTANCE.BuildLabeledIntent(URLtest,theIntent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7441 次 |
| 最近记录: |