1 00:00:00,690 --> 00:00:04,870 Back in our music machine app, let's talk about how to set up a broadcast receiver. 2 00:00:04,870 --> 00:00:07,350 We need to create a broadcast receiver class and 3 00:00:07,350 --> 00:00:08,870 we need to register it in the manifest. 4 00:00:08,870 --> 00:00:11,920 We need the class need to register it, so let's create the class first. 5 00:00:13,220 --> 00:00:17,600 So let's right click on our package, select new Java class and 6 00:00:17,600 --> 00:00:24,900 let's name it NetworkConnectionReceiver. 7 00:00:24,900 --> 00:00:27,240 It needs to extend the broadcast receiver base class. 8 00:00:27,240 --> 00:00:29,960 So let's add extends BroadcastReceiver. 9 00:00:31,440 --> 00:00:35,430 So move the cursor and hit Alt+Enter to implement the methods and 10 00:00:35,430 --> 00:00:37,230 we want to implement the on receive method. 11 00:00:38,800 --> 00:00:42,430 For now let's just log the intents action in here, just to make sure it's working. 12 00:00:42,430 --> 00:00:44,465 We'll act on that later. 13 00:00:44,465 --> 00:00:47,688 Log.i and for a tag let's just do a 14 00:00:47,688 --> 00:00:52,331 NetworkConnectionReceiver.class.getSimple- Name and 15 00:00:52,331 --> 00:00:57,690 then we can mark intent.getAction. 16 00:00:57,690 --> 00:00:59,940 So now we can register this in the manifest. 17 00:00:59,940 --> 00:01:02,990 When we do it this way we refer to it as a manifest receiver. 18 00:01:02,990 --> 00:01:05,930 We can actually register these in code too which we'll take a look at shortly. 19 00:01:07,080 --> 00:01:10,230 Let's take a quick look at the documentation for manifest receivers. 20 00:01:10,230 --> 00:01:13,190 As expected this allows an app to receive actions or 21 00:01:13,190 --> 00:01:15,369 data broadcast by other applications. 22 00:01:16,380 --> 00:01:19,320 This last part here is especially important even if it's not 23 00:01:19,320 --> 00:01:21,040 currently running. 24 00:01:21,040 --> 00:01:25,160 This is what is so cool about manifest receivers, by registering them this way 25 00:01:25,160 --> 00:01:29,180 our app can respond to a system wide broadcast even if the app isn't running. 26 00:01:29,180 --> 00:01:30,690 It just needs to be installed on the system. 27 00:01:32,330 --> 00:01:33,230 Right let's try it. 28 00:01:33,230 --> 00:01:35,640 Let's open up the manifest. 29 00:01:35,640 --> 00:01:40,000 And we'll add it here inside the application tags, add a new line and 30 00:01:40,000 --> 00:01:41,080 type receiver. 31 00:01:42,410 --> 00:01:45,590 Okay, and look at that in auto complete we already see our custom class. 32 00:01:45,590 --> 00:01:48,580 So just hit enter and close the receiver tag. 33 00:01:50,520 --> 00:01:53,300 Next, we need an intent filter because this is just another 34 00:01:53,300 --> 00:01:55,870 way to use intense and filters to share information. 35 00:01:58,420 --> 00:01:59,990 Now, what kind of action do we need? 36 00:02:01,670 --> 00:02:03,010 I've already researched and used this. 37 00:02:03,010 --> 00:02:04,420 But let's try it from scratch. 38 00:02:04,420 --> 00:02:06,920 Let's Google something like Android. 39 00:02:06,920 --> 00:02:14,560 How to detect network connectivity change. 40 00:02:14,560 --> 00:02:16,640 Hm, this first link certainly looks promising. 41 00:02:18,790 --> 00:02:21,020 Over here in the nave we see something called monitor for 42 00:02:21,020 --> 00:02:23,000 changes in connectivity. 43 00:02:23,000 --> 00:02:24,210 And look here it is. 44 00:02:24,210 --> 00:02:25,840 Here's the exact action we need. 45 00:02:27,060 --> 00:02:29,780 There's some good advice in here too about being careful about 46 00:02:29,780 --> 00:02:31,490 how often we monitor this. 47 00:02:31,490 --> 00:02:34,060 Actively monitoring something too much can drain the battery. 48 00:02:35,220 --> 00:02:36,800 Let's follow this link here at the bottom for 49 00:02:36,800 --> 00:02:39,199 a moment manipulating broadcast receivers on demand. 50 00:02:40,940 --> 00:02:43,310 Reading through here, we see some additional advice. 51 00:02:43,310 --> 00:02:46,430 A side effect of this approach is that your app will wake the device 52 00:02:46,430 --> 00:02:48,500 each time any of these receivers is triggered, 53 00:02:48,500 --> 00:02:51,410 potentially much more frequently than required. 54 00:02:51,410 --> 00:02:55,530 A better approach is to disable or enable the broadcast receivers at runtime. 55 00:02:55,530 --> 00:02:58,450 That way you can use the receiver you declared in the manifest 56 00:02:58,450 --> 00:03:02,190 as passive alarms that are triggered by system events only when necessary. 57 00:03:03,260 --> 00:03:04,240 Interesting. 58 00:03:04,240 --> 00:03:07,220 We don't want to wake the device all the time, we'll come back to this, but for 59 00:03:07,220 --> 00:03:09,620 now let's continue with our manifest example. 60 00:03:10,990 --> 00:03:12,558 Right, so let's add that action we just saw, 61 00:03:12,558 --> 00:03:18,745 00:03:27,010 y_change.<="" div=""> And now we have the two required components. 63 00:03:27,010 --> 00:03:27,510 Let's try it out. 64 00:03:27,510 --> 00:03:32,980 You can run the app and 65 00:03:32,980 --> 00:03:37,100 then while it's running, lets drag down the notification drawer. 66 00:03:37,100 --> 00:03:40,000 In here from the quick settings turn on airplane mode. 67 00:03:40,000 --> 00:03:41,080 That'll change our connection. 68 00:03:42,380 --> 00:03:45,040 And look here it is a log chart, it shows the intent action. 69 00:03:46,040 --> 00:03:49,190 Remember this is also receiving broadcasts when our app is closed. 70 00:03:50,330 --> 00:03:52,110 Let's go back to our app and 71 00:03:52,110 --> 00:03:56,560 let's back out of it and let's change the network status again. 72 00:03:57,715 --> 00:04:01,140 I'll turn airplane mode off and we should see in the log, 73 00:04:03,130 --> 00:04:05,980 there it is, another connection change. 74 00:04:07,300 --> 00:04:08,240 Okay, let's break for a moment. 75 00:04:08,240 --> 00:04:11,470 And then we'll look at how to be more responsible by only monitoring when 76 00:04:11,470 --> 00:04:12,340 our app is active.