Broadcast Receivers (Android Development Fundamentals, Unit 3: Lesson 7.3)

Broadcast Receivers (Android Development Fundamentals, Unit 3: Lesson 7.3)

[MUSIC PLAYING] SPEAKER: Android applications
won’t do anything until a certain
type of message is broadcast by the Android
operating system, or any other applications. Suppose you want your
app to react in some way when a system event occurs. You may, for example,
have built a music app. And you want it
to launch the app to start playing music if
headphones are connected, and stop playing music if
the headphones are removed. How can your app tell
when these events occur? Well, for this we have broadcast
receivers, which you will understand in a few minutes. This whole mechanism
involves the implementation of broadcast intents and
broadcast receivers together. So in this session
we will learn about how broadcast intents broadcast
the system-wide messages to other components, and how
broadcast receivers listen for that specific
broadcast intent. And we also learn how to
create custom broadcast. We’ll also discuss
the security concerns, and how to get over it with
using local broadcasts, which allows you to send
broadcast within your app, instead system-wide
applications. Broadcast intents are intent
objects that are broadcast. Intents facilitates
communication between two components. So in addition to providing
a mechanism for launching application activities,
intents are also used as a way to broadcast system wide
messages to other components on the system. Broadcast intents
here in this mechanism is completely separate
from the intents used to start activities, which
is a full operation that shows what the user is
currently interacting with, and send to our start activity. It will only affect one
activity to accomplish the specific task. Whereas, a broadcast intent
is a background operation that the user is
normally not aware of, and is sent via sendBroadcast. By default, it can affect
multiple applications, which are registered
for receiving the intent. Broadcast receivers
are components that listens to what is going
on and allows you to register for the system or
application events, means they do
something in response to that message
that is broadcasted. An application listens for
specific incoming events sent by sendBroadcast, and
by registering a broadcast receiver. Broadcast intents
can be originated from the system when certain
events happen in the operating system. For example, a broadcast
announcing the screen has turned off, or
the battery is low, or picture was captured. Or can be originated by
any other application. You can even set your app
up to launch your music when you connect to the headphones. So it will run in the background
when it receives the message that headphones are connected,
and respond by launching your music application. Although broadcaster receivers
don’t display a user interface, they run in the background, even
when your activity is closed. So for example, your music
app, to post a notification to tell the user about an
upcoming event of headphones connected, they may create a
status bar notification to alert the user when a
broadcast event occurs. So broadcast receivers
are implemented by extending the Android
broadcast receiver class and overriding the
onReceive method, which is required to listen for
specific broadcast intents. When a broadcast intent
is received and delivered to onReceive, at this
point, the method has five seconds within which
to perform any necessary task before receiver is destroyed. It is important to note that a
broadcast receiver doesn’t need to be running all the time. In the event that the
matching intent is detected, the Android runtime
system will automatically start up the broadcast
receiver before calling the onReceive method. Like the system
completes a boot, or lose a connection
to a wireless network when Wi-Fi state changes. So you can specify
these actions as shown on the screen inside
intent filters of your app. Your app can broadcast
custom intent as well. That means they will be
defined by developers. Custom intent can contain data
that user gave from the screen. If you want that application
to broadcast a custom intent, we need to send one. To broadcast an intent to
all the interested broadcast receivers, there are two
major classes of broadcast. Either use normal
broadcast, which is sent with using
sendBroadcast method, or with order broadcasts,
which are sent with sendOrderBroadcast method. So after sending
the custom intent, you need to register
the receiver, which defines the action for
custom intent and intent filter for the receiver
inside androidmanifest.xml. SendBroadcast method
are completely asynchronous, that is, the
broadcast events or intents are received by
all the receivers in an asynchronous fashion. The receivers are in
an undefined order, often at the same time. It’s efficient, but receivers
cannot use results from others receivers. sendOrderBroadcast are delivered
to one receiver at a time. As each receiver
executes intent, it can propagate a result
to the next receiver, or it can completely
abort the broadcast so that it won’t be
passed to other receivers. The other receivers
can be controlled with the android.priority
attribute, of the matching intent filter. Receivers with the same priority
will run in an arbitrary order. If you want to allow
receivers to propagate results or abort the broadcast, you
must send an order broadcast using sendOrderBroadcast. Let’s now understand how this
whole mechanism of broadcast receivers can be implemented. In order to create the broadcast
receiver, these are the steps. A new class needs
to be created, which is a subclass of the
broadcast receiver class with the onReceive method
being implemented of this base class. And whenever the
event occurs, Android calls to onReceive method. An application listens for
a specific broadcast intents by registering a
broadcast receiver. There are two ways to register
it, either within the code, for example, within an activity,
or within a manifest file. To register the broadcast
receivers statically, you register it in
AndroidManifest.xml. The same effect can be achieved
by registering the broadcast receiver programmatically
or dynamically, using the register receive
method of the activity class together when an appropriate
configured intent with that object. So as you see on
the screen, this is a class which is extending
Android broadcast receiver class, like this. Here you can see the
onReceive method. So as you can see, this is
how the broadcast receivers are implemented, by extending
the Android broadcast receiver class and overriding
the onReceive method. So we have created a new class
named CustomReceiver, which is extending BroadcastReceiver. So after creating the
broadcast receivers, the next step we know
is to register it. Let’s give it a shot
with AndroidManifest. Here, receiver
entry must be added containing one or
more intent filters, each containing the action
string of the broadcast intent for which the
receiver is required to list the specific intent,
as you can see in the example. And remember, just like
any other component, we define this receiver element
inside application element. Alternatively, through
the static registration, you can also register
and unregister a receiver at one time via the context
class, Register receiver method either in onCreate, or in
onResume activity methods. And unregister receiver
methods either in onDestroy or in onPause activity methods. You can see the code snippet
on the bottom of the slide. And as you can see, the register
receiver method of the activity class together when it
appropriately configured intent for the object. Do not forget to unregister
dynamically registerReceiver by using unregister
receiver method. If you forget this,
the Android system reports a leaked
broadcast receiver error. For instance, if you
registered a receive in onResume method
of an activity, you should unregister it
inside onPause method. Broadcast receiver is very
important component of Android. Here is the list of a
few important system events, like the
action runtime changed, or when system booted
completed, and a lot more. So feel free to check
out this broadcast action from these links. We talked about implementing
onReceive method of base class, that is of
broadcast receivers class. If the event for which
the broadcast receiver has registered happens,
the onReceive method of the receiver is called
by the Android system. Here, this example shows
the onReceive method takes two arguments. The context. The context object
you can use to access additional information or to
start services, or activities. And second, the intent
object, with the action you can use to
register your receiver. Custom broadcast,
the action string which identifies the
broadcast event must be unique and typically uses the
application’s Java package names and text. For example, the
following broadcast intent including a
unique action string, and includes the package
name as a prefix. This action needs
to get described in the activity and the
broadcast receiver as well. You know that how we
have sent custom intents using sendBroadcast
method, which we have learned from previous slides. So here is how you are going
to attach the action using custom intents. And here is the
code to unregister your registered
broadcast receiver, by calling
unregisterReceiver method. Android incorporates
security features and works with developers and
device implementers to keep the Android
platform and ecosystem safe. And when we are working
with broadcast receivers, we need to consider a few
things in terms of security. So let’s understand
them one by one. Using broadcast in
Android applications sometimes introduce problems, as
receivers used with the context and can be received by
other applications too. So you must consider
cross-app boundaries. The intent namespace is global. Make sure that the intent action
names and other strings are written in a namespace
you own, or else, you make conflict with
other applications. When you use register
receiver method, any other application
may send broadcast to that register receiver. You can control who
can send broadcasts by using the permissions. And when you use send
broadcast, or send message, normally any other application
can receive this broadcast. But you can also control who
can receive such broadcast through permissions. This means access
permissions can be enforced by sender or the receiver. To enforce a permission
when sending, you supply a
permission argument. To send broadcast
only receivers who have been granted this
permission by requesting it with the user permission tag
in their manifest.xml file will be able to
receive the broadcast. This receiver will only
receive broadcast intents that have the required permission. And to ensure a
permission when receiving, you supply a
permission when registering your receiver, either when
calling registerReceiver or in receiver tag in
your AndroidManifest.xml. So the only
broadcasters who have been granted this permission
by using users permission tag in AndroidManifest.xml
will be able to send an intent
to the receiver. So none of the
security issues exist when using local
broadcast manager class since it used to implement
secure communication mechanism within app components. Use a local broadcast manager
to broadcast and to registered the receiver, as provides
you a secure way since there are no cross-app communication. This example at the
bottom of the slide shows how to use local broadcast
manager to send and receive broadcasts. It really is just as easy
as creating an intent object and calling sendBroadcast, using
that intent object, and same for registerReceiver as well. In the onResume,
intent filter is created to register receiver
for designated action. Intent filter constructor takes
the action as a parameter. So it can receive the
broadcast with this action. This code shows about
registering receiver for the intent filter
by using local broadcast manager and registerReceiver. After that, receiver
has the ability to get broadcasts
with the action named ACTION_CUSTOM_BROADCAST. To learn more, be sure to
check out the reference links about broadcast receivers. Our next practical will help
you practice on implementation of broadcast receivers. Keep learning, and
thanks for watching.


  1. Post
  2. Post
    K. Arslan

    she may know too much but she is not a good instructor, sorry but truth.. Lalat is perfect, it would be better if he explained all course..

Leave a Reply

Your email address will not be published. Required fields are marked *