AlertDialog.show 在服务中被忽略

时间:2023-04-24
本文介绍了AlertDialog.show 在服务中被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个运行后台线程的服务.我想做的是显示从我的后台线程启动的 AlertDialog.我知道这不是通知用户的推荐方式,并且它中断工作流程(因为它们可以随时在任何应用程序中弹出时间),但它适合我的用例.

I have a service running a background thread. What I'd like to do is to show an AlertDialog initiated from my background thread. I know that this is not the recommended way of notifying the user and that it interrupts the workflow (as they can pop-up in any application at any time) but it's a suitable way for my use case.

有一个处理程序注册到后台线程并显示一个带有处理程序的 Toast 通知工作正常.但是切换后到 AlertDialog 什么也没有发生.我的 showDialog 逻辑是默默无视.没有对话窗口出现,没有日志条目.有点奇怪,因为我希望至少有一个日志条目说我正在做有什么不对或什么的.

There is a handler registered with the background thread and showing a Toast notification withing the handler works fine. But after switching to an AlertDialog nothing happens anymore. My showDialog logic is silently ignored. No Dialog window appears, no log entry. It's a bit strange as I'd expect at least a log entry saying that I'm doing something wrong or whatever.

显示从服务后台线程?有些人似乎推荐一个Dialog主题Activity 以获得类似的行为.

Are there any limitations for showing an AlertDialog initiated from a service background thread? Some people seem to recommend a Dialog themed Activity to get a similar behavior.

非常感谢任何澄清或帮助使其工作!

Any clarification or help making it work is greatly appreciated!

伊夫

推荐答案

可以从后台线程打开对话框.诀窍是启动一个看起来像对话框的活动:

It is possible to open a dialog from a background thread. The trick is to start an activity which looks like a dialog:

<activity android:label="@string/app_name" android:name="YourDialog" android:theme="@android:style/Theme.Dialog"/>

然后,开始您的活动:

Intent dialog = new Intent(this, YourDialog.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialog);

请注意,这是异步的,不会阻塞.如果要处理结果,则必须使用 startService() 并传递自定义活动来指示结果.

Note that this is asyncrhonous and doesn't block. If you want to process the result, you'll have to use startService() and pass a custom activity to indicate a result.

伊曼纽尔

这篇关于AlertDialog.show 在服务中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 onDismiss() 从 DialogFragment 获取信息 下一篇:使用 AlertDialog 的模态对话框功能

相关文章