Android get Application Version number and version name _android

Source: Internet
Author: User

using the Package Manager

Package com.liunan.myfirstapp.util;
Import Android.content.Context;
Import Android.content.pm.ApplicationInfo;
Import Android.content.pm.PackageInfo;
Import Android.content.pm.PackageManager;
/** * Created by Liu Nan on 2016-03-22. * * public class Packageutils {/** * get version Name * @param context * @return Version name */public static String Getversionname (Co ntext context) {//Get Package Manager Packagemanager PM = Context.getpackagemanager ();//Get package info try {packageinfo PackageInfo = pm.getpa
Ckageinfo (Context.getpackagename (), 0);
Returns the version number return packageinfo.versionname;
catch (Packagemanager.namenotfoundexception e) {e.printstacktrace ();} return null; /** * Get Version number * @param context * @return Version number */public static int Getversioncode (context contexts) {//Get Package Manager Packageman
Ager pm = Context.getpackagemanager (); Get package information try {packageinfo PackageInfo = Pm.getpackageinfo (Context.getpackagename (), 0);//back to version number return
Packageinfo.versioncode;
catch (Packagemanager.namenotfoundexception e) {e.printstacktrace ();}return 0; /** * Get the name of the app * @param context * @return name */public static String Getappname (context contexts) {Packagemanager pm
= Context.getpackagemanager (); Get package information try {packageinfo PackageInfo = Pm.getpackageinfo (Context.getpackagename (), 0);//Get Application information ApplicationInfo
ApplicationInfo = Packageinfo.applicationinfo;
Get albelres int labelres = applicationinfo.labelres;
Returns the name of the app return Context.getresources (). getString (Labelres);
catch (Packagemanager.namenotfoundexception e) {e.printstacktrace ();} return null; }
}

Let's share a piece of code to introduce Android get app version number and version name

/Below is the tool method for obtaining version information

The version name is public
static String Getversionname (context context) {return
getpackageinfo. Versionname
}
//Version number public
static int Getversioncode (context) {return
getpackageinfo. Versioncode
}
private static PackageInfo Getpackageinfo (context context) {
PackageInfo pi = null;
try {
Packagemanager pm = Context.getpackagemanager ();
Pi = Pm.getpackageinfo (Context.getpackagename (),
packagemanager.get_configurations);
return pi;
} catch (Exception e) {
e.printstacktrace ();
}
return pi;
}

About Android Get application version number and version name of knowledge, small make up to everyone here, hope to help!

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.