Recently, I needed to update an activity in Android with data I gathered from a background Service. I couldn’t find any decent examples on the web or StackOverflow, so I decided to put one together. I really like when someone has a complete working sample, so I will provide that as well you can download the source code from Github at the end of this article. The Android project is very simple, with just two classes, the activity and the background service. We’ll take a look first at the background service to see how the data is generated.
The first thing that needs to do is to create a string called BROADCAST_ACTION, this is simply a string that identifies what kind of action is taking place. Most of time, it’s common to use the package name with the kind of action added to the end, so we’ll do that here. The next step is to create a handler that will be used to broadcast our data every 5 seconds. It’s better to use a Handler instead of Timer because a Timer creates a new thread.
public class BroadcastService extends Service { private static final String TAG = "BroadcastService"; public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent"; private final Handler handler = new Handler(); Intent intent; int counter = 0;
In onCreate, I’ve created a Intent and passed the BROADCAST_ACTION to the constructor of the intent. Notice that the Intent was defined as a global variable above. The intent will be called repeatedly in the Handler below and there is no reason to create a new intent every 5 seconds, so I’ll create it once here.
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}In onStart, first we call removeCallbacks to remove any existing callbacks to the handler and make sure we don’t get more callbacks than we want. Then we call our handler with a one second delay. This will start our runnable object below.
@Override
public void onStart(Intent intent, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}Our runnable object creates a new thread, performs whatever code is in the run method and then shuts down. In the run method, we do two things call the DisplayLoggingInfo method and then calls handler.postDelayed again but this time with a 5 second delay. This is how the repeating timer is created. One of the things that is nice about doing it this way, is that you can put a variable in place of 5000 and that way you can externally control the interval between call to the runnable object.
private Runnable sendUpdatesToUI = new Runnable() { public void run() { DisplayLoggingInfo(); handler.postDelayed(this, 5000); // 5 seconds } };
In the following method, we add some data to our intent that was created above. I’m just adding the date and a simple counter that increments itself. Then we call sendBroadcast with that intent which sends a message and whoever is registered to receive that message will then get it.
private void DisplayLoggingInfo() { Log.d(TAG, "entered DisplayLoggingInfo"); intent.putExtra("time", new Date().toLocaleString()); intent.putExtra("counter", String.valueOf(++counter)); sendBroadcast(intent); }
At this point, the Service is completed and now it’s time to take a look at our Activity and see how we consume and display our data. The first thing we’ll do is create an intent with the name of the Service class. This will be passed to startService and stopService.
public class BroadcastTest extends Activity { private static final String TAG = "BroadcastTest"; private Intent intent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); intent = new Intent(this, BroadcastService.class); }
Now, we’ll create a BroadcastReceiver to receive the message that is going to be broadcast from the Service above. The BroadcastReceiver has one method, onReceive, it gets called and then the BroadcastReceiver object is destroyed. In the onReceive method, we call updateUI passing in our intent which is holding the data to display.
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateUI(intent); } };
In onResume and onPause, we start and stop our Service. This happens when the screen (Activity) displays and goes away. We also register our BroadcastReceiver by passing in an IntentFilter. Do you see that the IntentFilter is using the same static string, BROADCAST_ACTION, that we created in the Service above. This is how we identify the message that is broadcast. in onPause, we also make sure that we call unregisterReceiver to stop listening for broadcasts.
@Override public void onResume() { super.onResume(); startService(intent); registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION)); } @Override public void onPause() { super.onPause(); unregisterReceiver(broadcastReceiver); stopService(intent); }
This last bit of code is pretty straight forward. We get our data out of the intent and set our two TextViews with the data. They will be updated every 5 seconds.
private void updateUI(Intent intent) { String counter = intent.getStringExtra("counter"); String time = intent.getStringExtra("time"); Log.d(TAG, counter); Log.d(TAG, time); TextView txtDateTime = (TextView) findViewById(R.id.txtDateTime); TextView txtCounter = (TextView) findViewById(R.id.txtCounter); txtDateTime.setText(time); txtCounter.setText(counter); }
I’ve made the entire project available for download here on GitHub. You can either clone the project or download it with the Downloads button.



59 Comments to 'How to Update the UI in an Android Activity Using Data from a Background Service'
February 7, 2011 12:39pm
Like the nature of android os, I guess android documentation for developers heavily depends on many. Your complete working example saves me a lot of time, Thank you.
February 11, 2011 4:44pm
I thlnk this is great
February 11, 2011 8:47pm
Thank you guys, appreciate it.
Nick
March 17, 2011 10:56am
Really, I can’t express you how grateful I am.
thank you very much!
April 15, 2011 1:07pm
Excelente explicación thks
May 3, 2011 9:37pm
Awesome, it WORKED! Thanks.
May 5, 2011 8:22pm
Thanks for the excellent work
June 13, 2011 11:58am
Thank you very much!
June 18, 2011 10:17pm
You made a real difference… because you can communicate with words, and write code. Thanks for taking the time to post and clarify this stuff for me!
July 2, 2011 9:51pm
Great example! Thank you Nick!
July 27, 2011 3:48pm
I can’t thank you enough.
I put kudos on our site here:
http://www.wikispeedia.org
-jim
August 15, 2011 12:00am
[...] Fox published a great post about how to create a background service, so I have used his example to expand it with the use of [...]
August 21, 2011 4:06pm
Thank you so much for this. I wish I’d found this tutorial 4 hours ago!
August 22, 2011 7:54am
WOOOOOWWW VERY VERY tnks for this.
=)
August 27, 2011 3:06am
Thanks Nick Fox
Rockssssssssssss
August 30, 2011 10:15am
Thanks a lot for this.
September 11, 2011 12:39pm
Great demonstration, just one thing to note. Complete newbies might be completely confused as to why nothing is invoked once the code above is complete. Well I didn’t understand the service architecture. I needed to add
to the AndroidManifest. I used the log to determine the intent was not being found and pulled this entry out of the xml in your source but it would be nice to mention that this is the last piece.
All in all this is an amazing walkthrough! Thanks!
September 11, 2011 12:41pm
Looks like my post removed the xml –
service android:name=”.BroadcastService”
September 18, 2011 4:50pm
Hi,
Thank you so much for this working example!!!
It helped me a lot!
September 23, 2011 12:28am
come from stackoverflow.com, this article helps me very much ,thanks to the author.
September 29, 2011 11:31am
Hi Nick, great code and great explain.
Your code run very well, but when the activity is sleeping the broadcastReceiver, in the onReceive, don’t wake up the activity and the main activity don’t receive any info.
How to repair this?
I was a lot of hours investigating and anything…
I need help whith this.
thanks and sorry by my english.
October 9, 2011 8:55pm
thanks..very well explained. and the project works perfectly.
October 13, 2011 2:03pm
Thank you very much. I’m the kind of guy that understands things watching it working.
That complete working example was just perfect!
November 10, 2011 5:30am
This example is superb…
Thank you so much …
December 3, 2011 12:41pm
Thanks for your documentation, it was great to find it, and it was simple to implement in my alread developed service.
People who write like you write don’t do it for money, I am sure, but I think I owe you some, so I am glad to see the “donate” button
December 29, 2011 12:06pm
It’s misleading to call this a background service. The sendUpdatesToUI runnable doesn’t create a new thread – it just adds the runnable to the main thread’s message queue. If you were doing something more involved in DisplayLoggingInfo, you would probably see an ANR.
Creating a worker thread in the activity that updates the TextViews through a UI thread Handler or using an IntentService if multiple activities need to receive the broadcast would be better approaches.
January 3, 2012 11:54am
YOU ROCK MAN!! THANK YOU!!
January 14, 2012 7:39am
Very straightforward and complete example which is often hard to find in the sea of useless “To Do” articles online. Please consider writing more articles as you seem to have a natural knack for it. Thank you for taking the time.
January 15, 2012 8:57pm
Thank you! Great article.
February 1, 2012 1:42am
Thank You Nick,
It helped me to clear out my doubts
and refine my concepts towards UI Thread
: ))))
Regards:
abhi
February 23, 2012 12:41am
This was exactly what I needed! I think I understand Laura’s comment above, but for straight UI updates this works well for my purposes. You can also mod this to just have the service update the UI activity when it needs to.
Thanks so much!
March 13, 2012 2:28am
Very Nice Tutorial.
March 20, 2012 5:17am
very helpfull, thanks!
April 17, 2012 1:04am
Thanks for the example, helped me out a lot writing an Android application which periodically calls an API to listen for updates on which location to navigate to. (Building a location based game).
May 3, 2012 2:50pm
Can someone explain this bit of Java to me?
[CODE]
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 10000); // 10 seconds
}
};
[/CODE]
What are you doing there exactly? I understand what you’re doing with regards to services and all that, but from a language standpoint, I’m confused; what is this? It looks like you’re making a new runnable (in the class definition, instead of inside a function, which I thought you weren’t allowed to do), but then you’ve suddenly got braces right afterwards, and another function definition within it. I’m confused on so many levels! Java has always looked nearly identical to C++ until now.
May 4, 2012 8:30am
Nick, Thanks for this – just what I needed!
Eric, All that does is makes a runnable – its basically a class where you implement a method run() which gets run usually after a period.
You can put code inside the run method to do whatever.
Then pass the runnable to a handler (usually using postdelayed(), with a delayed time to run it after).
May 4, 2012 1:58pm
How do I register the service in the manifest file?
I did it like this:
….
I must be doing it wrong, because its not running the service.
May 7, 2012 9:49am
Oops, I don’t know why the site replaced my XML stuff with …, here’s what I did:
(replaced ‘<' with ')', since I think the site might be processing tags a little differently)
(application
…..
(activity
…..
(/activity)
(service android:name=".ClockService" /)
(/application)
June 28, 2012 11:14am
invoking any application from this service is that doable???
so probably prompting user by saying that this service wants to run this application and when the user allows it will invoke the application???
August 19, 2012 10:48pm
Hey Thnx a lot for such a wonderful tutorial
September 14, 2012 8:24am
Hey men, thank you, from Colombia.
This example work amazing.
Bye
November 13, 2012 2:21pm
Hi, I need to update my UI even in onPause. Is it possible or not?
Thank you.
November 19, 2012 6:37am
AWESOME tutorial!!!
struggled with this for a while and couldn’t get descent tutorials.
Used this to broadcast data between the Android IOIO as a service and the rest of my application. excellent tutorial!
November 22, 2012 1:39am
Thanks a lot… This is the only example I found on web which exactly fits my requirement.
December 6, 2012 1:54am
Really good tutorial
December 12, 2012 12:39am
thanx a lot..
xactly wat i needed..keep up da good work..
December 19, 2012 5:26am
Really Good Tutorial,Thank You Nick.
December 19, 2012 5:27am
Really Good Tutorial,Thank You Nick
January 3, 2013 5:08am
Grt tutorial.. Thank you..
Actually I need to udpate list view on data from background service from a web service call. On onReceive
I have updatedUI. Problem is every time list view is updated with new data,it focuses on first item. What I want is to set the position of item to last element in a list before the broadcast.Can you give me any suggestions on this.
January 23, 2013 2:59am
Tutorial was good.Thanks for the help.
January 23, 2013 1:22pm
How do i listen this into multiple activity?
February 11, 2013 11:17pm
Thanks, its a nice tutorial, this is what i am looking for ! I have one small question reagrding this. if i want to use broadvasted data on multiple activities then do i need to create the broadcastReciever and need to call register-unregister service for each activity ? or is there any better way to do the same.
Thanks & Regards
Juned
February 20, 2013 3:03pm
Hi, Thanks for the tutorial, works really well. I just have one question. I want to display random array of boolean values, which will update continuously.
What I did is here:
for(int i =0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if (arrBool[i] == true) {
intent.putExtra("extra", Array.getBoolean(arrBool, i));
}
if (arrBool[i] == false) {
intent.putExtra("extra", Array.getBoolean(arrBool, i));
}
}
——————————————–
Now it is not getting that boolean array into BroadcastTest.java. I used textview.setText(ArrBool[]) It is not working. I was able to display one value at a time, but not array. I'd really appreciate if anybody could help me with this.
Thanks
Mayur
February 21, 2013 12:00pm
[...] value’s of MainActivity from Service, need to use BroadcastReceiver [...]
February 27, 2013 1:01pm
[...] broadcasted intent in activity where i want to show this values. here is the reference link to update UI from background service.How do i achieve this using multithreading. my ultimate goal is to read socket and get the status [...]
March 13, 2013 3:00am
i convey my gratitude for providing this.
March 15, 2013 3:38am
Muy interesante!!, Muchisimas gracias
April 15, 2013 6:27am
brilliant! THANKS!
April 18, 2013 6:21pm
Your are my hero!
This example helped me tremendously for one of my projects. I spent days looking for what I finally found here. Your example is not only easy to understand, but it is only very well explained. Thank you so much.
Leave a comment