package com.example.myapplication2;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
/***
* 获取版本信息
*/
public class SystemHelper {
/**
* 获取本地软件版本号
*/
public static int getLocalVersion(Context context) {
int localVersion = 0;
try {
PackageInfo packageInfo = context.getApplicationContext()
.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
localVersion = packageInfo.versionCode;
Log.d("当前版本号:" , String.valueOf(localVersion));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
/**
* 获取本地软件版本号名称
*/
public static String getLocalVersionName(Context context) {
String localVersion = "";
try {
PackageInfo packageInfo = context.getApplicationContext()
.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
localVersion = packageInfo.versionName;
Log.d("当前版本名称:", localVersion);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
}
activity中使用
Context context = getApplicationContext();
SystemHelper.getLocalVersion(context);
SystemHelper.getLocalVersionName(context);