封装的工具类:
import android.content.Content;
import android.content.pm.PackageManager;
public class APKVersionCodeUtils {
public static int getVersionCode(Context mContext) {
int versionCode = 0;
try {
versionCode = mContext.getPackageManager().
getPackageInfo(mContext.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().
getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verName;
}
}
调用方式:
String versionName = APKVersionCodeUtils.getVerName(this);
String versionCode = APKVersionCodeUtils.getVersionCode(this) + "";
Log.i(TAG,"versionName------"+versionName);
Log.i(TAG,"versionName------"+versionName);