How to get the app version number in Android

Source: Internet
Author: User

Application Scenarios:

1. Display the version number of the application in the interface;
2, the user launches the application, the background to determine whether the application is the latest version.
Each of these scenarios requires that the app's version number be automatically obtained in the program.

Idea Introduction :
In Android, The version number of the application is configured in the Androidmanifest.xml file, and the PackageInfo class encapsulates all the information obtained from the configuration file, describing the overall information of the package content, so you can use the Versionname property of the PackageInfo object to get the application's The version number.
How do you get PackageInfo objects? can be obtained by Packagemanager objects. Packagemanager is a class that retrieves various information about an application package that is currently installed on the device. The Getpackageinfo method in the Packagemanager object can get the PackageInfo object, which needs to pass two parameters: the application package name and the condition. Typically, the application's package name can be obtained through the Getpackagename () method of the activity or context (activity inherits from the context), and the addition can have many settings, typically set to 0.
Finally, the Packagemanager object is fetched, and the context object provides the Getpackagemanager () method to get the object.

In summary, the template code is as follows: (Note that the method encapsulated here is in an activity, so use this instead of the context object)

1/** 2  * Get version number 3  * @return The current app version number 4  */5 public String GetVersion () {6     try {7         Packagemanager mana GER = This.getpackagemanager (); 8         PackageInfo info = manager.getpackageinfo (this.getpackagename (), 0); 9         String Version = info.versionname;10< C7/>return this.getstring (r.string.version_name) + version;11     } catch (Exception e) {         e.printstacktrace ();         return this.getstring (r.string.can_not_find_version_name);     }15}

How to get the app version number in Android

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.