0

According to https://developer.android.com/reference/android/os/Build.VERSION.html#RELEASE, Build.VERSION.RELEASE contains string like "3.4b5" while I was expecting a sequence of numbers separated with a dot(.) like "8.1.0".

Any ideas on when I'll be seeing characters in OS versions and a good comparison logic to compare current OS version with a fixed string(say 7.0.0).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jayaprakash Mara
  • 323
  • 4
  • 11

1 Answers1

0

For comparing version numbers you could do something like this:

//  compare two strings, e.g. "5.8.0" with "1.88.3"
public int compare(String v1, String v2) {
    String s1 = normalisedVersion(v1);  //  normalized to "   5   8   0"
    String s2 = normalisedVersion(v2);  //  normalized to "   1  88   3"
    return s1.compareTo(s2);            //  returns -1 for lower, 0 equal and 1 for greater
}

public String normalisedVersion(String version) {
    return normalisedVersion(version, ".", 4);
}

public String normalisedVersion(String version, String sep, int maxWidth) {
    String[] split = Pattern.compile(sep, Pattern.LITERAL).split(version);
    StringBuilder sb = new StringBuilder();
    for (String s : split) {
        sb.append(String.format("%" + maxWidth + 's', s));
    }
    return sb.toString();
}
coyer
  • 4,122
  • 3
  • 28
  • 35