ProgressDialog:如何防止setButton-Listener内部的Dialog自动关闭? [英] ProgressDialog: How to prevent automatic dismissing of Dialog inside setButton-Listener?
问题描述
我有以下情况:
我在UI-Thread中显示一个ProgressDialog,同时在Backgroundthread中进行Networkdiscovery.这样很好.该对话框具有停止按钮,因此用户可以决定是否要停止发现.
现在是我的问题:要停止发现,还需要在背景线程中完成一些繁重的工作量过程,因此UI线程中的Progresswheel不会挂断.从理论上讲,Progresswheel应该继续旋转,直到Backgroundthread完成其工作为止.但是setButton(...)
-Method一旦到达Methodend且线程尚未完成工作,就会自动关闭ProgressDialog.目前,我没有机会从Backgroundthread中通过Messagehandler手动关闭该对话框,因为OS会自动将其关闭.
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
});
推荐答案
我找到了一个很好的解决此问题的方法:
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.
public class MyProgressDialog extends ProgressDialog {
@Override
public void dismiss() {
// do nothing
}
public void dismissManually() {
super.dismiss();
}
}
这篇关于ProgressDialog:如何防止setButton-Listener内部的Dialog自动关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!