0

I want my app to make a call to server always in background checking for any updates , I am new to android and not sure how to perform this to start with. I just want a single call always running in background. Please guide me with this to start with.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
jenil
  • 141
  • 11
  • try IntentService or Service https://developer.android.com/training/run-background-service/create-service.html – Napolean Apr 22 '15 at 06:56
  • You can try [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) if you want to check for update once like when starts your app etc. other wise comment of @jenil is good. – Waqar Khan Apr 22 '15 at 06:57
  • I want to check updated even if app is not started – jenil Apr 22 '15 at 07:00
  • @Napolean Can we keep service always running in background even when app is closed? – jenil Apr 24 '15 at 08:07
  • yes you can have it done by using Service class which once started using startService(Intent ) keeps on running indefinitely until unless stopped by using stopService(Intent) or stopSelf() or killed by Android system under memory pressure. – Napolean Apr 30 '15 at 06:36

1 Answers1

0

You can use Async Task for that. In your doInBackground() method, call your service providing some timespan.

Snippet to add timestamp:

Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
    new AddStringTask().execute();
} }, 0, 5000);
Hems
  • 1
  • 8
  • Does this makes a call to server to run for years ? my use case is like has in whatsapp even though we are not online it pops up messages by running in background – jenil Apr 22 '15 at 07:02
  • Web services run even for years. In your case, use notifications which are the result of Your service call – Hems Apr 22 '15 at 07:04
  • 1
    [link](http://stackoverflow.com/questions/15293925/push-notification-on-android-with-webservices) this may help you – Hems Apr 22 '15 at 07:07
  • http://pastebin.com/GuzC1ZFk ,i am already calling webservice like this,so u mean to say this already running in background? – jenil Apr 22 '15 at 07:08
  • that is eventhough i have come out of my app,this call is made again n again? – jenil Apr 22 '15 at 07:09
  • Yes,if you have used asynctask then it is in background. But to run it say for every 5 sec,give the webservice call after every 5 seconds. – Hems Apr 22 '15 at 07:10
  • any snippet for this ? for setting timer – jenil Apr 22 '15 at 07:12
  • and once it runs in background how will i get notification of updation on device main screen ,like whatsapp? – jenil Apr 22 '15 at 07:14
  • [This link may help you to give timestamp](http://stackoverflow.com/questions/6207362/how-to-run-an-async-task-for-every-x-mins-in-android) – Hems Apr 22 '15 at 07:19
  • pastebin.com/GuzC1ZFk please see this,i need to add timer only on invokeGetReminderWS,how to do this ?please edit in my code – jenil Apr 22 '15 at 08:17
  • I guess this AsyncTask wont work when app is closed we need to use Service – jenil Apr 22 '15 at 08:52
  • jaude yar, bore zal ata mazch mla yet nahiye,tuz kuthun karu,itk pan nahi yet mla Android... – Hems Apr 22 '15 at 08:54
  • Yes,sorry i meant to say that u must use background services for that purpose – Hems Apr 22 '15 at 08:58