In the Android manifest.xml manifest file, we can sometimes see the following configuration content similar to the beginning of the < meta data... > element:
< activity >, < Application >, < Service > and < receiver >
As follows:
2, How to get the value of the < mate data... > element configuration:
1. Configure the < mate data... > element under the < application... > element
xml snippet:
2. Configure the < mate data... > element under the < activity... > element
xml snippet:
4. Configure the < mate data... > element under the < receiver... > element
xml snippet:
The tag < meta data > is used to provide additional data for components. It is a key value pair in itself. You can customize the name and value. It can be included in the following components:
- <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" />
< activity >, < Application >, < Service > and < receiver >
1, How to configure the < mate data... > element:
- <meta-data android:name="string"
- android:resource="resource specification"
- android:value="string" />
Note: general values can be specified through the value attribute, but if you want to specify the id of a resource, you need to use the resource attribute to configure.
The specified API key value is the API key value stored in the resource file string, for example: aizasybhbfogvqclaa8p1jjeqazico2nfiybbo
- <meta-data android:name="api_key" android:value="@string/api_key" />
As follows:
The specified res id value is the resource id number of the res UU id instead of the res UU id value in the string
- <meta-data android:name="resId" android:resource="@string/res_id" />
2, How to get the value of the < mate data... > element configuration:
1. Configure the < mate data... > element under the < application... > element
xml snippet:
Java snippet:
- <application...>
- .....
- <meta-data
- android:name="api_key"
- android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />
- </application>
- 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. Configure the < mate data... > element under the < activity... > element
xml snippet:
Java snippet:
- <activity ...>
- .....
- <meta-data android:name="resource_id"
- android:resource="@string/ice" />
- </activity>
- try {
- ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
- PackageManager.GET_META_DATA);
- //Obtained the resource id value corresponding to @ string/ice
- 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. Configure the < mate data... > element under the < service... > element
xml snippet:Java snippet:
- <service android:name="MetaDataService">
- .....
- <meta-data android:name="service_meta_data" android:value="xxxxxxx" />
- </service>
- 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. Configure the < mate data... > element under the < receiver... > element
xml snippet:
Java snippet:
- <receiver android:name="MetaDataReceiver">
- .....
- <meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />
- </receiver>
- 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();
- }
- Reprinted from: http://blog.csdn.net/janice0529/article/details/41583587