forked from zilianliuxue/AndroidStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionUtil.java
More file actions
52 lines (49 loc) · 1.74 KB
/
Copy pathVersionUtil.java
File metadata and controls
52 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
/**
* 基本功能:app版本工具
*/
public class VersionUtil {
/**
* 获取版本号
*
* @return 当前应用的版本号
*/
public static String getVersion(Context context) {
try {
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
String version = info.versionName;
return version;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 版本比较
*
* @param nowVersion app版本
* @param serverVersion 服务器版本
* @return
*/
public static boolean compareVersion(String nowVersion, String serverVersion) {
if (nowVersion != null && serverVersion != null) {
String[] nowVersions = nowVersion.split("\\.");
String[] serverVersions = serverVersion.split("\\.");
if (nowVersions != null && serverVersions != null && nowVersions.length > 1 && serverVersions.length > 1) {
int nowVersionsFirst = Integer.parseInt(nowVersions[0]);
int serverVersionFirst = Integer.parseInt(serverVersions[0]);
int nowVersionsSecond = Integer.parseInt(nowVersions[1]);
int serverVersionSecond = Integer.parseInt(serverVersions[1]);
if (nowVersionsFirst < serverVersionFirst) {
return true;
} else if (nowVersionsFirst == serverVersionFirst && nowVersionsSecond < serverVersionSecond) {
return true;
}
}
}
return false;
}
}