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.
FirstReceiver
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
.