android 元素 的用法
16lz
2021-01-26
<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 singleBundle
object and made available to the component as thePackageItemInfo.metaData
field.Ordinary values are specified through the
value
attribute. However, to assign a resource ID as the value, use theresource
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
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 );
更多相关文章
- inputtype 代码对应表
- Android(安卓)String.xml Html格式化方法
- android Notification 代码备份
- Android(安卓)动画的基本原理
- [转]Write your own Android(安卓)Authenticator
- SwipeRefreshLayout使用
- android上特效(源代码)
- Android权限 (代码区2)
- C ++头文件的十大错误,如何解决这些问题