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

Can I get other application's available screen orientation in android?

我计划在 android 中开发屏幕方向服务,以允许您 pside 在某些 phone 中使用俯视图。例如我的是 Nexus6,不能从自动旋转

向下旋转 upside

我正在使用其他旋转控制应用程序。而且它缺少我需要的一项功能

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

我用过的app都不能这样设置。当传感器向下对齐时,它强制横向应用程序为纵向,结果非常难看

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

可能吗?

ps。这是我要开发的服务示例

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

在 Activity 的 AndroidMaifest 中使用这个 android:screenOrientation="sensor"

您可以阅读其他应用的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();
    }
}

"problem" 是你将得到 二进制 xml 这不仅仅是一个转换为字节数组的字符串;它是 xml 个文件的压缩格式。

你需要解压这个数组得到一个String然后你可以解析它得到screenOrientation的值。

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

然后您将以这种方式获得字符串:

String xml = AndroidXMLDecompress.decompressXML(binaryXml);