Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Using LocalBroadcastManager to Broadcast and Receive within your application

Author: Ian Darwin
Published? true
FormatLanguage: WikiFormat

Problem:

You want to use the Broadcast send/receive mechanism, but don't want other apps to see your broadcasts.

Solution:

Use the LocalBroadcastManager to register your receiver and to send your broadcast Intents.

Discussion:

There are both efficiency and security issues with using the normal BroadcastReceiver mechanism described in 934 to send broadcasts that are actually only meant for use within your application. Why bother everybody else if they shouldn't care? And, what about those who might bother but shouldn't? Using the LocalBroadcastManager to register your receiver and to send your broadcast Intents eliminates both problems.

This thus makes a viable replacement for use of custom Javabean-style event notifiers.

To use it, the receiver must be registered using

LocalBroadcastManager.getInstance(aContext).registerReceiver(handler, receiveFilter);

Then, the sending code has merely to call

LocalBroadcastManager.getInstance(aContext).sendBroadcast(sendableIntent);

In both cases the Context can be an Activity or Service.

To keep the complete example simple, the sending code and the receiving code are in the same Activity; this is not the normal state of affairs.

Be aware that the BroadcastReceiver runs on the same thread that the intent is Broadcast on; in our example this is the main/GUI thread!

public class MainActivity extends Activity {

	private static final String TAG = "LocalBroadcastDemo";
	private IntentFilter receiveFilter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "MainActivity.onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		receiveFilter = new IntentFilter(getClass().getName());
		LocalBroadcastManager.getInstance(this).
			registerReceiver(handler, receiveFilter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; items go in the action bar if present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private BroadcastReceiver handler = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "BroadcastReceiver() {...}.onReceive()");
			Toast.makeText(MainActivity.this, 
				"Message received", Toast.LENGTH_LONG).show();
		}	
	};

	/** 
	 * Create and dispatch an Intent via the LocalBroadcastManager.
	 * Called from a Button with android:onClick="send"
	 */
	public void send(View v) {
		Log.d(TAG, "MainActivity.send()");
		Intent sendableIntent = new Intent(getClass().getName());
		LocalBroadcastManager.getInstance(this).
			sendBroadcast(sendableIntent);
	}
}

XXX Can you use XML to register the receiver in this scenario?

Download:

The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/,in the subdirectory LocalBroadcastDemo.
No records found.