Use of <meta-data> in Android

Source: Internet
Author: User

,<meta-data> elements can be used as child elements in Androidmanifest.xml, and are included in <activity>, <application>, <service>, and < receiver> element, but
Different parent elements, and the methods that are read when they are applied are also different.
1: Application in the activity.
XML Code snippet:
<activity...>
<meta-data android:name= "mymsg" android:value= "Hello my Activity" ></meta-data>
</activity>

Java Code snippet:
Activityinfo Info=this.getpackagemanager ()
. Getactivityinfo (Getcomponentname (),
Packagemanager.get_meta_data);
String msg=info.metadata.getstring ("mymsg");
System.out.println ("mymsg:" +msg);
2: Application in the application.
XML Code snippet:
<application...>
<meta-data android:value= "Hello my Application" android:name= "mymsg" ></meta-data>
</application>

Java Code snippet:
ApplicationInfo appInfo = This.getpackagemanager ()
. Getapplicationinfo (Getpackagename (),
Packagemanager.get_meta_data);
String msg=appinfo.metadata.getstring ("mymsg");
System.out.println ("mymsg:" +msg);
3: Application in service.
XML Code snippet:
<service android:name= "MetadataService" >
<meta-data android:value= "Hello my Service" android:name= "mymsg" ></meta-data>
</service>
Java Code snippet:
ComponentName cn=new componentname (this, metadataservice.class);
ServiceInfo Info=this.getpackagemanager ()
. GetServiceInfo (CN, Packagemanager.get_meta_data);
String msg=info.metadata.getstring ("mymsg");
System.out.println ("mymsg:" +msg);
4: Application in receiver.
XML Code snippet:
<receiver android:name= "Metadatareceiver" >
<meta-data android:value= "Hello my Receiver" android:name= "mymsg" ></meta-data>
<intent-filter>
<action android:name= "Android.intent.action.PHONE_STATE" ></action>
</intent-filter>
</receiver>
Java Code snippet:
ComponentName cn=new componentname (context, metadatareceiver.class);
Activityinfo Info=context.getpackagemanager ()
. Getreceiverinfo (CN, Packagemanager.get_meta_data);
String msg=info.metadata.getstring ("mymsg");
System.out.println ("mymsg:" +msg);

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.