This video explains Android's Broadcast and Broadcast Receiver system, which allows apps to communicate system-wide events, either from the Android system itself or other apps, enabling reactive behavior without necessarily opening user interfaces.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
hey guys and welcome back to a new video
on a new episode of Android basics in
this video I will talk about broadcasts
and broadcast receivers
coming straight to the point what is the
broadcast in Android it is comparable to
what I covered in the last video when
when I talked about intents just that we
don't send this intent this intention we
have to only one app but to potentially
many apps and also the apps that receive
such a broadcast will rather silently
handle it and not open an activity for
example so in short broadcasts are just
system-wide events your app can actually
consume and receive this can either be
sent by the Android system itself or
also by your apps or also with other
apps and your app can then register a
so-called broadcast receiver which gets
triggered when such a broadcast is
received so an example for such a
broadcast would be when the Android
device is fully booted up what the
Android system will then do is it will
send a broadcast to all apps that are
registered to receive this specific boot
completed broadcast so that your app
could react to that when the device is
booted up to for example start
synchronizing some data or whatever you
you want to do another example would be
if you for example have a music player
app and then your Android device gets an
incoming call then your phone calling
app could send a broadcast so other apps
can react to to that fact that the user
is currently getting a call and your
music player app could for example pause
the playback so that the user can fully
accept the call and doesn't hear any
music and before we actually get to
sending broadcasts with our own app I
want to show you how we can react to
broadcast the Android system sense and
the example I'll just show you is simply
reacting to when airplane mode is
changed so when the user toggles off
airplane mode we want to react to that
in our app and when it's turned on again
we also want to know that and luckily
the NR system will send a broadcast in
exactly that case when the user toggles
airplane mode because that is obviously
something that happens outside of the
influence of our app so our app kind of
needs to receive that event that comes
from the system and to receive that
again we use a so-called broadcast
receiver which is nothing else than just
the class so here in our word package we
can create this airplane mode receiver
for example
Anders needs to inherit from broadcast
receiver every broadcast receiver then
needs an on-received function which is
then triggered when the broadcast is
fired and here let's actually rename
these variables there's one two context
and this one to intent here you can then
have an if check for example to check
the intent that gets attached to this
broadcast so that is always the case if
you send the broadcast you again need to
Define its intention in form of an
intent you can attach data to it you can
attach an action to it but the whole
difference now to the last video where
we just mentioned intense and formal
form of starting activities or starting
other Android components the difference
now is that this intent now gets
delivered to many apps such as our app
here for example when the anode system
sends the intent when the airplane mode
was changed so we can now check if we're
actually dealing with the intent we
think we're dealing with by checking if
the action
is actually equal to intent.action
airplane mode changed because that will
be the action that the anode system will
attach when this happens and now to
actually find out if the user enabled or
disabled airplane mode we can have a
variable is turned on and that is equal
to settings that is just something you
need to find out by Googling how you um
now get the actual extra or the actual
data all of that intent in this case
it's not even inside of that intent for
some reason but we can retrieve whether
the user has actually airplane mode
enabled or not and we get that with
settings Global and Jet integer we pass
a Content resolver we can get that from
our context
context.content resolver the name of
that setting is settings dot Global dot
airplane mode on and if that's not equal
to zero that means it's on if it is zero
it's turned off so we can then simply
have a print line statement or so saying
um is airplane mode enabled and then we
print is turned on and right now this
won't do anything because we didn't
actively register this receiver as such
so what we want to do is we want to go
to main activity and here on create we
say register receiver that is how we
register a broadcast receiver inside of
our app so that it is active and it will
receive such events so register receiver
we say um let's actually have a variable
for that private variable
airplane mode receiver is a new airplane
mode receiver
we pass that here
and we also need to specify an intent
filter which I covered in the last video
so we just specify what kinds of intents
our receivers should be able to receive
in this case that is just an intent
filter and here we can pass the action
of the intense one to be able to receive
so simply intend that action airplane
mode changed and it's really important
here that you also unregister this
receiver when it's not needed anymore in
the case of an activity that would be in
on Destroy so we overwrite undestroy
when the activity is destroyed we want
to unregister this airplane mode
receiver again never Now launch this on
our device here's our app let's open a
locket to see our print line statements
um so is airplane mode enabled is
airplane mode enabled let's have that
active check our device and if we now
toggle airplane mode
then we actually get the log is airplane
mode enabled now it's true because I
just enabled it if we disable it we can
get a log this time with force and now
what I didn't mention yet is that the
type of broadcast receiver we declared
here is a so-called Dynamic broadcast
receiver and that simply means we
dynamically declare when we need it here
with this register receiver function and
we also dynamically unregister it when
our app does not need it anymore however
it should be obvious that this approach
will only work as long as our app is
actually active because if we close our
app then obviously there is no receiver
anymore since we unregistered it before
but taking a look at the example I
mentioned before that the Android system
sends a broadcast when the device is
fully booted up at that point our app
isn't even launched so how would it
receive that broadcast and for these
things that is the other side of the
metal those are called Static receivers
that we need to receive inside the
broadcasts so static receivers will also
be triggered if our app isn't even
active or launched however there are a
lot of restrictions with such static
receivers so there are only a few
exceptions for broadcast actions such as
the boot complete one that are allowed
to be declared as static receivers the
reason is simply that it increases
battery usage if you have a static
receiver because all apps that declare
to receive a specific broadcast such as
the boot completed one always need to
have that little kind of receiver and
service in the background that checks or
that receives such events and that is
why Android only allows that for these
few exceptions of broadcasts or if a
broadcast is specifically targeted
towards a specific app so for example if
you specify the exact package name of
the app again you where you want to send
the broadcast to but if you actually
have a broadcast that is not covered by
these exceptions then to declare such a
static receiver you can do this in the
Manifest file so not inside of nativity
here you would then just go below
activity have a receiver and you would
specify the airplane mode receiver and
in here you can then again specify your
entire can filter just like we did in
the last video for the activity in this
case it's just a broadcast receiver and
you specify the tabs of intent and the
action you actually want to receive with
that receiver but just doing this for
the airplane mode won't work because
that will only work with
um Dynamic receivers so if you have
haven't broadcast that fits under these
restrictions you can only do it inside
of your app and your app can only
receive this broadcast if it is actually
active but if you have such a static
receiver then you don't need this
register receiver and unregister
receiver function because it just counts
for your whole app and it will also
trigger when the app is actually closed
but let's also take a look at how we can
send broadcasts from our app to another
app and what I'll take a look at this
different app so there's no different
code base I just prepared a little
column with a centered button and when
we now click this button we want to send
the broadcast to the other app that I
showed you previously and in Android
this is super simple so if you are
inside of an activity you can just say
send broadcast that takes makes an
intent which you need to attach here
and here we can just attach a custom
action for this custom intent for
example we say
Test action for example and again you
could of course pass some extras to this
intent as a data you could specify an
exact package name if you actually want
to use the static receivers obviously
it's only sent to that specific app but
let's say we want to send this broadcast
to all apps that want to receive this
test action then we're already good with
this piece of code we can move back to
our other app to now Define Our receiver
to also receive these Test action
intents so let's just create a different
receiver for that in our root package
is again a broadcast receiver
we overwrite on receive and here we can
that action is equal to test action
then we print received test intent we
then go back to main activity to
register this test receiver we can
duplicate this line
test receiver and this would be just a
test receiver in this case
then in here or actually let's duplicate
this or copy it
have our test receiver
also register that this time the intent
filter doesn't specify this upload mode
action instead it specifies Test action
and then down here on Destroy we again
unregister that receiver
test receiver and we can launch this so
this is now the app that receives the
broadcast we want to send from our other
app that we just coded if we take a look
here in lockhead and we search for Test
action or what do they call it receive
test intent
test intent and I Now launch the other
app here so this one where I
um called the send broadcast function
take a look at our emulator
then we have the send broadcast button
if I now click this then you can see our
other app receives this since it's a
test and hand it actually contained This
Test action and this is now a reliable
way for two apps to communicate with
each other but again since that is a
dynamic receiver this will only be
reliable if your other apps of the
receiving app is actually open if it's
closed it will only receive these
broadcasts if the other apps so the app
that sends the broadcast specifically
mentions that it wants to send the
broadcast to your specific app with that
specific package name so I hope you
learned something new if you did then
definitely leave a subscribe here if you
haven't already because then you will
get two more videos every single week
about Android development so you can
become an industry ready Auto developer
step by step have an amazing rest of
your week see you back in the next video
bye bye foreign
foreign [Music]
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.