获取的方法:
/* 获取本地软件版本号 */ public static int getLocalVersion(Context ctx) { int localVersion = 0; try { PackageInfo packageInfo = ctx.getApplicationContext() .getPackageManager() .getPackageInfo(ctx.getPackageName(), 0); localVersion = packageInfo.versionCode; Log.d("TAG", "当前版本号:" + localVersion); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return localVersion; } /** * 获取本地软件版本号名称 */ public static String getLocalVersionName(Context ctx) { String localVersion = ""; try { PackageInfo packageInfo = ctx.getApplicationContext() .getPackageManager() .getPackageInfo(ctx.getPackageName(), 0); localVersion = packageInfo.versionName; Log.d("TAG", "当前版本名称:" + localVersion); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return localVersion; } 在需要显示的地方就可以在TextView中显示了:
//显示版本名称 tv_version_name.setText("泛贝 " + getLocalVersionName(this));