正文  高级教程 > APK程序信息 >

Android开发 在同一个app中使用不一样的sdk

这个是android_sdk的例子使用的方法public static ContactAccessor getInstance() {if (sInstance == null) { String className; /* * Check the version of the SDK we are running on. ......

这个是android_sdk的例子使用的方法

 

 

public static ContactAccessor getInstance() {
if (sInstance == null) { 
String className; 

/* 
* Check the version of the SDK we are running on. Choose an 
* implementation class designed for that version of the SDK. 
* 
* Unfortunately we have to use strings to represent the class 
* names. If we used the conventional ContactAccessorSdk5.class.getName() 
* syntax, we would get a ClassNotFoundException at runtime on pre-Eclair SDKs. 
* Using the above syntax would force Dalvik to load the class and try to 
* resolve references to all other classes it uses. Since the pre-Eclair 
* does not have those classes, the loading of ContactAccessorSdk5 would fail. 
*/ 
@SuppressWarnings("deprecation") 
int sdkVersion = Integer.parseInt(Build.VERSION.SDK); // Cupcake style 
if (sdkVersion < Build.VERSION_CODES.ECLAIR) { 
className = "com.example.android.businesscard.ContactAccessorSdk3_4"; 
} else { 
className = "com.example.android.businesscard.ContactAccessorSdk5"; 
} 

/* 
* Find the required class by name and instantiate it. 
*/ 
try { 
Class<? extends ContactAccessor> clazz = 
Class.forName(className).asSubclass(ContactAccessor.class); 
sInstance = clazz.newInstance(); 
} catch (Exception e) { 
throw new IllegalStateException(e); 
} 
} 

return sInstance; 
}