package com.example.grenaderose.redthunder.utils; import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; /**
* Description :
* Author : hanbao
* Date : 2019/3/9 0009 下午 4:30
*/
public class PackUtils { //获取版本名称
public static String getVersionName(Context ctx) {
//包管理器
PackageManager pm = ctx.getPackageManager(); //获取包的信息(app的信息)
try {
PackageInfo packageInfo = pm.getPackageInfo(ctx.getPackageName(), 0); String versionName = packageInfo.versionName;//版本名称
int versionCode = packageInfo.versionCode;//版本号 System.out.println("versionName:" + versionName);
System.out.println("versionCode:" + versionCode); return versionName; } catch (PackageManager.NameNotFoundException e) {
//未找到包名的异常
e.printStackTrace();
} return "";
} //获取版本号
public static int getVersionCode(Context ctx) {
//包管理器
PackageManager pm = ctx.getPackageManager(); //获取包的信息(app的信息)
try {
PackageInfo packageInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
int versionCode = packageInfo.versionCode;//版本号 return versionCode;
} catch (PackageManager.NameNotFoundException e) {
//未找到包名的异常
e.printStackTrace();
} return -1;
}
}