Acquisition <meta-data> data in Android development

Source: Internet
Author: User

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

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.