0

what is the best way to start AsyncTask again after it has completed. My application is where I need to do some background processing that updates UI every 30 secs or so.

So will this be good

MyAsyncTask t;

onCreate(){
 postDelayed(myrunnable,..)
}

myrunnable(){
if(t && t.getStatus()== FINISHED){
t = new MyAsyncTask();
t.execute()

 postDelayed(myrunnable,30000)
}else
postDelayed(myrunnable,2000)
}

Update:Edited the code, So if this sequence seems valid please say yes or suggest changes

  • You question has already been answered here http://stackoverflow.com/questions/3617453/best-way-to-periodically-executing-asynctasks-in-android?rq=1 – Saqib Razaq Jul 15 '13 at 13:09
  • I have to use AsyncTask only not timers –  Jul 15 '13 at 13:10
  • see [this post](http://stackoverflow.com/questions/6531950/how-to-execute-async-task-repeatedly-after-fixed-time-intervals) – Rarw Jul 15 '13 at 13:10
  • I dont want it on main UI thread –  Jul 15 '13 at 13:12

2 Answers2

1

You could:

  • Register your class responsible of the AsyncTask execution as listener in your AsyncTask.
  • Implement a callback in your AsyncTask when the task is finished at the end of the doInBackground method.
  • Execute the next AsyncTask when you receive the callback.

Example: Your listener interface:

public interface IProgress {

  public void onProgressUpdate(final boolean isFinished);

}

Your activity implements IProgress:

public class MyActivity implements IProgress {


  public void onProgressUpdate(final boolean isFinished) {

    if (isFinished) {
        // delayed execution
        myHandler.postDelayed(new MyRunnable(MyActivity.this), 30000);
    }
  }

Your AsyncTask contains a IProgress instance and you call onProgressUpdate at the end of doInBackground or in onPostExecute:

...

  private final IProgress progressListener;

  public MyAsyncTask( final IProgress progressListener) {
    super();    
    this.progressListener = progressListener;    
  }

...

  @Override
  protected void onPostExecute(final int result) {
    progressListener.onProgressUpdate(true);
    super.onPostExecute(result);
  }

Create a runnnable for the postDelayed:

  protected class MyRunnable implements Runnable {

    private final IProgress progressListener;

    public MyRunnable(final IProgress progressListener) {
      super();
      this.progressListener= progressListener;
    }

    public void run() {
            new MyAsyncTask(progressListener).execute();
    }
  };
L. G.
  • 9,642
  • 7
  • 56
  • 78
0

Async-task class within a specific time, like 30sec time interval.

You can use http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html.

  • AsyncTask is great and I would like to use it. What I dont understand is how to know that AysnTask is fully destoryed, before starting a new one. –  Jul 15 '13 at 13:14
  • @manish go through with this http://developer.android.com/reference/android/os/AsyncTask.html it will help u ... –  Jul 15 '13 at 13:16
  • Hi Rahul, I am reading it all day. No luck. There is no status field which I can check. Can you please point out to something specific –  Jul 15 '13 at 13:17
  • if ScheduledExecuotr is answer the i am not going that way –  Jul 15 '13 at 13:24