反思是必要的,如果我使用"如果(android.os.Build.VERSION.SDK_INT> = 11)" [英] Is reflection necessary if I use "if (android.os.Build.VERSION.SDK_INT>=11)"

查看:788
本文介绍了反思是必要的,如果我使用"如果(android.os.Build.VERSION.SDK_INT> = 11)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,针对API 11(3.0),但的minSdkVersion是7(2.1)。

I'm working on an app that targets API 11 (3.0) but minSDKVersion is 7 (2.1).

我产生我的preferenceActivity编程,而不是使用XML。在蜂窝,preference布局有一个内置专为一个图标,可以去旁边的每个preference。

I generate my PreferenceActivity programmatically instead of with XML. In Honeycomb, preference layouts have a built-in spot for an icon that can go next to each preference. You can set it with prefScreen.setIcon(R.drawable.my_icon);

所以我不想做这个API 7-10。从这个崩溃足够的保护?

So I don't want to do this on API 7-10. Is this sufficient protection from crashes?

if (android.os.Build.VERSION.SDK_INT>=11)
    prefScreen.setIcon(R.drawable.myIcon);

更复杂的解决方案,我知道是安全的使用反射来检查,如果该方法尝试使用它之前存在。

The more elaborate solution that I know is safe is to use reflection to check if that method exists before trying to use it.

推荐答案

据<一href="http://developer.android.com/training/basics/activity-lifecycle/starting.html">http://developer.android.com/training/basics/activity-lifecycle/starting.html,它暗示了它的安全使用SDK_INT不断在Android 2.0及以上的包裹调用更新的API,而无需使用反射。

According to http://developer.android.com/training/basics/activity-lifecycle/starting.html, it's implied that it's safe to use the SDK_INT constant on Android 2.0 and above to wrap calls to newer APIs, without using reflection.

注意:从执行使用SDK_INT以prevent旧系统的   新的API的工作以这种方式在Android 2.0(API级别5)和更高   只要。旧版本会遇到一个运行时异常。

Caution: Using the SDK_INT to prevent older system's from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.

这篇关于反思是必要的,如果我使用&QUOT;如果(android.os.Build.VERSION.SDK_INT&GT; = 11)&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆