ProgressDialog:如何防止 setButton-Listener 内的对话框自动关闭?

发布于 2024-11-03 17:26:27 字数 1337 浏览 3 评论 0原文

我有以下场景: 我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙沙粒小 2024-11-10 17:26:27

我找到了一个非常好的解决方案来解决这个问题:
1. 从 ProgressDialog 派生您的类。
2.重写dismiss()方法,并且不做任何事情来阻止它自动关闭。
3. 为了手动关闭它,添加一个新方法。
4.后台线程结束后,调用这个新方法。

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

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();
    }
}
苏佲洛 2024-11-10 17:26:27

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

您可以将 ResultReceiverMessenger 作为 Parcelable(不涉及任何工作)传递到用于启动的 Intent 中。 服务

Use a Service to control the spawning of new background threads. Then use either a ResultReceiver or Messenger to pass back UI updates such as progress status (ie start, update, finish);

You can pass the ResultReceiver or Messenger as a Parcelable (no work involved) in the Intent used to start the Service.

兮颜 2024-11-10 17:26:27

一种方法可能是拥有自己的自定义对话框视图,而不是 ProgressDialog,您可以在其中控制它何时消失。

One way might be to have your own cusom dialog view, instead of a ProgressDialog, where you can control when it disappears.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文