問題描述
我有一個運行后臺線程的服務.我想做的是顯示從我的后臺線程啟動的 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 在服務中被忽略的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!