logo

Broadcast receivers are Android’s implementation of the Publish/Subscribe messaging pattern, more precisely is an Observer pattern. Applications (known as publishers) can generate broadcasts to simply send events not knowing who, if anyone, will get them. Receivers (known as subscribers) that want the information subscribe to specific messages via filters. If the message matches a filter, the subscriber is activated (if it’s not already running) and notified of the message.

A BroadcastReceiver is a piece of code to which an app subscribes in order to get notified when an action happens. That action is in a form of an intent broadcast. When the right

intent is fired, the receiver wakes up and executes. The “wakeup” happens in form of a onReceive() callback method.

The class BroadcastReceiver defines the method onReceive(). Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active. Therefore you cannot perform any asynchronous operation.

Here’s an example that tries explain how to send custom intents and grab them with broadcast receivers. This example has two receivers and an android manifest file:

So Create a project named “com.mobisys.android.broadcast_receiver_ex“.
We do not need an Activity.

Receiving Broadcast message

a) It listens to Broadcast Intents
b) must be registered(either in code or within the app manifest)
c) use Intent Filter to specify which Intents it is listening for i.e.
The AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
	<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	      package="com.mobisys.android.broadcast_receiver_ex"
	      android:versionCode="1"
	      android:versionName="1.0">
	    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
	        <receiver android:name=".OutgoingReceiver" android:enabled="true">
	            <intent-filter>
	                <action android:name="android.intent.action.PHONE_STATE"></action>
	            </intent-filter>
	        </receiver>
	        <receiver android:name=".IncomingReceiver" android:enabled="true">
	            <intent-filter>
	                <action android:name="com.mobisys.android.intent.action.TEST"></action>
	            </intent-filter>
	        </receiver>
	 
	    </application>
	    <uses-sdk android:minSdkVersion="3" />
	 
	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
	</manifest>

FirstReceiver

package com.mobisys.android.broadcast_receiver_ex;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class IncomingReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		
		if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {
		        System.out.println("*****GOT THE INTENT********");
		        
		}
	}
}

Sending Broadcast message

We can send Broadcast message using method context.sendBroadcast().

SecondReceiver

package com.mobisys.android.broadcast_receiver_ex;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OutgoingReceiver extends BroadcastReceiver{

	public static final String CUSTOM_INTENT = "com.mobisys.android.intent.action.TEST";
		 
		    @Override
		    public void onReceive(Context context, Intent intent) {
		        System.out.println("*****HIT OUTGOING****************");
		        Intent i = new Intent();
		        i.setAction(CUSTOM_INTENT);
		        context.sendBroadcast(i);
		    }
		}

Let’s look at the explanation, what happens in above code.So notice that we first define our custom intent as:

public static final String CUSTOM_INTENT = "com.mobisys.android.intent.action.TEST";

Now, in our manifest, our intent filter only allows for intents with actions
com.mobisys.android.intent.action.TEST” to be received and so technically we don’t even need to do the check in the IncomingReceiver.

The flow of our little example works like this – first we define the OutgoingReceiver to intercept all intents broadcasted with action READ_PHONE_STATE (i.e. phone calls, this is how we trigger the first receiver). Once the OutgoingReceiver intercepts the first broadcasted intent, it will broadcast the second custom intent using the context.sendBroadcast() method which will then be received by our second receiver IncomingReceiver.

Run the application. You can see the logcat.

And so we see that once the call is initiated, it triggers the OutgoingReceiver and prints the first line, which then triggers the second broadcast intent, hits our IncomingReceiver, and prints the second line.

Download the source code

Click here to download.

AUTHOR: Vikas Hiran
2 Comments
  • That’s nice that you share source code. The idea is nice. Are the messages sent through internet?

    January 16, 2012
  • dhinesh

    very useful…

    May 26, 2014

Leave a Comment

Your email address will not be published.