android Progress dialog -> publishProgress method

2019-09-15 16:40发布

问题:

I'm trying to display 3 different messages in a progress dialog.

In onPreExecute I create the dialog and set the first message.

In doInBackground I call the publishProgres("sentence 2", "sentence 3") with two other sentences/messages that has to be displayed.

If in the onProgressUpdate I use myDialog.setMessage(values[0]); it shows me the first sentence sent and if I use myDialog.setMessage(values[1]); it shows me the second message sent.

But my question is, what can I do to make the dialog display the first sentence sent and after some time the second message?

The hole thing is that I'm ding a connection to an url, doing some other stuff and updating the data of a list at the end and have to display a dialog that shows three different messages while the processes are taking place...

回答1:

publishProgress is invoked to publish updates on the UI thread while the background computation is still running. You can call it during specific intervals to publish the updates, so that the user is informed of the task being run. A typical case is as below:

 protected Void doInBackground(String.. params ) {
     publishProgress("sentence 1");
     //do some time consuming task 
     //..
     publishProgress("sentence 2");
     //do some more time consuming task 
     //..
     publishProgress("sentence 3");
     //do some more time consuming task 
     //..
 }

 protected void onProgressUpdate(String... progress) {
     myDialog.setMessage(progress[0]);
 }

If you have a loop, you can call publishProgress during each or a specific number of iterations.