Tech tip of this week

Android: Sending broadcasts with in application using LocalBroadcastManager

LocalBroadcastManager is a Helper to register for and send broadcasts of Intents to local objects within your process.

Advantages:

  • The data you are broadcasting won't leave your app. So, you don't need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system.

LocalBroadcastManager available only with support library.
android.support.v4.content.LocalBroadcastManager


Broadcast the given intent to all interested BroadcastReceivers. This call is asynchronous;
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("event-type"));

Register a receive for any local broadcasts that match the given IntentFilter.
LocalBroadcastManager.getInstance(context).registerReceiver(listener, new IntentFilter("event-type"));

Unregister a previously registered BroadcastReceiver.
LocalBroadcastManager.getInstance(this).unregisterReceiver(<receiver name>);

We can't register LocalBroadcastreceiver in AndroidManifest.xml

No comments:

Post a Comment