標簽<meta-data>是提供組件額外的數(shù)據(jù)用的卑笨,它本身就是一個鍵值對,可以自定義名稱和值纱皆。它可以包含在以下組件當中:
<activity>
<activity-alias>
<application>
<provider>
<receiver>
- 在Activity的應用湾趾。
xml代碼段:
<activity...>
<meta-data android:name="myMsg" android:value="hello my activity"></meta-data>
</activity>
java代碼段:
ActivityInfo info=this.getPackageManager()
.getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
String msg=info.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);
- 在application的應用。
xml代碼段:
<application...>
<meta-data android:value="hello my application" android:name="myMsg"></meta-data>
</application>
java代碼段:
ApplicationInfo appInfo = this.getPackageManager()
.getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String msg=appInfo.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);
- 在service的應用派草。
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");
System.out.println("myMsg:"+msg);
- 在receiver的應用搀缠。
xml代碼段:
<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代碼段:
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);