Android的:如何在code根据API的版本?根据、版本、如何在、Android

2023-09-04 23:56:56 作者:零度de遇见.

在Android中,我得到的SDK轻松版本( Build.VERSION.SDK ),但我需要使用LabeledIntent只有当平台比1.6更新(> Build.VERSION_ codeS.DONUT

我想,反思是必要的(我已阅读此链接但目前尚不清楚的一类或给我)。

这是在code,但它给了我一个例外,因为在我的Andr​​oid 1.6,如果该包存在,即使不应用的条件编译验证:

 意图theIntent = ....;
      如果(的Integer.parseInt(Build.VERSION.SDK)> Build.VERSION_ codeS.DONUT)
   {
 尝试{
             意图int​​entChooser = Intent.createChooser(意向,这些节目之间的选择);
              Parcelable [] parcelable =新Parcelable [1];
              parcelable [0] =新android.content.pm.LabeledIntent(theIntent,,Texto平,0);
               intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,parcelable);
  activity.startActivity(intentChooser);
   }
   赶上(例外五)
   {
    activity.startActivity(theIntent);
   }

  } 其他
  {
   activity.startActivity(intentMedicamento);
  }
 

我怎么解决它,有几点需要正确的答案

@Commonsware告诉我这样做的方式。我们创建了一个桥梁类,以便根据不同的API级别,你比如一个使用API​​级别或使用另一个API级别的其他类的类。 唯一的细节1初学者能忘记的是,你必须编译与最新的SDK你goint要参考你的应用程序。

 公共抽象类LabeledIntentBridge {
 公共抽象的意图BuildLabeledIntent(字符串URL,意图theintent);

 公共静态最终LabeledIntentBridge INSTANCE = buildBridge();

 私有静态LabeledIntentBridge buildBridge(){
  INT SDK =新的整数(Build.VERSION.SDK).intValue();

  如果(SDK小于5){
   返程(新LabeledIntentOld());
  }

  返回(新LabeledIntentNew());
 }
}
 
android版本与对应的API版本

所以在 LabeledIntentNew ,我包括所有的code,它是指 LabeledIntent 仅在API可用LEVEL 5在 LabeledIntentOld ,我可以实现另一种控制,在我的情况下,我回到了自己的意图没有做罢了。

这一类的调用是这样做的:

  LabeledIntentBridge.INSTANCE.BuildLabeledIntent(URLtest,theIntent);
 

解决方案

按照包装类模式的记录在你的网页链接到上述。

In Android I get the version of the SDK easily (Build.VERSION.SDK) but I need to use LabeledIntent only if the platform is newer than 1.6 (>Build.VERSION_CODES.DONUT)

I suppose that Reflection is necessary (I have read this link but it is not clear for a class or to me).

This is the code but it gives me an exception because in my Android 1.6, the compiler verifies if the package exists even if the condition is not applied:

 Intent theIntent=....;
      if(Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.DONUT)
   {    
 try{
             Intent intentChooser = Intent.createChooser(intent,"Choose between these programs");
              Parcelable[] parcelable = new Parcelable[1];
              parcelable[0] = new android.content.pm.LabeledIntent(theIntent, "", "Texto plano", 0);
               intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, parcelable); 
  activity.startActivity(intentChooser);
   }
   catch(Exception e)
   {
    activity.startActivity(theIntent);
   }

  } else
  {
   activity.startActivity(intentMedicamento);
  }

HOW I SOLVED IT, SOME NOTES TO THE RIGHT ANSWER

@Commonsware show me the way to do it. We create a bridge class so that depending on the API LEVEL, you instance one class that uses an API LEVEL or another class that uses another API LEVEL. The only detail one beginner could forget is that you have to compile your app with the newest SDK you are goint to make reference.

public abstract class LabeledIntentBridge {
 public abstract Intent BuildLabeledIntent(String URL, Intent theintent);

 public static final LabeledIntentBridge INSTANCE=buildBridge();

 private static LabeledIntentBridge buildBridge() {
  int sdk=new Integer(Build.VERSION.SDK).intValue();

  if (sdk<5) {
   return(new LabeledIntentOld());
  }

  return(new LabeledIntentNew());
 }
}

So in the LabeledIntentNew, I included all the code that refers to LabeledIntent only available in API LEVEL 5. In LabeledIntentOld, I can implement another kind of control, in my case I return the intent itself without doing nothing more.

The call to this class is done like this:

LabeledIntentBridge.INSTANCE.BuildLabeledIntent(URLtest,theIntent);

解决方案

Follow the wrapper class pattern documented in the page you linked to above.

 
精彩推荐
图片推荐