SoFunction
Updated on 2025-03-04

Android implementation gets the values ​​of meta-data and

Sometimes parameters in meta-data are used, such as defined channel numbers, similar to Youmeng statistics. It will also be used to define the defaultConfig tag under the Android tag in the file, and add the manifestPlaceholders tag, which may also use the channel value.

<meta-data
      android:name="UMENG_CHANNEL"
      android:value="0"/>

First, get the value in meta-data and look at the method directly:

 /**
    * Different types need to be obtained differently. The following is the String type
    * @param context AM
    * @param metaName meta-data defined name
    * @param defaultValue default value
    * @return
    */
  public static String getAppMetaDataString(Context context, String metaName, String defaultValue) {
    try {
      // Use getApplicationinfo under the application tag, if it is an activity, use getActivityInfo under the activity      //For Sting type, use getString, Boolean type getBoolean, and see the API for details.      String value = ()
            .getApplicationInfo((), PackageManager.GET_META_DATA)
          .(metaName, defaultValue);
      return value;
    } catch ( e) {
      ();
      return defaultValue;
    }
  }

Get the value in:

 manifestPlaceholders = [
        JPUSH_PKGNAME : applicationId,
        JPUSH_APPKEY : "xxxxxxxxxxxxxxxxx", //The appkey corresponding to the package name registered on JPush.        JPUSH_CHANNEL : "developer-default", //Temporarily fill in the default value.    ]

The principle is the same. The value under the manifestPlaceholders tag needs to be displayed in the manifest file. Here the position-occupying aurora channel number (ps: I don't know if the aurora document is directly obtained):

Here, name is the name of the name, just do not repeat it. The value is the value defined under manifestPlaceholders, and ${variable value} must be used.

<meta-data android:name="JPUSH_CHANNEL"
          android:value="${JPUSH_CHANNEL}"/>

Same method

 /**
    * Get MetaData information
    *
    * @param name
    * @param def
    * @return
    */
  public static String getMetaDataValue(Context context, String name,
                     String def) {
    String value = getMetaDataValue(context, name);
    return (value == null) ? def : value;
  }

  public static String getMetaDataValue(Context context, String name) {
    Object value = null;
    PackageManager packageManager = ();
    ApplicationInfo applicationInfo;
    try {
      applicationInfo = (
          (), PackageManager.GET_META_DATA);
      if (applicationInfo != null &amp;&amp;  != null) {
        value = (name);
      }
    } catch ( e) {
      throw new RuntimeException(
          "Could not read the name in the manifest file.", e);
    }
    if (value == null) {
      throw new RuntimeException("The name '" + name
          + "' is not defined in the manifest file's meta data.");
    }
    return ();
  }

So I went back to the previous step and got the value in meta-data. After all, is it still familiar with PackageManager

Supplementary knowledge:The pitfall of obtaining android meta data value

When getting meta data, it is normal to set a normal non-pure number string in value and use getString to get it.

However, if a string containing a pure number, use getString to get it back to null. Maybe you will think of using getInt, getLong to get, sorry, it may also be null.

There are two solutions:

one.

Continue to use value="1234567890", but add "\" (backslash + space) to the beginning of a pure numeric string so that the system will automatically read as a string instead of other formats, such as

<meta-data
  android:name="appkey"
  android:value="\ 1234567890" />

two.Use the resources attribute to get the resource id through getInt, and then get the value corresponding to the resource id. This method is very scalable and can obtain all resources instead of just strings.

<meta-data
  android:name="appkey"
  android:resource="@string/AppKey" />

Define the AppKey value as

<string name="AppKey">1234567890</string>

3. How to obtain meta resources under Application

public static String getStringMetaData(String name) {
  int valueId = 0;
  try {
    ApplicationInfo appInfo = getApplicationContext().getPackageManager()
        .getApplicationInfo(getApplicationContext().getPackageName(),
            PackageManager.GET_META_DATA);
    valueId = (name);
    if (valueId != 0) {
      return getApplicationContext().getResources().getString(valueId);
    }
  } catch ( e) {
    ();
  }
  return "";
}

The above article on Android implementation to obtain the value of meta-data is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.