Android reads the meta of each part

Keywords: Android Java xml Google

In the Android manifest.xml manifest file, we can sometimes see the following configuration content similar to the beginning of the < meta data... > element:
  1. <meta-data  
  2.     android:name="com.google.android.maps.v2.API_KEY"  
  3.     android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />  
  4. <meta-data  
  5.     android:name="com.google.android.gms.version"  
  6.     android:value="@integer/google_play_services_version" />  
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:
< activity >, < Application >, < Service > and < receiver >


1, How to configure the < mate data... > element:

The configuration syntax of the tag < meta data > element is as follows:
  1. <meta-data android:name="string"  
  2.      android:resource="resource specification"  
  3.      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.


As follows:
  1. <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: aizasybhbfogvqclaa8p1jjeqazico2nfiybbo

As follows:
  1. <meta-data android:name="resId" android:resource="@string/res_id" />  
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


2, How to get the value of the < mate data... > element configuration:
1. Configure the < mate data... > element under the < application... > element
xml snippet:
  1. <application...>  
  2.     .....  
  3.     <meta-data  
  4.           android:name="api_key"  
  5.           android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />  
  6. </application>  
Java snippet:
  1. try {  
  2.     ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),  
  3.             PackageManager.GET_META_DATA);  
  4.     String value = appInfo.metaData.getString("api_key");  
  5.     Log.d("Tag"" app key : " + value);  // Tag﹕ app key : AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo  
  6. catch (PackageManager.NameNotFoundException e) {  
  7.     e.printStackTrace();  
  8. }  

2. Configure the < mate data... > element under the < activity... > element
xml snippet:
  1. <activity ...>  
  2.     .....  
  3.     <meta-data android:name="resource_id"  
  4.           android:resource="@string/ice" />  
  5. </activity>  
Java snippet:
  1. try {  
  2.     ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),  
  3.             PackageManager.GET_META_DATA);  
  4.     //Obtained the resource id value corresponding to @ string/ice  
  5.     int value = activityInfo.metaData.getInt("resource_id");  
  6.     Log.d("Activity Tag""resource_id : " + value);  // Activity Tag﹕ resource_id : 2131361808  
  7. catch (PackageManager.NameNotFoundException e) {  
  8.     e.printStackTrace();  
  9. }  


3. Configure the < mate data... > element under the < service... > element

xml snippet:
  1. <service android:name="MetaDataService">  
  2.       .....  
  3.       <meta-data android:name="service_meta_data" android:value="xxxxxxx" />  
  4. </service>  
Java snippet:
  1. try {  
  2.      ComponentName cn=new ComponentName(this, MetaDataService.class);  
  3.      ServiceInfo info=this.getPackageManager()  
  4.                 .getServiceInfo(cn, PackageManager.GET_META_DATA);  
  5.      String value = info.metaData.getString("service_meta_data");  
  6.      Log.d("Service TAG"" value == " + value);  
  7. catch (PackageManager.NameNotFoundException e) {  
  8.      e.printStackTrace();  
  9. }  

4. Configure the < mate data... > element under the < receiver... > element
xml snippet:
  1. <receiver android:name="MetaDataReceiver">  
  2.       .....  
  3.       <meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />  
  4. </receiver>  
Java snippet:
  1. try {  
  2.      ComponentName cn=new ComponentName(this, MetaDataReceiver.class);  
  3.      ActivityInfo info=context.getPackageManager()  
  4.                              .getReceiverInfo(cn, PackageManager.GET_META_DATA);  
  5.      String value = info.metaData.getString("receiver_meta_data");  
  6.      Log.d("Receiver TAG"" value == " + value);  
  7. catch (PackageManager.NameNotFoundException e) {  
  8.      e.printStackTrace();  
  9. }  
  10. Reprinted from: http://blog.csdn.net/janice0529/article/details/41583587

Posted by beginPHP on Wed, 01 Apr 2020 17:04:31 -0700