Android的落后code兼容性 [英] Android backward code compatibility
问题描述
我正在开发一个使用
android.hardware.Camera.parameters.getSupportedPictureSizes()
android.hardware.Camera.parameters.getSupportedPictureSizes()
这是只能从SDK版本8,我希望能与SDK 4兼容的,所以我这样做:
This is only available from SDK version 8 and I would like to be compatible with SDK 4, so I've done this:
如果(Build.VERSION.SDK_INT> = 8){...}
if(Build.VERSION.SDK_INT >=8){...}
但在模拟器,它接缝,它尝试检查引用这个功能,它失败:
But on the emulator, it seams that it tries to check the reference to this function, and it fails:
11月二号至2日:20:10.930:ERROR / dalvikvm(1841):找不到方法android.hardware.Camera $ Parameters.getSupportedPictureSizes,从方法com.test.demo.CameraCustom.takeAPicture引用
02-02 11:20:10.930: ERROR/dalvikvm(1841): Could not find method android.hardware.Camera$Parameters.getSupportedPictureSizes, referenced from method com.test.demo.CameraCustom.takeAPicture
有关如何解决这个向后兼容性问题的任何想法?
Any idea about how to solve this backward compatibility issue?
我试着使用inkocation着这片code里面surfaceChanged。显然,code工作的情况下直接invokation:
I've tried to use inkocation with this piece of code inside surfaceChanged. Obviously, the code works directly without invokation:
try{
windowmanager_defaultdisplay_Rotation = getWindowManager().getDefaultDisplay().getClass().getMethod("getRotation");
Log.v(MainMenu.TAG, "getRotation exist");
}catch(Exception e){
Log.v(MainMenu.TAG, "getRotation dont exist");
}
try{
windowmanager_defaultdisplay_Rotation.invoke(null, null);
Log.v(MainMenu.TAG, "getRotation invoking ok, rotation ");
}catch(Exception e){
Log.v(MainMenu.TAG, "exception invoking getRotation "+e.toString());
}
我得到getRotation存在,但随后异常调用getRotation显示java.lang.NullPointerException。
I get "getRotation exist" but then "exception invoking getRotation java.lang.NullPointerException.
你知道吗?
推荐答案
您不能加载包含呼叫 getSupportedPictureSizes()
的API 7级及以前code。因此,你需要根据的决定建立
的在的加载了code包含版本相关的声明。
You cannot load code containing calls to getSupportedPictureSizes()
on API level 7 and before. Hence, you need to make your decision based upon Build
before you load the code containing the version-dependent statement.
您的选项包括:
- 禁用菜单选项,按钮,或什么,导致使用该活动
getSupportedPictureSizes()
,根据API级别 - 使用条件的类加载或类似的技术来加载基于API的水平,一个合适的实现,其中的合适的实现使用
getSupportedPictureSizes()
仅在API级别8或更高版本
- Disable the menu choice, button, or whatever that leads to the activity that uses
getSupportedPictureSizes()
, based upon API level - Use conditional class loading or similar techniques to load a suitable implementation based upon API level, where the "suitable implementation" uses
getSupportedPictureSizes()
only on API level 8 or higher
后者的技术的一个例子可以在此示例项目时,其中我支持面向前方的API 9级相机,但仍然可以在旧版本的Android上运行。
An example of the latter technique can be seen in this sample project, where I support forward-facing cameras on API level 9, yet still can run on older versions of Android.
这篇关于Android的落后code兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!