如何覆盖屏幕?
Android 4.0 +
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
WindowManager.LayoutParams params = ViewUtils.generateFullScreenParams(true);
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(overlayView, params);
}
...
}
和布局参数创建者
public static WindowManager.LayoutParams generateFullScreenParams() {
return new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
}
好的,看起来不错。
标记TYPE_SYSTEM_ALERT 不覆盖系统栏(android< 5.0),不覆盖软按钮,但我可以处理onTouch事件
标记TYPE_SYSTEM_OVERLAY 覆盖系统栏,不覆盖按钮,无法处理onTouch事件。
有什么想法吗?
答案 0 :(得分:1)
public static WindowManager.LayoutParams generateFullScreenParams() {
return new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
}
在此代码中,您添加了WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
删除此行的作品,并在查看其作品时添加触摸事件
对于“屏幕覆盖图”权限,我是指此帖子How to enable screen overlay permission by default
答案 1 :(得分:0)
您可以使用我的library删除此方法addWatermarkWithinApplicationLifecycle(),该方法会将覆盖层粘贴到应用程序生命周期中(您要在整个电话应用程序中显示覆盖层)我试图以一种可重用的方式进行操作,您可以通过一个简单的方法调用来隐藏和显示覆盖。也请不要忘记添加这些权限-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION"/>
我的图书馆的gif预览-