且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查蜂窝或更高的运行,并据此要求该版本的方法?

更新时间:2023-09-15 16:42:46

  

要更precise,我MinSDK为7(Android 2.1的),TargetSDK是8(安卓2.2),我需要测试是否蜂窝的Andr​​oid 3.0或更高版本上运行。根据这一点,我怎么能称之为蜂巢的方法?

步骤1:将您的构建目标,你要直接调用,因此对编译的最高API级别。您的构建目标(例如,项目>属性> Android的)是不是与你的机器人:targetSdkVersion

步骤#2:由于其他答案均表示,可以再有条件地调用保护块中的方法:

 如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_ codeS.HONEYCOMB){
  //调用东西API等级11+
}
 

  

问题的第二部分出现了,因为只调用该蜂窝方法,将无法编译,因为我对建设2.2。

您需要更改您的构建目标是API级别11或者,如果你想直接调用API级别11或更高的方法高。

For Android Versions 3.0 and higher, I want to call a certain method. Is there a way to check if a certain method is available in the running Android Version?

To be more precise, my MinSDK is 7 (Android 2.1), TargetSDK is 8 (Android 2.2) and I need to test if HoneyComb Android 3.0 or higher is running. Depending on that, how can I call that HoneyComb method?

The second part of the question arises, because simply calling that HoneyComb method, will not compile, as I am building against 2.2.

To be more precise, my MinSDK is 7 (Android 2.1), TargetSDK is 8 (Android 2.2) and I need to test if HoneyComb Android 3.0 or higher is running. Depending on that, how can I call that HoneyComb method?

Step #1: Set your build target to the highest API level you wish to call directly and therefore compile against. Your build target (e.g., Project > Properties > Android) is not related to your android:targetSdkVersion.

Step #2: As the other answers have indicated, you can then conditionally call methods within a guard block:

if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
  // call something for API Level 11+
}

The second part of the question arises, because simply calling that HoneyComb method, will not compile, as I am building against 2.2.

You need to change your build target to be API Level 11 or higher if you wish to directly call API Level 11 or higher methods.