The complexity of changing the progress dialog message in an asynchronous task

I created an async task and want to change the progress dialog message at different stages of doBackground. Here is the code:

public class sc extends AsyncTask<Integer,String,Void> { ProgressDialog dialog; protected void onPreExecute() { dialog=new ProgressDialog(Loc.this); dialog.show(); } @Override protected Void doInBackground(Integer... params) { onProgressUpdate("Contacting server..Please wait.."); //Do some work onProgressUpdate("Processing the result"); //Do some work onProgressUpdate("Calculating.."); dialog.dismiss(); return null; } protected void onProgressUpdate(String ui) { dialog.setMessage(ui); } } 

But the problem is that only the first message is always displayed in the progress dialog. Please help me find a solution.

+4
source share
3 answers
 protected Void doInBackground(Integer... params) { onProgessUpdate("Contacting server..Please wait.."); ... } 

Urrrm, no, that won't work.

Try ...

 publishProgress("Contacting server..Please wait.."); 

You must " publish " your progress to doInBackground(..) to call onProgressUpdate(...) .

Also do not call dialog.dismiss() in doInBackground(...) instead in onPostExecute(...) .

+8
source

I think it should be ..

 publishProgress("Your Dialog message.."); 

not

 onProgessUpdate("Processing the result"); 

in doInBack .. ()

Sort of,

 protected Long doInBackground(URL... urls) { publishProgress("Hello"); return null; } protected void onProgressUpdate(String msg) { dialog.setMessage(msg); } 
+3
source

The problem may also be that you did not set the "start message". If you do not set the message for your ProgressDialog before trying to do this inside onProgressUpdate , this will not work.

 ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("Title"); progressDialog.setMessage("Initial message needed"); public class foo extends AsyncTask<Void,Integer,Void> { ... } 

Also note that if you need to update the execution as well as the message, you can use the Integer argument with one of the integers that determines the amount of execution, and another that defines the message as the index of the String[] array of messages (if the messages are known in advance) .

0
source

Source: https://habr.com/ru/post/1393740/


All Articles