有没有办法获得手机当前正在运行的API版本?


当前回答

试试这个:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
     // only for gingerbread and newer versions
 }

其他回答

很简单:

   String manufacturer = Build.MANUFACTURER;
   String model = Build.MODEL;
   int version = Build.VERSION.SDK_INT;
   String versionRelease = Build.VERSION.RELEASE;

Log.e("MyActivity", "manufacturer " + manufacturer
            + " \n model " + model
            + " \n version " + version
            + " \n versionRelease " + versionRelease
    );

输出:

E/MyActivity:   manufacturer ManufacturerX
                model SM-T310 
                version 19 
                versionRelease 4.4.2

SDK。INT支持Android 1.6及以上版本

所有版本都支持SDK

所以我这样做:

String sdk_version_number = android.os.Build.VERSION.SDK;

出处:CommonsWare

android.os.Build.VERSION.SDK应该给出API级别的值。你可以很容易地在android文档中找到api级别到android版本的映射。我认为,8代表2.2,7代表2.1,以此类推。

我改进了我使用的代码

public static float getAPIVerison() {

    float f=1f;
    try {
        StringBuilder strBuild = new StringBuilder();
        strBuild.append(android.os.Build.VERSION.RELEASE.substring(0, 2));
        f= Float.valueOf(strBuild.toString());
    } catch (NumberFormatException e) {
        Log.e("myApp", "error retriving api version" + e.getMessage());
    }

    return f;
}

我通常喜欢在函数中添加这些代码来获得Android版本:

int whichAndroidVersion;

whichAndroidVersion= Build.VERSION.SDK_INT;
textView.setText("" + whichAndroidVersion); //If you don't use "" then app crashes.

例如,上面的代码将文本设置到我的textView为“29”现在。