我可以在Android中获得其他应用程序的可用屏幕方向吗?

Tha*_*ina 11 android screen-orientation android-activity

我有一个计划在Android中开发屏幕定位服务,以允许在一些手机中颠倒肖像.例如,我的是Nexus6,不能从自动旋转上下颠倒

我正在使用其他旋转控制应用程序.它缺少我需要的一个功能

我想允许来自传感器的力旋转.但是应用程序的首选方向限制为纵向或横向.如果应用程序设计为纵向模式,它可以是0或180度而不是90或270,反之亦然

我使用的所有应用程序都不能这样设置.当传感器向下对齐并且结果非常难看时,它强制横向应用程序为纵向

要做到这一点,我想我需要获得ApplicationInfo或类似的东西,并获得应用程序在其清单中设置的"android:screenOrientation"的值

可能吗?

PS.这是我想要开发的服务样本

https://play.google.com/store/apps/details?id=com.pranavpandey.rotation&hl=en

Oma*_*lak 4

您可以阅读其他应用程序的Manifest文件:

PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES);
for(ApplicationInfo app : apps){
    try {
        ZipFile apk = new ZipFile(app.publicSourceDir);
        ZipEntry manifest = apk.getEntry("AndroidManifest.xml");
        if (manifest != null){
            byte[] binaryXml = toByteArray(apk.getInputStream(manifest));
            // decode binary Xml
        }
        apk.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static byte[] toByteArray(InputStream in) throws IOException {
    try {
        byte[] buf = new byte[1024*8];
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            int len;
            while ((len = in.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
            return bos.toByteArray();
        }
    } finally {
        in.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

“问题”是你将得到二进制 xml,它不仅仅是转换为字节数组的字符串;它是 xml 文件的压缩格式。

您需要解压缩该数组以获取 a String,然后您可以解析它以获取 的值screenOrientation

我发现这个GIST可以完成这项工作,但IndexOutOfBounds在某些情况下会引发错误...最难的部分(解码 binray xml)已经完成,您只需修复异常问题。

然后你会这样得到字符串:

String xml = AndroidXMLDecompress.decompressXML(binaryXml);
Run Code Online (Sandbox Code Playgroud)