Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulty in changing the message of progress dialog in async task

I have created an async task and want to change the message of progress dialog during 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, progress dialog is only showing the first message always. Kindly help me to find a solution.

like image 286
roy mathew Avatar asked Jan 30 '12 11:01

roy mathew


2 Answers

protected Void doInBackground(Integer... params) 
{
    onProgessUpdate("Contacting server..Please wait..");
    ...
}

Urrrm, nope, that won't work.

Try...

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

You have to "publish" your progress in doInBackground(..) in order for onProgressUpdate(...) to be called.

Also don't call dialog.dismiss() in doInBackground(...) call it in onPostExecute(...) instead.

like image 110
Squonk Avatar answered Oct 19 '22 22:10

Squonk


I think it should be..

publishProgress("Your Dialog message..");

not

onProgessUpdate("Processing the result"); 

in doInBack..()

Something like,

protected Long doInBackground(URL... urls) {
      publishProgress("Hello");
     return null;
 }

 protected void onProgressUpdate(String msg) {
     dialog.setMessage(msg);

 }
like image 28
user370305 Avatar answered Oct 20 '22 00:10

user370305