参考:http://developer.android.com/guide/topics/manifest/meta-data-element.html
http://my.oschina.net/xesam/blog/135333
http://blog.csdn.net/top_code/article/details/11952563
meta-data键值对,所有的名值对被包装成Bundle供组件使用,因此使用方式同Bundle。metadata普通值由value属性给出,资源ID由resource属性给出。
定义meta-data:
<meta-data android:name="pepelu_string_1" android:value="pepelu_value" /> <meta-data android:name="pepelu_string_2" android:value="@string/meta_pepelu" /> <meta-data android:name="pepelu_int_1" android:resource="@drawable/ic_launcher" />
相关资源文件:
<string name="meta_pepelu">activity meta data pepeplu</string> //R: public static int ic_launcher=0x7f020017;
使用:
//获取activity 的meta-data字段:string try { ActivityInfo itemInfo = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); String tag = itemInfo.metaData.getString("pepelu_string_1"); Log.d("pepelu", "activity meta-data=" + tag); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } //获取int字段 try { ActivityInfo info2=getPackageManager().getActivityInfo(getComponentName(),PackageManager.GET_META_DATA); int picId=info2.metaData.getInt("pepelu_int_1"); Log.d("pepelu","activity int ="+picId); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); }
再其他组件中使用meta-data:
//在Activity应用<meta-data>元素。 ActivityInfo info = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); info.metaData.getString("meta_name"); //在application应用<meta-data>元素。 ApplicationInfo appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); appInfo.metaData.getString("meta_name"); //在service应用<meta-data>元素。 ComponentName cn = new ComponentName(context, MetaDataService.class); ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA); info.metaData.getString("meta_name"); //在receiver应用<meta-data>元素。 ComponentName cn = new ComponentName(context, MetaDataReceiver.class); ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA); info.metaData.getString("meta_name");
修改meta-data的值,实现在application中的数据传递,不能。