Skip to content

Instantly share code, notes, and snippets.

@carlos-wong
Last active November 25, 2016 15:55
Show Gist options
  • Save carlos-wong/5e2cd4e45436326d1b4e01a61668a860 to your computer and use it in GitHub Desktop.
Save carlos-wong/5e2cd4e45436326d1b4e01a61668a860 to your computer and use it in GitHub Desktop.
android asynctask keep run repeat
public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {       
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {       
                    try {
                        PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
                        // PerformBackgroundTask this class is the class that extends AsynchTask 
                        performBackgroundTask.execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment