无法拦截覆盖服务上的触摸
Cannot intercept touch on overlay service
我正在尝试使用服务和透明覆盖阻止触摸设备。
该代码似乎在 Android Oreo 上运行良好,但在 运行 Android 版本低于 Oreo 的设备上无法正常运行:
private void setWindowParams(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
}
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
FrameLayout noTouchUi = new FrameLayout(this);
wm.addView(noTouchUi,params);
}
我在这里设置了一个 FrameLayout noTouchUi
以使用 onTouchListener
的服务拦截触摸作为覆盖
noTouchUi.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG,"touch event");
return true;
}
});
这在 Android 8 上正常工作,但在 Android 7 上不起作用,叠加层显示正常,但叠加层后面的项目被点击。我想做的是阻止该点击。
更改类型自:
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
至:
WindowManager.LayoutParams.TYPE_PHONE
并删除标志:
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
和:
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
我正在尝试使用服务和透明覆盖阻止触摸设备。
该代码似乎在 Android Oreo 上运行良好,但在 运行 Android 版本低于 Oreo 的设备上无法正常运行:
private void setWindowParams(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
}
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
FrameLayout noTouchUi = new FrameLayout(this);
wm.addView(noTouchUi,params);
}
我在这里设置了一个 FrameLayout noTouchUi
以使用 onTouchListener
noTouchUi.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG,"touch event");
return true;
}
});
这在 Android 8 上正常工作,但在 Android 7 上不起作用,叠加层显示正常,但叠加层后面的项目被点击。我想做的是阻止该点击。
更改类型自:
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
至:
WindowManager.LayoutParams.TYPE_PHONE
并删除标志:
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
和:
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH