Manifest the meta-data and extension data acquisition configuration element

Introduction What is -meta

 

 In AndroidManifest.xml manifest file we sometimes see similar to the following <meta-data ...> element start configuration elements:

[html]  view plain  copy
 
  1. <meta-data  
  2.     android:name="com.google.android.maps.v2.API_KEY"  
  3.     android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />  
  4. <meta-data  
  5.     android:name="com.google.android.gms.version"  
  6.     android:value="@integer/google_play_services_version" />  

Tag <meta-data> to provide additional data for the component, which is itself a key-value pair can customize the name and value. It may be included in which the following components:
<Activity>, <file application>, <-Service> and <receiver>

 

First, how to configure <mate-data ...> element:

Configuration tab <meta-data> element of the following syntax:

[html]  view plain copy  
 
  1. <meta-data android:name="string"  
  2.      android:resource="resource specification"  
  3.      android:value="string" />  

Description: The general value can be specified by the value attribute, but if you want to specify a resource id, you will need to use the resource properties to configure.

 

As the following configuration elements:

[html]  view plain  copy
 
  1. <meta-data android:name="api_key" android:value="@string/api_key" />  

Api_key api_key specified value is stored in the resource file as the string: AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo

as follows content:

[html]  view plain copy  
 
  1. <meta-data android:name="resId" android:resource="@string/res_id" />  

ResId value is specified for the value of a resource id number res_id res_id string instead of


two, how to obtain the <mate-data ...> value of the element configuration:
1, arranged in the <application ...> the element <mate -data ...> element
xml code section:

[html]  view plain  copy
 
  1. <application...>  
  2.     .....  
  3.     <meta-data  
  4.           android:name="api_key"  
  5.           android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />  
  6. </application>  

Java snippet:

[java]  view plain  copy
 
  1. try {  
  2.     ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),  
  3.             PackageManager.GET_META_DATA);  
  4.     String value = appInfo.metaData.getString("api_key");  
  5.     Log.d("Tag", " app key : " + value);  // Tag﹕ app key : AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo  
  6. catch (PackageManager.NameNotFoundException e) {  
  7.     e.printStackTrace ();  
  8. }  


2, the configuration <mate-data ...> element in the <activity ...> element
xml snippet:

[html]  view plain  copy
 
  1. <activity ...>  
  2.     .....  
  3.     <meta-data android:name="resource_id"  
  4.           android:resource="@string/ice" />  
  5. </activity>  

Java snippet:

[java]  view plain  copy
 
  1. try {  
  2.     ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),  
  3.             PackageManager.GET_META_DATA);  
  4.     // acquired is @ string / ice corresponding resource id value  
  5.     int value = activityInfo.metaData.getInt("resource_id");  
  6.     Log.d("Activity Tag", "resource_id : " + value);  // Activity Tag﹕ resource_id : 2131361808  
  7. catch (PackageManager.NameNotFoundException e) {  
  8.     e.printStackTrace ();  
  9. }  

 

3, the configuration <mate-data ...> element in the <service ...> element

xml snippet:

[html]  view plain  copy
 
  1. <service android:name="MetaDataService">  
  2.       .....  
  3.       <meta-data android:name="service_meta_data" android:value="xxxxxxx" />  
  4. </service>  

Java snippet:

[java]  view plain  copy
 
  1. try {  
  2.      ComponentName cn=new ComponentName(this, MetaDataService.class);  
  3.      ServiceInfo info=this.getPackageManager()  
  4.                 .getServiceInfo(cn, PackageManager.GET_META_DATA);  
  5.      String value = info.metaData.getString("service_meta_data");  
  6.      Log.d("Service TAG", " value == " + value);  
  7. catch (PackageManager.NameNotFoundException e) {  
  8.      e.printStackTrace ();  
  9. }  


4, the configuration <mate-data ...> element in the <receiver ...> element
xml snippet:

[html]  view plain  copy
 
  1. <receiver android:name="MetaDataReceiver">  
  2.       .....  
  3.       <meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />  
  4. </receiver>  

Java snippet:

[java]  view plain  copy
 
    1. try {  
    2.      ComponentName cn=new ComponentName(this, MetaDataReceiver.class);  
    3.      ActivityInfo info=context.getPackageManager()  
    4.                              .getReceiverInfo(cn, PackageManager.GET_META_DATA);  
    5.      String value = info.metaData.getString("receiver_meta_data");  
    6.      Log.d("Receiver TAG", " value == " + value);  
    7. catch (PackageManager.NameNotFoundException e) {  
    8.      e.printStackTrace ();  
    9. }  

meta-dataIs the application's manifest file AndroidManifest.xmlin application, activity, activity-alias, provider, receiver, servicesub-label under, over intent-filter. League of Friends like these statistical services generally require more than one of these AppID parent tag, they just need to be defined in the Application, if you want to add a component meta-data, it meta-datamust be defined within the stated components thereof.

Basics

In fact, meta-datawe get a time Bundle, Knowing this, we can not fear it, do not believe you.

public Bundle metaData;

Look at the scenario, defined in the Application APPID, we can also go directly to a resource to the meta-data

  1.  
    <application
  2.  
    ....
  3.  
    <meta-data
  4.  
    android:name= "jerey"
  5.  
    android:value= "sdf123456">
  6.  
    </meta-data>
  7.  
    </application>

Application meta-data obtaining method of:

  1.  
    ApplicationInfo appInfo = null;
  2.  
    appInfo = this.getPackageManager()
  3.  
    .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
  4.  
    Bundle bundle = appInfo.metaData;
  5.  
    Log.i(TAG, "bundle.getString(jerey) : " + bundle.getString("jerey"));

Printing can be seen as follows:

I/MainActivity: bundle.getString("jerey") : sdf123456

If we use in this way as follows:

  1.  
    <string-array name="test1">
  2.  
    <item>test1_hello</item>
  3.  
    <item>test1_hello2</item>
  4.  
    </string-array>
  5.  
     
  6.  
    <meta-data
  7.  
    android:name="test_array"
  8.  
    android:resource="@array/test1">
  9.  
    </meta-data>
  10.  
     

Getting, get to that id, R.id the id, we also need to get getResource.get .... We tested it:

  1.  
    int id = bundle.getInt("test_array");
  2.  
    String[] aString = getResources().getStringArray(id);
  3.  
    for (int i = 0; i < aString.length; i++) {
  4.  
    Log.i(TAG, "onCreate: " + aString[i]);
  5.  
    }

logcat can be seen:

  1.  
    10-17 15:57:55.966 5247-5247/avatar.com.demos I/MainActivity: onCreate: test1_hello
  2.  
    10-17 15:57:55.966 5247-5247/avatar.com.demos I/MainActivity: onCreate: test1_hello2

Now that you know how to get Applicationparameters, then the other are exactly the same.

  1.  
    Activity in the application // <meta-data> element.
  2.  
    ActivityInfo info = this.getPackageManager()
  3.  
    .getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
  4.  
    info.metaData.getString( "meta_name");
  5.  
     
  6.  
    In service application // <meta-data> element.
  7.  
    ComponentName cn = new ComponentName( this, ×××Service.class);
  8.  
    ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
  9.  
    info.metaData.getString( "meta_name");
  10.  
     
  11.  
    Application of the receiver // <meta-data> element.
  12.  
    ComponentName cn = new ComponentName(context, ×××Receiver. class);
  13.  
    ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
  14.  
    info.metaData.getString( "meta_name");

Extended application - with PackageManager

As an extension scenario, we all know, the Android system open multi-application function, that say you have three browser, then send us a urltime, if there is no default browser, let us choose a browser.

  1.  
    Intent intent = new Intent();
  2.  
    intent.setAction( "android.intent.action.VIEW");
  3.  
    Uri content_url = Uri.parse( "https://www.baidu.com");
  4.  
    intent.setData(content_url);
  5.  
    startActivity(intent);

Now we like the pop-up selection screen a little more description, each capable of receiving this Action applications are able to introduce myself, in small print on the selected interface displays the prompt below.

This demand, we are certainly not say in advance remember description of each application, and this must be to control the reverse, that is, each application provides its own separate introduction. This time meta-datait played a role, we can make use of each Activity are added to make public a jump called titlein meta-data, then we can get to it, and how to obtain it, see the following operation (I actually can suddenly odd thought this example, OMG)

1 operation, use PackageManager query Intent feature, this feature is often used to determine whether there is a recipient of Intent, such as select a picture from an album, in order to prevent the album did not tend to do such a judgment.

  1.  
    Intent intent = new Intent();
  2.  
    intent.setAction( "android.intent.action.VIEW");
  3.  
    PackageManager packageManager = getPackageManager();
  4.  
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);

Get List<ResolveInfo> activities, then what?

  1.  
    for (ResolveInfo info : activities) {
  2.  
    ActivityInfo mActivityInfo = info.activityInfo;
  3.  
    mActivityInfo.packageName;
  4.  
    mActivityInfo.name;
  5.  
    Bundle data = info.activityInfo.metaData;
  6.  
    }
  7.  
     

 

Guess you like

Origin www.cnblogs.com/Alex80/p/11861944.html