0:01 hey guys and welcome back to a new video
0:03 on a new episode of Android basics in
0:05 this video I will talk about broadcasts
0:08 and broadcast receivers
0:10 coming straight to the point what is the
0:13 broadcast in Android it is comparable to
0:15 what I covered in the last video when
0:18 when I talked about intents just that we
0:20 don't send this intent this intention we
0:23 have to only one app but to potentially
0:26 many apps and also the apps that receive
0:28 such a broadcast will rather silently
0:30 handle it and not open an activity for
0:32 example so in short broadcasts are just
0:35 system-wide events your app can actually
0:37 consume and receive this can either be
0:40 sent by the Android system itself or
0:41 also by your apps or also with other
0:43 apps and your app can then register a
0:46 so-called broadcast receiver which gets
0:47 triggered when such a broadcast is
0:49 received so an example for such a
0:51 broadcast would be when the Android
0:53 device is fully booted up what the
0:55 Android system will then do is it will
0:58 send a broadcast to all apps that are
1:00 registered to receive this specific boot
1:02 completed broadcast so that your app
1:05 could react to that when the device is
1:06 booted up to for example start
1:08 synchronizing some data or whatever you
1:10 you want to do another example would be
1:12 if you for example have a music player
1:15 app and then your Android device gets an
1:17 incoming call then your phone calling
1:20 app could send a broadcast so other apps
1:22 can react to to that fact that the user
1:24 is currently getting a call and your
1:26 music player app could for example pause
1:28 the playback so that the user can fully
1:30 accept the call and doesn't hear any
1:33 music and before we actually get to
1:35 sending broadcasts with our own app I
1:37 want to show you how we can react to
1:39 broadcast the Android system sense and
1:41 the example I'll just show you is simply
1:43 reacting to when airplane mode is
1:45 changed so when the user toggles off
1:47 airplane mode we want to react to that
1:49 in our app and when it's turned on again
1:51 we also want to know that and luckily
1:53 the NR system will send a broadcast in
1:55 exactly that case when the user toggles
1:56 airplane mode because that is obviously
1:58 something that happens outside of the
2:00 influence of our app so our app kind of
2:02 needs to receive that event that comes
2:04 from the system and to receive that
2:06 again we use a so-called broadcast
2:08 receiver which is nothing else than just
2:11 the class so here in our word package we
2:15 can create this airplane mode receiver
2:16 for example
2:18 Anders needs to inherit from broadcast
2:21 receiver every broadcast receiver then
2:24 needs an on-received function which is
2:25 then triggered when the broadcast is
2:27 fired and here let's actually rename
2:29 these variables there's one two context
2:31 and this one to intent here you can then
2:34 have an if check for example to check
2:36 the intent that gets attached to this
2:38 broadcast so that is always the case if
2:40 you send the broadcast you again need to
2:42 Define its intention in form of an
2:44 intent you can attach data to it you can
2:46 attach an action to it but the whole
2:48 difference now to the last video where
2:49 we just mentioned intense and formal
2:51 form of starting activities or starting
2:53 other Android components the difference
2:54 now is that this intent now gets
2:57 delivered to many apps such as our app
2:58 here for example when the anode system
3:01 sends the intent when the airplane mode
3:03 was changed so we can now check if we're
3:04 actually dealing with the intent we
3:07 think we're dealing with by checking if
3:08 the action
3:10 is actually equal to intent.action
3:12 airplane mode changed because that will
3:14 be the action that the anode system will
3:16 attach when this happens and now to
3:19 actually find out if the user enabled or
3:21 disabled airplane mode we can have a
3:23 variable is turned on and that is equal
3:25 to settings that is just something you
3:28 need to find out by Googling how you um
3:30 now get the actual extra or the actual
3:32 data all of that intent in this case
3:34 it's not even inside of that intent for
3:36 some reason but we can retrieve whether
3:38 the user has actually airplane mode
3:39 enabled or not and we get that with
3:43 settings Global and Jet integer we pass
3:45 a Content resolver we can get that from
3:47 our context
3:49 context.content resolver the name of
3:53 that setting is settings dot Global dot
3:56 airplane mode on and if that's not equal
3:59 to zero that means it's on if it is zero
4:00 it's turned off so we can then simply
4:04 have a print line statement or so saying
4:07 um is airplane mode enabled and then we
4:10 print is turned on and right now this
4:12 won't do anything because we didn't
4:14 actively register this receiver as such
4:16 so what we want to do is we want to go
4:19 to main activity and here on create we
4:21 say register receiver that is how we
4:23 register a broadcast receiver inside of
4:25 our app so that it is active and it will
4:28 receive such events so register receiver
4:30 we say um let's actually have a variable
4:33 for that private variable
4:36 airplane mode receiver is a new airplane
4:38 mode receiver
4:40 we pass that here
4:42 and we also need to specify an intent
4:44 filter which I covered in the last video
4:47 so we just specify what kinds of intents
4:49 our receivers should be able to receive
4:51 in this case that is just an intent
4:53 filter and here we can pass the action
4:55 of the intense one to be able to receive
4:57 so simply intend that action airplane
4:59 mode changed and it's really important
5:01 here that you also unregister this
5:03 receiver when it's not needed anymore in
5:05 the case of an activity that would be in
5:09 on Destroy so we overwrite undestroy
5:10 when the activity is destroyed we want
5:13 to unregister this airplane mode
5:15 receiver again never Now launch this on
5:17 our device here's our app let's open a
5:21 locket to see our print line statements
5:23 um so is airplane mode enabled is
5:27 airplane mode enabled let's have that
5:29 active check our device and if we now
5:32 toggle airplane mode
5:34 then we actually get the log is airplane
5:36 mode enabled now it's true because I
5:39 just enabled it if we disable it we can
5:41 get a log this time with force and now
5:44 what I didn't mention yet is that the
5:46 type of broadcast receiver we declared
5:48 here is a so-called Dynamic broadcast
5:50 receiver and that simply means we
5:52 dynamically declare when we need it here
5:54 with this register receiver function and
5:57 we also dynamically unregister it when
5:58 our app does not need it anymore however
6:00 it should be obvious that this approach
6:03 will only work as long as our app is
6:04 actually active because if we close our
6:07 app then obviously there is no receiver
6:08 anymore since we unregistered it before
6:10 but taking a look at the example I
6:12 mentioned before that the Android system
6:14 sends a broadcast when the device is
6:16 fully booted up at that point our app
6:19 isn't even launched so how would it
6:20 receive that broadcast and for these
6:22 things that is the other side of the
6:24 metal those are called Static receivers
6:25 that we need to receive inside the
6:28 broadcasts so static receivers will also
6:30 be triggered if our app isn't even
6:32 active or launched however there are a
6:34 lot of restrictions with such static
6:36 receivers so there are only a few
6:39 exceptions for broadcast actions such as
6:41 the boot complete one that are allowed
6:43 to be declared as static receivers the
6:45 reason is simply that it increases
6:47 battery usage if you have a static
6:49 receiver because all apps that declare
6:51 to receive a specific broadcast such as
6:53 the boot completed one always need to
6:55 have that little kind of receiver and
6:57 service in the background that checks or
6:59 that receives such events and that is
7:01 why Android only allows that for these
7:04 few exceptions of broadcasts or if a
7:06 broadcast is specifically targeted
7:08 towards a specific app so for example if
7:10 you specify the exact package name of
7:12 the app again you where you want to send
7:13 the broadcast to but if you actually
7:15 have a broadcast that is not covered by
7:18 these exceptions then to declare such a
7:20 static receiver you can do this in the
7:22 Manifest file so not inside of nativity
7:25 here you would then just go below
7:27 activity have a receiver and you would
7:30 specify the airplane mode receiver and
7:31 in here you can then again specify your
7:34 entire can filter just like we did in
7:36 the last video for the activity in this
7:37 case it's just a broadcast receiver and
7:39 you specify the tabs of intent and the
7:41 action you actually want to receive with
7:43 that receiver but just doing this for
7:45 the airplane mode won't work because
7:48 that will only work with
7:51 um Dynamic receivers so if you have
7:53 haven't broadcast that fits under these
7:55 restrictions you can only do it inside
7:56 of your app and your app can only
7:58 receive this broadcast if it is actually
7:59 active but if you have such a static
8:01 receiver then you don't need this
8:03 register receiver and unregister
8:05 receiver function because it just counts
8:07 for your whole app and it will also
8:08 trigger when the app is actually closed
8:11 but let's also take a look at how we can
8:13 send broadcasts from our app to another
8:14 app and what I'll take a look at this
8:16 different app so there's no different
8:17 code base I just prepared a little
8:20 column with a centered button and when
8:22 we now click this button we want to send
8:23 the broadcast to the other app that I
8:25 showed you previously and in Android
8:27 this is super simple so if you are
8:30 inside of an activity you can just say
8:32 send broadcast that takes makes an
8:34 intent which you need to attach here
8:36 and here we can just attach a custom
8:38 action for this custom intent for
8:40 example we say
8:42 Test action for example and again you
8:44 could of course pass some extras to this
8:46 intent as a data you could specify an
8:48 exact package name if you actually want
8:50 to use the static receivers obviously
8:52 it's only sent to that specific app but
8:54 let's say we want to send this broadcast
8:56 to all apps that want to receive this
8:58 test action then we're already good with
9:01 this piece of code we can move back to
9:03 our other app to now Define Our receiver
9:06 to also receive these Test action
9:08 intents so let's just create a different
9:10 receiver for that in our root package
9:17 is again a broadcast receiver
9:19 we overwrite on receive and here we can
9:28 that action is equal to test action
9:32 then we print received test intent we
9:34 then go back to main activity to
9:36 register this test receiver we can
9:37 duplicate this line
9:40 test receiver and this would be just a
9:43 test receiver in this case
9:45 then in here or actually let's duplicate
9:47 this or copy it
9:49 have our test receiver
9:52 also register that this time the intent
9:54 filter doesn't specify this upload mode
9:57 action instead it specifies Test action
9:59 and then down here on Destroy we again
10:01 unregister that receiver
10:05 test receiver and we can launch this so
10:07 this is now the app that receives the
10:08 broadcast we want to send from our other
10:10 app that we just coded if we take a look
10:14 here in lockhead and we search for Test
10:16 action or what do they call it receive
10:18 test intent
10:21 test intent and I Now launch the other
10:24 app here so this one where I
10:26 um called the send broadcast function
10:29 take a look at our emulator
10:31 then we have the send broadcast button
10:34 if I now click this then you can see our
10:36 other app receives this since it's a
10:38 test and hand it actually contained This
10:40 Test action and this is now a reliable
10:42 way for two apps to communicate with
10:44 each other but again since that is a
10:45 dynamic receiver this will only be
10:47 reliable if your other apps of the
10:49 receiving app is actually open if it's
10:51 closed it will only receive these
10:54 broadcasts if the other apps so the app
10:55 that sends the broadcast specifically
10:57 mentions that it wants to send the
10:59 broadcast to your specific app with that
11:01 specific package name so I hope you
11:03 learned something new if you did then
11:05 definitely leave a subscribe here if you
11:06 haven't already because then you will
11:08 get two more videos every single week
11:09 about Android development so you can
11:11 become an industry ready Auto developer
11:13 step by step have an amazing rest of
11:15 your week see you back in the next video
11:16 bye bye foreign
11:19 foreign [Music]