關於版本支持,您將編寫一些額外的代碼來檢測設備操作系統版本(請參閱android.os.Build.VERSION),確保它可以在1.5設備上運行。我強烈建議在自己的類中隔離該代碼,並且在版本檢查後僅實例化該類。這樣你的代碼保持乾淨(不是「哈克」),並且你不會意外地從類字段引用1.6+類。在我的代碼中,我擁有如下所示的版本測試類:
public class Android8 {
private static final String TAG = "Android8";
// public test variables
public static final boolean IS_V8;
public static final boolean AT_LEAST_V8;
private static final Object pimpl;
static {
int sdk_int = 0;
try {
Field field = Build.VERSION.class.getField("SDK");
String sdk_str = (String)field.get(null);
sdk_int = Integer.parseInt(sdk_str);
} catch(Throwable e) {
}
IS_V8 = (sdk_int==8);
AT_LEAST_V8 = (sdk_int>=8);
if(AT_LEAST_V8) {
pimpl = new Implementation();
} else {
pimpl = null;
}
}
// Version safe interface
public static void Camera_setDisplayOrientation(Camera camera, int degrees) {
if(AT_LEAST_V8)
((Implementation)pimpl).Camera_setDisplayOrientation(camera, degrees);
}
// Will cause a verify error if loaded in a pre Android8 environment
private static final class Implementation {
public static void Camera_setDisplayOrientation(Camera camera, int degrees) {
camera.setDisplayOrientation(degrees);
}
}
}
來源
2011-03-26 18:30:14
Anm
不錯的問題+1 – Sameer 2012-02-02 09:13:16