Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

broadcastreceiver - How can I display a dialog from an Android broadcast receiver?

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?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...