In
this tutorial I will explain how to send broadcasts within your own
app using the LocalBroadcastManager.
To begin
with, let’s start with a FAQ.
What is a
BroadcastReceiver?
An
Android app is made of components,
including Application, Activity, Service, ContentProvider and
BroacastReceiver, each of which used for a particular purpose.
So a
BroadcastReceiver is a component (optional)
of an Android app.
What
are BroadcastReceivers generally used for?
BroadcastReceiver
is a component running in the background and listening
to “broacasts” sent by the Android
system or by an Android app.
For
example, when the user turns on or off the screen, when the level of
the battery changes, when wifi is connected/disconnected, etc., the
Android system sends a “broacast”
with the relevant information about the event that has occured. You
can set up a BroadcastReceiver that
intercepts this event and performs a
certain action in response (for example, starting a Service).
In
your own app you can also send
broacasts containing information and set up a BroacastReceiver that
listens to these broadcasts and performs certain actions in response.
We already saw an example in a previous tutorial about the
IntentService class, with which you can use a Service to perform slow
or long running operations in a background thread.
When
the IntentService finishes its task you may want to
communicate the result back
to the calling Activity: to do this you have to send a broadcast
within your IntentService containing the relevant information, and
set up a BroadcastReceiver within you Activity to listen to the
result.
So,
what is a local BroadcastReceiver?
BroadcastReceiver
is generally used to send/receive broacasts between different
processes (for example, different apps)
because it implements an inter process communication system.
But if
the “broadcaster” and the BroadcastReceiver belong to
the same process (for example, sending
broacasts within your own app), a local BroadcastReceiver, which is
managed by the LocalBroadcastManager class, is much
more efficient.
In
addition to that a local BroadcastReceiver is more
secure because all relevant information
stays within your own app and is not visible outside.
So
a local BroadcastReceiver is a more efficient and secure
BroacastReceiver that works only within your own app.
Now let’s
see how to implement a local BroadcastReceiver within your Activity (here you can download the code in a more readable format):
public
class YourClass extends Activity {
//the
main code of the Activity is omitted for brevity
//
used for filtering broadcasts (the receiver listens only to Intents
with “myBroacast” set as action in the constructor)
public
static final String MY_ACTION = “myBroacast”;
@Override
protected
void onResume() {
super.onResume();
//call the superclass first
LocalBroadcastManager
myLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter
myIntentFilter = new IntentFilter(MY_ACTION);
//set
up the local LocalBroadcastReceiver
myLocalBroadcastReceiver
= new BroadcastReceiver() {
@Override
public
void onReceive(Context context, Intent intent) {
//handle
the broadcast here
}
};
//register
the local BroadcastReceiver with the specified intent filter
myLocalBroadcastManager.registerReceiver(myLocalBroadcastReceiver,
myIntentFilter);
}
@Override
protected
void onPause() {
super.onPause();
LocalBroadcastManager
myLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
//always
unregister the receiver when it’s no longer needed to save
resources
myLocalBroadcastManager.unregisterReceiver(myLocalBroadcastReceiver);
}
//instance
variables
private
BroadcastReceiver myLocalBroadcastReceiver;
}
When
you want to send a local broadcast,
you just have to use the following code:
//create
an Intent with the action “myBroadcast” (the filter used in our
local receiver)
Intent
localBroadcastIntent = new Intent(“myBroacast”);
//you
can add additional info to the Intent
localBroadcastIntent.putExtra(“risult”,
risult);
LocalBroadcastManager
myLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
myLocalBroadcastManager.sendBroacast(localBroadcastIntent);
Using directed broadcasts
Another
way of sending broacasts within you own app is by using directed
broadcasts, explicitly specifying the
BroadcastReceiver that has to intercept them.
You
just have to set the ComponentName
in the Intent:
Intent
myIntent = new Intent(action);
myIntent.setComponent(new
ComponentName(packageName, className));
sendBroadcast(myIntent);
action = String
representing the filter used by our BroadcastReceiver
packageName = String
representing the name of the package of our BroadcastReceiver (like
“com.androidthetechnicalblog.example”)
className = String
representing the name of the class of our BroadcastReceiver
This way our
broadcast is received only by the
specified BroadcastReceiver.
Directed broadcasts
can also be used with different processes/applications.
If you like this blog please share it on Facebook, Twitter or Google+ using the icons or links below.
You can also subscribe using the form on the right to be notified via mail or feed rss when new tutorials are posted.