Starting AsyncTask recursively after a gap of 5 minutes

android, android-asynctask

Solution

Use this code in the onPostExecute.

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            new MyAsyncTask().execute("my String");
        }
    }, 5*60*1000);

Problem

I want to create an instance of a class (which extends `Asynctask`) and call its `execute()` method after every 5 minutes. For that I tried to call `Thread.sleep(5*60*1000))` in `onPostExecute()` method and then create a new instance of the class. The code is as below. ``` public class MyAsyncTask extends AsyncTask<String, Void, String> { protected String doInBackground(String... arg0) { //whatever I want to do } protected void onPostExecute(String result) { Thread.sleep(5*60*1000); new MyAsyncTask().execute("my String"); } } ``` But using this code blocks the UI for 5 minutes. I read somewhere that the code in `onPostExecute()` is executed in the UI thread. This explains why the UI is blocked. But then how do I create a new instance of `AsyncTask` without blocking the UI ? Any suggestions ? Thanks.

Original source