Android APP reads version information in AndroidManifest. xml, androidmanifest. xml

Source: Internet
Author: User

Android APP reads version information in AndroidManifest. xml, androidmanifest. xml

The Android APP version information is stored at the top of the AndroidManifest. xml file. For example:

There are two attributes, "android: versionCode" and "android: versionName". versionCode is of the int type and is used by the program. Generally, this attribute is used for version control, versionName is of the String type and is displayed to users. For example, the current version is displayed on the page of the APP. In the new project, versionCode is 1 by default, and versionName is 1.0 by default.

Next we will use a program to read these two attributes.

The layout file code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "APP version:"/> <TextView android: id = "@ + id/tvVersion" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> </LinearLayout>

The Activity code is as follows:

Package chengyujia. androidtest; import android. app. activity; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException; import android. OS. bundle; import android. widget. textView; public class VersionActivity extends Activity {private TextView tvVersion; @ Override protected void onCreate (Bundle savedInstanceState) {super. OnCreate (savedInstanceState); setContentView (R. layout. activity_version); tvVersion = (TextView) findViewById (R. id. tvVersion); showVersion ();} private void showVersion () {// you can directly call getPackageManager () in the Activity to obtain the PackageManager instance. PackageManager packageManager = getPackageManager (); // you can directly call getPackageName () in the Activity to obtain the full name of the installation package. String packageName = getPackageName (); // flags provides 10 options and their combinations. If you only get the version number, flags = 0 can be int flags = 0; PackageInfo packageInfo = null; try {// obtain AndroidManifest through packageInfo. information in xml. PackageInfo = packageManager. getPackageInfo (packageName, flags);} catch (NameNotFoundException e) {e. printStackTrace ();} if (packageInfo! = Null) {// here we get the version information. Int versionCode = packageInfo. versionCode; String versionName = packageInfo. versionName; tvVersion. setText ("versionCode =" + versionCode + "\ nversionName =" + versionName );}}}

Run the command to see the effect:

The default value is displayed. When a new version of our APP is released, you need. modify the two values in xml. versionCode is of the int type, which is generally auto-incrementing from 1. If the value is not of the int type, an error is returned. For example:

VersionName is of the String type, as long as it is a String, such:

Run the following command to view the details:

If you want to read the version information, write this. It's time to have dinner ^_^.

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.