Configuration and Acquisition of meta data Extended Element Data in Android Manifest

  • 2021-11-29 08:29:49
  • OfStack

In the AndroidManifest. xml manifest file, we sometimes see something similar to the following < meta-data ... > Element starts with:


<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" />

Label < meta-data > It is used to provide additional data for components. It is a key-value pair and can customize its name and value. It can be included in the following components:

< activity > , < application > , < service > And < receiver >

1. How to configure < mate-data... > Elements:

Label < meta-data > The configuration syntax of the element is as follows:


<meta-data android:name="string"
   android:resource="resource specification"
   android:value="string" />

Note: 1-like values can be specified through the value attribute, but if you want to specify the id of 1 resource, you need to use the resource attribute to configure it.

Such as the following configuration:

< 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:

< meta-data android:name="resId" android:resource="@string/res_id" / >

The specified resId value is the resource id number of res_id instead of the res_id value in string

2. How to get < mate-data... > Element configuration value:

1. In < application... > Configuration under element < mate-data... > Element

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. In < activity... > Configuration under element < mate-data... > Element

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);
      //  What you get is  @string/ice  Corresponding resources 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. In < service... > Configuration under element < mate-data... > Element

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. In < receiver... > Configuration under element < mate-data... > Element

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();
}

Related articles: