title: Periodically run AsyncTask after previous task has been finished
body: <p>I have an AsyncTask which currently runs only once and takes about between 5 - 30 secs to finish.</p>
  
  <p>What I want to do is:</p>
  
  <p><strong>1. Start the App<br>
  2. Run the Task<br>
  3. Wait for it to Finish.<br>
  4. Wait for a Fixed amount of time e.g. 5 seconds<br>
  5. Repeat Step 2.</strong></p>
  
  <p>I looked around several posts which suggest using <code>Handlers</code> and that's how I have tried to do it:</p>
  
  <pre><code> @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
  
          final Activity activity = this;
  
          final Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
              public void run() {
  
                  // Start AsyncTask
                  new MyTask(activity).execute();
              }
          }, 5000); //Every 5 seconds
      }
  </code></pre>
  
  <p><code>MyTask</code> is a sub-class of AsyncTask in some another .java file.</p>
  
  <p>This only runs the task once on start-up after a delay of 5 seconds. It doesn't re-execute itself as it should.</p>
  
  <p>Note that I want to run the next task only if its not already running and run it 5 seconds after it.</p>
  
  <p>So, why doesn't it work as excepted?  </p>