1 // 比较版本号
2 public static int compareVersion(String newVersion, String currentVersion) {
3 if (TextUtils.isEmpty(newVersion)) {
4 if (TextUtils.isEmpty(currentVersion)) {
5 return 0;
6 } else {
7 return -1;
8 }
9 } else if (TextUtils.isEmpty(currentVersion)) {
10 return 1;
11 }
12
13 String[] first = newVersion.split("\\.");
14 String[] second = currentVersion.split("\\.");
15 Log.d(TAG, "first: " + Arrays.asList(first) + ", second: " + Arrays.asList(second));
16
17 int count = Math.min(first.length, second.length);
18 for (int i = 0; i < count; i++) {
19 try {
20 int firstVersionNumber = Integer.parseInt(first[i]);
21 int secondVersionNumber = Integer.parseInt(second[i]);
22
23 if (firstVersionNumber < secondVersionNumber) {
24 return -1;
25 } else if (firstVersionNumber > secondVersionNumber) {
26 return 1;
27 }
28
29 } catch (Exception ignored) {
30 }
31 }
32
33 if (first.length < second.length) {
34 return -1;
35 } else if (first.length > second.length) {
36 return 1;
37 }
38
39 return 0;
40 }