-1

Hello i would like to perform some lengthy operation in a background thread and in the mean time show the user a "HORIZONTAL PROGRESS BAR" and keep updating this as the operation in the background is happening . I know i can achieve this with the asynctask but till now i have only been using progressdialog and it is much simpler with that . Please help .

protected HttpResponse doInBackground(Void... params) 
    {
        Client.getParams().setIntParameter("http.connection.timeout",5000);
        try
        {

            return Client.execute(loginPost);
        }
        catch(Exception e)
        {
            return null;
        }
    }

Now this being my doInBackground() method , how can i really know the status of the ongoing operation to be able to update my progressBar

Saiesh
  • 641
  • 2
  • 7
  • 23
  • Without knowing the full definition of the `Client` class, it's very unlikely anyone can help with this. Any answer would likely involve modifying `Client` rather than the code you've shown. Your question also isn't really about AsyncTask at all. – Jeffrey Yasskin Dec 28 '11 at 18:13

1 Answers1

1

With AsyncTask, updating a ProgressBar isn't much more complicated. Move the lengthy operation to your override of AsyncTask.doInBackground, wherever you're currently calling pbar.setProgress(percent_done), call publishProgress(percent_done), and implement AsyncTask.onProgressUpdate(Integer... values) as

pbar.setProgress(values[0]);
Jeffrey Yasskin
  • 5,171
  • 2
  • 27
  • 39
  • Ok i get it but the thing is with a progressdialog i don;t really have any updation . I just dismiss the dialog onPostExecute() . Basically i am posting some content to a DB and getting a http response in the doInackground method . So i can't really pass an Integer value to the onProgressUpdate . – Saiesh Dec 28 '11 at 07:12
  • The DoInBackground() method has just one line which is returning a Httpresponse and in the process also executing the post . So how exactly will i know the progress ??? – Saiesh Dec 28 '11 at 07:13
  • If you just need to know when the background task is complete, `AsyncTask.onPostExecute(Result)` gets called when that happens. You can dismiss your dialog from your implementation of that method. If you're really asking how to get progress updates from some code that you didn't mention, then you're going to have to at least say what code you need progress updates from. It may be impossible to tell how far along a DB update is. – Jeffrey Yasskin Dec 28 '11 at 07:23