Android Gets the value of the label in manifest

Source: Internet
Author: User

Of course, we can also define a value by the meta-data tag inside the menifest. Then in our application directly to read it, so that the next time if you need to modify the value, we do not have to find our code, but in the revision of our version number, together we can modify. More convenient and convenient.

You must have seen this scenario when it comes to packaging multiple markets. For example, the AU, there is a meter in the SDK there is a step: under the application label to define a meta-data to the different market assignment and the introduction of different AppID.

So how do we define the value of Meta-data in our own application? And how do we get the values we want in the code?

Then click More and follow me to see how to operate it!!!

Shaped like:

The code is as follows Copy Code
<application ...>
<meta-data android:value= "12345" android:name= "APPID"/>
<activity ...>
<meta-data android:name= "Data_name" android:value= "Hello my Activity" ></meta-data>
</activity>
</application>

From above can see, our meta-data not only can put under Application label, also can put under activity, actually also can put to service, receiver below ...

Let's take a look at how to get it separately.

1. Get the Meta-data in the application label:
Shaped like:

The code is as follows Copy Code

<application...>
<meta-data android:value= "My_data" android:name= "Data_name"/>
</application>
Code:

ApplicationInfo appInfo = Context.getpackagemanager ()
. Getapplicationinfo (Context.getpackagename (),
Packagemanager.get_meta_data);
String dataname=appinfo.metadata.getstring ("Data_name");

2. Get the Meta-data in the activity label:
Shaped like:

The code is as follows Copy Code

<activity ...>
<meta-data android:value= "my_activity" android:name= "Data_name"/>
</activity>

Code:

The code is as follows Copy Code
Activityinfo activityinfo = Context.getpackagemanager ()
. Getactivityinfo (Activity.getcomponentname (),
Packagemanager.get_meta_data);
String dataname=activityinfo.metadata.getstring ("Data_name");

Note: Activity.getcomponentname () can also use the following substitution for componentname to obtain an activity instance:

The code is as follows Copy Code
To generate a new componentname by specifying an activity class
ComponentName componentname =new componentname (context, splashactivity.class);

3. Get the Meta-data in the service tag:
Shaped like:

The code is as follows Copy Code
< service ...>
<meta-data android:value= "My_service" android:name= "Data_name"/>
</service >

Code:

The code is as follows Copy Code
ComponentName componentname=new componentname (context, myservice.class);
ServiceInfo serviceinfo = Context.getpackagemanager ()
. GetServiceInfo (ComponentName,
Packagemanager.get_meta_data);
String dataname=serviceinfo.metadata.getstring ("Data_name");

Note: There is no Getcomponentname () method in the service to quickly get to the current componentname, so you can only generate a new componentname by specifying the service class

4. Get the Meta-data in the receiver label:
Shaped like:

The code is as follows Copy Code
< receiver ...>
<meta-data android:value= "My_receiver" android:name= "Data_name"/>
</receiver >

Code:

The code is as follows Copy Code
ComponentName componentname=new componentname (context, myservice.class);
Activityinfo activityinfo = Context.getpackagemanager ()
. Getreceiverinfo (ComponentName,
Packagemanager.get_meta_data); Www.111cn.net
String dataname=activityinfo.metadata.getstring ("Data_name");

Note: In the receiver can not receiverinfo, but with the activityinfo to deal with.

Final Note: When we get the value, we use the following: XxxInfo.metaData.getString ("Data_name"); All values are obtained as strings, if they are int in Meta-data, If the value obtained using the GetString () method is empty, the metadata provides so many get methods to obtain.
If the data type of the metaData is not determined, you can get it directly using object = Metadata.get (key) and then convert to the data type you want.

Related Article

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.