Sometimes making an app is a feature that requires a version of the information, then you need to read the current version number, let me introduce a method
Set the version number of the program in Androidmanifest.xml, such as android:versionname= "1.0.0"
If you want to get this version number in the code, you can use the following method (these modify the version number only need to modify the settings in the Androidmanifest.xml, do not change the code)
All we have to read is Versionname, okay, on the code.
/**
* Returns the current program version name
*/
public static 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;
if (Versionname = = NULL | | versionname.length () <= 0) {
Return "";
}
} catch (Exception e) {
LOG.E ("Versioninfo", "Exception", e);
}
return versionname;
}
I'll just change the versionname in Androidmanifest.xml .
Just call this method to get the version number, such as a Textvew.settext (getappversionname (this)); context This parameter into an activity on the line, OK, so simple.
Get the app version number or the current version number