Custom Broadcast Intents and Receivers3:24 with Ben Jakuben
In this video we'll see how to create custom broadcast intents and receivers, which work the exact same way as the Local broadcast we created in the previous video. The custom components both utilize a shared common action.
Custom broadcast intents and 0:00 receivers work the exact same way as the local broadcast we just created. 0:02 The only thing that's different from a system broadcast, 0:06 is that the custom components both utilize a shared common action. 0:08 Let's take a look in detail activity. 0:13 We already saw how to do this with our song example for intent filters. 0:15 We could very easily switch this to a broadcast intent and 0:19 receiver by getting rid of the chooser and 0:21 sending the intent by the send broadcast method versus the start activity method. 0:24 Let's try it out. 0:29 Let's delete these two lines and then call send broadcast with our custom intent. 0:29 Now we need to open our custom filter project again. 0:37 I've closed mine so I will select Open, Recent, Custom Intent Tester. 0:40 The first thing we need is a new custom broadcast receiver so 0:49 I'm going to expand into the package, and right click and say new Java class, and 0:54 name it song receiver, hit OK, and we want to extend the broadcast receiver class, 1:00 and we want to implement the methods for on receiver, we want to use this 1:06 same intent being passed in, to open main activity, has all the data we want. 1:11 But we can't do it the normal way. 1:17 It's an existing implicit intent so 1:19 we need to convert it into an explicit intent. 1:21 Fortunately it's pretty easy to do. 1:24 First we need to use intent.setClass and this is how we make it explicit. 1:26 You pass in the context and then we name the class which is MainActivity.class. 1:31 Next, we need to flag this as a new task. 1:38 So type intent.set flags. 1:41 And then we pass in an intent flag. 1:44 Intent.flag activity, new task. 1:45 This is a special flag we need to set when starting a new activity. 1:52 This is normally hidden from us when we construct an explicit intent and 1:55 start it with start activity. 1:58 Finally, we can call the start activity. 2:00 But we need the context, 2:02 context.startActivity and we pass in our intent. 2:04 Back in the manifest we need to add a new manifest receiver 2:10 that uses the same intent filter as we already have in main activity So 2:15 I'm going to copy the SHARE SONG intent filter. 2:19 Then here before the end of the application tag, 2:23 I'm going to add a receiver. 2:25 The name is the new class we just added, Song Receiver, which it detected. 2:27 And inside we can put that intent filter. 2:32 All right, with this routing in place we should now be able to display 2:36 song information once again. 2:39 Let's try it out. 2:40 Okay once the app starts, let's close out of it and 2:45 we'll go into MusicMachine and we can share a song. 2:50 I'll share the Underdog by Spoon and then from the detail, select Share song. 2:56 And it goes automatically to Custom Intent Tester. 3:04 Cool. It's working. 3:06 Nice work once again. 3:09 There's one more thing I'd like to show you and that's how to create and 3:10 use notifications to communicate with users asynchronously 3:13 via the notification drawer. 3:16 It's pretty common and at this point pretty easy. 3:18 So take a short break and then let's get to it 3:21
You need to sign up for Treehouse in order to download course files.Sign up