Broadcast Receiver: Two ways to implement part-2

Read Time < 1 minute
In the previous tutorial, we discussed how to implement/register broadcast receiver inside an activity. Broadcast receiver is a standalone application component which means it will continue running even when other application component are not running. That’s why we unregister broadcast receiver in onPause on the activity.

Second way of using broadcast receiver is to register it inside manifest.xml. When registered (and not unregistered) broadcast receiver exists as long as application exists. There could be many use cases of using broadcast receiver this way. These could be waiting for specific event to occur and start/stop other application component upon the occurrence of that event, initiate network communication or communicate with contentprovider . . .etc.

This is pretty simple to implement. We need to register it using receiver tag in manifest.xml and define its intent-filter (same as we did in activity).

      android:name=".IndependentReceiver"  >

Lets define the broadcast receiver itself:

public class IndependentReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        int recval = intent.getIntExtra("counter", 0);
        Log.d("sohail", "independentreceiver: broadcast received with counter="+recval);


Now we can send broadcast to this receiver with action sohail.aziz.r2  e.g

  Intent in = new Intent();
  in.putExtra("counter", 202);
  Log.d("sohail", "onHandleIntent: sending broadcast");

Notice that as we are not unregistered this receiver, so it will continue running throughout the application life.
Browse and Download source BroadcastExample.

1 thought on “Broadcast Receiver: Two ways to implement part-2”

Leave a Comment

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