Android gets the version number of the current app and the version number of the current system

Source: Internet
Author: User

1. Get the current program version name

We can set the version number of the program in Androidmanifest.xml, such as android:versionname= "1.0", if you want to get this version number in the code, you can use the following method ( These modifications to the version number only need to modify the settings in the Androidmanifest.xml, without modifying the code):

/*** Returns the current program version name*/      Public Staticstring Getappversionname (Context context) {string Versionname= ""; Try {             //---Get the package info---Packagemanager pm =Context.getpackagemanager (); PackageInfo Pi= Pm.getpackageinfo (Context.getpackagename (), 0); Versionname=Pi.versionname; Versioncode=Pi.versioncode; if(Versionname = =NULL|| Versionname.length () <= 0) {                 return""; }         } Catch(Exception e) {LOG.E ("Versioninfo", "Exception", E); }         returnVersionname; }   

2. Get the version number of the current app

 Public Static throws Exception  {          //  Get an instance  of Packagemanager        packagemanager packagemanager =  Getpackagemanager ();           // Getpackagename () is the package name of your current class, and 0 means to get  version information        PackageInfo packinfo = Packagemanager.getpackageinfo (Getpackagename (), 0);           = packinfo.versionname;           return version;  }  

3. Get the version number of the current system

/**       * Phone system version       */public       static  String getsdkversion ()          { return Android.os.Build.VERSION.RELEASE;      }     /**       * Mobile      System APIlevel */public      staticint  Getsdkapilevel () {          return  Android.os.Build.VERSION.SDK_INT;    }   

Android gets the version number of the current app and the version number of the current system

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.