如何从AndroidManifest.xml获取大量元数据标签

时间:2014-09-02 06:57:34

标签: android metadata android-manifest

我想得到一个sting frome元数据标签,但我不能使用metadata.getString(),因为它返回null。所以,我使用metadata.getInt(),但我无法获得正确的值。我该怎么做?<meta-data android:name="GDT_BANNER_POST_ID" android:value="9079537218417626401"/>

2 个答案:

答案 0 :(得分:1)

使用此:

ActivityInfo app = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
Object value = (Object)metaData.get(name);

答案 1 :(得分:0)

使用方法,例如:

    private String retrieveKey(Context aContext, String aKey) {

    // get the key from the manifest 
    final PackageManager pm = aContext.getPackageManager();
    try { 
        final ApplicationInfo info = pm.getApplicationInfo(aContext.getPackageName(),
                PackageManager.GET_META_DATA);
        if (info.metaData == null) {
            Log.i("Key not found in manifest", aKey);
        } else { 
            final String value = String.valueOf(info.metaData.get(aKey));
            if (value == null) {
                Log.i("Key not found in manifest", aKey);
            } else { 
                return value.trim();
            } 
        } 
    } catch (final PackageManager.NameNotFoundException e) {
        Log.i("Key not found in manifest", aKey);
    } 
    return ""; 
} 

在你的清单中,添加:

<meta-data
            android:name="MY_KEY"
            android:value="MY_VALUES" />

在您的代码中,只需检索它:

String myKey = retrieveKey(this, "MY_KEY");