<!-- appid -->
<meta-data
android:name="APP_ID"
android:value="037810BCE1D2260F32017643AC7D980C" />
<!-- 公布渠道(可选) -->
<meta-data
android:name="APP_CHANNEL"
android:value="QQ_CENTER" />
标签<meta-data>是提供组件额外的数据用的,它本身就是一个键值对,能够自己定义名称和值。它能够包括在下面组件其中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wwj.metadata"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wwj.metadata.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 在Activity定义的meta-data -->
<meta-data
android:name="myMsg"
android:value="hello my activity" >
</meta-data>
</activity>
<!-- 在Application定义的meta-data -->
<meta-data
android:name="myMsg"
android:value="hello my application" >
</meta-data>
</application>
</manifest>
package com.wwj.metadata;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在Activity的应用
try {
ActivityInfo info = this.getPackageManager().getActivityInfo(
getComponentName(), PackageManager.GET_META_DATA);
String msg = info.metaData.getString("myMsg");
System.out.println("myMsg:" + msg);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/* 1 :在Activity的应用。
xml代码段:
<activity...>
<meta-data android:name="myMsg" android:value="hello my activity"></meta-data>
</activity>
java代码段:
ActivityInfo info=this.getPackageManager()
.getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
String msg=info.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);
2:在application的应用。
xml代码段:
<application...>
<meta-data android:value="hello my application" android:name="myMsg"></meta-data>
</application>
java代码段:
ApplicationInfo appInfo = this.getPackageManager()
.getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String msg=appInfo.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);
3:在service的应用。
xml代码段:
<service android:name="MetaDataService">
<meta-data android:value="hello my service" android:name="myMsg"></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("myMsg");
System.out.println("myMsg:"+msg);
4: 在receiver的应用。
xml代码段:
<receiver android:name="MetaDataReceiver">
<meta-data android:value="hello my receiver" android:name="myMsg"></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("myMsg");
System.out.println("myMsg:"+msg);*/
}