Android AlertDialog level(置顶)

需求

在项目中需要用到不依赖Activity Context启动的Dialog,并且Dialog的显示层级要在最顶部(没错,就是流氓软件!)

方案

在网络上搜索“Android AlertDialog 置顶”、“Android AlertDialog level”都找不到我想要的,最后搜索“Android AlertDialog Priority”找到了一篇文章,提示了我。
我重新整理了一下,Dialog是在WindowManager显示的,而WindowManager分很多不同的层,使用AlertDialog的alertDialog.getWindow().setType(WindowManager.LayoutParams.***)方法可以设置这个层,这里就是关键了。从源码(WindowManager.java)里面看,type有很多,我们只关注系统等级且没有被隐藏的,目前有如下类型:

     /**
     * Start of system-specific window types.  These are not normally
     * created by applications.
     *window类型:第一个系统窗口。这些弹窗不是通过application正常创建的。
     */
    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类型:状态栏窗口。因为只能有个一个状态栏window,所以它被放在屏幕最上面,
     * 且其他window在它下面。在多用户系统,可以显示在所有用户的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类型:通话窗口。这些非应用window为用户提供通话的交互,特别是来电的时候
     * 在多用户系统,可以显示在所有用户的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类型:系统警告窗口,例如低电弹窗。这些window总在应用之上的window层。
     * 在多用户系统,只显示在自己的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为守卫的意思)。
     * 在多用户系统,可以显示在所有用户的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对应的窗口。
     * 在多用户系统,只显示在自己的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类型:系统覆盖窗口,用来在顶部展示。
     * 这类window需要释放输入焦点,不然会中断屏幕保护。
     * 在多用户系统,只显示在自己的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需要释放输入焦点,不然会中断屏幕保护。
     * 在多用户系统,可以显示在所有用户的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类型:从状态栏下拉下来的面板。(变量名又是系统对话框,估计就是和它同级吧)
     * 在多用户系统,可以显示在所有用户的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类型:屏保弹出的对话框
     * 在多用户系统,可以显示在所有用户的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类型:系统错误窗口
     * 在多用户系统,只显示在自己的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类型:输入法窗口,出现在正常UI之上。
     * 应用可能会重置大小
     * 在多用户系统,只显示在自己的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类型:输入法中备选窗口,出现在当前输入法窗口之上。
     * 在多用户系统,只显示在自己的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类型:墙纸对应的窗口,放置在任何想在其之上的窗口之后。
     * 在多用户系统,只显示在自己的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类型:从状态栏下拉下来的面板。
     * 在多用户系统,可以显示在所有用户的window之上。
     */
      public static final int TYPE_STATUS_BAR_PANEL   = FIRST_SYSTEM_WINDOW+14;

PS:我测试了其中部分类型,文首说提醒我的那篇文章说系统弹窗不需要依赖Activity,其实部分还是需要的,我以 TYPE_SYSTEM_ERROR为准测试各个类型弹窗的层级其结果为:

类型 直接调用结果
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, 但是能点击背景
注意:我只是根据表象判断,大家可以各取所需。知道如何再深入源码查找窗口层级变化的大神可以指教指教。

代码

//权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

//代码
AlertDialog alertDialog;
// 创建构建器
AlertDialog.Builder builder = new AlertDialog.Builder(mContext,AlertDialog.THEME_HOLO_LIGHT);
// 设置参数
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();
//设置层级
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中弹出,当然你选择的等级应该不需要Activity才行

注意事项

  • 其实,并不建议使用系统级弹窗,会在其他应用之上,影响用户体验。
  • 当窗口的层级高于屏幕保护(锁屏)的层级时,连续按开机键,弹窗会驻留在锁屏界面,有可能遮挡用户的锁屏密码界面,甚至无法解锁,注意在锁屏时关闭弹窗。
  • 如上第二条,即使及时关闭弹窗,在5.0以上的系统也会闪一下;而在4.2.2上暂时没有发现。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容