android开发app升级时比较版本号的实现方法

public static boolean isVersionNew(String newVer, String lastVer) {
        if (TextUtils.isEmpty(newVer) || TextUtils.isEmpty(lastVer)) {
            return false;
        }
        String[] newVerSplit = newVer.split("\\.");
        String[] lastVerSplit = lastVer.split("\\.");

        int maxLen = newVerSplit.length > lastVerSplit.length ? newVerSplit.length : lastVerSplit.length;
        for (int i = 0; i < maxLen; i++) {
            int newVerNum = strToInt(i < newVerSplit.length ? newVerSplit[i] : "0");
            int lastVerNum = strToInt(i < lastVerSplit.length ? lastVerSplit[i] : "0");
            if (newVerNum > lastVerNum) {
                return true;
            } else if (newVerNum < lastVerNum) {
                return false;
            }
        }
        return false;
    }

    private static int strToInt(String numStr) {
        try {
            return Integer.parseInt(numStr);
        } catch (Exception e) {
            return 0;
        }
    }
测试用例:
isVersionNew("13.02.01","13.01.90")
isVersionNew("13.02.10","13.02.01")
posted @ 2022-12-19 16:17  yongfengnice  阅读(198)  评论(0编辑  收藏  举报