如何显示在Android的对话框没有活动背景? [英] How do I display a dialog in android without an Activity context?

查看:167
本文介绍了如何显示在Android的对话框没有活动背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该是简单的,但我没有找到答案的任何地方。我有一个Android应用程序在后台执行网络任务。如果错误回来,我要显示一个错误对话框。当任务返回时,我不知道哪个活动是在前台。根据<一href="http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext">this帖子,看起来我们不能使用应用程序上下文来显示一个对话框(实际上我得到的崩溃,如果我尝试)。

所以,我怎么能得到当前活动的背景下?同样,接收器在网络任务中的应用程序上下文中运行,而不是在一个特定的活动。任何其他的想法?

编辑:我要澄清。我不想,如果我不是前台应用程序显示一个错误对话框。我只关心在我们的应用程序是在前台,现在的情况。

解决方案
  

如果一个错误回来,我要显示一个错误对话框。

请只有这样做,如果你知道用户主动使用你的应用程序。如果你打断他们在别的中间(玩游戏,看电影,读一本书)的用户将非常非常恼火。

  

所以,我怎么能得到当前活动的背景下?

您不知道。至多,你让当前的活动知道它需要做一些事情。

  

任何其他的想法?

一种可能是使用一个有序的广播,使的如果的有前景的活动,它得到控制,否则你养通知来让用户了解这个问题,没有弹出一个对话框。接收有序广播的活动可以显示一个 AlertDialog 或以其他方式让用户知道这个问题。我写了一篇关于如何做到这一点在一篇博客文章细节(和一本书章节,对于这个问题),和的这里是展示该技术的示例应用程序。

或者,有服务电话 startActivity()启动的对话为主题的活动

This seems like it should be simple, but I'm not finding an answer anywhere. I have an Android application that performs network tasks in the background. If an error comes back, I want to display an error dialog. When the task returns, I don't know which Activity is in the foreground. Based on this post, it looks like we can't use the application context to display a dialog (and indeed I do get the crash if I try).

So how can I get the context of the current activity? Again, the receiver for the network task is running in the Application context, not in a particular Activity. Any other ideas?

Edit: I should clarify. I don't want to display an error dialog if I'm not the foreground application. I'm only interested in the case where our app is in the foreground for now.

解决方案

If an error comes back, I want to display an error dialog.

Please only do this if you know that the user is actively using your application. The user will be very very annoyed if you interrupt them in the middle of something else (playing a game, watching a movie, reading a book).

So how can I get the context of the current activity?

You don't. At most, you let the current activity know that it needs to do something.

Any other ideas?

One possibility is to use an ordered broadcast, so if you have a foreground activity, it gets control, otherwise you raise a Notification to let the user know about the problem without popping a dialog. The activity that receives the ordered broadcast can display an AlertDialog or otherwise let the user know about the problem. I wrote about the details of how to do this in a blog post (and a book chapter, for that matter), and here is a sample application demonstrating the technique.

Or, have the service call startActivity() to start up a dialog-themed activity.

这篇关于如何显示在Android的对话框没有活动背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆