AlertDialog.show在服务中默默忽略 [英] AlertDialog.show silently ignored within a service

查看:183
本文介绍了AlertDialog.show在服务中默默忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有一个处理程序注册了后台线程,并显示一个
吐司通知与处理程序工作正常。但是在将
切换到AlertDialog后,没有任何事情发生。我的showDialog逻辑是
默默忽略。无对话窗口出现,无日志条目。这是一个有点
的奇怪,因为我期望至少一个日志条目,说我正在做
一些错误或任何东西。



有没有显示从
服务后台线程启动的AlertDialog的任何限制?有些人似乎推荐一个Dialog主题的
活动来获得类似的行为。



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



Yves

解决方案

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

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

然后,开始您的活动:

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

请注意,这是不可忽视的,不会阻止。如果要处理结果,您必须使用startService()并传递自定义活动来指示结果。



Emmanuel


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.

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.

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!

Yves

解决方案

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"/>

Then, to start your activity:

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

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.

Emmanuel

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

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