How to use/locate LocalBroadcastManager
as described in google docs and Service broadcast doc?
I tried to google it, but there is no code available to start with?
The documents say that I should use it if I want to do broadcast internally with in my app's process but I don't know where to look for this.
Any help/comment?
Update: I know how to use Broadcasts but don't know how to get LocalBroadcastManager
available in my project.
LocalBroadcastManager is used to register and send a broadcast of intents to local objects in your process. It has lots of advantages: You broadcasting data will not leave your app. So, if there is some leakage in your app then you need not worry about that.
LocalBroadcastManager is basically an event bus with a lot of unnecessary ceremony around Intents and intent filters. So one replacement is easy, and functions quite similarly: you can use any event bus library.
localbroadcastmanager has been fully deprecated. There will be no further releases of this library. Developers should replace usages of LocalBroadcastManager with other implementations of the observable pattern. Depending on the use case, suitable options may be LiveData or reactive streams.
I'll answer this anyway. Just in case someone needs it.
An activity that watches for notifications for the event named "custom-event-name"
.
@Override public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We are registering an observer (mMessageReceiver) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called whenever an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiver", "Got message: " + message); } }; @Override protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); super.onDestroy(); }
The second activity that sends/broadcasts notifications.
@Override public void onCreate(Bundle savedInstanceState) { ... // Every time a button is clicked, we want to broadcast a notification. findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendMessage(); } }); } // Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiverActivity. private void sendMessage() { Log.d("sender", "Broadcasting message"); Intent intent = new Intent("custom-event-name"); // You can also include some extra data. intent.putExtra("message", "This is my message!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); }
With the code above, every time the button R.id.button_send
is clicked, an Intent is broadcasted and is received by mMessageReceiver
in ReceiverActivity
.
The debug output should look like this:
01-16 10:35:42.413: D/sender(356): Broadcasting message 01-16 10:35:42.421: D/receiver(356): Got message: This is my message!
I'd rather like to answer comprehensively.
LocalbroadcastManager included in android 3.0 and above so you have to use support library v4 for early releases. see instructions here
Create a broadcast receiver:
private BroadcastReceiver onNotice= new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // intent can contain anydata Log.d("sohail","onReceive called"); tv.setText("Broadcast received !"); } };
Register your receiver in onResume of activity like:
protected void onResume() { super.onResume(); IntentFilter iff= new IntentFilter(MyIntentService.ACTION); LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, iff); } //MyIntentService.ACTION is just a public static string defined in MyIntentService.
unRegister receiver in onPause:
protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice); }
Now whenever a localbroadcast is sent from applications' activity or service, onReceive of onNotice will be called :).
Edit: You can read complete tutorial here LocalBroadcastManager: Intra application message passing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With