Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Using a Custom Intent Filter6:57 with Ben Jakuben
Let's finish up our custom intent and filter combo by creating a new intent filter in a second app.
So now we just need to create a new App to receive this custom intent. 0:00 Let's create a new project. 0:05 File, new, new project and let's name this CustomIntentTester. 0:06 Just leave all the defaults click Next, Next, 0:14 select empty activity, next and finish. 0:18 Okay so all we want to do here is add a new intent filter. 0:26 Let's open up the manifest. 0:29 So here in manifest, double click on it, and 0:31 we can just add a second intent filter to main activity. 0:34 Activities can filter as many intents as we can throw them. 0:37 So under the first one let's add a line in the new intent filter. 0:40 And now let's add an action element, action and then enter a name and 0:46 for the name, we want to use the exact value we defined in our other app. 0:52 I'm going to copy and paste it to make sure I don't get a typo. 0:57 So let's switch over here and in detail activity, we defined it at the top. 1:00 I'm gonna copy the string value, switch back, and paste it here as the name. 1:05 All I need to do is end my action, and now I'm set. 1:12 For the category, 1:15 remember that implicit intents require us to use the default category. 1:17 So let's add category, and recall name and 1:20 then it's already at the top of my list category default. 1:24 All right, so now we need to do something with it in main activity. 1:29 Let's go back there and let's just display the song details in a TextView. 1:31 First will need an ID for the TextView. 1:36 So let's open up the layout file. 1:38 And we have a Hello World TextView. 1:42 I'm going to delete the text and then add 1:44 android:id=@+/textView. 1:49 Let's go back to main activity and 1:55 now we can create a text view named text view. 1:59 And set it equal to a cast to text view FindViewById(R.id.textview). 2:05 Next we can get the intent used to start this activity. 2:13 Intent intent equals getIntent. 2:15 So first we want to check the action to make sure that this is the right intent. 2:19 So like before, we wanna lead with the string in case the action is null. 2:24 So we say if string value, which I happened still have in my clipboard, 2:27 that equals then we pass in intent.getAction. 2:32 That's in curly braces and now we need a song class again. 2:36 There are actually a couple ways to do this. 2:42 I wanna show you how to make it work with a parcelable class, but 2:44 that means we need to copy the song class into this app. 2:48 That's fine for now but 2:51 we should always be wary of having the same code in two places. 2:53 If we really wanna share models across apps, 2:56 we should package them into a jar file. 2:58 Anyhow, let's go back to Music Machine. 3:01 Open up the Songs class in the Models package, right-click on it and 3:03 select Copy. 3:07 Go back to our custom intent tester, right click on the main package here and 3:10 select paste. 3:14 Click OK and here's our song class once again. 3:16 So now we can use this back in main activity. 3:20 We'll say song song=intent.getParcelableExtra. 3:23 And for the name we will use EXTRA_SONG. 3:29 Notice that we need to be really careful to use the exact same value that we have 3:36 in our other app. 3:40 It's starting to feel a little brittle. 3:42 Perhaps these both should be defined in the song class itself. 3:44 I'll leave that as an exercise for you if you want to clean it up. 3:47 Now we can print the song details in the text view. 3:51 TextView.setText(song.toString). 3:54 I overwrote the to string method to print the details out line after line. 4:01 So let's try this out. 4:06 Let's run the app. 4:07 Pick the same emulator. 4:09 OK and then when it loads let's switch back to Music Machine. 4:13 Let's tap on a song and try to share. 4:18 Go to the menu tap on share song and ouch we get an error 4:23 if we scroll up in logcat, let's see what it says. 4:29 Okay, this says, BadParcelableException: ClassNotFound when unmarshalling. 4:37 If you Google this error you can read about problems trying to share 4:44 a parcelable object across apps. 4:47 Each app has its own Java class loader and parceling in one app and 4:50 then unparceling in another causes problems. 4:54 Fortunately, we can address that by putting the class in the same exact 4:57 package on both apps. 5:00 So let's get rid of this log view and 5:03 right-click on Java here in the project navigator. 5:06 Select New then Package 5:10 select the main package that's already in here and click OK. 5:14 Now we want to type the package name of our custom class from the other app. 5:17 So we have com.teamtreehouse.musicmachine.models. 5:22 Click OK and there we go, we have our new package. 5:31 So we can drag the song class in here now. 5:37 So I'm going to drag song in here. 5:40 Click on the re-factor button and now let's try it again. 5:43 This time if we switch back to Music Machine, and let's try sharing this again. 5:49 Click on the menu, share song, and this time it worked. 5:56 Look we can see all the details of the song in our custom intent tester. 6:01 I should point out another way to share complex data from one app to another. 6:07 We can choose to implement a content provider 6:10 which is used to provide different types of content to other apps that ask for it. 6:13 The classic example is the Photos app which provides images or 6:17 videos when other apps prompt the user to pick one. 6:21 Now we won't cover content providers in this course but 6:24 check the teacher's notes for more information if you're interested. 6:27 All right. We've built up a solid understanding of 6:31 intents. 6:33 We've used both explicit and implicit intents and now understand why you can set 6:34 a specific activity for an intent and sometimes let Android find a handler. 6:39 We've also seen how to be good listeners and 6:44 set up an activity to handle certain intents. 6:45 Will take this further in the next section where we'll learn how to 6:48 listen across the entire Android system with a tool called broadcast receivers. 6:51 See you there. 6:56
You need to sign up for Treehouse in order to download course files.Sign up