Android reads meta-data custom data under the Application and other nodes in the Manifest file.
This section describes how to use key-value pairs for custom configuration in meta-date. It is generally used as a channel identifier. It can be used in ,,, , Node. All values are stored in the same Bundle. You can use PackageItemInfo. metaData to store the data.
Usage:
Resource and value can be used directly, but note that if you use
The result is the ID value, which is the number in the R file, not the actual resource value. Resource is used to obtain the resource value (getInt ).
Android: name: Make sure it is unique.
Android: value: the value can be:
1. Use getString () to obtain the string
2. An integer such as 100 is obtained using getInt ().
3. boolean value: "true", "false", obtained using getBoolean ()
4. color value: "# rgb", "# argb", "# rrggbb", or "# aarrggbb", obtained using getInt ()
5. floating point value: "1.2" using getFloat ()
Note: The Bundle uses return (String) o; to obtain the StringValue from the code. If the value of a String you want to configure is "000, however, when packaging metadata as a bundle, "000" is parsed to an integer 0. Therefore, getString () is used to obtain null.
Instance: add the following to the Manifest node:
In the custom Application, there is a method:
/*** Get the channel version configured in Manifest *2014-10-14
* @ Return * @ author RANDY. ZHANG */public String getHaiwanVersion () {String channel = ""; try {channel = this. getPackageManager (). getApplicationInfo (getPackageName (), PackageManager. GET_META_DATA ). metaData. getString ("HAIWAN_CHANNEL");} catch (NameNotFoundException e) {e. printStackTrace ();} return channel ;}
Other nodes:
// Apply in Activity
Element. ActivityInfo info = this. getPackageManager (). getActivityInfo (getComponentName (), PackageManager. GET_META_DATA); info. metaData. getString ("meta_name"); // used in application
Element. ApplicationInfo appInfo = this. getPackageManager (). getApplicationInfo (getPackageName (), PackageManager. GET_META_DATA); appInfo. metaData. getString ("meta_name"); // used in service
Element. ComponentName cn = new ComponentName (this, MetaDataService. class); ServiceInfo info = this. getPackageManager (). getServiceInfo (cn, PackageManager. GET_META_DATA); info. metaData. getString ("meta_name"); // apply it in the consumer er
Element. ComponentName cn = new ComponentName (context, MetaDataReceiver. class); ActivityInfo info = context. getPackageManager (). getReceiverInfo (cn, PackageManager. GET_META_DATA); info. metaData. getString ("meta_name ");