in the Androidmanifest.xml, The <meta-data> element is a key-value pair that is often included in elements such as <application>, <activity>, <service>, and <receiver>, However , the <meta-data> read method is different in different parent elements.
<meta-data> basic structure: <meta-data android:name= "string" android:resource= "resource Specification" Android:value = "String"/> (where resource, value is selected), a component element can contain any number of <meta-data> child elements, all of their values will be collected in the bundle object, as the component's The Packageiteminfo.metadata field.
Android:name the name of the metadata item, you need to ensure uniqueness.
A reference to the Android:resource resource that is assigned to the item is the ID of the resource. This ID can be obtained from <meta-data> via Bundle.getint ().
Android:value the value assigned to this item, the data type that can be specified as a value, and the bundle method that the component uses to retrieve those values is as follows.
| Data type |
Example |
Get method |
| String |
"ABCD" |
GetString () |
| Integer |
"1" |
GetInt () |
| Boolean |
"True" |
Getboolean () |
| Color |
"#rgb", "#argb", "#rrggbb" |
GetInt () |
| Floating point |
"1.1" |
GetFloat () |
The general value can be specified by the Value property, but if you want to specify a resource ID as a value, use the Resource property instead. if <meta-data android:name= "Test_name" android:value= "Test_value"/>, then the Read method in the different parent elements is as follows:
1. Read <meta-data> in <application>
1 ImportAndroid.content.pm.ApplicationInfo;2 ImportAndroid.content.pm.PackageManager;3 Importandroid.content.pm.PackageManager.NameNotFoundException;4 Try5 {6ApplicationInfo info = This. Getpackagemanager (). Getapplicationinfo (Getpackagename (), packagemanager.get_meta_data);7String value = info.metaData.getString ("Test_name");8 }9 Catch(namenotfoundexception e)Ten { One e.printstacktrace (); A}View Code2. Read <meta-data> in <activity>
1 ImportAndroid.content.pm.ActivityInfo;2 ImportAndroid.content.pm.PackageManager;3 Importandroid.content.pm.PackageManager.NameNotFoundException;4 Try5 {6Activityinfo info = This. Getpackagemanager (). Getactivityinfo (Getpackagename (), packagemanager.get_meta_data);7String value = info.metaData.getString ("Test_name");8 }9 Catch(namenotfoundexception e)Ten { One e.printstacktrace (); A}View Code3. Read <meta-data> in <service>
1 New ComponentName (This, MetadataService. Class); 2 This . Getpackagemanager (). GetServiceInfo (CN, packagemanager.get_meta_data); 3 String value = info.metaData.getString ("Test_name");
View Code4. Read <meta-data> in <receiver>
1 New ComponentName (context, metadatareceiver. Class); 2 activityinfo info = Context.getpackagemanager (). Getreceiverinfo (CN, packagemanager.get_meta_data); 3 String value = info.metaData.getString ("Test_name");
View Code
Acquisition <meta-data> data in Android development