例如,这里是清单文件中的元数据:
<meta-data
android:name="AppId"
android:value="347" />
这是我的代码:
Activity instance = getActivity();
ApplicationInfo appInfo = instance.getPackageManager().getApplicationInfo(instance.getPackageName(), PackageManager.GET_META_DATA);
if (appInfo.metaData != null)
result = appInfo.metaData.getString(key);
我发现&#34;结果&#34;一片空白!为什么?它可以保持&#34; AppId&#34;作为我的字符串?
答案 0 :(得分:1)
尝试这样做。
Activity instance = getActivity();
ApplicationInfo appInfo = instance.getPackageManager().getApplicationInfo(instance.getPackageName(), PackageManager.GET_META_DATA);
if (appInfo.metaData != null)
result = String.valueOf(appInfo.metaData.getInt(key));
答案 1 :(得分:1)
你应该注意applicationInfo.metaData是Bundle
也许你可以用我写的这个方法。
@Nullable
public static String getAppMetaData(Context ctx, String key) {
if (ctx == null || TextUtils.isEmpty(key)) {
return null;
}
String resultData = null;
try {
PackageManager packageManager = ctx.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null) {
if (applicationInfo.metaData != null) {
resultData = String.valueOf(applicationInfo.metaData.get(key));
}
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return resultData;
}
无论值是String还是int