android 開發中:
在AndroidManifest.xml中,<meta-data>元素可以作為子元素,
被包含在<activity>、<application> 、<service>和<receiver>元素中,
不同的父元素,在應用時讀取的方法也不同。
1 :在Activity應用<meta-data>元素。
xml代碼段:
<activity…>
<meta-data android:name="data_Name" android:value="hello my activity"></meta-data>
</activity>
java代碼段:
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>元素。
xml代碼段:
<application…>
<meta-data android:value="hello my application" android:name="data_Name"></meta-data>
</application>
java代碼段:
ApplicationInfo appInfo = this.getPackageManager()
.getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String msg=appInfo.metaData.getString("data_Name");
Log.d(TAG, " msg == " + msg );
3:在service應用<meta-data>元素。
xml代碼段:
<service android:name="MetaDataService">
<meta-data android:value="hello my service" android:name="data_Name"></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("data_Name");
Log.d(TAG, " msg == " + msg );
4: 在receiver應用<meta-data>元素。
xml代碼段:
<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代碼段:
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 );