我对android上的可访问性很陌生。在浏览类和文档时,我在TYPE_ACCESSIBILITY_OVERLAY类中遇到了WindowManager
documentation表示(仅相关文本)
例如,如果有全屏辅助功能覆盖
可触摸,下面的窗口将由
无障碍服务,即使它们被一个可触摸的
窗户。
所以我开始实现这个目标,一个全屏的可访问性覆盖,并尝试反省它下面的窗口
扩展AccessibilityService并在调用onServiceConnected时添加我的全屏覆盖(添加覆盖的灵感来自here

@Override
protected void onServiceConnected() {
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    FrameLayout mLayout = new FrameLayout(this);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
    lp.format = PixelFormat.TRANSLUCENT;
    lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    lp.gravity = Gravity.TOP;
    wm.addView(mLayout, lp);

    mLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Here I'm getting the touch events on the overlay I added
            return false;
        }
    });
}

现在,问题是,我该如何反省或找到这个覆盖层下面的窗口?即使在onAccessibilityEvent回调中,我也只得到这个覆盖窗口。getWindows()总是有一个size of 1。它是否反驳了上述关于TYPE_ACCESSIBILITY_OVERLAY的断言?
相关信息:要接收覆盖上的触摸事件,我已在服务设置中禁用了TouchExplorationMode
android:canRequestTouchExplorationMode="false"

最佳答案

您似乎缺少的是配置上的flagRetrieveInteractiveWindows。这些属性和窗口布局参数配置应该可以工作,而不需要您禁用canRequestTouchExplorationMode来获取事件,并且让getWindows返回您下面的可访问的indowinfo实例:

    <?xml version="1.0" encoding="utf-8"?>
    <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
        android:packageNames="test.demo.com.tests"
        android:accessibilityEventTypes="typeAllMask"
        android:accessibilityFlags="flagRetrieveInteractiveWindows|flagReportViewIds|flagIncludeNotImportantViews"
        android:accessibilityFeedbackType="feedbackAllMask"
        android:notificationTimeout="100"
        android:canRetrieveWindowContent="true"
        />

在连接的服务上:
    @Override
    protected void onServiceConnected() {
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        FrameLayout layout = new FrameLayout(this);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS|
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP;

        windowManager.addView(layout, params);
        layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //You can either get the information here or on onAccessibilityEvent
                return false;
            }
        });
    }

编辑:
添加FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS以完成全屏显示,删除canRequestTouchExplorationMode,因为与此属性相关联的标志不应包括在内,因此没有任何用处。

07-28 02:19