笔记:
项目需要将一个悬浮窗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;
在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() {
}
}
经测试有效
方法2:
此方法比方法1更简单,来源于WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE的源码注释。翻译截图如下
也就是说,只要在触摸路径中,它是该UID下唯一的TYPE_APPLICATION_OVERLAY窗口,并且alpha等于或小于最大遮挡不透明度也就是0.8。那么久可以使用WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE来实现触摸事件穿透文章来源:https://www.toymoban.com/news/detail-541316.html
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;
经测试有效文章来源地址https://www.toymoban.com/news/detail-541316.html
到了这里,关于android 12后WindowManager事件穿透类型悬浮窗无效问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!