From 47345ab92feb419fcddc0ba445a40c7e96e34793 Mon Sep 17 00:00:00 2001 From: zengfantian Date: Sat, 4 Jul 2015 00:39:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5gainMetaData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/zftlive/android/tools/ToolData.java | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/com/zftlive/android/tools/ToolData.java b/src/com/zftlive/android/tools/ToolData.java index e1394d9..30b6e3a 100644 --- a/src/com/zftlive/android/tools/ToolData.java +++ b/src/com/zftlive/android/tools/ToolData.java @@ -1,5 +1,15 @@ package com.zftlive.android.tools; +import android.app.Activity; +import android.app.Application; +import android.app.Service; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ServiceInfo; import android.inputmethodservice.ExtractEditText; import android.util.Log; import android.view.View; @@ -142,6 +152,39 @@ public class ToolData { return data; } + /** + * 读取AndroidManifest.xml配置的meta-data数据 + * @param mContext 上下文 + * @param target Activity/BroadcastReceiver/Service/Application + * @param key 配置的name + * @return + */ + public static String gainMetaData(Context mContext, Class target, String key) { + String result = ""; + try { + int flags = PackageManager.GET_META_DATA; + Object obj = target.newInstance(); + if (obj instanceof Activity) { + ActivityInfo info2 = mContext.getPackageManager().getActivityInfo(((Activity) mContext).getComponentName(), flags); + result = info2.metaData.getString(key); + } else if (obj instanceof Application) { + ApplicationInfo info1 = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), flags); + result = info1.metaData.getString(key); + } else if (obj instanceof Service) { + ComponentName cn1 = new ComponentName(mContext, target); + ServiceInfo info3 = mContext.getPackageManager().getServiceInfo(cn1, flags); + result = info3.metaData.getString(key); + } else if (obj instanceof BroadcastReceiver) { + ComponentName cn2 = new ComponentName(mContext, target); + ActivityInfo info4 = mContext.getPackageManager().getReceiverInfo(cn2, flags); + result = info4.metaData.getString(key); + } + } catch (Exception e) { + Log.e(TAG, "读取meta元数据失败,原因:" + e.getMessage()); + } + return result; + } + /** * 请求分页 * @param pageNo 分页号码