Get the app version number or the current version number

Source: Internet
Author: User

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

Related Article

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.