Android -- get application version name and version number, Android -- version number

Source: Internet
Author: User

Android -- get application version name and version number, Android -- version number

Use 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 Context * @ return version name */public static String getVersionName (context) {// obtain Package Manager PackageManager pm = context. getPackageManager (); // get package information try {PackageInfo packageInfo = pm. getPackageInfo (context. getPackageName (), 0); // return the version number return packageInfo. versionName;} catch (PackageManager. nameNotFoundException e) {e. printStackTrace ();} return null;}/*** get the version * @ param context Context * @ return version */public static int getVersionCode (context) {// obtain Package Manager PackageManager pm = context. getPackageManager (); // get package information try {PackageInfo packageInfo = pm. getPackageInfo (context. getPackageName (), 0); // return the version number return packageInfo. versionCode;} catch (PackageManager. nameNotFoundException e) {e. printStackTrace ();} return 0;}/*** get the App name * @ param context Context * @ return name */public static String getAppName (context) {PackageManager pm = context. getPackageManager (); // get package information try {PackageInfo packageInfo = pm. getPackageInfo (context. getPackageName (), 0); // obtain the application information ApplicationInfo applicationInfo = packageInfo. applicationInfo; // obtain albelRes int labelRes = applicationInfo. labelRes; // return the App name return context. getResources (). getString (labelRes);} catch (PackageManager. nameNotFoundException e) {e. printStackTrace ();} return null ;}}

 

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.