Backward Compatibility for Applications(向后兼容)--摘自--android.doc.resources.articles

 

1.Setting the minSdkVersion

设置最小支持的sdk

<manifest>
   ...
   
<uses-sdkandroid:minSdkVersion="3"/>
   ...
 
</manifest>

2.Using reflection

使用反射检测要 使用的类 在当前运行环境下 是否有需要调用的属性或方法。

like android.os.Debug.dumpHprofData(String filename). The Debug class has existed since Android 1.0, but the method is new in Anroid 1.5 (API Level 3). If you try to call it directly, your app will fail to run on devices running Android 1.1 or earlier.

public class Reflect{
   
private static Method mDebug_dumpHprofData;

   
static{
       initCompatibility
();
   
};

   
private static void initCompatibility(){
       
try{
           mDebug_dumpHprofData
=Debug.class.getMethod(
                   
"dumpHprofData",newClass[]{String.class});
           
/* success, this is a newer device */
       
}catch(NoSuchMethodException nsme){
           
/* failure, must be older device */
       
}
   
}

   
private static void dumpHprofData(String fileName)throws IOException{
       
try{
           mDebug_dumpHprofData
.invoke(null, fileName);
       
}catch(InvocationTargetException ite){
           
/* unpack original exception when possible */
           
Throwable cause = ite.getCause();
           
if(cause instanceof IOException){
               
throw(IOException) cause;
           
}else if(cause instanceofRuntimeException){
               
throw(RuntimeException) cause;
           
}else if(cause instanceofError){
               
throw(Error) cause;
           
}else{
               
/* unexpected checked exception; wrap and re-throw */
               
thrownewRuntimeException(ite);
           
}
       
}catch(IllegalAccessException ie){
           
System.err.println("unexpected "+ ie);
       
}
   
}

   
public void fiddle(){
       
if(mDebug_dumpHprofData !=null){
           
/* feature is supported */
           
try{
               dumpHprofData
("/sdcard/dump.hprof");
           
}catch(IOException ie){
               
System.err.println("dump failed!");
           
}
       
}else{
           
/* feature not supported, do something else */
           
System.out.println("dump not supported");
       
}
   
}
}

3.Using a wrapper class

使用一个包裹类。

public class NewClass{
   
private static int mDiv =1;

   
private int mMult;

   
public static void setGlobalDiv(int div){
       mDiv
= div;
   
}

   
public NewClass(int mult){
       mMult
= mult;
   
}

   
public int doStuff(int val){
       
return(val * mMult)/ mDiv;
   
}
}

包裹类:

class WrapNew Class{
   
private NewClass mInstance;

   
/* class initialization fails when this throws an exception */
   
static{
       
try{
           
Class.forName("NewClass");
       
}catch(Exception ex){
           
throw new RuntimeException(ex);
       
}
   
}

   
/* calling here forces class initialization */
   
public static void checkAvailable(){}

   
public static void setGlobalDiv(int div){
       
NewClass.setGlobalDiv(div);
   
}

   
public WrapNewClass(int mult){
       mInstance
=new NewClass(mult);
   
}

   
public int doStuff(int val){
       
return mInstance.doStuff(val);
   
}
}

 

4.Testing is key

进行完整的sdk测试

转载于:https://www.cnblogs.com/maneater/archive/2012/04/19/2456688.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值