Obtain the App version number or the current version number.
Sometimes the App requires a version information function, and 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 the version number in the code, you can use the following method (when modifying the version number, you only need to modify the settings in AndroidManifest. xml. You do not need to modify the code)
What we want to read is versionName. Okay, go to 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;
}
In the future, you only need to modify the versionName in AndroidManifest. xml.
You only need to call this method to obtain the version number, for example, a textVew. setText (getAppVersionName (this); the context parameter is used to input an Activity. Well, that's easy.