using the Package Manager
Package com.liunan.myfirstapp.util;
Import Android.content.Context;
Import Android.content.pm.ApplicationInfo;
Import Android.content.pm.PackageInfo;
Import Android.content.pm.PackageManager;
/** * Created by Liu Nan on 2016-03-22. * * public class Packageutils {/** * get version Name * @param context * @return Version name */public static String Getversionname (Co ntext context) {//Get Package Manager Packagemanager PM = Context.getpackagemanager ();//Get package info try {packageinfo PackageInfo = pm.getpa
Ckageinfo (Context.getpackagename (), 0);
Returns the version number return packageinfo.versionname;
catch (Packagemanager.namenotfoundexception e) {e.printstacktrace ();} return null; /** * Get Version number * @param context * @return Version number */public static int Getversioncode (context contexts) {//Get Package Manager Packageman
Ager pm = Context.getpackagemanager (); Get package information try {packageinfo PackageInfo = Pm.getpackageinfo (Context.getpackagename (), 0);//back to version number return
Packageinfo.versioncode;
catch (Packagemanager.namenotfoundexception e) {e.printstacktrace ();}return 0; /** * Get the name of the app * @param context * @return name */public static String Getappname (context contexts) {Packagemanager pm
= Context.getpackagemanager (); Get package information try {packageinfo PackageInfo = Pm.getpackageinfo (Context.getpackagename (), 0);//Get Application information ApplicationInfo
ApplicationInfo = Packageinfo.applicationinfo;
Get albelres int labelres = applicationinfo.labelres;
Returns the name of the app return Context.getresources (). getString (Labelres);
catch (Packagemanager.namenotfoundexception e) {e.printstacktrace ();} return null; }
}
Let's share a piece of code to introduce Android get app version number and version name
/Below is the tool method for obtaining version information
The version name is public
static String Getversionname (context context) {return
getpackageinfo. Versionname
}
//Version number public
static int Getversioncode (context) {return
getpackageinfo. Versioncode
}
private static PackageInfo Getpackageinfo (context context) {
PackageInfo pi = null;
try {
Packagemanager pm = Context.getpackagemanager ();
Pi = Pm.getpackageinfo (Context.getpackagename (),
packagemanager.get_configurations);
return pi;
} catch (Exception e) {
e.printstacktrace ();
}
return pi;
}
About Android Get application version number and version name of knowledge, small make up to everyone here, hope to help!