Android 從AndroidManifest獲取meta-data

語法以下:java

 

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

 

 

<meta-data>標籤能夠做爲子標籤,能夠被包含在<activity>、<application> 、<service>和<receiver>中:android

 

1. Activity標籤中:app

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

 在代碼中動態獲取:this

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

 

 

2.application標籤中:blog

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

 

在代碼中動態獲取:ci

 

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

 

 

3.在service標籤中:get

 

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

 在代碼中獲取:string

 

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標籤中:it

 <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>

 

 

在代碼中獲取:io

 

ComponentName cn=new ComponentName(context, MetaDataReceiver.class);
    ActivityInfo info=context.getPackageManager()
                             .getReceiverInfo(cn, PackageManager.GET_META_DATA);
    String msg=info.metaData.getString("Data_Name");
相關文章
相關標籤/搜索