Android reads meta-data custom data under the Application and other nodes in the Manifest file.

Source: Internet
Author: User

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 ");
      
     
    
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.