android 12后WindowManager事件穿透类型悬浮窗无效问题
android 12后WindowManager事件穿透类型悬浮窗无效问题
笔记:
项目需要将一个悬浮窗WindowManager仅显示view给用户看,不可操作,将触摸事件穿透到后面的窗口。WindowManager.LayoutParams代码如下
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
在android12以下的机型中测试都没问题,触摸事件可以穿透悬浮窗。但在android 12机型测试时,触摸事件无法穿透悬浮窗了。
原因
查了android开发者官网的android12变化后,发现
解决
方法1:
例外情况下运行执行穿透操作,这里就使用无障碍窗口
创建个无障碍服务,在服务连接后回调里创建个无障碍窗口类型的悬浮窗就好了
public class MyAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
createWindow();
}
public void createWindow() {
//悬浮窗类型
int type;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
} else {
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = type;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW;
wm.addView(view, lp);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
经测试有效
方法2:
此方法比方法1更简单,来源于WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE的源码注释。翻译截图如下
也就是说,只要在触摸路径中,它是该UID下唯一的TYPE_APPLICATION_OVERLAY窗口,并且alpha等于或小于最大遮挡不透明度也就是0.8。那么久可以使用WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE来实现触摸事件穿透
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW;
//将alpha设置为最大遮挡不透明度
lp.alpha = 0.8f;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
经测试有效