自动化工具 安卓屏幕旋转角度监控

甬力君 · July 17, 2018 · 2042 hits

0x00.实现背景
之前试过使用反射截取安卓屏幕,文章在此:点我查看
现在试试屏幕旋转监听,具体实现参考谷歌源码:源码 1源码 2

0x01.实现原理(仅支持安卓 4.4 及以上,以下也没啥用)

  • sdk < 26: WindowManager 对象的 getRotation 方法
  • sdk >= 26: WindowManager 对象的 getDefaultDisplayRotation 方法

0x02.关键代码:

public int getRotation() {
        try {
            Method getter = windowManager.getClass().getMethod("getDefaultDisplayRotation");
            return (Integer) getter.invoke(windowManager);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Method getter = windowManager.getClass().getMethod("getRotation");
            return (Integer) getter.invoke(windowManager);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

0x03.demo 尝鲜
点此下载:RotationWatcher
运行方式:

  • adb push RotationWatcher.dex /data/local/tmp/RotationWatcher.dex
  • adb shell app_process -Djava.class.path=/data/local/tmp/RotationWatcher.dex /data/local/tmp RotationWatcher

0x04.完成,效果如下

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up