in the androidmanifest.xml manifest file, we sometimes see the following similar <meta-data ... > element start configuration content:
<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"/>
the label <meta-data> is used to provide additional data for the component, which is itself a key-value pair that can be customized by name and value. It can be included in the following components:
<activity>, <application>, <service> and <receiver>
First, how to configure the <mate-data...> elements:
the configuration syntax for tags <meta-data> elements is as follows:
<meta-data android:name= "string" android:resource= "resource Specification" android:value= "string"/ >
Description: A generic value can be specified by the Value property, but if you want to specify the ID of a resource, you need to configure it with the resource property.
such as the following configuration content:
<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, such as: Aizasybhbfogvqclaa8p1jjeqazhico2nfiybbo
such as the following configuration content:
<meta-data android:name= "ResId" android:resource= "@string/res_id"/>
The specified Resid value is the resource ID number for res_id instead of the res_id value in string
second, how to get the value of the <mate-data...> element configuration:
1. Configure <mate-data...> elements under <application...> elements
XML code snippet:
<application...> ..... <meta-data android:name= "Api_key" android:value= "Aizasybhbfogvqclaa8p1jjeqazhico2nfiybbo"/></ Application>
Java code snippet:
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 <mate-data...> elements under <activity...> elements
XML code snippet:
<activity ...> ..... <meta-data android:name= "resource_id" android:resource= "@string/ice"/></activity>
Java code snippet:
try { Activityinfo activityinfo = Getpackagemanager (). Getactivityinfo (Getcomponentname (), Packagemanager.get_meta_data); Gets the @string/ice corresponding resource ID value 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 <mate-data...> elements under <service...> elements
XML code snippet:
<service android:name= "MetadataService" > ..... <meta-data android:name= "Service_meta_data" android:value= "xxxxxxx"/></service>
Java code snippet:
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 <mate-data...> elements under <receiver...> elements
XML code snippet:
<receiver android:name= "Metadatareceiver" > ..... <meta-data android:name= "Receiver_meta_data" android:value= "xxxxxxx"/></receiver>
Java code snippet:
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 ();}
Configuration and acquisition of Meta-data extended element data in Android learning manifest