<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
Bundle
object and made available to the component as the
PackageItemInfo.metaData
field.
Ordinary values are specified through the value
attribute. However, to assign a resource ID as the value, use the resource
attribute instead. For example, the following code assigns whatever value is stored in the@string/kangaroo
resource to the "zoo
" name:
<meta-data android:name="zoo" android:value="@string/kangaroo" />
On the other hand, using the resource
attribute 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 theresource
attribute 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 );