Android Service and BroadcastReceiver Example

Android Service with BroadcastReceiver

A very useful and important concept of Android is services, I was going through Truiton, and saw I have nothing on Android Services. So I decided, to write some interesting stuff that one can do with Android services. First off lets start with some basic intro to services.

Android Services Introduction

There are times when one wants to perform some long running operation on a device. Like processing of some data, establishing a two way connection over a network; to download a file from the internet, or maybe just play some music on a device. In all these situations one would require an Android component which can do processing in the background, i.e. should not have an UI. That component is called Android Service.

Please Note: Do not get confused by the word background here, Android Service runs on main thread, unless a new thread is spawned for it.

Although in this Android Service and BroadcastReceiver Example, I have taken care of this situation by doing all the processing in a separate thread. By taking this approach I averted the situation where app could be laggy, or go in an ANR state. Also its a good programming practice in Android to do heavy operations in a thread other than main thread.

Broadly speaking Android Services are of two types, started and bound, although a single service could act as started and bound both. I will show how this could be done in a separate tutorial. Here in this Android Service and BroadcastReceiver Example, I would mainly concentrate on started services. Lets start off by declaring the service in app manifest:


Android Service : Started

When a service is not bound to any app component, and is started by a call to startService() method, its called a started service. A started service once started has the capability of running indefinitely based on the flags, that were used to start it. Now you might be wondering when will it stop. The answer is; it will only stop if an app component stops it by calling stopService(). Or the service itself has a logic to stop by calling the stopSelf() method.

Android Service : Send Broadcast to Activity

Although communication between a service and an activity should be done through an IBinder interface by binding a service to a component. But for instance if there is a situation when you need to broadcast an intent to entire system, then one can also send a broadcast. This Android Service and BroadcastReceiver Example shows how to this. Although official android documentation mentions nothing regarding this procedure, so why am I doing it? because I can do it and I am not violating any guidelines. Have a look at the service code below:

The class above shows how to create a service in Android, which can broadcast the results to the system. As per the specifications to start a service at-least an onBind() method is required. Although the main logic in a started service should be written in onStartCommand() method. Another important method here is onCreate, where you can initialize the service, as what all things need to be taken care of when service is created.

A service can be started in three modes:


In this example we are performing a service activity communication through START_REDELIVER_INTENT flag. By using this flag in onStartCommand() method, we are specifying that, if Android system kills this service due to low memory. Then it should start this service again and re-deliver the last intent it received. Also it would receive all the intents, that are pending.

As you can see that in the above Android service, all work is done in onStartCommand() method. In this method I am sending broadcasts with different types of data in it. This demonstrates that we can send any type of data we want to send through broadcasts.

Please Note: In the above example all the broadcasts are system wide broadcasts, if you want to make broadcasts accessible only to your app, then please use the LocalBroadcastManager.

Android Broadcast Receiver: Receiving Broadcasts From Service

Now I will create an activity, where I would listen to the broadcasts of the service created above. To do that I would create a layout first, with a single TextView:

Moving on I would define an activity with BroadcastReceiver where this layout would be used:

The above class demonstrates how to receive broadcasts in android dynamically. That is by declaring a BroadcastReceiver like this we don’t need to specify the receiver in manifest. It may be an advantage or disadvantage, but by doing this the activity would listen to broadcasts only when its in foreground, i.e. when its visible.

Now since we are using a started service, it will keep on running until its stopSelf() method is called. Or one can call its stopService() method from any other class. In this example I called stopService() method after receiving the final broadcast, and stopped the service. Have a look at the final screenshot:

Android Service

Source Code

Here you can see that all the three broadcasts from BroadcastService are received. Also from the link above you can access the source code for this Android Service and BroadcastReceiver Example. On a closing note, I would like to say this example is not the perfect way one should use an Android service, its just an alternate approach which does not break any guidelines. Hope this helped.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *

6 thoughts on “Android Service and BroadcastReceiver Example