Android 读取元素的数据



Android 读取<meta-data>元素的数据   :存放keyvalue值供程序代码调用
在AndroidManifest.xml中,<meta-data>元素可以作为子元素,被包含在<activity>、<application>、<service>和<receiver>元素中,但 不同的父元素,在应用时读取的方法也不同。 1 :在Activity的应用。     xml代码段:    <activity...> 
       <meta-data android:name="myMsg" android:value="hello my activity"></meta-data>     </activity>     
    java代码段: 
   ActivityInfo info=this.getPackageManager() 
                          .getActivityInfo(getComponentName(),                           PackageManager.GET_META_DATA);     String msg=info.metaData.getString("myMsg");     System.out.println("myMsg:"+msg); 2:在application的应用。    xml代码段:   <application...> 
       <meta-data android:value="hello my application" android:name="myMsg"></meta-data>    </application>    
   java代码段: 
    ApplicationInfoappInfo = this.getPackageManager() 
                                  .getApplicationInfo(getPackageName(),                           PackageManager.GET_META_DATA);     String msg=appInfo.metaData.getString("myMsg");     System.out.println("myMsg:"+msg); 3:在service的应用。    xml代码段: 
   <service android:name="MetaDataService"> 
      <meta-data android:value="hello my service" android:name="myMsg"></meta-data>    </service> 



       var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);   




   java代码段: 
   ComponentNamecn=new ComponentName(this, MetaDataService.class);    ServiceInfo info=this.getPackageManager() 
                        .getServiceInfo(cn, PackageManager.GET_META_DATA);     String msg=info.metaData.getString("myMsg");     System.out.println("myMsg:"+msg); 4: 在receiver的应用。    xml代码段: 
    <receiver android:name="MetaDataReceiver">             <meta-data android:value="hello my receiver" android:name="myMsg"></meta-data>             <intent-filter> 
                <action android:name="android.intent.action.PHONE_STATE"></action>             </intent-filter>     </receiver>    java代码段: 
    ComponentNamecn=new ComponentName(context, MetaDataReceiver.class);     ActivityInfo info=context.getPackageManager() 
                             .getReceiverInfo(cn, PackageManager.GET_META_DATA);     String msg=info.metaData.getString("myMsg");     System.out.println("myMsg:"+msg);

你可能感兴趣的:(Android 读取元素的数据)