Android-Data Reading from androidmanifest. xml: version number, application name, custom K-V data (meta-data)

Source: Internet
Author: User

 

Turn: http://www.2cto.com/kf/201208/151123.html

In Android development, we often need to know the version information of the current application. You can perform automatic upgrade judgment. Extended Custom Data meta-data. How can we obtain this information in androidmanifest. xml?

The following example shows how to read the data.

1. version information and application name

2 appliction meta-data

3. Meta-data in the activity

Androidmanifest. XML is defined as follows:
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "HPC. Cai. test01"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name">
<Meta-data
Android: Name = "tel"
Android: value = "cdma-13366350377">
</Meta-data>
<Meta-data
Android: Name = "channel"
Android: value = "Eben"/>

<Activity
Android: Name = "HPC. Cai. Test. test1activity"
Android: Label = "@ string/app_name">
<Meta-data
Android: Name = "name"
Android: value = "self-improvement">
</Meta-data>
<Meta-data
Android: Name = "city"
Android: value = "Beijing"/>

<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

Java code:

/**
* Activity metadata reading
*/
Private void testactivitymetadata (){
Activityinfo Info;
Try {
Info = getpackagemanager (). getactivityinfo (getcomponentname (),
Packagemanager. get_meta_data );

String MSG = info. Metadata. getstring ("name ");
System. Out. println ("name:" + MSG );

MSG = info. Metadata. getstring ("city ");
System. Out. println ("City:" + MSG );

} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
/**
* Appliction metadata reading
*/
Private void testapplicationmetadata (){
Applicationinfo Info;
Try {
Info = This. getpackagemanager (). getapplicationinfo (
Getpackagename (), packagemanager. get_meta_data );

String MSG = info. Metadata. getstring ("tel ");
System. Out. println ("Tel:" + MSG );

MSG = info. Metadata. getstring ("channel ");
System. Out. println ("channel:" + MSG );

} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
/**
* Test version information
*/Www.2cto.com
Private void getversion (){
Packageinfo PKG;
Try {
PKG = getpackagemanager (). getpackageinfo (getapplication (). getpackagename (), 0 );
String appname = PKG. applicationinfo. loadlabel (getpackagemanager (). tostring ();
String versionname = PKG. versionname;
System. Out. println ("appname:" + appname );
System. Out. println ("versionname:" + versionname );
} Catch (namenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

The output is as follows:

16:37:54. 442: I/system. Out (10213): appname: test1
16:37:54. 442: I/system. Out (10213): versionname: 1.0
16:37:54. 442: I/system. Out (10213): Tel: cdma-13366350377
16:37:54. 442: I/system. Out (10213): Channel: Eben
16:37:54. 452: I/system. Out (10213): Name: Self-improvement
16:37:54. 452: I/system. Out (10213): City: Beijing

 

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.