Skip to content Skip to sidebar Skip to footer

Check And Pull Data From Server Continuously In Android Application

I want to load data from server synchronously and show it in a view like ListView or TextView without any pulling request each time. I need the data will be loaded if there is any

Solution 1:

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
}

The AsyncTask does run on a separate thread, but cannot be started from other threads than the UI thread. The handler is there for allowing that.


Post a Comment for "Check And Pull Data From Server Continuously In Android Application"