应用版本的比较

时间:2016-08-23 12:20:30

标签: android

如何比较Android中的应用版本

我获得了最新版本代码和当前版本代码,但问题是

NSRange

let oldRect = (change?[NSKeyValueChangeOldKey] as! NSValue).rectValue

那么如何比较android

中的浮点值

5 个答案:

答案 0 :(得分:1)

使用以下方法比较版本号:

首先将float转换为String。

public static int versionCompare(String str1, String str2) {
    String[] vals1 = str1.split("\\.");
    String[] vals2 = str2.split("\\.");
    int i = 0;
    // set index to first non-equal ordinal or length of shortest version string
    while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
      i++;
    }
    // compare first non-equal ordinal number
    if (i < vals1.length && i < vals2.length) {
        int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
        return Integer.signum(diff);
    }
    // the strings are equal or one string is a substring of the other
    // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
    return Integer.signum(vals1.length - vals2.length);
}

请参阅以下SO问题:Efficient way to compare version strings in Java

答案 1 :(得分:1)

<强> NumberFormatException

  

抛出以指示应用程序已尝试转换a   字符串到其中一个数字类型,但字符串没有   适当的格式。

根据您的要求调用 Float.compare

 float cur_Version = 1.0; //current version
 float latest_Version= 1.0.0; //latest version

 int checking= Float.compare(cur_Version , latest_Version);

 if(checking> 0) {
    System.out.println("cur_Version is greater than latest_Version");
 }
 else if(checking< 0) {
    System.out.println("cur_Version is less than latest_Version");
 }
 else {
    System.out.println("cur_Version is equal to latest_Version");
 }

要获得良好的方法,您可以访问 How do you compare two version Strings in Java?

答案 2 :(得分:1)

我编写了一个用于比较版本号的小型Android库:https://github.com/G00fY2/version-compare

它基本上是这样做的:

  public int compareVersions(String versionA, String versionB) {
    String[] versionTokensA = versionA.split("\\.");
    String[] versionTokensB = versionB.split("\\.");
    List<Integer> versionNumbersA = new ArrayList<>();
    List<Integer> versionNumbersB = new ArrayList<>();

    for (String versionToken : versionTokensA) {
      versionNumbersA.add(Integer.parseInt(versionToken));
    }
    for (String versionToken : versionTokensB) {
      versionNumbersB.add(Integer.parseInt(versionToken));
    }

    final int versionASize = versionNumbersA.size();
    final int versionBSize = versionNumbersB.size();
    int maxSize = Math.max(versionASize, versionBSize);

    for (int i = 0; i < maxSize; i++) {
      if ((i < versionASize ? versionNumbersA.get(i) : 0) > (i < versionBSize ? versionNumbersB.get(i) : 0)) {
        return 1;
      } else if ((i < versionASize ? versionNumbersA.get(i) : 0) < (i < versionBSize ? versionNumbersB.get(i) : 0)) {
        return -1;
      }
    }
    return 0;
  }

此代码段不提供任何错误检查或处理。除此之外,我的库还支持“1.2-rc”&gt;等后缀。 “1.2-β”。

答案 3 :(得分:1)

我参加聚会有点晚了,但是我为大家提供了一个很好的解决方案!

1。使用此类:

public class VersionComparator implements Comparator {

public boolean equals(Object o1, Object o2) {
    return compare(o1, o2) == 0;
}

public int compare(Object o1, Object o2) {
    String version1 = (String) o1;
    String version2 = (String) o2;

    VersionTokenizer tokenizer1 = new VersionTokenizer(version1);
    VersionTokenizer tokenizer2 = new VersionTokenizer(version2);

    int number1, number2;
    String suffix1, suffix2;

    while (tokenizer1.MoveNext()) {
        if (!tokenizer2.MoveNext()) {
            do {
                number1 = tokenizer1.getNumber();
                suffix1 = tokenizer1.getSuffix();
                if (number1 != 0 || suffix1.length() != 0) {
                    // Version one is longer than number two, and non-zero
                    return 1;
                }
            }
            while (tokenizer1.MoveNext());

            // Version one is longer than version two, but zero
            return 0;
        }

        number1 = tokenizer1.getNumber();
        suffix1 = tokenizer1.getSuffix();
        number2 = tokenizer2.getNumber();
        suffix2 = tokenizer2.getSuffix();

        if (number1 < number2) {
            // Number one is less than number two
            return -1;
        }
        if (number1 > number2) {
            // Number one is greater than number two
            return 1;
        }

        boolean empty1 = suffix1.length() == 0;
        boolean empty2 = suffix2.length() == 0;

        if (empty1 && empty2) continue; // No suffixes
        if (empty1) return 1; // First suffix is empty (1.2 > 1.2b)
        if (empty2) return -1; // Second suffix is empty (1.2a < 1.2)

        // Lexical comparison of suffixes
        int result = suffix1.compareTo(suffix2);
        if (result != 0) return result;

    }
    if (tokenizer2.MoveNext()) {
        do {
            number2 = tokenizer2.getNumber();
            suffix2 = tokenizer2.getSuffix();
            if (number2 != 0 || suffix2.length() != 0) {
                // Version one is longer than version two, and non-zero
                return -1;
            }
        }
        while (tokenizer2.MoveNext());

        // Version two is longer than version one, but zero
        return 0;
    }
    return 0;
}

// VersionTokenizer.java
public static class VersionTokenizer {
    private final String _versionString;
    private final int _length;

    private int _position;
    private int _number;
    private String _suffix;
    private boolean _hasValue;

    VersionTokenizer(String versionString) {
        if (versionString == null)
            throw new IllegalArgumentException("versionString is null");

        _versionString = versionString;
        _length = versionString.length();
    }

    public int getNumber() {
        return _number;
    }

    String getSuffix() {
        return _suffix;
    }

    public boolean hasValue() {
        return _hasValue;
    }

    boolean MoveNext() {
        _number = 0;
        _suffix = "";
        _hasValue = false;

        // No more characters
        if (_position >= _length)
            return false;

        _hasValue = true;

        while (_position < _length) {
            char c = _versionString.charAt(_position);
            if (c < '0' || c > '9') break;
            _number = _number * 10 + (c - '0');
            _position++;
        }

        int suffixStart = _position;

        while (_position < _length) {
            char c = _versionString.charAt(_position);
            if (c == '.') break;
            _position++;
        }

        _suffix = _versionString.substring(suffixStart, _position);

        if (_position < _length) _position++;

        return true;
    }
}

}

2。创建此功能

    private fun isNewVersionAvailable(currentVersion: String, latestVersion: String): Boolean {
    val versionComparator = VersionComparator()
    val result: Int = versionComparator.compare(currentVersion, latestVersion)
    var op = "=="
    if (result < 0) op = "<"
    if (result > 0) op = ">"
    System.out.printf("%s %s %s\n", currentVersion, op, latestVersion)
    return if (op == ">" || op == "==") {
        false
    } else op == "<"
}

3。并通过

调用

例如isNewVersionAvailable("1.2.8","1.2.9"),其中1.2.8是您当前的版本,1.2.9是最新版本,返回true!

答案 4 :(得分:0)

为什么要把这个搞得这么复杂? 只需缩放主要版本、次要版本和补丁版本即可:

fun getAppVersionFromString(version: String): Int { // "2.3.5"
    val versions = version.split(".") // [2, 3, 5]
    val major = versions[0].toIntOrDefault(0) * 1000 // 2000
    val minor = versions[1].toIntOrDefault(0) * 100 // 300
    val patch = versions[2].toIntOrDefault(0) * 10 // 50
    return major + minor + patch // 2350
}

这样,当你比较 9.10.10 和 10.0.0 时,第二个更大。