在接入第三方渠道SDK的时候,经常会看到其配置文件AndroidManifest.xml有类似如下的定义:
[html] view plaincopy
- <!-- appid -->
- <meta-data
- android:name="APP_ID"
- android:value="037810BCE1D2260F32017643AC7D980C" />
- <!-- 发布渠道(可选) -->
- <meta-data
- android:name="APP_CHANNEL"
- android:value="QQ_CENTER" />
标签<meta-data>是提供组件额外的数据用的,它本身就是一个键值对,可以自定义名称和值。它可以包含在以下组件当中:
<activity>
<activity-alias>
<application>
<provider>
<receiver>
给各位看一个示例吧:
这是我定义的一个AndroidMenifest.xml配置文件
[html] view plaincopy
- <?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>
笔者这里在Application级别和Activity级别都定义了一个meta-data,我们如何来取得这两个组件的值呢?如下:
[java] view plaincopy
- 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);*/
- }
转自:http://blog.csdn.net/wwj_748/article/details/25079991
时间: 2024-10-10 01:37:20