利用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
stringthe 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
NullPointerExceptionif string is null.

如:

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

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

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

return; // 不提示

}


哎呀,自己太笨了……


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值