<meta-data>

SYNTAX:
<meta-data android:name="string"     android:resource="resource specification"     android:value="string" />
CONTAINED IN:
<activity>
<activity-alias>
<application>
<provider>
<receiver>
<service>
DESCRIPTION:
A name-value pair for an item of additional, arbitrary data that can be supplied to the parent component. A component element can contain any number of <meta-data>subelements. The values from all of them are collected in a single Bundleobject and made available to the component as the PackageItemInfo.metaDatafield.

Ordinary values are specified through thevalueattribute. However, to assign a resource ID as the value, use theresourceattribute instead. For example, the following code assigns whatever value is stored in the@string/kangarooresource to the "zoo" name:

<meta-data android:name="zoo" android:value="@string/kangaroo" />

On the other hand, using theresourceattribute would assign "zoo" the numeric ID of the resource, not the value stored in the resource:

<meta-data android:name="zoo" android:resource="@string/kangaroo" />

It is highly recommended that you avoid supplying related data as multiple separate<meta-data>entries. Instead, if you have complex data to associate with a component, store it as a resource and use theresourceattribute to inform the component of its ID.

ATTRIBUTES:
android:name
A unique name for the item. To ensure that the name is unique, use a Java-style naming convention — for example, " com.example.project.activity.fred".
android:resource
A reference to a resource. The ID of the resource is the value assigned to the item. The ID can be retrieved from the meta-data Bundle by the Bundle.getInt()method.
android:value
The value assigned to the item. The data types that can be assigned as values and the Bundle methods that components use to retrieve those values are listed in the following table:
Type Bundle method
String value, using double backslashes (\\) to escape characters — such as "\\n" and "\\uxxxxx" for a Unicode character. getString()
Integer value, such as "100" getInt()
Boolean value, either "true" or "false" getBoolean()
Color value, in the form "#rgb", "#argb", "#rrggbb", or "#aarrggbb" getInt()
Float value, such as "1.23" getFloat()
INTRODUCED IN:
API Level 1


在 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 );

更多相关文章

  1. inputtype 代码对应表
  2. Android(安卓)String.xml Html格式化方法
  3. android Notification 代码备份
  4. Android(安卓)动画的基本原理
  5. [转]Write your own Android(安卓)Authenticator
  6. SwipeRefreshLayout使用
  7. android上特效(源代码)
  8. Android权限 (代码区2)
  9. C ++头文件的十大错误,如何解决这些问题

随机推荐

  1. Android(安卓)网络框架之Retrofit2使用详
  2. 连周鸿祎和蒋涛都打call的书:Android安全
  3. Android(安卓)八款开源 Android(安卓)游
  4. java/android 设计模式学习笔记(23)---解释
  5. Android(六)Activity、请求码与结果码、Int
  6. [Android]关于换肤功能的遐想篇
  7. Android心得1.3--Android一些常见设置和
  8. [Inside 快訊] Android(安卓)2.3.3是目前
  9. android merge标签
  10. 「Do.014」Android(安卓)实战项目(5)—— g