The android: versionCode and android: versionName fields represent the version code and version name respectively. VersionCode is an integer and versionName is a string. VersionName is shown to users, and it is not easy to compare the size. During the upgrade check, you can check versionCode to compare the size before and after the publication.
How can I read versionCode and versionName from AndroidManifest. xml in an application? You can use the PackageManager API by referring to the following code:
[Html]
Public static int getVerCode (Context context ){
Int verCode =-1;
Try {
VerCode = context. getPackageManager (). getPackageInfo (
"Com. myapp", 0). versionCode;
} Catch (NameNotFoundException e ){
Log. e (TAG, e. getMessage ());
}
Return verCode;
}
Public static String getVerName (Context context ){
String verName = "";
Try {
VerName = context. getPackageManager (). getPackageInfo (
"Com. myapp", 0). versionName;
} Catch (NameNotFoundException e ){
Log. e (TAG, e. getMessage ());
}
Return verName;
}
Public static int getVerCode (Context context ){
Int verCode =-1;
Try {
VerCode = context. getPackageManager (). getPackageInfo (
"Com. myapp", 0). versionCode;
} Catch (NameNotFoundException e ){
Log. e (TAG, e. getMessage ());
}
Return verCode;
}
Public static String getVerName (Context context ){
String verName = "";
Try {
VerName = context. getPackageManager (). getPackageInfo (
"Com. myapp", 0). versionName;
} Catch (NameNotFoundException e ){
Log. e (TAG, e. getMessage ());
}
Return verName;
}
You can also write android: versionName = "1.2.0" in AndroidManifest as android: versionName = "@ string/app_versionName", and then in values/strings. add the corresponding string to the xml file. After implementation, you can use the following code to obtain the version name:
[Html]
Public static String getVerName (Context context ){
String verName = context. getResources ()
. GetText (R. string. app_versionName). toString ();
Return verName;
}
Public static String getVerName (Context context ){
String verName = context. getResources ()
. GetText (R. string. app_versionName). toString ();
Return verName;
}
Similarly, the apk Application name can be obtained as follows:
[Html]
Public static String getAppName (Context context ){
String verName = context. getResources ()
. GetText (R. string. app_name). toString ();
Return verName;
}
Public static String getAppName (Context context ){
String verName = context. getResources ()
. GetText (R. string. app_name). toString ();
Return verName;
}