1 00:00:00,000 --> 00:00:04,875 [MUSIC] 2 00:00:04,875 --> 00:00:08,318 We're moving right along in terms of the different ways we can share data in 3 00:00:08,318 --> 00:00:09,400 Android. 4 00:00:09,400 --> 00:00:13,530 Let's revisit our table from earlier that highlighted some of the most common ways. 5 00:00:13,530 --> 00:00:16,790 We've covered all these at the top that have to do with intent. 6 00:00:16,790 --> 00:00:20,680 The next thing we want to learn is how to combine intents with broadcast receivers 7 00:00:20,680 --> 00:00:24,280 to enable an even wider mechanism of broadcasting information 8 00:00:24,280 --> 00:00:25,760 throughout an entire Android system. 9 00:00:27,240 --> 00:00:30,450 Think of something that might be useful for every app to know. 10 00:00:30,450 --> 00:00:33,340 How about when the battery has dropped below a certain level? 11 00:00:33,340 --> 00:00:37,890 A well designed app could listen for that kind of broadcast and adjust its usage. 12 00:00:37,890 --> 00:00:41,060 Maybe it's trying to download a big file that is draining the battery. 13 00:00:41,060 --> 00:00:44,930 Responsible developers, like us, will pause the download or 14 00:00:44,930 --> 00:00:47,490 at least notify the user that they may want to cancel it. 15 00:00:48,830 --> 00:00:52,230 Speaking of downloads, what about if the network connection drops? 16 00:00:52,230 --> 00:00:55,750 If that happens, we'd like our apps to gracefully pause a download and wait for 17 00:00:55,750 --> 00:00:57,350 the network to connect again. 18 00:00:57,350 --> 00:00:59,070 Or if the user is in the app, 19 00:00:59,070 --> 00:01:01,059 let them know that the network is no longer available. 20 00:01:02,450 --> 00:01:05,990 There are quite a few system events like this and of course we can broadcast and 21 00:01:05,990 --> 00:01:08,240 subscribe to custom events too. 22 00:01:08,240 --> 00:01:12,680 A great example is to schedule an alarm to post a notification about a future event. 23 00:01:12,680 --> 00:01:16,650 We can deliver that alarm to a broadcast receiver, in an app, which means that we 24 00:01:16,650 --> 00:01:19,890 don't need to worry about keeping the app running until that time is triggered. 25 00:01:21,030 --> 00:01:24,410 Before we start to build a broadcast receiver, I'd like to spend a minute 26 00:01:24,410 --> 00:01:26,440 talking about some important things to remember about them. 27 00:01:27,750 --> 00:01:31,490 As I just said broadcast receivers are used to respond to changes in the state 28 00:01:31,490 --> 00:01:34,980 of a device, as well as events from other applications. 29 00:01:34,980 --> 00:01:39,510 They utilize intents but a different kind known as a Broadcast Intent. 30 00:01:39,510 --> 00:01:41,970 There is no way for a Broadcast Receiver to see or 31 00:01:41,970 --> 00:01:44,920 capture Intents used with start activity. 32 00:01:44,920 --> 00:01:47,680 And Broadcast Intents will never find or start an activity. 33 00:01:49,260 --> 00:01:52,690 Another thing to remember about Broadcast Receivers, is that they only exist for 34 00:01:52,690 --> 00:01:53,900 a short time frame, 35 00:01:53,900 --> 00:01:58,030 specifically the duration of a special method called onReceive. 36 00:01:58,030 --> 00:01:59,750 Because of this short time span, 37 00:01:59,750 --> 00:02:03,440 they can't do long running work like you'd see in a thread or service. 38 00:02:03,440 --> 00:02:06,640 On that same note, they can't interact with the user interface directly. 39 00:02:06,640 --> 00:02:10,180 So we can't show a dialog toast or snack bar or 40 00:02:10,180 --> 00:02:13,410 make any other changes to the UI, at least not directly. 41 00:02:14,930 --> 00:02:17,940 What that means is if there is more work to be done it must be kicked off 42 00:02:17,940 --> 00:02:18,840 as a separate service. 43 00:02:20,080 --> 00:02:21,260 All right, that's enough buildup. 44 00:02:21,260 --> 00:02:22,860 Let's get to some code. 45 00:02:22,860 --> 00:02:24,180 Let's take a look at how this works 46 00:02:24,180 --> 00:02:26,220 by monitoring the network connection of our device.