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