1.获取自己应用内部的版本号
/**
* 获取自己应用内部的版本号
*/
public static int getVersionCode(Context context) {
PackageManager manager = context.getPackageManager();
int code = 0;
try {
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
code = info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return code;
}
2.获取自己应用内部的版本名
/**
* 获取自己应用内部的版本名
*/
public static String getVersionName(Context context) {
PackageManager manager = context.getPackageManager();
String name = null;
try {
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
name = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return name;
}
3.获取其他应用的版本号
public static int getVersionCode(Context context,String packageName) {
PackageManager manager = context.getPackageManager();
int code = 0;
try {
PackageInfo info = manager.getPackageInfo(packageName, 0);
code = info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return code;
}
4.获取其他应用的版本名
public static String getVersionName(Context context,String packageName) {
PackageManager manager = context.getPackageManager();
String name = null;
try {
PackageInfo info = manager.getPackageInfo(packageName, 0);
name = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return name;
}
作者:懵懵懂懂_YOYO
链接:https://www.jianshu.com/p/e8a6831283b3
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。