Use:
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
And you can get the version code by using this
int verCode = pInfo.versionCode;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…