AndroidManifest. xml & lt; manifest android: versionCode and

Source: Internet
Author: User

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

 

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.