获取application和activity下的meta-data的值

获取application下meta-data的值

public static String getAppKey(Context c,String appKey) {
		try {
			ApplicationInfo ai = c.getPackageManager().getApplicationInfo(
					c.getPackageName(), PackageManager.GET_META_DATA);
			Object EP_APPKEY = ai.metaData.get(appKey);
			if (EP_APPKEY instanceof Integer) {
				long longValue = ((Integer) EP_APPKEY).longValue();
				String value = String.valueOf(longValue);
				return value;
			} else if (EP_APPKEY instanceof String) {
				String value = String.valueOf(EP_APPKEY);
				return value;
			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
		}
		return null;

	}

获取activity下meta-data的值

ActivityInfo info=this.getPackageManager()
                          .getActivityInfo(getComponentName(),
                          PackageManager.GET_META_DATA);
    String msg =info.metaData.getString("data_Name");

获取service下的meta-data

 ComponentName cn=new ComponentName(this, MetaDataService.class);
   ServiceInfo info=this.getPackageManager()
                        .getServiceInfo(cn, PackageManager.GET_META_DATA);
    String msg=info.metaData.getString("data_Name");

获取receiver下的meta-data

 ComponentName cn=new ComponentName(context, MetaDataReceiver.class);
    ActivityInfo info=context.getPackageManager()
                             .getReceiverInfo(cn, PackageManager.GET_META_DATA);
    String msg=info.metaData.getString("data_Name");



你可能感兴趣的:(Android转载)