-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionUtils.java
More file actions
41 lines (39 loc) · 1.23 KB
/
VersionUtils.java
File metadata and controls
41 lines (39 loc) · 1.23 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
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public class VersionUtils {
/**
* 获取版本号
*
* @param context
* @return
*/
public static String getVersionName(Context context) {
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "1.0.0";
}
/**
* 获取版本code
*
* @param context
* @return
*/
public static int getVersionCode(Context context) {
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
}