The,<meta-data> element can be used as a child element in Androidmanifest.xml.
Are included in the <activity>, <application>, <service>, and <receiver> elements, different parent elements, and the methods that are read at the time of application are also different.
1: In the Activity app <meta-data> elements. XML Code snippet:
<activity...>
<meta-data android:name= "Data_name" 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 ("Data_name");
LOG.D (TAG, "msg = =" + msg);
2: Application <meta-data> elements in application. XML Code snippet:
<application...>
<meta-data android:value= "Hello my Application" android:name= "Data_name" ></meta-data>
</application>
Java Code snippet:
ApplicationInfo appInfo = This.getpackagemanager ()
. Getapplicationinfo (Getpackagename (),
Packagemanager.get_meta_data);
String msg=appinfo.metadata.getstring ("Data_name");
LOG.D (TAG, "msg = =" + msg);
3: In service Application <meta-data> elements. XML Code snippet:
<service android:name= "MetadataService" >
<meta-data android:value= "Hello my Service" android:name= "Data_name" ></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 ("Data_name");
LOG.D (TAG, "msg = =" + msg);
4: In receiver application <meta-data> elements. XML Code snippet:
<receiver android:name= "Metadatareceiver" >
<meta-data android:value= "Hello my Receiver" android:name= "Data_name" ></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 ("Data_name");
LOG.D (TAG, "msg = =" + msg);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Get Meta-data