如何显示从Android的广播接收器的对话? [英] How can I display a dialog from an Android broadcast receiver?
问题描述
在理想情况下,我不希望启动一个活动来做到这一点。当失去了WiFi连接,我的应用程序需要关闭,因为这是一个致命的错误给我们。我想显示错误信息,并让用户preSS的确定按钮,然后退出程序。什么是去了解这一点的最好方法是什么?
Ideally, I do not want to start an activity to do this. When the WiFi connection is lost, my app needs to close because this is a fatal error for us. I want to display an error message and have the user press an Ok button and then exit the app. What is the best way to go about this?
谢谢!
推荐答案
AFAIK,唯一的活动可以显示对话框。如果是这样,如果你的的BroadcastReceiver
是由注册的活动,通过 registerReceiver()
,你设置 - 只使用该活动。但是,如果你的的BroadcastReceiver
注册在清单中,我想你将别无选择,只能做别的事情。
AFAIK, only activities can display dialogs. If so, and if your BroadcastReceiver
is registered by an activity via registerReceiver()
, you're set -- just use that activity. If, however, your BroadcastReceiver
is registered in the manifest, I think you will have no choice but to do something else.
例如,您可以发送一个有序广播意图
。您当前正在运行的活动 - 如果有的话 - 将有一个高优先级的BroadcastReceiver
为意图
,然后就可以弹出一个对话框,当它接收广播。但是,如果没有你们的活动都在屏幕上,你可以有一个明显的注册低优先级的BroadcastReceiver
拿起广播,如果你想显示通知
什么的。 这里是一个博客帖子,涵盖多一点这种模式。
For example, you could send an ordered broadcast Intent
. Your currently-running activity -- if any -- would have a high-priority BroadcastReceiver
for that Intent
, then can pop a dialog when it receives the broadcast. If, however, none of your activities are on screen, you could have a manifest-registered low-priority BroadcastReceiver
pick up the broadcast, if you wanted to display a Notification
or something. Here is a blog post that covers a bit more about this pattern.
这篇关于如何显示从Android的广播接收器的对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!