package="com.example.metadatatest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="mTag" android:value="@string/meta_application"></meta-data>
<activity
android:name="com.example.metadatatest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 一般的值可以通过value属性来指定,但是如果要指定一个资源id作为一个值,那么就要用resource属性来代替。 -->
<meta-data android:name="mTag" android:value="@string/meta_activity"></meta-data>
<!--利用resource属性将指定 mResource 的资源id号-->
<meta-data android:name="mResource" android:resource="@drawable/ic_launcher"></meta-data>
</activity>
<service android:name="com.example.metadatatest.service.DemoService">
<meta-data android:name="mTag" android:value="@string/meta_service"></meta-data>
</service>
<receiver android:name="com.example.metadatatest.receiver.DemoReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<meta-data android:name="mTag" android:value="@string/meta_broadcast"></meta-data>
</receiver>
</application>
</manifest>
/**
* 读取application 节点 meta-data 信息
*/
private void readMetaDataFromApplication() {
try {
ApplicationInfo appInfo = this.getPackageManager()
.getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String mTag = appInfo.metaData.getString("mTag");
Log.e(TAG, "mTag=" + mTag);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 读取BroadcastReceiver 节点 meta-data 信息
*/
private void readMetaDataFromBroadCast() {
try {
ComponentName cn = new ComponentName(this, DemoReceiver.class);
ActivityInfo info = this.getPackageManager().getReceiverInfo(cn,
PackageManager.GET_META_DATA);
String mTag = info.metaData.getString("mTag");
Log.e(TAG, "mTag=" + mTag);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 读取Service 节点 meta-data 信息
*/
private void readMetaDataFromService() {
try {
ComponentName cn = new ComponentName(this, DemoService.class);
ServiceInfo info = this.getPackageManager().getServiceInfo(cn,
PackageManager.GET_META_DATA);
String mTag = info.metaData.getString("mTag");
Log.e(TAG, "mTag=" + mTag);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 读取Activity 节点 meta-data 信息
*/
private void readMetaDataFromActivity() {
ActivityInfo info;
try {
info = this.getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
String mTag = info.metaData.getString("mTag");
Log.e(TAG, "mTag=" + mTag);
//读取图片资源id
int mResource = info.metaData.getInt("mResource");
iv_pic.setImageResource(mResource);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}