Broadcast Intents and Receivers2:27 with Ben Jakuben
So far with Intents, we've focused on sending information to a specific target. In some cases we want to broadcast information to a wide audience of subscribers, and for that we need Broadcast Intents and Receivers.
[MUSIC] 0:00 We're moving right along in terms of the different ways we can share data in 0:04 Android. 0:08 Let's revisit our table from earlier that highlighted some of the most common ways. 0:09 We've covered all these at the top that have to do with intent. 0:13 The next thing we want to learn is how to combine intents with broadcast receivers 0:16 to enable an even wider mechanism of broadcasting information 0:20 throughout an entire Android system. 0:24 Think of something that might be useful for every app to know. 0:27 How about when the battery has dropped below a certain level? 0:30 A well designed app could listen for that kind of broadcast and adjust its usage. 0:33 Maybe it's trying to download a big file that is draining the battery. 0:37 Responsible developers, like us, will pause the download or 0:41 at least notify the user that they may want to cancel it. 0:44 Speaking of downloads, what about if the network connection drops? 0:48 If that happens, we'd like our apps to gracefully pause a download and wait for 0:52 the network to connect again. 0:55 Or if the user is in the app, 0:57 let them know that the network is no longer available. 0:59 There are quite a few system events like this and of course we can broadcast and 1:02 subscribe to custom events too. 1:05 A great example is to schedule an alarm to post a notification about a future event. 1:08 We can deliver that alarm to a broadcast receiver, in an app, which means that we 1:12 don't need to worry about keeping the app running until that time is triggered. 1:16 Before we start to build a broadcast receiver, I'd like to spend a minute 1:21 talking about some important things to remember about them. 1:24 As I just said broadcast receivers are used to respond to changes in the state 1:27 of a device, as well as events from other applications. 1:31 They utilize intents but a different kind known as a Broadcast Intent. 1:34 There is no way for a Broadcast Receiver to see or 1:39 capture Intents used with start activity. 1:41 And Broadcast Intents will never find or start an activity. 1:44 Another thing to remember about Broadcast Receivers, is that they only exist for 1:49 a short time frame, 1:52 specifically the duration of a special method called onReceive. 1:53 Because of this short time span, 1:58 they can't do long running work like you'd see in a thread or service. 1:59 On that same note, they can't interact with the user interface directly. 2:03 So we can't show a dialog toast or snack bar or 2:06 make any other changes to the UI, at least not directly. 2:10 What that means is if there is more work to be done it must be kicked off 2:14 as a separate service. 2:17 All right, that's enough buildup. 2:20 Let's get to some code. 2:21 Let's take a look at how this works 2:22 by monitoring the network connection of our device. 2:24
You need to sign up for Treehouse in order to download course files.Sign up