原連接:http://hi.baidu.com/_sherry_liu/item/39cb539da1e5e0ccb7253183java
在AndroidManifest.xml中,<meta-data>元素能夠做爲子元素,被包含在<activity>、<application> 、<service>和<receiver>元素中,但 不一樣的父元素,在應用中讀取的方法也不一樣。 1 :在Activity的應用。 xml代碼段:android
<activity...> <meta-data android:name="myMsg" android:value="hello my activity"></meta-data> </activity>
java代碼段:app
ActivityInfo info=this.getPackageManager() .getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); String msg=info.metaData.getString("myMsg"); Log.d("out", "msg=" + msg);
2:在application的應用。 xml代碼段:this
<application...> <meta-data android:value="hello my application" android:name="myMsg"></meta-data> </application>
java代碼段:code
ApplicationInfo appInfo = this.getPackageManager() .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); String msg=appInfo.metaData.getString("myMsg"); Log.d("out", "msg=" + msg);
3:在service的應用。 xml代碼段:xml
<service android:name="MetaDataService"> <meta-data android:value="hello my service" android:name="myMsg"></meta-data> </service>
java代碼段: ComponentName cn=new ComponentName(this, MetaDataService.class);
ServiceInfo info=this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA); String msg=info.metaData.getString("myMsg"); Log.d("out", "msg=" + msg);get
4: 在receiver的應用。 xml代碼段:it
<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代碼段:io
ComponentName cn=new ComponentName(context, MetaDataReceiver.class); ActivityInfo info=context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA); String msg=info.metaData.getString("myMsg"); Log.d("out", "msg=" + msg);
存下來備用……class