標籤:android
Android-自訂meta-data擴充資料
2014年5月5日在接入第三方渠道SDK的時候,經常會看到其設定檔AndroidManifest.xml有類似如下的定義:
<!-- 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設定檔
<?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,我們如何來取得這兩個組件的值呢?如下:
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 {@Overrideprotected 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);*/}