Registering in the Manifest4:13 with Ben Jakuben
Broadcast Receivers must be registered, either in the Manifest or in code. By registering in the Manifest we can ensure that our receiver will receive messages even if our app is not currently running.
Back in our music machine app, let's talk about how to set up a broadcast receiver. 0:00 We need to create a broadcast receiver class and 0:04 we need to register it in the manifest. 0:07 We need the class need to register it, so let's create the class first. 0:08 So let's right click on our package, select new Java class and 0:13 let's name it NetworkConnectionReceiver. 0:17 It needs to extend the broadcast receiver base class. 0:24 So let's add extends BroadcastReceiver. 0:27 So move the cursor and hit Alt+Enter to implement the methods and 0:31 we want to implement the on receive method. 0:35 For now let's just log the intents action in here, just to make sure it's working. 0:38 We'll act on that later. 0:42 Log.i and for a tag let's just do a 0:44 NetworkConnectionReceiver.class.getSimple- Name and 0:47 then we can mark intent.getAction. 0:52 So now we can register this in the manifest. 0:57 When we do it this way we refer to it as a manifest receiver. 0:59 We can actually register these in code too which we'll take a look at shortly. 1:02 Let's take a quick look at the documentation for manifest receivers. 1:07 As expected this allows an app to receive actions or 1:10 data broadcast by other applications. 1:13 This last part here is especially important even if it's not 1:16 currently running. 1:19 This is what is so cool about manifest receivers, by registering them this way 1:21 our app can respond to a system wide broadcast even if the app isn't running. 1:25 It just needs to be installed on the system. 1:29 Right let's try it. 1:32 Let's open up the manifest. 1:33 And we'll add it here inside the application tags, add a new line and 1:35 type receiver. 1:40 Okay, and look at that in auto complete we already see our custom class. 1:42 So just hit enter and close the receiver tag. 1:45 Next, we need an intent filter because this is just another 1:50 way to use intense and filters to share information. 1:53 Now, what kind of action do we need? 1:58 I've already researched and used this. 2:01 But let's try it from scratch. 2:03 Let's Google something like Android. 2:04 How to detect network connectivity change. 2:06 Hm, this first link certainly looks promising. 2:14 Over here in the nave we see something called monitor for 2:18 changes in connectivity. 2:21 And look here it is. 2:23 Here's the exact action we need. 2:24 There's some good advice in here too about being careful about 2:27 how often we monitor this. 2:29 Actively monitoring something too much can drain the battery. 2:31 Let's follow this link here at the bottom for 2:35 a moment manipulating broadcast receivers on demand. 2:36 Reading through here, we see some additional advice. 2:40 A side effect of this approach is that your app will wake the device 2:43 each time any of these receivers is triggered, 2:46 potentially much more frequently than required. 2:48 A better approach is to disable or enable the broadcast receivers at runtime. 2:51 That way you can use the receiver you declared in the manifest 2:55 as passive alarms that are triggered by system events only when necessary. 2:58 Interesting. 3:03 We don't want to wake the device all the time, we'll come back to this, but for 3:04 now let's continue with our manifest example. 3:07 Right, so let's add that action we just saw, 3:10 <action android:name"android.net.conn.connectivit- 3:12 y_change.<="" div=""></action> And now we have the two required components. 3:18 Let's try it out. 3:27 You can run the app and 3:27 then while it's running, lets drag down the notification drawer. 3:32 In here from the quick settings turn on airplane mode. 3:37 That'll change our connection. 3:40 And look here it is a log chart, it shows the intent action. 3:42 Remember this is also receiving broadcasts when our app is closed. 3:46 Let's go back to our app and 3:50 let's back out of it and let's change the network status again. 3:52 I'll turn airplane mode off and we should see in the log, 3:57 there it is, another connection change. 4:03 Okay, let's break for a moment. 4:07 And then we'll look at how to be more responsible by only monitoring when 4:08 our app is active. 4:11
You need to sign up for Treehouse in order to download course files.Sign up