ProgressDialog: How to prevent automatic dismissing of Dialog inside setButton-Listener?

£可爱£侵袭症+ 提交于 2019-12-14 01:40:33

问题


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:


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();
    }
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/5801264/progressdialog-how-to-prevent-automatic-dismissing-of-dialog-inside-setbutton-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!