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


当前回答

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

所有版本都支持SDK

所以我这样做:

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

出处:CommonsWare

其他回答

我改进了我使用的代码

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;
}
Build.VERSION.RELEASE;

这将给你你的版本的实际数字;又名2.3.3或2.2。 使用Build.VERSION的问题。SDK_INT是如果你有一个根手机或自定义rom,你可能有一个非标准的操作系统(也就是我的android是运行2.3.5),这将返回null时使用Build.VERSION。所以Build.VERSION.RELEASE无论是否使用标准Android版本都可以正常工作!

要使用它,你可以这样做;

String androidOS = Build.VERSION.RELEASE;

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

我更喜欢把版本作为数字来处理,而不是这样写:

  public static float getAPIVerison() {

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

    return f.floatValue();
}

是这样的:

String versionRelease = BuildConfig.VERSION_NAME;

versionRelease :- 2.1.17

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