访问机器人:INSTALLLOCATION清单属性 [英] Accessing android:installLocation manifest attribute

查看:105
本文介绍了访问机器人:INSTALLLOCATION清单属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个Android 2.2的应用程序,会发现可以移动到SD卡上安装的应用程序。做到这一点的权限是EN codeD中的<一个href="http://developer.android.com/guide/topics/manifest/manifest-intro.html">AndroidManifest.xml文件为根级属性<一href="http://developer.android.com/guide/appendix/install-location.html">android:installLocation". PackageInfo 似乎有一个接口一应俱全,但这个属性。我可以打开安装APK并提取AndroidManifest.xml文件,但它似乎是在一些二进制编码格式,其中一些随机的互联网人都写一个去codeR的,但是,似乎是一个可怕的很多工作。

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 root-level attribute "android:installLocation". PackageInfo seems to have an interface to everything but this attribute. I can open the installed apk and extract the AndroidManifest.xml file, but it seems to be in some binary encoding format, which some random internet people have written a decoder for, but that seems like an awful lot of work.

有没有办法,我失去了一个接口?

Is there an interface that I'm missing?

推荐答案

事实证明,虽然没有直接的API调用来获取 INSTALLLOCATION ,我也不知道有手动解析二进制XML,因为所提供的 XmlResourceParser 工作就可以了。

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 determined
private static final int auto = 0;
private static final int internalOnly = 1;
private static final int 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))) {
                            case auto:
                                // Do stuff
                                break;
                            case internalOnly:
                                // Do stuff
                                break;
                            case preferExternal:
                                // Do stuff
                                break;
                            default:
                                // Shouldn't happen
                                // Do stuff
                                break;
                        }
                        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.

这篇关于访问机器人:INSTALLLOCATION清单属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆