Take the 2-minute tour ×




Thanks in advance

share|improve this question

3 Answers 3

up vote 73 down vote accepted


You have three major options, as I see it:

  1. Polling. The Activity periodically asks the Service for the latest data. IMHO, this option sucks, but it's certainly possible.

  2. Callbacks. Per jax's answer, the Activity registers a callback object ("observer") with the Service. The Service invokes a method on the callback when the data changes, which in turn updates the UI. You can see an example of using that with a Service here.

  3. Broadcast Intents. The Service broadcasts an Intent via sendBroadcast() on a data change. The Activity registers a BroadcastReceiver using registerReceiver(), and that BroadcastReceiver is notified of an incoming broadcast. This triggers the Activity to load the latest data from the Service, or possibly just to get the latest data out of extras in the broadcast Intent. You can see an example of using that technique with a Service here.

share|improve this answer
Great example of number 3 here ... websmithing.com/2011/02/01/… –  Steven Elliott May 9 '11 at 14:16
What if i closed the application. How can i handle the case of running AlarmManager with Status Activity. –  Basbous Apr 26 '12 at 9:16
What are the pro and cons of techniques 2 and 3? –  resus Apr 15 '13 at 14:49
@resus: #2 requires binding, which introduces more "state" and makes activity configuration changes more challenging. #3, as written, is truly broadcast to the whole device. In the three years since this answer was written, other options have been added. I would recommend that you consider LocalBroadcastManager from the Android Support package (like #3, but private to your app) or using the Otto event bus: square.github.io/otto –  CommonsWare Apr 15 '13 at 15:10
@Ewoks The LocalBroadcastManager is more effecient, as the intents are only sent internally and not globally like the old method. Quote from BroadCastReceiver documentation: "...This will give you a much more efficient implementation (no cross-process communication needed) "... –  Leffy Oct 22 '13 at 12:48

This sound like a good candidate for the Observer Pattern. Basically your activity (The Observer) will register itself with the background service (The Observable) and you can push or pull data from your Activity. Your Observer in this case will be your Activity and the Observable will be your Service.

If you know nothing about Design Patterns buy "Head First Design Patterns", it is easy to read and is full of great information.

PS: I am reading it now.

share|improve this answer
You are right but this answer is in no way android specific... –  Janusz Mar 18 '10 at 10:46

You will have a background thread running that calculates the changes in the list. This thread now needs a possibility to notify the GUI that the list was updated.

You can use some kind of ArrayAdapter to get the data into the ListView. The ArrayAdapter has a method called adpater.notifyDataSetChanged() every time you call this method the adapter will see that the corresponding data has changed and then notify the listview that it should update at the next possibility.

share|improve this answer
Better yet, if you are going to use ArrayAdapter, just call the add(), insert(), and remove() methods on ArrayAdapter to change its contents. You do not need notifyDataSetChanged() then. –  CommonsWare Mar 18 '10 at 12:05

Your Answer


