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