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?
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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With