Obtain the App version number or the current version number.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.