转载: Android 读取<meta-data>元素的数据
很多时候我们需要在Android Manifest 清单文件中的 Activity、Service、BroadcastReceiver或者Application节点上配置一些称为 元数据的信息,然后在程序启动的时候去获取清单文件中配置的元数据信息。例如 友盟统计SDK的 API Key 和 Channel ID,都是在清单文件中进行配置的。
在AndroidManifest.xml中,<meta-data>元素可以作为子元素,被包含在<activity>、<application> 、<service>和<receiver>元素中,但
不同的父元素,在应用时读取的方法也不同,下面通过一个demo工程来演示如何读取meta-data 数据。
Android Manifest 文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.metadatatest"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="8" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <meta-data android:name="mTag" android:value="@string/meta_application"></meta-data>
- <activity
- android:name="com.example.metadatatest.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <!-- 一般的值可以通过value属性来指定,但是如果要指定一个资源id作为一个值,那么就要用resource属性来代替。 -->
- <meta-data android:name="mTag" android:value="@string/meta_activity"></meta-data>
- <!--利用resource属性将指定 mResource 的资源id号-->
- <meta-data android:name="mResource" android:resource="@drawable/ic_launcher"></meta-data>
- </activity>
- <service android:name="com.example.metadatatest.service.DemoService">
- <meta-data android:name="mTag" android:value="@string/meta_service"></meta-data>
- </service>
- <receiver android:name="com.example.metadatatest.receiver.DemoReceiver">
- <intent-filter android:priority="1000">
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- <meta-data android:name="mTag" android:value="@string/meta_broadcast"></meta-data>
- </receiver>
- </application>
- </manifest>
这个名字值是额外的任意的可以提供给父组件的数据。一个组件元素能够包含任意数量的meta-data子元素。它们所有的值都会被收集在Bundle对象中并且使其可以作为组件的 PackageItemInfo.metaData 字段。
一般的值可以通过value属性来指定,但是如果要指定一个资源id作为一个值,那么就要用resource属性来代替。
MainActivity 代码
- package com.example.metadatatest;
- import com.example.metadatatest.receiver.DemoReceiver;
- import com.example.metadatatest.service.DemoService;
- 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.content.pm.ServiceInfo;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends Activity implements OnClickListener {
- private static final String TAG = MainActivity.class.getSimpleName();
- private ImageView iv_pic;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findView();
- }
- private void findView() {
- Button bt_read_activity = (Button) findViewById(R.id.bt_read_activity);
- Button bt_read_service = (Button) findViewById(R.id.bt_read_service);
- Button bt_read_broadcast = (Button) findViewById(R.id.bt_read_broadcast);
- Button bt_read_application = (Button) findViewById(R.id.bt_read_application);
- iv_pic = (ImageView) findViewById(R.id.iv_pic);
- bt_read_activity.setOnClickListener(this);
- bt_read_service.setOnClickListener(this);
- bt_read_broadcast.setOnClickListener(this);
- bt_read_application.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.bt_read_activity:
- readMetaDataFromActivity();
- break;
- case R.id.bt_read_service:
- readMetaDataFromService();
- break;
- case R.id.bt_read_broadcast:
- readMetaDataFromBroadCast();
- break;
- case R.id.bt_read_application:
- readMetaDataFromApplication();
- break;
- default:
- break;
- }
- }
- /**
- * 读取application 节点 meta-data 信息
- */
- private void readMetaDataFromApplication() {
- try {
- ApplicationInfo appInfo = this.getPackageManager()
- .getApplicationInfo(getPackageName(),
- PackageManager.GET_META_DATA);
- String mTag = appInfo.metaData.getString("mTag");
- Log.e(TAG, "mTag=" + mTag);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取BroadcastReceiver 节点 meta-data 信息
- */
- private void readMetaDataFromBroadCast() {
- try {
- ComponentName cn = new ComponentName(this, DemoReceiver.class);
- ActivityInfo info = this.getPackageManager().getReceiverInfo(cn,
- PackageManager.GET_META_DATA);
- String mTag = info.metaData.getString("mTag");
- Log.e(TAG, "mTag=" + mTag);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取Service 节点 meta-data 信息
- */
- private void readMetaDataFromService() {
- try {
- ComponentName cn = new ComponentName(this, DemoService.class);
- ServiceInfo info = this.getPackageManager().getServiceInfo(cn,
- PackageManager.GET_META_DATA);
- String mTag = info.metaData.getString("mTag");
- Log.e(TAG, "mTag=" + mTag);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取Activity 节点 meta-data 信息
- */
- private void readMetaDataFromActivity() {
- ActivityInfo info;
- try {
- info = this.getPackageManager().getActivityInfo(getComponentName(),
- PackageManager.GET_META_DATA);
- String mTag = info.metaData.getString("mTag");
- Log.e(TAG, "mTag=" + mTag);
- //读取图片资源id
- int mResource = info.metaData.getInt("mResource");
- iv_pic.setImageResource(mResource);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- }