Skip to content Skip to sidebar Skip to footer

Accessing Android:installlocation Manifest Attribute

I'm trying to write an Android 2.2 app that will find installed apps that can be moved to the SD card. The permission to do this is encoded in the AndroidManifest.xml file as the

Solution 1:

As it turns out, while there's no direct API call to get installLocation, neither do I have to parse the binary XML manually, as the provided XmlResourceParser works on it.

// Experimentally determinedprivatestaticfinalintauto = 0;
privatestaticfinalint internalOnly = 1;
privatestaticfinalint preferExternal = 2;

AssetManager am = createPackageContext(packageName, 0).getAssets();
XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
int eventType = xml.getEventType();
xmlloop:
while (eventType != XmlPullParser.END_DOCUMENT) {
    switch (eventType) {
        case XmlPullParser.START_TAG:
            if (! xml.getName().matches("manifest")) {
                break xmlloop;
            } else {
                attrloop:
                for (int j = 0; j < xml.getAttributeCount(); j++) {
                    if (xml.getAttributeName(j).matches("installLocation")) {
                        switch (Integer.parseInt(xml.getAttributeValue(j))) {
                            caseauto:
                                // Do stuffbreak;
                            case internalOnly:
                                // Do stuffbreak;
                            case preferExternal:
                                // Do stuffbreak;
                            default:
                                // Shouldn't happen// Do stuffbreak;
                        }
                        break attrloop;
                    }
                }
            }
            break;
        }
        eventType = xml.nextToken();
    }

Uh, I guess there's a switch in there with one case that should probably just be an if. Oh well. You get the point.

Solution 2:

Considering all other direct attributes of the manifest tag are available from PackageInfo, I think you're right to look for it there.

I know it's not in the doc, but did you try anyway? Something like

PackageInfopkg= ...;
Stringloc= pkg.installLocation();

I know this is probably very naive considering the doc may even be generated automatically - and I wouldn't dare suggest it if I could try it myself (stuck on API 7 at the moment due to retarded OS not supported anymore in 8)

If it doesn't work, I'm afraid they just overlooked that - I can't imagine they would put it elsewhere all of a sudden. In that case, you'll probably be stuck parsing the manifests by yourself indeed.

Solution 3:

In the older API's from 2007, there was public fields in the PackageInfo class that gave all information on the internalLocation and other relevant information. For security reasons I am guessing they got rid of those convenient fields.

Solution 4:

You may access this attribute by next example:

PackageInfopackageInfo= context.getPackageManager().getPackageInfo(mPackageName, 0);
if (packageInfo.installLocation != PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
    .....
}

http://developer.android.com/reference/android/content/pm/PackageInfo.html#installLocation was introduced in API 21

But this field exists even in Android 2.3 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/content/pm/PackageInfo.java/

Post a Comment for "Accessing Android:installlocation Manifest Attribute"