如何检入 Android 版本(如 1.5 或 1.6)的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3014117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check in code the Android version like 1.5 or 1.6
提问by ilana
I need to check in code what Android release version is running currently on target device. Can you supply code example?
我需要检查代码中当前在目标设备上运行的 Android 版本。你能提供代码示例吗?
回答by demonreeper
I was looking for this and didn't find a solution - ended up here and I figured it out myself so for anyone out there looking for this:
我一直在寻找这个,但没有找到解决方案 - 最后到了这里,我自己想通了,所以任何人都在寻找这个:
int SDK_INT = android.os.Build.VERSION.SDK_INT;
int SDK_INT = android.os.Build.VERSION.SDK_INT;
this returns os sdk level 7 eclair 8 froyo etc
这将返回 os sdk level 7 eclair 8 froyo 等
回答by dimwinds
To get the build version of Android like: 2.2, 2.3.3, 4.0 or 4.0.3 ... use the following code:
要获取 Android 的构建版本,例如:2.2、2.3.3、4.0 或 4.0.3 ...使用以下代码:
String deviceVersion = Build.VERSION.RELEASE;
回答by Dror Cohen
I think this is a duplicate of my own question: ndk version at run time. Short answer: no easy way for a native application to do it (you could however run a Java app and communicate with it to get the version).
我认为这是我自己的问题的重复:运行时的 ndk 版本。简短回答:本机应用程序没有简单的方法来做到这一点(但是,您可以运行 Java 应用程序并与其通信以获取版本)。
回答by zed_0xff
can you execute getprop ro.build.version.release
shell command on your device ?
你能getprop ro.build.version.release
在你的设备上执行shell 命令吗?
回答by ilana
This works
这有效
also import the followng:
还导入以下内容:
import com.android.phonetests.TEST_INTERFACE;
import android.os.Build;
import android.app.ActivityThread;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
private int GetSDKVersion()
{
int version = 0;
IPackageManager pm = ActivityThread.getPackageManager();
try
{
//returns a ref to my application according to its application name
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.android.phonetests", 0);
if (applicationInfo != null)
{
version = applicationInfo.targetSdkVersion; ////this makes the same -> version = Build.VERSION.SDK_INT
Log.i(LOG_TAG,"[DBG] version: " + version);
//2 is 5
//2.01 6 (Donut - 2.01)
//2.2 7 (Eclair - 2.2) currently it is Eclair_MR1 (Major Release)
switch (version)
{
case Build.VERSION_CODES.ECLAIR_MR1:
Log.i(LOG_TAG,"[DBG] version: ECLAIR");//2.2 7 (Eclair - 2.2) currently it is Eclair_MR1 (Major Release)
break;
case Build.VERSION_CODES.DONUT:
Log.i(LOG_TAG,"[DBG] version: DONUT");//2.01 6 (Donut - 2.01)
break;
}
}
}
catch (android.os.RemoteException e){}
return version;
}