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 ;}}