android 获取application下meta-data中的值

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

	@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;
	}

}
    

源码下载:http://download.csdn.net/detail/u012527802/9070361

来源:http://blog.csdn.net/u012527802/article/details/48157637

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>