Android: how to send an HTTP request through a service every 10 seconds

I need to get status from the server every 10 seconds.

I tried to do this by sending an http request through a service.

The problem is that my code runs only once.

This is the code of my service:

public class ServiceStatusUpdate extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { while(true) { try { Thread.sleep(10000); } catch (InterruptedException e) { new DoBackgroundTask().execute(Utilities.QUERYstatus); e.printStackTrace(); } return START_STICKY; } } private class DoBackgroundTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { String response = ""; String dataToSend = params[0]; Log.i("FROM STATS SERVICE DoBackgroundTask", dataToSend); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(Utilities.AGENT_URL); try { httpPost.setEntity(new StringEntity(dataToSend, "UTF-8")); // Set up the header types needed to properly transfer JSON httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Accept-Encoding", "application/json"); httpPost.setHeader("Accept-Language", "en-US"); // Execute POST HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { response = EntityUtils.toString(responseEntity); } else { response = "{\"NO DATA:\"NO DATA\"}"; } } catch (ClientProtocolException e) { response = "{\"ERROR\":" + e.getMessage().toString() + "}"; } catch (IOException e) { response = "{\"ERROR\":" + e.getMessage().toString() + "}"; } return response; } @Override protected void onPostExecute(String result) { Utilities.STATUS = result; Log.i("FROM STATUS SERVICE: STATUS IS:", Utilities.STATUS); super.onPostExecute(result); } } } 

Thanks a lot Avi

+8
java android service
source share
5 answers

Put a handler inside onPostExecute to send an HTTP request in 10 seconds

 new Handler().postDelayed(new Runnable() { public void run() { requestHttp(); } }, 10000); 

After 10 seconds, doInBackground will start again, after that onPostExecute again, the handler again, and so on and so forth.

+15
source

You can just use the CountDownTimer class in android.

 public class AlertSessionCountDownTimer extends CountDownTimer { public AlertSessionCountDownTimer(long startTime, long interval) { super(startTime, interval); } @Override public void onFinish() { //YOUR LOGIC ON FINISH 10 SECONDS } @Override public void onTick(long millisUntilFinished) { //YOUR LOGIC ON TICK } } 

Hope this helps you.

+1
source

The reason code runs only once is because there is a return in the timer loop. However, I would advise moving the loop to a background job or using one of the other answers given here, rather than implementing a loop in onStartCommand() , which never returns.

0
source
 Handler handler_proc = new Handler(); final Runnable runnable_proc = new Runnable() { public void run() { requestHttp(new OnFinish() { @Override public void onSuccess() { // pause should be considered since the end of the previous http request. // Otherwise, new requests will occur before the previous one is complete, // if http_timeout > delay handler_proc.postDelayed(runnable_proc, 10000); } }); } } handler_proc.post(runnable_proc); 
0
source

put return START_STICKY; after that, and the service will work correctly

0
source

All Articles