Android 从AndroidManifest获取meta-data

语法如下:

 

<meta-data android:name="string"
           android:resource="resource specification"
           android:value="string" />

 

 

<meta-data>标签可以作为子标签,可以被包含在<activity>、<application> 、<service>和<receiver>中:

 

1. Activity标签中:

 <activity...>
       <meta-data android:name="Data_Name" android:value="DataValue"></meta-data>
    </activity>

 在代码中动态获取:

ActivityInfo info=this.getPackageManager()
                          .getActivityInfo(getComponentName(),
                          PackageManager.GET_META_DATA);
    String msg =info.metaData.getString("Data_Name");

 

 

2.application标签中:

<application...>
       <meta-data android:value="DataValue" android:name="Data_Name"></meta-data>
   </application>

 

在代码中动态获取:

 

 ApplicationInfo appInfo = this.getPackageManager()
                                  .getApplicationInfo(getPackageName(),
                          PackageManager.GET_META_DATA);
    String msg=appInfo.metaData.getString("Data_Name");

 

 

3.在service标签中:

 

<service android:name="MetaDataService">
      <meta-data android:value="DataValue" android:name="Data_Name"></meta-data>
   </service>

 在代码中获取:

 

ComponentName cn=new ComponentName(this, MetaDataService.class);
   ServiceInfo info=this.getPackageManager()
                        .getServiceInfo(cn, PackageManager.GET_META_DATA);
    String msg=info.metaData.getString("Data_Name");

 

 

4. 在receive标签中:

 <receiver android:name="MetaDataReceiver">
            <meta-data android:value="DataValuer" android:name="Data_Name"></meta-data>
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
    </receiver>

 

 

在代码中获取:

 

ComponentName cn=new ComponentName(context, MetaDataReceiver.class);
    ActivityInfo info=context.getPackageManager()
                             .getReceiverInfo(cn, PackageManager.GET_META_DATA);
    String msg=info.metaData.getString("Data_Name");

 

   

posted @ 2016-08-26 14:57  狂奔的小狮子  阅读(5790)  评论(0编辑  收藏  举报