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


当前回答

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

其他回答

很简单:

   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

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

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

是这样的:

String versionRelease = BuildConfig.VERSION_NAME;

versionRelease :- 2.1.17

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

综上所述,以下是我用来检测设备是否有Froyo或更新的Android OS(2.2+)的代码:

public static boolean froyoOrNewer() {
    // SDK_INT is introduced in 1.6 (API Level 4) so code referencing that would fail
    // Also we can't use SDK_INT since some modified ROMs play around with this value, RELEASE is most versatile variable
    if (android.os.Build.VERSION.RELEASE.startsWith("1.") ||
        android.os.Build.VERSION.RELEASE.startsWith("2.0") ||
        android.os.Build.VERSION.RELEASE.startsWith("2.1"))
        return false;

    return true;
}

显然,如果你需要通用的检查器,你可以修改if条件来考虑1.0和1.5版本的Android。你可能会得到这样的结果:

// returns true if current Android OS on device is >= verCode 
public static boolean androidMinimum(int verCode) {
    if (android.os.Build.VERSION.RELEASE.startsWith("1.0"))
        return verCode == 1;
    else if (android.os.Build.VERSION.RELEASE.startsWith("1.1")) {
        return verCode <= 2;
    } else if (android.os.Build.VERSION.RELEASE.startsWith("1.5")) {
        return verCode <= 3;
    } else {
        return android.os.Build.VERSION.SDK_INT >= verCode;
    }
}

如果代码不适合你,请告诉我。