Configuration and acquisition of meta-data extension element data in Manifest in Android learning, manifestmeta-data
In the AndroidManifest. xml configuration file, we sometimes see the configuration content starting with the <meta-data...> element similar to the following:
<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" />
Tag <meta-data> provides additional data for the component. It is a key-Value Pair and can be customized. It can be included in the following components:
<Activity>, <application>, <service>, and <worker ER>
1. How to configure the <mate-data...> element:
The tag <meta-data> element configuration syntax is as follows:
<meta-data android:name="string" android:resource="resource specification" android:value="string" />
Note: The general value can be specified through the value attribute, but if you want to specify a resource id, you need to use the resource attribute for configuration.
The configuration is as follows:
<meta-data android:name="api_key" android:value="@string/api_key" />
The specified api_key value is the api_key value stored in the resource file string, for example, AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo.
The configuration is as follows:
<meta-data android:name="resId" android:resource="@string/res_id" />
The specified resId value is the ID of the res_id resource instead of the res_id value in the string.
2. How to obtain the value of <mate-data...> element configuration:
1. Configure the <mate-data...> element under the <application...> element.
Xml code segment:
<application...> ..... <meta-data android:name="api_key" android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" /></application>
Java code segment:
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 code segment:
<activity ...> ..... <meta-data android:name="resource_id" android:resource="@string/ice" /></activity>
Java code segment:
Try {ActivityInfo activityInfo = getPackageManager (). getActivityInfo (getComponentName (), PackageManager. GET_META_DATA); // obtain the resource id value of @ 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 code segment:
<service android:name="MetaDataService"> ..... <meta-data android:name="service_meta_data" android:value="xxxxxxx" /></service>
Java code segment:
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 <er...> element
Xml code segment:
<receiver android:name="MetaDataReceiver"> ..... <meta-data android:name="receiver_meta_data" android:value="xxxxxxx" /></receiver>
Java code segment:
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();}