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方法。詳細的介紹大家參考任老師的書來學習,平時的開發中會使用方法就可以了。