Android AlertDialog level(置頂)

需求

在項目中需要用到不依賴Activity Context啟動的Dialog围辙,并且Dialog的顯示層級要在最頂部(沒錯弹囚,就是流氓軟件9Ф浮)

方案

在網(wǎng)絡(luò)上搜索“Android AlertDialog 置頂”对雪、“Android AlertDialog level”都找不到我想要的乔夯,最后搜索“Android AlertDialog Priority”找到了一篇文章,提示了我征椒。
我重新整理了一下娇哆,Dialog是在WindowManager顯示的,而WindowManager分很多不同的層勃救,使用AlertDialog的alertDialog.getWindow().setType(WindowManager.LayoutParams.***)方法可以設(shè)置這個層碍讨,這里就是關(guān)鍵了。從源碼(WindowManager.java)里面看蒙秒,type有很多垄开,我們只關(guān)注系統(tǒng)等級且沒有被隱藏的,目前有如下類型:

     /**
     * Start of system-specific window types.  These are not normally
     * created by applications.
     *window類型:第一個系統(tǒng)窗口税肪。這些彈窗不是通過application正常創(chuàng)建的。
     */
    public static final int FIRST_SYSTEM_WINDOW     = 2000;

    /**
     * Window type: the status bar.  There can be only one status bar
     * window; it is placed at the top of the screen, and all other
     * windows are shifted down so they are below it.
     * In multiuser systems shows on all users' windows.
     * window類型:狀態(tài)欄窗口榜田。因為只能有個一個狀態(tài)欄window益兄,所以它被放在屏幕最上面,
     * 且其他window在它下面箭券。在多用戶系統(tǒng)净捅,可以顯示在所有用戶的window之上。
     */
    public static final int TYPE_STATUS_BAR         = FIRST_SYSTEM_WINDOW;

    /**
     * Window type: the search bar.  There can be only one search bar
     * window; it is placed at the top of the screen.
     * In multiuser systems shows on all users' windows.
     * window類型:搜索條窗口辩块。(后面同上蛔六,略)
     */
    public static final int TYPE_SEARCH_BAR         = FIRST_SYSTEM_WINDOW+1;

    /**
     * Window type: phone.  These are non-application windows providing
     * user interaction with the phone (in particular incoming calls).
     * These windows are normally placed above all applications, but behind
     * the status bar.
     * In multiuser systems shows on all users' windows.
     * window類型:通話窗口。這些非應(yīng)用window為用戶提供通話的交互废亭,特別是來電的時候
     * 在多用戶系統(tǒng)国章,可以顯示在所有用戶的window之上。
     */
    public static final int TYPE_PHONE              = FIRST_SYSTEM_WINDOW+2;

    /**
     * Window type: system window, such as low power alert. These windows
     * are always on top of application windows.
     * In multiuser systems shows only on the owning user's window.
     * window類型:系統(tǒng)警告窗口豆村,例如低電彈窗液兽。這些window總在應(yīng)用之上的window層。
     * 在多用戶系統(tǒng)掌动,只顯示在自己的window之上四啰。
     */
    public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;

    /**
     * Window type: keyguard window.
     * In multiuser systems shows on all users' windows.
     * @removed
     * window類型:屏保(guard為守衛(wèi)的意思)。
     * 在多用戶系統(tǒng)粗恢,可以顯示在所有用戶的window之上柑晒。
     */
    public static final int TYPE_KEYGUARD           = FIRST_SYSTEM_WINDOW+4;

    /**
     * Window type: transient notifications.
     * In multiuser systems shows only on the owning user's window.
     * window類型:Toast對應(yīng)的窗口。
     * 在多用戶系統(tǒng)眷射,只顯示在自己的window之上匙赞。
     */
    public static final int TYPE_TOAST              = FIRST_SYSTEM_WINDOW+5;

    /**
     * Window type: system overlay windows, which need to be displayed
     * on top of everything else.  These windows must not take input
     * focus, or they will interfere with the keyguard.
     * In multiuser systems shows only on the owning user's window.
     * window類型:系統(tǒng)覆蓋窗口佛掖,用來在頂部展示腕柜。
     * 這類window需要釋放輸入焦點多艇,不然會中斷屏幕保護滔韵。
     * 在多用戶系統(tǒng)森缠,只顯示在自己的window之上亡鼠。
     */
    public static final int TYPE_SYSTEM_OVERLAY     = FIRST_SYSTEM_WINDOW+6;

    /**
     * Window type: priority phone UI, which needs to be displayed even if
     * the keyguard is active.  These windows must not take input
     * focus, or they will interfere with the keyguard.
     * In multiuser systems shows on all users' windows.
     * window類型:在屏幕保護下的通話窗口嗓蘑。
     * 這類window需要釋放輸入焦點源祈,不然會中斷屏幕保護榆苞。
     * 在多用戶系統(tǒng)猛拴,可以顯示在所有用戶的window之上羹铅。
     */
    public static final int TYPE_PRIORITY_PHONE     = FIRST_SYSTEM_WINDOW+7;

    /**
     * Window type: panel that slides out from the status bar
     * In multiuser systems shows on all users' windows.
     * window類型:從狀態(tài)欄下拉下來的面板。(變量名又是系統(tǒng)對話框愉昆,估計就是和它同級吧)
     * 在多用戶系統(tǒng)职员,可以顯示在所有用戶的window之上。
     */
    public static final int TYPE_SYSTEM_DIALOG      = FIRST_SYSTEM_WINDOW+8;

    /**
     * Window type: dialogs that the keyguard shows
     * In multiuser systems shows on all users' windows.
     * window類型:屏保彈出的對話框
     * 在多用戶系統(tǒng)跛溉,可以顯示在所有用戶的window之上焊切。
     */
    public static final int TYPE_KEYGUARD_DIALOG    = FIRST_SYSTEM_WINDOW+9;

    /**
     * Window type: internal system error windows, appear on top of
     * everything they can.
     * In multiuser systems shows only on the owning user's window.
     * window類型:系統(tǒng)錯誤窗口
     * 在多用戶系統(tǒng),只顯示在自己的window之上芳室。
     */
    public static final int TYPE_SYSTEM_ERROR       = FIRST_SYSTEM_WINDOW+10;

    /**
     * Window type: internal input methods windows, which appear above
     * the normal UI.  Application windows may be resized or panned to keep
     * the input focus visible while this window is displayed.
     * In multiuser systems shows only on the owning user's window.
     * window類型:輸入法窗口专肪,出現(xiàn)在正常UI之上。
     * 應(yīng)用可能會重置大小
     * 在多用戶系統(tǒng)堪侯,只顯示在自己的window之上嚎尤。
     */
    public static final int TYPE_INPUT_METHOD       = FIRST_SYSTEM_WINDOW+11;

    /**
     * Window type: internal input methods dialog windows, which appear above
     * the current input method window.
     * In multiuser systems shows only on the owning user's window.
     * window類型:輸入法中備選窗口,出現(xiàn)在當前輸入法窗口之上伍宦。
     * 在多用戶系統(tǒng)芽死,只顯示在自己的window之上。
     */
    public static final int TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12;

    /**
     * Window type: wallpaper window, placed behind any window that wants
     * to sit on top of the wallpaper.
     * In multiuser systems shows only on the owning user's window.
     * window類型:墻紙對應(yīng)的窗口次洼,放置在任何想在其之上的窗口之后关贵。
     * 在多用戶系統(tǒng),只顯示在自己的window之上滓玖。
     */
    public static final int TYPE_WALLPAPER          = FIRST_SYSTEM_WINDOW+13;

    /**
     * Window type: panel that slides out from over the status bar
     * In multiuser systems shows on all users' windows.
     * window類型:從狀態(tài)欄下拉下來的面板坪哄。
     * 在多用戶系統(tǒng),可以顯示在所有用戶的window之上势篡。
     */
      public static final int TYPE_STATUS_BAR_PANEL   = FIRST_SYSTEM_WINDOW+14;

PS:我測試了其中部分類型翩肌,文首說提醒我的那篇文章說系統(tǒng)彈窗不需要依賴Activity,其實部分還是需要的禁悠,我以 TYPE_SYSTEM_ERROR為準測試各個類型彈窗的層級其結(jié)果為:

類型 直接調(diào)用結(jié)果
TYPE_INPUT_METHOD 報錯需要Activity
TYPE_STATUS_BAR_PANEL 相當于 TYPE_SYSTEM_ERROR
TYPE_INPUT_METHOD_DIALOG 在 TYPE_SYSTEM_ERROR之下
TYPE_WALLPAPER 報錯需要Activity
TYPE_SYSTEM_DIALOG 在 TYPE_SYSTEM_ERROR之下
TYPE_PRIORITY_PHONE 在 TYPE_SYSTEM_ERROR之下
TYPE_TOAST 在 TYPE_SYSTEM_ERROR之下
TYPE_STATUS_BAR 報錯需要Activity
TYPE_SYSTEM_OVERLAY 相當于 TYPE_SYSTEM_ERROR念祭, 但是能點擊背景
注意:我只是根據(jù)表象判斷,大家可以各取所需碍侦。知道如何再深入源碼查找窗口層級變化的大神可以指教指教粱坤。

代碼

//權(quán)限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

//代碼
AlertDialog alertDialog;
// 創(chuàng)建構(gòu)建器
AlertDialog.Builder builder = new AlertDialog.Builder(mContext,AlertDialog.THEME_HOLO_LIGHT);
// 設(shè)置參數(shù)
builder.setTitle("提示")
  .setNeutralButton("忽略", new DialogInterface.OnClickListener() {// 中那個按鈕
       @Override
       public void onClick(DialogInterface dialog,int which) {
                dismissDialog();
        }
   })
  .setOnCancelListener(new DialogInterface.OnCancelListener() {
      @Override
         public void onCancel(DialogInterface dialog) {
                dismissDialog();
         }
   });
 alertDialog= builder.create();
//設(shè)置層級
Window window = mAlertDialog.getWindow();
    if (window == null) {
        Log.w("system dialog","dialog window is null");
        return;
    }
window.setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL);//這里我選了最高級
注意:彈窗可以在Service中彈出隶糕,也可以在BroadcastReceiver中彈出,當然你選擇的等級應(yīng)該不需要Activity才行

注意事項

  • 其實站玄,并不建議使用系統(tǒng)級彈窗枚驻,會在其他應(yīng)用之上,影響用戶體驗株旷。
  • 當窗口的層級高于屏幕保護(鎖屏)的層級時再登,連續(xù)按開機鍵,彈窗會駐留在鎖屏界面晾剖,有可能遮擋用戶的鎖屏密碼界面锉矢,甚至無法解鎖,注意在鎖屏?xí)r關(guān)閉彈窗齿尽。
  • 如上第二條沽损,即使及時關(guān)閉彈窗,在5.0以上的系統(tǒng)也會閃一下循头;而在4.2.2上暫時沒有發(fā)現(xiàn)绵估。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市卡骂,隨后出現(xiàn)的幾起案子壹士,更是在濱河造成了極大的恐慌,老刑警劉巖偿警,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異唯笙,居然都是意外死亡螟蒸,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門崩掘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來七嫌,“玉大人,你說我怎么就攤上這事苞慢∷性” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵挽放,是天一觀的道長绍赛。 經(jīng)常有香客問我,道長辑畦,這世上最難降的妖魔是什么吗蚌? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮纯出,結(jié)果婚禮上蚯妇,老公的妹妹穿的比我還像新娘敷燎。我一直安慰自己,他們只是感情好箩言,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布硬贯。 她就那樣靜靜地躺著,像睡著了一般陨收。 火紅的嫁衣襯著肌膚如雪饭豹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天畏吓,我揣著相機與錄音墨状,去河邊找鬼。 笑死菲饼,一個胖子當著我的面吹牛肾砂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播宏悦,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼镐确,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了饼煞?” 一聲冷哼從身側(cè)響起源葫,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎砖瞧,沒想到半個月后息堂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡块促,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年荣堰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片竭翠。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡振坚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出斋扰,到底是詐尸還是另有隱情渡八,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布传货,位于F島的核電站屎鳍,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏问裕。R本人自食惡果不足惜哥艇,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望僻澎。 院中可真熱鬧貌踏,春花似錦十饥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至眷昆,卻和暖如春蜒秤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背亚斋。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工作媚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人帅刊。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓纸泡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親赖瞒。 傳聞我的和親對象是個殘疾皇子女揭,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容