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.
An example of an Activity and a Service implementing a LocalBroadcastManager can be found in the developer docs. I personally found it very useful.
EDIT: The link has since then been removed from the site, but the data is the following: https://github.com/carrot-garden/android_maven-android-plugin-samples/blob/master/support4demos/src/com/example/android/supportv4/content/LocalServiceBroadcaster.java
In Eclipse, eventually I had to add Compatibility/Support Library by right-clicking on my project and selecting:
Once it was added, then I was able to use
LocalBroadcastManager
class in my code.When you'll play enough with LocalBroadcastReceiver I'll suggest you to give Green Robot's EventBus a try - you will definitely realize the difference and usefulness of it compared to LBR. Less code, customizable about receiver's thread (UI/Bg), checking receivers availability, sticky events, events could be used as data delivery etc.
I'll answer this anyway. Just in case someone needs it.
ReceiverActivity.java
An activity that watches for notifications for the event named
"custom-event-name"
.SenderActivity.java
The second activity that sends/broadcasts notifications.
With the code above, every time the button
R.id.button_send
is clicked, an Intent is broadcasted and is received bymMessageReceiver
inReceiverActivity
.The debug output should look like this:
we can also use interface for same as broadcastManger here i am sharing the testd code for broadcastManager but by interface.
first make an interface like:
2-this is the first class that need implementation
3-here is the the second class that implement the same interface whose method call automatically
so by this approach we can use the interface functioning same as broadcastManager.
How to change your global broadcast to LocalBroadcast
1) Create Instance
2) For registering BroadcastReceiver
3) For sending broadcast message
4) For unregistering broadcast message