Configuration and acquisition of meta-data extension element data in Manifest in Android Learning

Source: Internet
Author: User

Configuration and acquisition of meta-data extension element data in Manifest in Android Learning
In the AndroidManifest. xml configuration file, we sometimes see the following similar Configuration content starting with the element:

 
 
Tag Is used to provide additional data for the component. It is a key-Value Pair and can be customized. It can be included in the following components:
,, And


1. How to configure Element:

Tag The element configuration syntax is as follows:
     

Note: The general value can be specified through the value attribute, but if you want to specify a resource id, you need to use the resource attribute for configuration.


The configuration is as follows:
     
The specified api_key value is the api_key value stored in the resource file string, for example, AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo.

The configuration is as follows:
     
The specified resId value is the ID of the res_id resource instead of the res_id value in the string.


Ii. How to obtain Element configuration value:
1. Configure under the element Element
Xml code segment:
    .....    
       
Java code segment:
        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. Configure under the element Element
Xml code segment:
    .....    
        
Java code segment:
Try {ActivityInfo activityInfo = getPackageManager (). getActivityInfo (getComponentName (), PackageManager. GET_META_DATA); // obtain the resource id value of @ string/ice. 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. Configuration under Element Element

Xml code segment:
        
               .....      
         
        
Java code segment:
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. Configuration under Element Element
Xml code segment:
          
                 .....      
           
          
Java code segment:
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();}




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.