LocalBroadcastManager: Intra application message passing

Read Time 2 minutes
Using Broadcast receivers is good practice if you want to send and receive data between different applications. However its not good at all if you are using them for communication internal to application.

There are many reasons for NOT using broadcast:

  1.  A broadcast is sent system-wide, so this is not performance efficient.
  2.  When we use broadcast receiver and sender, any other application can send and receives broadcast messages to and from our application. This can be a serious security thread for our application.

Read more about security issues and configuration of broadcast at developer.android
Instead of using normal broadcast we should use Local broadcast for sending and receiving in-app messages between activities and services. Local broadcast is included in android 3.0 and is provided as support package v4 for early release development. For setting up support library read developer.android

ResultReceiver can be used to return data back to activity. Here is a simple IntentService: Providing data back to Activity #android .However by ResultRceiver, we can only return data to that specific activity which started the service.

To send data/intent application-wide, LocalBroadcasts should be used. LocalBroadcastManager is used to send and receive local broadcast. Lets start with a simple example. We have a simple IntentService say MyIntentService:

package sohail.aziz.mylocalbroadcast;

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class MyIntentService extends IntentService{

//ACTION should include application package convention, just to show that this can
//be any string
    public static final String ACTION="mycustomactionstring";
    public MyIntentService() {
        // TODO Auto-generated constructor stub
       Log.d("sohail","service started");

    protected void onHandleIntent(Intent arg0) {
        // TODO Auto-generated method stub
        Log.d("sohail","onHandleIntent called");
        Intent in=new Intent(ACTION);  //you can put anything in it with putExtra
        Log.d("sohail","sending broadcast");


A simple IntentService which sends local broadcast application-wide. Lets receive this in activity.

package sohail.aziz.mylocalbroadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MylocalbroadcastExampleActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    TextView tv;
    Button bt;
    protected void onPause() {
        // TODO Auto-generated method stub
    protected void onResume() {
        // TODO Auto-generated method stub
        IntentFilter iff= new IntentFilter(MyIntentService.ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, iff);
    private BroadcastReceiver onNotice= new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            // intent can contain anydata
            Log.d("sohail","onReceive called");
            tv.setText("Broadcast received !");
    public void onCreate(Bundle savedInstanceState) {
        bt= (Button)findViewById(R.id.btStart);
    public void onClick(View v) {
            Log.d("sohail","starting service");
            Intent i= new Intent(this, MyIntentService.class);

Here we are registering broadcast receiver onNotice as LocalBroadcast Receiver. Intent-filter is defined with ACTION defined in MyIntentService.

You just need to define service in AndroidManifest.xml:


and thats it !
Browse and download source MyLocalbroadcastManager.

7 thoughts on “LocalBroadcastManager: Intra application message passing”

  1. Thank you for this. After looking at no less than about 30 examples and none of them working, including the pieces on the android developer site, or not having any flaws/bugs/deprecations, yours works and is a complete example… whoohooo!

  2. Hello
    Im trying to use your example in my application service, however


    always produces the error

    getinstance(android.content.context) in LocalBroadcastManager cannot be applied to (NAME OF MY SERVICE)

    Do you have any suggestions as to why ?

    Thank you

Leave a Comment

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