获取app版本号的工具类

时间:2022-11-26 17:34:13

/**
* 这个类用来获取这款app的版本号
*/
public class PackageUtil {

public static String getPackageVersion(Context context){

String version = "1.0";

//通过上下文获取Packagemanager
PackageManager manager = context.getPackageManager();

try {
//通过manager获取package信息
PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);

//获取了app版本号
version = packageInfo.versionName;


} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return version;

}
}