ProgressDialog:如何防止 setButton-Listener 内的对话框自动关闭?
我有以下场景: 我在 UI 线程中显示 ProgressDialog,同时在后台线程中执行 Networkdiscovery。这很好用。该对话框有一个停止按钮,因此用户可以决定是否要停止发现。
现在这是我的问题:为了停止发现,一些繁重的工作负载过程也在后台线程中完成,因此 UI 线程中的进度轮不会挂起。理论上,进度轮应该继续旋转,直到后台线程完成其工作。但是,一旦 setButton(...)
方法到达 Methodend,并且此时线程尚未完成其工作,它就会自动关闭 ProgressDialog。目前,我没有机会通过后台线程的消息处理程序手动关闭对话框,因为它被操作系统自动关闭。
这是我的代码的一部分:
//Initialize ProgressDialog
ProgressDialog discoveryProgressDialog;
discoveryProgressDialog = new ProgressDialog(context);
discoveryProgressDialog.setTitle("Discover Network");
discoveryProgressDialog.setCancelable(true);
discoveryProgressDialog.setIndeterminate(false); //setting this to true does not solve the Problem
//add Buttonlistener to Progressdialog
discoveryProgressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Stop", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
stopDiscoveryTask(); //starts the Backgroundthread
//something here to wait for Thread-Finishing and not freezing
//the UI-Thread or something to prevent dismissing the
//Dialog automatically
}//<--- End will be reached directly after Backgroundthread has started which triggers automatically the dismiss of the Progress-Dialog
});
i have following Scenario:
Im showing a ProgressDialog in the UI-Thread and on the same time doing a Networkdiscovery in a Backgroundthread. This works just fine. The Dialog has a Stop-Button, so the User can decide if he wants to stop the discovery.
Now here is my Problem: To stop the discovery some heavy workload-process is also done in a Backgroundthread so the Progresswheel in the UI-Thread does not hang up. In theory the Progresswheel should continue spinning until the Backgroundthread has finished its work. But the setButton(...)
-Method automatically dismisses the ProgressDialog, once it reaches the Methodend and by that time the Thread has not finished its work yet. Currently I have no chance to dismiss the Dialog manually through a Messagehandler from the Backgroundthread, because it got dismissed by the OS automatically.
This is a portion of my code:
//Initialize ProgressDialog
ProgressDialog discoveryProgressDialog;
discoveryProgressDialog = new ProgressDialog(context);
discoveryProgressDialog.setTitle("Discover Network");
discoveryProgressDialog.setCancelable(true);
discoveryProgressDialog.setIndeterminate(false); //setting this to true does not solve the Problem
//add Buttonlistener to Progressdialog
discoveryProgressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Stop", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
stopDiscoveryTask(); //starts the Backgroundthread
//something here to wait for Thread-Finishing and not freezing
//the UI-Thread or something to prevent dismissing the
//Dialog automatically
}//<--- End will be reached directly after Backgroundthread has started which triggers automatically the dismiss of the Progress-Dialog
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个非常好的解决方案来解决这个问题:
1. 从 ProgressDialog 派生您的类。
2.重写dismiss()方法,并且不做任何事情来阻止它自动关闭。
3. 为了手动关闭它,添加一个新方法。
4.后台线程结束后,调用这个新方法。
I found a so so good solution to fix this problem:
1. Derive your class from ProgressDialog.
2. Override dismiss() method, and do nothing there to prevent it from dismissing automatically.
3. In order to dismiss it manually, add a new method.
4. After the background thread ends, call this new method.
使用
Service
来控制新后台线程的生成。然后使用ResultReceiver
或Messenger
传回 UI 更新,例如进度状态(即开始、更新、完成);您可以将
ResultReceiver
或Messenger
作为Parcelable
(不涉及任何工作)传递到用于启动的Intent
中。服务
。Use a
Service
to control the spawning of new background threads. Then use either aResultReceiver
orMessenger
to pass back UI updates such as progress status (ie start, update, finish);You can pass the
ResultReceiver
orMessenger
as aParcelable
(no work involved) in theIntent
used to start theService
.一种方法可能是拥有自己的自定义对话框视图,而不是 ProgressDialog,您可以在其中控制它何时消失。
One way might be to have your own cusom dialog view, instead of a ProgressDialog, where you can control when it disappears.