利用compareTo实现版本号判断

     当app版本升级需要取服务端版本号与本地app版本号对比,若服务端版本号 >= 本地版本号,则提示用户更新。取服务端版本号过程我就不赘述了。

今天主要想要讲的是版本号的判断,之前我的做法是利用字符串截取、按位逐一判断的方法去实现,其实有一个更简单的方法

int java.lang.String.compareTo(String string)


public int compareTo (String string)

Added in  API level 1

Compares the specified string to this string using the Unicode values of the characters. Returns 0 if the strings contain the same characters in the same order. Returns a negative integer if the first non-equal character in this string has a Unicode value which is less than the Unicode value of the character at the same position in the specified string, or if this string is a prefix of the specified string. Returns a positive integer if the first non-equal character in this string has a Unicode value which is greater than the Unicode value of the character at the same position in the specified string, or if the specified string is a prefix of this string.

Parameters
string the string to compare.
Returns
  • 0 if the strings are equal, a negative integer if this string is before the specified string, or a positive integer if this string is after the specified string.
Throws
NullPointerException if string is null.

如:

String versionxt = getSoftVersion(); // 本地版本号

String versionInfowl = versionInfo.getVersion(); // 服务端版本号

if (versionxt.compareTo(versionInfowl) >= 0) {

return; // 不提示

}


哎呀,自己太笨了……


你可能感兴趣的:(利用compareTo实现版本号判断)