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


当前回答

试试这个:

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

其他回答

我改进了我使用的代码

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”现在。

是这样的:

String versionRelease = BuildConfig.VERSION_NAME;

versionRelease :- 2.1.17

请确保您的导入包是正确的(导入包your_application_package_name,否则将无法正常工作)。

明白了。它使用Context类的getApplicationInfo()方法。

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