Android: Broadcast Reciever

Howdy Friends!

In Simple language Broadcast Receivers  respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents.

e.g an event is fire and if your application has a broadcast reciever then you can listen to the event and perform certain action like SMS Recievers (that responds when a SMS is delivered to the android system).

In this Blog we are going to create a local reciever that will respond to the local event created by our application.

LocalBroadcastManager

This is a helper class to register for and send broadcasts of Intents to local objects within your process.

This helper class generate local events thus it will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.

Note:    If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won’t receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won’t be called if the user moves back in the history stack.

Let us now generate some events and receive in our Activity(s) receiver.

 

Step 1. Create a local event

 

Step 2. Create a Broadcast Receiver in  the class where we want to perform an action

 

Step 3. Register and Unregister the broadcast receiver

Enjoy listening to the local events.

That’s all folk..  Stay Updated

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close