-1

I am using AsyncTask to perform some work, I need the progress update but i don't know how to get the progress update here. dowork() can have any task to perform, I need progress update of the work. Thank you for your help!

class MyTask extends AsyncTask<Integer, Integer, String> {
    @Override
    protected void doInBackground(Integer... params) {


        dowork();
             //how to return integer value of the progress here?
               }
    @Override
    protected void onPostExecute(String result) {
         txt.setText("done");
               }
    @Override
    protected void onPreExecute() {
        txt.setText("Starting...");
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        txt.setText("completed..."+ values[0]+"%");
            }
}
Aahil
  • 33
  • 2
  • 7

4 Answers4

2

You can publish the progress like this:

publishProgress(yourStatus);

Write it in the doInBackground method. Your onProgressUpdate seems fine.

To call publishProgress from your doWork method you could add an abstract class like that:

public abstract class UpdateResult {
    public abstract void updateStatus(Integer status);
}

Now you change your method from doWork() to doWork(UpdateResult result) and call the updateStatus method like this:

void doWork(UpdateResult result) {
.
. some work
.
result.updateStatus(50);
.
. 
}

and call it like:

doWork(new UpdateResult(){
@Override
public void updateStatus(Integer status){
       publishProgress(status)
}
});

Example (I didnt test this):

void doWork(UpdateResult result) {

for (int i = 0; i < 100; i++) {
    result.updateStatus(i);
}

}

class MyTask extends AsyncTask<Integer, Integer, String> {
    @Override
    protected String doInBackground(Integer... params) {

        doWork(new UpdateResult(){
        @Override
        public void updateStatus(Integer status){
               publishProgress(status);
        }
        });

     return "Done";
    }
    @Override
    protected void onPostExecute(String result) {
         txt.setText(result);
    }
    @Override
    protected void onPreExecute() {
        txt.setText("Starting...");
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        txt.setText("completed..."+ values[0]+"%");
    }
}
Daniel
  • 753
  • 9
  • 15
  • it is not updating the progress, what should be my `onProgressUpdate()` then? – Aahil Feb 04 '16 at 08:26
  • Your onProgressUpdate should be fine...maybe post your code? :) – Daniel Feb 04 '16 at 08:31
  • here, `result.updateStatus(i);` will be updated by loop, but when we have some other work to do, how will it update, it should update according to the time the work would take to be done. – Aahil Feb 04 '16 at 08:55
  • You can call the updateStatus when ever you want....for example: Step 1 -> Do work -> result.updateStatus(25) -> Step 2 -> Do work -> result.updateStatus(50) -> Step 3 -> Do work -> result.updateStatus(100) If you want to see the download progress, you will have to get the total size via getContentLength() and the already downloaded size and simply call result.updateStatus((int) (total * 100 / fileLength)); Check this out: http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Daniel Feb 04 '16 at 09:00
  • I was looking for an asynctask that can automatically update the progress according to the time the work would take, still thank you for you help. Cheers! – Aahil Feb 04 '16 at 09:11
0

You need to call this method http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...)

inside your doInBackground() to send continous updates.

Napolean
  • 5,303
  • 2
  • 29
  • 35
0

You need to use publishProgress(variable); variable is Integer object in your case .

0

you can use publishProgress (Progress... values) to post values in doInBackground, this will call onProgressUpdate method

一条虫
  • 1
  • 1