我不熟悉 Android 上的辅助功能。在浏览类和文档时,我在 WindowManager
类中遇到了 TYPE_ACCESSIBILITY_OVERLAY
。
documentation说(只有相关的文字)
For example, if there is a full screen accessibility overlay that is touchable, the windows below it will be introspectable by an accessibility service even though they are covered by a touchable window.
所以我开始着手实现这一点,一个全屏的可访问性覆盖层,并尝试检查它下面的窗口
扩展了 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() 大小始终为 1
。不是反驳了上面对TYPE_ACCESSIBILITY_OVERLAY
的断言吗?
相关信息:为了在覆盖层上接收触摸事件,我在服务设置中禁用了 touchExplorationMode
android:canRequestTouchExplorationMode="false"
最佳答案
您似乎缺少的是 flagRetrieveInteractiveWindows在你的配置上。这些属性和窗口布局参数配置应该可以工作,不需要您禁用 canRequestTouchExplorationMode
来获取事件并让 getWindows
返回您下面的 AccessibilityWindowInfo 实例:
<?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
,因为不应包含与此属性关联的标志,因此没有用。
关于android - 全屏 TYPE_ACCESSIBILITY_OVERLAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677833/