Android有两个版本号标识,分别为:versionCode,(整数值代表),versionName(字符串代表)
具体方法如下:
public static int getVerCode(Context context) {
int verCode = -1try {verCode = context.getPackageManager().getPackageInfo(
包名, 0).versionCode
} catch (NameNotFoundException e) {
Log.e(msg, e.getMessage())}return verCode}
public static String getVerName(Context context) {
String verName = try {verName = context.getPackageManager().getPackageInfo(
包名, 0).versionName
//获取版本名publicstatic String getAppVersionName(Context context) {
String versionName = ""
try {
// ---get the package info---
PackageManager pm = context.getPackageManager()
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0)
versionName = pi.versionName
versioncode = pi.versionCode
if (versionName == null || versionName.length() <= 0) {
return""
}
} catch (Exception e) {
Log.e("VersionInfo", "Exception", e)
}
return versionName
}
2.--------------------------------------------------------------------------
获取当前应用的版本号:
private String getVersionName() throws Exception
{
// 获取packagemanager的实例
PackageManager packageManager = getPackageManager()
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),0)
String version = packInfo.versionName
return version
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)