Android gets the value from the manifest configuration file metadata

Source: Internet
Author: User

Recently in work, also tasted some new knowledge, now summarized as follows
(1) Get meta data from the Androidmanifest.xml configuration file

Get data from manifest.xml config file public    static string Getmetavalue (context context, String Metakey) {        Bundle metaData = null;        String metavalue = null;        if (context = = NULL | | metakey = = NULL) {            return null;        }        try {            ApplicationInfo ai = Context.getpackagemanager (). Getapplicationinfo (                    context.getpackagename (), Packagemanager.get_meta_data);            if (null! = AI) {                metaData = ai.metadata;            }            if (null! = MetaData) {                metavalue = metadata.getstring (Metakey);            }        } catch (Namenotfoundexception e) { c17/>}        return metavalue;//xxx    }
<meta-data android:name= "api_key" android:value= "xxx"/>

(2) Get some controls in the layout file, as shown here is an activity

public class Customactivity extends Activity {    @Override public    void OnCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Resources resource = This.getresources ();        String pkgname = This.getpackagename ();        Setcontentview (Resource.getidentifier ("custom_activity", "Layout", Pkgname));
Get a layout file named Custom_activity under the Pkgname package
TextView Titleview = (TextView) This.findviewbyid (Resource.getidentifier ("title", "id", pkgname));
Get a widget with the ID of title under the Pkgname package
}}

After this activity is configured in Androidmanifest.xml, the configuration package name is the full path name.
Here is the View Resource.getidentifier () method analysis

    public int Getidentifier (string name, String Deftype, String defpackage) {        try {            return Integer.parseint (name); c3/>} catch (Exception e) {            //Ignore        }        return Massets.getresourceidentifier (name, Deftype, defpackage);
Getresources (). Getidentifier (name, Deftype, defpackage) }

Returns the identifier corresponding to the given resource_name, similar to the ID in the R file (personal understanding)!

Android gets the value from the manifest configuration file metadata

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.