Window和WindowManager

如果需要在桌面上显示一个类似悬浮窗的东西,这种效果需要使用Window来实现。Window是一个抽象类,它的具体实现是PhoneWindow。创建一个Window需要通过WindowManager来完成,WindowManager是外界访问Window的入口,Window的具体实现位于WindowMangerService中,WindowManger和WindowManagerService的交互是一个IPC过程。

WindowManager.LayoutParams中flags和type参数。此处列举常用的选项。

FLAG_NOT_FOCUSABLE

this window won't ever get key input focus, so the user can not send key or other button events to it. Those will instead go to whatever focusable window is behind it. This flag will also enable FLAG_NOT_TOUCH_MODAL whether or not that is explicitly set.

表示Window不需要获取焦点,也不需要接收各种输入事件,此标记会同时启用FLAG_NOT_TOUCH_MODAL,最终事件会直接传递给下层的具有焦点的Window。

FLAG_NOT_TOUCH_MODAL

even when this window is focusable (its FLAG_NOT_FOCUSABLE is not set), allow any pointer events outside of the window to be sent to the windows behind it. Otherwise it will consume all pointer events itself, regardless of whether they are inside of the window.

在此模式下,系统会将当前Window区域以外的单击事件传递给底层的Window,当前Window区域以内的单击事件则自己处理。这个标记很重要,一般来说都需要开启此标记,否则其他Window将无法收到单击事件。

FLAG_SHOW_WHEN_LOCKED

This constant was deprecated in API level 27.
Use R.attr.showWhenLocked or Activity.setShowWhenLocked(boolean) instead to prevent an unintentional double life-cycle event.
special flag to let windows be shown when the screen is locked. This will let application windows take precedence over key guard or any other lock screens. Can be used with FLAG_KEEP_SCREEN_ON to turn screen on and display windows directly before showing the key guard window. Can be used with FLAG_DISMISS_KEYGUARD to automatically fully dismisss non-secure keyguards. This flag only applies to the top-most full-screen window.

这个flag在API-27已经过时,可以使用R.attr.showWhenLocked或者Activity.setShowWhenLocked(boolean)来代替。
开启此模式可以让Window显示在锁屏界面上。

Type参数表示Window的类型,Window有三种类型,分别是应用Window、子Window、系统Window。应用类Window对应着一个Acitivity。子Window不能单独存在,它需要附属在特定的父Window之中,比如常见的一些Dialog就是一个子Window。系统Window是需要声明权限在能创建的Window,比如Toast和系统状态栏这些都是系统Window。

Window的添加,更新,删除

Window的添加使用addView方法,更新使用updateViewLayout方法,删除使用removeView方法。详细的介绍大家参考任老师的书来学习,平时的开发中会使用方法就可以了。

你可能感兴趣的:(Window和WindowManager)