1 <receiver android:name=".ViewWidget" android:label="@string/app_name"> 2 .... 3 <meta-data android:name="cn.android" android:value="liming"/> 4 <meta-data android:name=“age" android:value=“30"/> 5 <!-- 使用资源文件中存放的字符串数据 --> 6 <meta-data android:name=“other" android:value="@string/other" /> 7 <!-- 使用资源的ID作为resid的值 --> 8 <meta-data android:name=“resid" android:resource="@string/resid" /> 9 </receiver>
上面<meta-data>节点的可以包含在<activity> <activity-alias> <service> <receiver>四个元素中,用于为组件提供名值对数据,组件中可以定义多个<meta-data>节点,这些<meta-data>节点定义的值都会被存放在Bundle对象并作为组件PackageItemInfo.metaData字段值,要访问这些值可以这样做:
1 ActivityInfo info = context.getPackageManager().getReceiverInfo(new ComponentName(context, ViewWidget.class), PackageManager.GET_META_DATA); 2 3 String name = info.metaData.getString("cn.android"); 4 5 int age = info.metaData.getInt(“age"); 6 7 String other= info.metaData.getString("other"); 8 9 int id = info.metaData.getInt(“resid"); 10 11 //如果<meta-data>定义在Activity下,用getPackageManager().getActivityInfo()方法
demo:
1.清单文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.android.hzy.metadata" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="8" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.android.hzy.metadata.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 <meta-data android:name="name" android:value="火影忍者"/> 25 <meta-data android:name="age" android:value="7"/> 26 <meta-data android:name="other" android:value="@string/meta"/> 27 <!-- key值是一样的 value:引用的是id --> 28 <meta-data android:name="res" android:resource="@string/res"/> 29 </activity> 30 </application> 31 32 </manifest>
2.strings.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">元数据</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="menu_settings">Settings</string> 7 <string name="meta">META</string> 8 <string name="res">resourse</string> 9 10 </resources>
3.MainActivity
1 package com.android.hzy.metadata; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.pm.ActivityInfo; 6 import android.content.pm.PackageManager; 7 import android.content.pm.PackageManager.NameNotFoundException; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.View; 11 12 public class MainActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 public void get(View v){ 21 // activity的管理,动态的数据 22 // ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 23 24 try { 25 // 包管理器 26 PackageManager pm = getPackageManager(); 27 ComponentName component = new ComponentName(this,MainActivity.class); 28 29 ActivityInfo activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA); 30 Bundle bundle = activityInfo.metaData; 31 String name = bundle.getString("name"); 32 int age = bundle.getInt("age"); 33 String other = bundle.getString("other"); 34 String res = getString(bundle.getInt("res")); 35 36 Log.i("i", " name " + name); 37 Log.i("i", " age " + age); 38 Log.i("i", " other " + other); 39 Log.i("i", " res " + res); 40 41 } catch (Exception e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 }