在AndroidManifest.xml清單文件中 我們有時(shí)會(huì)看到如下類似的<meta-data ... >元素開(kāi)始的配置內(nèi)容:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
標(biāo)簽<meta-data>是提供組件額外的數(shù)據(jù)用的榕栏,它本身就是一個(gè)鍵值對(duì),可以自定義名稱和值熙揍。它可以包含在以下組件當(dāng)中:
<activity>渗稍、<application> 挑宠、<service> 和 <receiver>
一今妄、如何配置<mate-data...>元素:
標(biāo)簽<meta-data>元素的配置語(yǔ)法如下:
<meta-data android:name="string"
android:resource="resource specification"
android:value="string" />
說(shuō)明:一般的值可以通過(guò)value屬性來(lái)指定郑口,但是如果要指定一個(gè)資源的id,則需要使用resource屬性來(lái)配置。
如以下配置內(nèi)容:
<meta-data android:name="api_key" android:value="@string/api_key" />
指定的api_key值為存儲(chǔ)在資源文件string中的api_key值 如:AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo
指定的resId值則是為res_id的資源id號(hào) 而不是string中的res_id值
二盾鳞、如何獲取<mate-data...>元素配置的值:
1犬性、在<application...>元素下配置<mate-data...>元素
<application...>
.....
<meta-data
android:name="api_key"
android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />
</application>
Java代碼段:
try {
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String value = appInfo.metaData.getString("api_key");
Log.d("Tag", " app key : " + value); // Tag﹕ app key : AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
2、在<activity...>元素下配置<mate-data...>元素
<activity ...>
.....
<meta-data android:name="resource_id"
android:resource="@string/ice" />
</activity>
Java代碼段:
try {
ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
// 獲取到的是 @string/ice 對(duì)應(yīng)的資源id值
int value = activityInfo.metaData.getInt("resource_id");
Log.d("Activity Tag", "resource_id : " + value); // Activity Tag﹕ resource_id : 2131361808
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
3腾仅、在<service...>元素下配置<mate-data...>元素
xml代碼段:
<service android:name="MetaDataService">
.....
<meta-data android:name="service_meta_data" android:value="xxxxxxx" />
</service>
Java代碼段:
try {
ComponentName cn=new ComponentName(this, MetaDataService.class);
ServiceInfo info=this.getPackageManager()
.getServiceInfo(cn, PackageManager.GET_META_DATA);
String value = info.metaData.getString("service_meta_data");
Log.d("Service TAG", " value == " + value);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
4乒裆、在<receiver...>元素下配置<mate-data...>元素
<receiver android:name="MetaDataReceiver">
.....
<meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />
</receiver>
Java代碼段:
try {
ComponentName cn=new ComponentName(this, MetaDataReceiver.class);
ActivityInfo info=context.getPackageManager()
.getReceiverInfo(cn, PackageManager.GET_META_DATA);
String value = info.metaData.getString("receiver_meta_data");
Log.d("Receiver TAG", " value == " + value);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
本文是移值這篇博客的,大家可以自行翻閱原文鏈接