0

I started learning android i've been playing with it and so far so good but i have some doubts about Services, i started learning them today so by gently if a say something very wrong.

For example, i want my app to grab some information over the internet from time to time, this polling period is defined by the user, then the UI gets updated. I though about creating a Service that run lets says every 30 minutes, gets the information and updates the UI.

If i get it right:

  • An IntentService just executes an operation and stops by itself sending the result through an intent(right?), so i think it's not what i want.
  • A Bounded Service is most likely used when you want IPC or allow binding from external apps, which again i think it's not what i want.

I think a Local Service is probably what i need, using a LocalBroadcastReceiver to update the UI, how can i make it to run the operation every X minutes( Handler postDelayed, ScheduledExecutorService or Alarm Manager ? )

If i understand it right a Service if not bounded can run infinitely if it's not killed due to low memory problems, making it a foreground Service is the safest ?

Last thing and it's kind of a noob doubt, if the user leaves the application(Click Home Button or opens other app) the app is still in background but the activities are in "Paused" or "Stopped" mode will the Service still be able to talk to them ?

Sorry for long post and thank you.

Sergio Serra
  • 1,479
  • 3
  • 17
  • 25

2 Answers2

1

Your requirement : after every x minutes, start a service, pull some date, update UI.

Solution :

  1. Define or set an alarm for every x minutes, to trigger a receiver.
  2. From receiver start the service.
  3. In the service, start an async task to fetch the data in doInBackGround().
  4. Once data is fetched, from onPostExecute() send a broadcast to your activity.
  5. In the activity have a dynamic receiver registered for broadcast sent from service.
  6. From dynamic broadcast receiver update UI.
StarsSky
  • 6,721
  • 6
  • 38
  • 63
user1923551
  • 4,684
  • 35
  • 29
0

From what you've explained I wouldn't personally use a service.

The Android docs on services explain more but here is a snippet:

http://developer.android.com/guide/components/services.html

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

You could perhaps looks at using an AsyncTask, especially given that you only want it to run whilst the app is running:

http://developer.android.com/reference/android/os/AsyncTask.html

This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

There is a good answer here on how to run an AsyncTask repeatedly at specific time intervals: How to execute Async task repeatedly after fixed time intervals

Community
  • 1
  • 1
Scott Helme
  • 4,786
  • 2
  • 23
  • 35
  • Hi thanks, i explained myself wrong i've updated the question the operation will run even if the app is not running, i'm aware of asyncTask but i think is not the best option and it can be killed with configuration changes. – Sergio Serra Aug 30 '13 at 10:01
  • You could possibly look at the AlarmManager then. The only problem with a service is that it's running all the time even when it's not doing anything. With the AlarmManager you can set it to run your Intent every 30 minutes for example. – Scott Helme Aug 30 '13 at 10:19
  • Yes i updated the question with the Alarm Manager option, about running all the time, i'm not sure but if it's an intent service it will kill itself if there's no more work right ? – Sergio Serra Aug 30 '13 at 10:51