android-Custom Meta-data Extended Data
May 5, 2014when accessing the third-party channel SDK, it is often seen that its configuration file androidmanifest.xml has a definition similar to the following:
<!--AppID-- <meta-data android:name= "app_id" android:value= " 037810bce1d2260f32017643ac7d980c "/> <!--distribution channel (optional)-- <meta-data android:name=" App_ CHANNEL " android:value=" Qq_center "/>
The label <meta-data> is used to provide additional data for the component, which is itself a key-value pair that can be customized by name and value. It can be included in the following components:
<activity>
<activity-alias>
<application>
<provider>
<receiver>
Let 's show you an example:This is a androidmenifest.xml configuration file that I defined
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.wwj.metadata "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme "> <activity android:name=" com.wwj.metadata.MainActivity "android:label=" @string/app_n Ame "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> <!--in activity definition Meta-data--<meta-data android:name= "mymsg" android:v alue= "Hello my Activity" > </meta-data> </activity> <!--meta-data-<meta-data defined in application Android:name= "mymsg" android:value= "Hello my Application" > </meta-data> </application ></manifest>
I have defined a meta-data here at both the application level and the activity level, how do we get the values of these two components? as follows:
Package Com.wwj.metadata;import Android.app.activity;import Android.content.pm.activityinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;import Android.os.bundle;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// In the application of activity try {activityinfo info = This.getpackagemanager (). Getactivityinfo (Getcomponentname (), Packagemanager.get_meta_data); String msg = info.metaData.getString ("mymsg"); System.out.println ("mymsg:" + msg); catch (Namenotfoundexception e) {e.printstacktrace ();}} /*1: In the application of activity. XML snippet: <activity...> <meta-data android:name= "mymsg" android:value= "Hello my Activity" ></meta-data > </activity> Java code Snippet: Activityinfo Info=this.getpackagemanager (). Getactivi Tyinfo (Getcomponentname (), Packagemanager. Get_meta_data); String msg=info.metadata.getstring ("mymsg"); System.out.println ("mymsg:" +msg); 2: Application in Application. XML 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 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.CLA SS); 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 snippet: <receiver android:name= "Metadatareceiver" > <meta-data android:value= "Hello my Receiver" and Roid:name= "mymsg" ></meta-data> <intent-filter> <action android:name= "android.i Ntent.action.PHONE_STATE "></action> </intent-filter> </receiver> Java code snippet: Componen Tname cn=new componentname (context, metadatareceiver.class); Activityinfo Info=context.getpackagemanager (). Getreceiverinfo (CN, Packagemanager.get_meta_dat A); String msg=info.metadata.getstring ("mymsg"); System.out.println ("mymsg:" +msg); */}