清單檔案中mate-data資料共享
阿新 • • 發佈:2018-11-07
目錄
1.在<application...>元素下配置<mate-data...>元素
1) xml程式碼段:
<application...> <meta-data android:name="api_key" android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" /> </application>
2) Java程式碼段:
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String value = appInfo.metaData.getString("api_key");
2.Activity中
1) xml程式碼段:
<activity ...> <meta-data android:name="resource_id" android:resource="@string/ice" /> </activity>
2) Java程式碼段:
ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
int value = activityInfo.metaData.getInt("resource_id");
3.在<service...>中
1)xml程式碼段:
<service android:name="MetaDataService"> <meta-data android:name="service_meta_data" android:value="xxxxxxx" /> </service>
2)Java程式碼段:
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");
4.在<receiver...>
1)xml程式碼段:
<receiver android:name="MetaDataReceiver">
.....
<meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />
</receiver>
2)Java程式碼段:
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");