标签: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>
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"; @Override protected 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中的值
标签:android meta-data application activityinfo applicationinfo
原文地址:http://blog.csdn.net/u012527802/article/details/48157637