LocalBroadcastManager is used to send Broadcast Intents within an application, without exposing them to unwanted listeners.
Using LocalBroadcastManager is more efficient and safer than using context.sendBroadcast()
directly, because you don't need to worry about any broadcasts faked by other Applications, which may pose a security hazard.
Here is a simple example of sending and receiving local broadcasts:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("Some Action")) {
//Do something
}
}
});
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext);
manager.registerReceiver(receiver, new IntentFilter("Some Action"));
// onReceive() will be called as a result of this call:
manager.sendBroadcast(new Intent("Some Action"));//See also sendBroadcastSync
//Remember to unregister the receiver when you are done with it:
manager.unregisterReceiver(receiver);