在接入第三方渠道SDK的时候,经常会看到其配置文件AndroidManifest.xml有类似如下的定义:
-
- <meta-data
- android:name="APP_ID"
- android:value="037810BCE1D2260F32017643AC7D980C" />
-
-
- <meta-data
- android:name="APP_CHANNEL"
- android:value="QQ_CENTER" />
标签<meta-data>是提供组件额外的数据用的,它本身就是一个键值对,可以自定义名称和值。它可以包含在以下组件当中:
给各位看一个示例吧:
这是我定义的一个AndroidMenifest.xml配置文件
- <?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>
-
- <meta-data
- android:name="myMsg"
- android:value="hello my activity" >
- </meta-data>
- </activity>
-
-
- <meta-data
- android:name="myMsg"
- android:value="hello my application" >
- </meta-data>
- </application>
-
- </manifest>
笔者这里在Application级别和Activity级别都定义了一个meta-data,我们如何来取得这两个组件的值呢?如下:
- 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);
-
- 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();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
转自:http://blog.csdn.net/wwj_748/article/details/25079991
Android-自定义meta-data扩展数据,布布扣,bubuko.com
Android-自定义meta-data扩展数据
原文:http://www.cnblogs.com/oakpip/p/3718997.html