標籤:android meta-data application activityinfo applicationinfo
meta-data在AndroidManifest中是以鍵值對的形式存在的,可以將meta-data放在application根節點下,也可以放在某個activity節點下。因為存放的位置不同,因此擷取value時也要使用對應的方法,下面是我寫的一個小測試。
代碼如下:
先是在AndroidManifest中的application和mainActivity下添加兩個meta-data屬性。
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.lzy.test" android:value="測試" /> <activity android:name=".MainActivity" android:label="@string/app_name" > <meta-data android:name="com.example.metatest.MainActivity.com.lzy.activity.test" android:value="Activity測試" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
接下來在mainActivity中來擷取這兩個屬性的值。
package com.example.metatest;import android.app.Activity;import android.content.ComponentName;import android.content.pm.ActivityInfo;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.util.Log;import android.widget.Toast;/** * meta 測試 * * @author Administrator * */public class MainActivity extends Activity {public static final String TEST = "com.lzy.test";public static final String ACTIVITYTEST = MainActivity.class.getName()+".com.lzy.activity.test";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String test = getMetaDataStringApplication(TEST, null);String activityTest = getMetaDataStringFromActivity(ACTIVITYTEST, null);Toast.makeText(this, test+"--"+activityTest, Toast.LENGTH_LONG).show();}/** * 根據key從Application中返回的Bundle中擷取value * * @param key * @param defValue * @return */private String getMetaDataStringApplication(String key, String defValue) {Bundle bundle = getAppMetaDataBundle(getPackageManager(), getPackageName());if (bundle != null && bundle.containsKey(key)) {return bundle.getString(key);}return defValue;}/** * 擷取Application中的meta-data. * * @param packageManager * @param packageName * @return */private Bundle getAppMetaDataBundle(PackageManager packageManager,String packageName) {Bundle bundle = null;try {ApplicationInfo ai = packageManager.getApplicationInfo(packageName,PackageManager.GET_META_DATA);bundle = ai.metaData;} catch (NameNotFoundException e) {Log.e("getMetaDataBundle", e.getMessage(), e);}return bundle;}/** * 根據key從Activity中返回的Bundle中擷取value * * @param key * @param defValue * @return */private String getMetaDataStringFromActivity(String key, String defValue) {Bundle bundle = getActivityMetaDataBundle(getPackageManager(), getComponentName());if (bundle != null && bundle.containsKey(key)) {return bundle.getString(key);}return defValue;}/** * 擷取Activity中的meta-data. * * @param packageManager * @param component * @return */private Bundle getActivityMetaDataBundle(PackageManager packageManager, ComponentName component) {Bundle bundle = null;try {ActivityInfo ai = packageManager.getActivityInfo(component,PackageManager.GET_META_DATA);bundle = ai.metaData;} catch (NameNotFoundException e) {Log.e("getMetaDataBundle", e.getMessage(), e);}return bundle;}}
源碼下載
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
android 擷取application和activity下meta-data中的值