使用ddmlib.jar获取Android设备方向

时间:2011-04-06 12:45:27

标签: android screenshot

我正在开发一个程序,允许用户通过使用ddmlib库在他的计算机屏幕上显示他的Android手机的当前屏幕(通过USB连接到计算机)。我需要知道手机是横向还是纵向模式。

有没有办法从计算机上运行的程序中检查此值?

2 个答案:

答案 0 :(得分:1)

这是我的功能,但它很慢。现在我正在寻找更快的解决方案。

private int getLandscape() {
        int result = 0;
        try {
            String[] commands = new String[] { "adb", "-s", actualSerial,
                    "shell", "dumpsys input | grep 'SurfaceOrientation'" };
            Runtime rt = Runtime.getRuntime();
            Process pr;
            pr = rt.exec(commands);
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    pr.getInputStream()));
            String line = "";
            line = input.readLine();
            if (line != null) {
                String[] lines = line.split(":");
                result = Integer.parseInt(lines[1].trim());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

最慢的部分是从BufferedReader读取。

答案 1 :(得分:0)

class AReceiver implements IShellOutputReceiver {
    int x;
    int y;
    @Override
    public void addOutput(byte[] arg0, int arg1, int arg2) {
        String xS = new String(arg0);
        xS = xS.substring(26).trim();
        String[] screenRes = xS.split("x");
        x = Integer.parseInt(screenRes[0]);
        y = Integer.parseInt(screenRes[1]);
    }

    @Override
    public void flush() {

    }

    @Override
    public boolean isCancelled() {
        return false;
    }       
}
AReceiver receiver = new AReceiver();
try {
    dev.executeShellCommand("dumpsys window | grep \"mOverscanScreen\"", receiver);
    res[0] = receiver.x;
    res[1] = receiver.y;
} catch (TimeoutException | AdbCommandRejectedException | ShellCommandUnresponsiveException | IOException e) {
        e.printStackTrace();
}

使用executeShellCommand的Ffaster解决方案。在我的机器上,以前的解决方案需要大约400ms才能得到答案,这个解决方案在大约50ms中得到了解决方案:)祝你的应用程序好运!