Java 反射例子之android Application 查看和设置:meta-data

package com.example.gaode;

import java.lang.reflect.Field;
import java.util.Map;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

public class BaseApplication extends Application {

	@Override
	public void onCreate() {

		super.onCreate();

		// android Application 查看和设置:meta-data http://my.oschina.net/xesam/blog/135333		
		// 在application应用<meta-data>元素。
		ApplicationInfo appInfo;
		try {
			appInfo = this.getPackageManager().getApplicationInfo(
					getPackageName(), PackageManager.GET_META_DATA);
			Bundle bundle = appInfo.metaData;
			if (bundle == null) {
				System.out.println("bundle==null");
			}
			bundle.putString("ddd", "xxx");
			
			
			Class class1 = Class.forName("android.os.Bundle");			
			Field[] fields = class1.getFields();//和getDeclaredFields不同
			for (Field field : fields) {
				System.out.println(field.getName());
			}
			System.out.println("========");
			fields = class1.getDeclaredFields();
			for (Field field : fields) {
				System.out.println(field.getName());
			}
			
			Field field = class1.getDeclaredField("mMap");
			if (field != null) {
				
				field.setAccessible(true);
				Map<String, Object> mMap = (Map<String, Object>) field.get(bundle);
				if(mMap==null)
				{
					System.out.println("mMap==null");
					return ;
				}
				for(Map.Entry<String, Object> entry:mMap.entrySet())
				{
					String key=entry.getKey();
					Object value=entry.getValue();
					System.out.println(key+"="+value);
				}
				
			} else {
				System.out.println("field==null");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

你可能感兴趣的:(Java 反射例子之android Application 查看和设置:meta-data)