元数据(meta-data)

本章所介绍的元数据的知识,可能在定制系统时会用到.因为那是后需要修改底层框架的一些内容


元数据(meta-data)


一、元数据的定义

在AndroidManifest.xml中如下书写:

 

 <activity

            android:name="com.njupt.metadata.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

            

            <meta-data android:name="java_ns" android:value="章泽天11" />

            <meta-data android:name="balance" android:value="45000"/>

            <meta-data android:name="c++_ns" android:value="@string/second"/>

            <meta-data android:name="wife" android:resource="@string/first"/>

        </activity>


 


二、获取元数据

MainActivity

 

package com.njupt.metadata;



import android.os.Bundle;

import android.app.Activity;

import android.content.ComponentName;

import android.content.pm.ActivityInfo;

import android.content.pm.PackageManager;

import android.content.pm.PackageManager.NameNotFoundException;

import android.view.Menu;

import android.view.View;



public class MainActivity extends Activity {



	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

	}



	public void get(View v){

		try {

			PackageManager pm = getPackageManager();

			ComponentName component = new ComponentName(this,MainActivity.class);

			ActivityInfo info = pm.getActivityInfo(component,PackageManager.GET_META_DATA);

			Bundle bundle = info.metaData;

			String java_ns = bundle.getString("java_ns");

			int balance = bundle.getInt("balance");

			String c_ns = bundle.getString("c++_ns");

			String wife = getString(bundle.getInt("wife"));

			

			System.out.println("java_ns: " + java_ns + ", balance: " + balance +",c_ns: " + c_ns +",wife: " + wife);

		} catch (NameNotFoundException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

	}

	@Override

	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.

		getMenuInflater().inflate(R.menu.main, menu);

		return true;

	}



}


 

 

你可能感兴趣的:(meta)