Android ProgressDialog:如何防止在setButton侦听器中自动关闭对话框?

Android ProgressDialog:如何防止在setButton侦听器中自动关闭对话框?,android,multithreading,progressdialog,Android,Multithreading,Progressdialog,我有以下情况: 我在UI线程中显示ProgressDialog,同时在后台线程中执行网络发现。这个很好用。该对话框有一个“停止”按钮,因此用户可以决定是否要停止查找。 现在我的问题是:为了停止发现,一些繁重的工作负载过程也在后台线程中完成,这样UI线程中的Progresswheel就不会挂断。理论上,Progresswheel应该继续旋转,直到背景线程完成其工作。但是,setButton(…)-方法会在ProgressDialog到达Methodend并且线程尚未完成其工作时自动取消它。目前,我

我有以下情况: 我在UI线程中显示ProgressDialog,同时在后台线程中执行网络发现。这个很好用。该对话框有一个“停止”按钮,因此用户可以决定是否要停止查找。
现在我的问题是:为了停止发现,一些繁重的工作负载过程也在后台线程中完成,这样UI线程中的Progresswheel就不会挂断。理论上,Progresswheel应该继续旋转,直到背景线程完成其工作。但是,
setButton(…)
-方法会在ProgressDialog到达Methodend并且线程尚未完成其工作时自动取消它。目前,我没有机会从后台线程通过Messagehandler手动关闭对话框,因为它被操作系统自动关闭

这是我代码的一部分:

//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
});
//初始化进程对话框
ProgressDialog发现ProgressDialog;
discoveryProgressDialog=新建进度对话框(上下文);
setTitle(“发现网络”);
discoveryProgressDialog.setCancelable(true);
discoveryProgressDialog.setUndeterminate(false)//将此设置为true并不能解决问题
//将Buttonlistener添加到Progressdialog
discoveryProgressDialog.setButton(DialogInterface.BUTTON_中性,“停止”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
stopDiscoveryTask();//启动后台线程
//这里有一些等待线程完成,而不是冻结
//UI线程或其他阻止解除
//自动对话

}// 使用
Service
控制新后台线程的生成。然后使用
ResultReceiver
Messenger
传回UI更新,如进度状态(即开始、更新、完成)


您可以将
结果接收器
信使
作为
包裹
传递(不涉及任何工作)在用于启动
服务的
Intent
中,一种方法可能是拥有自己的cusom对话框视图,而不是ProgressDialog,您可以在其中控制它何时消失。

我找到了一个非常好的解决方案来解决此问题:
1.从ProgressDialog派生类。
2.重写dismise()方法,并且在该方法中不执行任何操作以防止其自动解除。
3.若要手动取消,请添加新方法。
4.后台线程结束后,调用此新方法。

public class MyProgressDialog extends ProgressDialog {
    @Override
    public void dismiss() {
        // do nothing
    }
    public void dismissManually() {
        super.dismiss();
    }
}