我有以下情景:
我在UI线程中显示ProgressDialog,并同时在后台线程中进行网络发现。这很好地工作。对话框有一个“停止”按钮,因此用户可以决定是否要停止发现。
现在我的问题是:为了停止发现,还需要在后台线程中执行一些重量级的工作,以便UI线程中的进度轮不会挂起。理论上,进度轮应该继续旋转,直到后台线程完成其工作。但是,一旦setButton(...)方法到达方法末尾时,它会自动关闭ProgressDialog,而此时线程尚未完成其工作。目前,我无法通过Backgroundthread中的Messagehandler手动解除对话框,因为它被操作系统自动关闭了。
现在我的问题是:为了停止发现,还需要在后台线程中执行一些重量级的工作,以便UI线程中的进度轮不会挂起。理论上,进度轮应该继续旋转,直到后台线程完成其工作。但是,一旦setButton(...)方法到达方法末尾时,它会自动关闭ProgressDialog,而此时线程尚未完成其工作。目前,我无法通过Backgroundthread中的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
});