Obtain the application version number.
/**
* Retrieves application's version number from the manifest
*
* @ Return versionName
*/
Public String getVersion (){
String version = "0.0.0 ";
Try {
PackageInfo packageInfo = getPackageManager (). getPackageInfo (
GetPackageName (), 0 );
Version = packageInfo. versionName;
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}
Return version;
}
How to obtain the app version number in Android
Application scenarios: 1. display the application version number on the interface; 2. Start the application and check whether the application is the latest version in the background. In the preceding scenario, the application version must be automatically obtained in the program. Idea Introduction: In Android, the application version is in AndroidManifest. the PackageInfo class encapsulates all information obtained from the configuration file and describes the overall information of the Package content, you can use the versionName attribute of the PackageInfo object to obtain the application version number. How can I obtain the PackageInfo object? You can obtain the PackageManager object. PackageManager is a class used to retrieve information about related application packages installed on devices. The getPackageInfo method in the PackageManager object can obtain the PackageInfo object. This method requires two parameters: application package name and condition. Generally, the package name of an application can be obtained through the getPackageName () method of Activity or Context (Activity inherited from Context). However, many settings can be added, usually set to 0. Finally, the PackageManager object is obtained. The Context object provides the getPackageManager () method to obtain the object. In summary, the template code is as follows: (Note that the encapsulated method is located in an Activity, so this is used directly to replace the Context object) 1/** 2 * Get version 3 * @ return current application version 4 */5 publicString getVersion () {6try {7 PackageManager manager = this. getPackageManager (); 8 PackageInfo = manager. getPackageInfo (this. getPackageName (), 0); 9 String version = info. versionName; 10returnthis. getString (R. string. version_name) + version; 11} catch (Exception e) {12
How can an Android app obtain the version number?
When detecting whether an application needs to be upgraded, you should first obtain the version number and compare it with the server to determine whether to upgrade Android with two version numbers: versionCode (which is represented by an integer ), versionName (string representation): public static int getVerCode (Context context) {int verCode =-1; try {verCode = context. getPackageManager (). getPackageInfo (package name, 0 ). versionCode;} catch (NameNotFoundException e) {Log. e (msg, e. getMessage ();} return verCode;} public static String getVerName (Context context) {String verName =; try {verName = context. getPackageManager (). getPackageInfo (package name, 0 ). versionName;