Skoky
Skoky

Reputation: 869

AndroidManifest meta-data on top level listing in java code

How can I get AndroidManifest's meta-data in java code. I don't need application's meta-data, but these from higher level in the manifest xml. My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.aaa.bbb">


   <application
    ...
    <activity
      ....
    </activity>
   </application>

<meta-data android:name="mykey" android:value="myvalue"/>

</manifest>

I need to extract mykey's value in my java code.

Upvotes: 1

Views: 789

Answers (2)

H&#233;ctor
H&#233;ctor

Reputation: 26034

I'm using this code for that. Hope it helps.

private String getStringFromManifest(String whatToGet) {
        String valueObtained = null;
        try {
            Bundle data = getManifestData();
            valueObtained = data.getString(whatToGet, null);
            if (valueObtained == null) {
                log.error("Value for '" + whatToGet + "' not found in AndroidManifest");
            }
        } catch (PackageManager.NameNotFoundException e) {
            log.error("Error in configuration");
        }
        return valueObtained;
    }


private Bundle getManifestData() throws PackageManager.NameNotFoundException {
    ComponentName myActivity = new ComponentName(this, this.getClass());
    return getPackageManager().getActivityInfo(myActivity, PackageManager.GET_META_DATA).metaData;
}

Upvotes: 1

Jordi Castilla
Jordi Castilla

Reputation: 26961

You must give some of your code so see what have you tried...

Anyway, the standard will be like this:

File xml_file=new File("manifest.xml");
if (xml_file.isFile() && xml_file.canRead()) {
    Document doc = builder.parse(xml_file);
    Element root = doc.getDocumentElement();
    NodeList nodel = root.getChildNodes();
    Node node = nodel.item(a);
    if(node instanceof Element) {
        // this is the meta-data element
        Element data = ((Element)node).getAttribute("meta-data");
        // if you want the attributes
        String key = data.getNamedItem("android:name").getNodeValue();
    }
}

Upvotes: 0

Related Questions