Friday 8 June 2012

How to execute Async task repeatedly after fixed time intervals


public void toCallAsynchronous() {
    TimerTask doAsynchronousTask;
    final Handler handler = new Handler();
    Timer timer = new Timer();


            doAsynchronousTask = new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    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

        }

3 comments: