如何比较Android版本

时间:2011-08-03 03:27:13

标签: android version android-version

我正在尝试在我的代码中创建一个条件来比较Android版本。类似于;

if(version < 2.2)
{// TODO } 
else
{// TODO }

有谁请让我知道怎么做?

3 个答案:

答案 0 :(得分:2)

您可以使用Build.VERSION.SDK_INT将它们作为整数版本进行比较,因此对于SDK v 2.2,您将获得值为8的整数,而SDK v 3.0将获得11

请查看此bunch of how to get android version question asked and answered again,建议您使用搜索框,并在撰写问题时浏览相关问题:)

答案 1 :(得分:1)

使用此代码:

int currentVersion = android.os.Build.VERSION.SDK_INT;
if (currentVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
    // Do something for lollipop and above versions
} else{
    // do something for phones running an SDK before lollipop
}

答案 2 :(得分:0)

Ttry this:

private boolean compareVersion(String currrentsVersion, String officialVersion) {
    isEqual = false;

    String[] string1 = currrentsVersion.split("[.]");
    String[] string2 = officialVersion.split("[.]");
    Integer[] number2 = new Integer[string2.length];

    Integer[] numbers = new Integer[string2.length];


    for (int i = 0; i < string2.length; i++) {
        if (string1.length-1<i)
            numbers[i] = 0;
        else
            numbers[i] = Integer.parseInt(string1[i]);
        System.out.println("number1 ::: " + numbers[i]);
    }


    for (int i = 0; i < string2.length; i++) {
        number2[i] = Integer.parseInt(string2[i]);
        System.out.println("number2 ::: " + number2[i]);
    }

    for (int i = 0; i < number2.length; i++) {
        if (number2[i] > numbers[i]) {
            isEqual = false;
            break;
        } else {
            isEqual = true;
        }
    }
    return isEqual;
}

在您想要比较版本的地方调用此方法。

if (compareVersion(getVersionInfo(), officialVersion)) 
    tvUpdateStatus.setText("Have the latest version installed :) ");
else 
    tvUpdateStatus.setText("Plz Update the Application");

我希望它会有所帮助。 ;)