内容简介:在SystemUI里添加了一个broadcastReceiver,需求接收到消息后弹出一个用户提示实现代码也很简单,
在SystemUI里添加了一个broadcastReceiver,需求接收到消息后弹出一个用户提示
实现代码也很简单,
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" />
这样弹窗就变成系统的弹窗了。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Coming of Age in Second Life
Tom Boellstorff / Princeton University Press / 2008-04-21 / USD 29.95
The gap between the virtual and the physical, and its effect on the ideas of personhood and relationships, is the most interesting aspect of Boellstorff's analysis... Boellstorff's portrayal of a virt......一起来看看 《Coming of Age in Second Life》 这本书的介绍吧!