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();}