Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send request to web service every 5 second

Tags:

android

I want to listen sql server database for know whether there are changes of data in android so I want to send request to web service every 5 second to know of new data value.How can I do this? Can you give a example about it?

like image 325
Rıdvan Avatar asked Sep 26 '14 11:09

Rıdvan


2 Answers

You can do it with AsyncTask,

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
}

More: How to execute Async task repeatedly after fixed time intervals

like image 103
My God Avatar answered Oct 02 '22 08:10

My God


Use Service class and within the service class implement thread scheduler that will send request every 5 seconds. Below is th ecode snippet:

public class ProcessingService extends Service {

private Timer timer = new Timer();


@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            sendRequest();
        }
    }, 0, 5000;//5 Seconds
}

@Override
public void onDestroy() {
    super.onDestroy();
    shutdownService();

}

}
like image 28
Vishal Ramachandran Avatar answered Oct 02 '22 09:10

Vishal Ramachandran