[Framework]从全局AlertDialog聊聊WindowManager

栏目: Android · 发布时间: 7年前

内容简介:在SystemUI里添加了一个broadcastReceiver,需求接收到消息后弹出一个用户提示实现代码也很简单,

在SystemUI里添加了一个broadcastReceiver,需求接收到消息后弹出一个用户提示

[Framework]从全局AlertDialog聊聊WindowManager

实现代码也很简单,

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(R.string.device_temp_high)
    .setMessage(warn_format)
    .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    }).setCancelable(false).create().show();

但实际上,这个mContext只能从onReceive(Context context, Intent intent)获取到一个。而这个 Context 根本无法更新UI。关于 Context 的具体讨论见: 带你掌握Android Context

所以,运行时会报出如下错误:

Unable to add window — token null is not for an application”

拿不到 Context ,emmm,怎么弹出这个对话框呢?

还好,Android系统留了一些后手。

将其转换成全局AlertDialog也很简单。

首先,对AlertDialog添加SYSTEM_ALERT类型

AlertDialog dlg = builder.create();
        dlg.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dlg.show();

其次,增加对应权限

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

这样弹窗就变成系统的弹窗了。

[Framework]从全局AlertDialog聊聊WindowManager


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Haskell Programming from first principles

Haskell Programming from first principles

Christopher Allen、Julie Moronuki / 2015 / USD 59.00

I am writing this book because I had a hard time learning Haskell. It doesn't have to be that way. I've spent the last couple years actively teaching Haskell online and in person. Along the way, I ......一起来看看 《Haskell Programming from first principles》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具