Android:系统覆盖

时间:2015-02-27 10:11:45

标签: java android android-layout

如何覆盖屏幕?

  1. 它应该是可触摸的
  2. view必须覆盖系统操作栏
  3. 视图必须覆盖软按钮/ home / recent_apps
  4. 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);
    }
    ...
    }
    
  5. 和布局参数创建者

    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事件。

    有什么想法吗?

2 个答案:

答案 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预览-

enter image description here