1 00:00:00,700 --> 00:00:03,800 At this point, you might be wondering how the heck does the Google Maps 2 00:00:03,800 --> 00:00:06,080 app know that we want to show a location. 3 00:00:06,080 --> 00:00:09,110 And how does the Android system know if any app can show a location 4 00:00:09,110 --> 00:00:10,650 on a map or not? 5 00:00:10,650 --> 00:00:13,780 Well, Android uses something called intent filters to track and 6 00:00:13,780 --> 00:00:17,020 register apps that can handle certain kinds of intents. 7 00:00:17,020 --> 00:00:18,100 The idea is pretty simple. 8 00:00:18,100 --> 00:00:20,709 If you want your app to handle a certain kind of intent, 9 00:00:20,709 --> 00:00:23,604 either one of the system ones like this location example, or 10 00:00:23,604 --> 00:00:26,136 a custom one that you or another developer creates. 11 00:00:26,136 --> 00:00:28,910 Then you need to say so in your apps manifest. 12 00:00:28,910 --> 00:00:32,860 The Android manifest file contains all that juicy metadata about 13 00:00:32,860 --> 00:00:35,157 your app that the operating system and 14 00:00:35,157 --> 00:00:39,127 Google Play can use to keep track of which apps do certain things. 15 00:00:39,127 --> 00:00:42,320 You've actually already seen intent filters in a manifest. 16 00:00:42,320 --> 00:00:44,230 We just haven't really talked in detail about them. 17 00:00:45,350 --> 00:00:47,630 Let's open up AndroidManifest.xml. 18 00:00:47,630 --> 00:00:49,260 So it's up here in the manifest folder. 19 00:00:49,260 --> 00:00:50,650 Double click on it. 20 00:00:50,650 --> 00:00:53,600 And check out the registration for MainActivity here. 21 00:00:54,760 --> 00:00:57,920 It has an element called intent-filter. 22 00:00:57,920 --> 00:01:01,300 And inside are two children, action and category. 23 00:01:02,390 --> 00:01:05,550 This is the standard mean launcher intent filter 24 00:01:05,550 --> 00:01:09,260 that specifies which activity is used to start an app. 25 00:01:09,260 --> 00:01:10,230 Wait a minute. 26 00:01:10,230 --> 00:01:12,980 That means that apps are started by intents. 27 00:01:12,980 --> 00:01:16,170 Yeah, these things are really used all over the place. 28 00:01:16,170 --> 00:01:18,550 Since we don't have a map viewer in this app, 29 00:01:18,550 --> 00:01:21,520 let's learn about intent filters with a different example. 30 00:01:21,520 --> 00:01:24,080 One of the most common things to share is simple text data. 31 00:01:24,080 --> 00:01:29,000 Many apps allow you to share data like, select text, names, URLs or whatever. 32 00:01:29,000 --> 00:01:32,300 And many other apps use intent filters to accept that data and 33 00:01:32,300 --> 00:01:34,510 do something with it, like save it or post online. 34 00:01:35,640 --> 00:01:36,680 For demonstration purposes, 35 00:01:36,680 --> 00:01:40,590 let's just display the text in the detail activity somewhere. 36 00:01:40,590 --> 00:01:42,990 So detail activity is listed down here at the bottom. 37 00:01:44,230 --> 00:01:47,050 And inside, we can add an intent filter element. 38 00:01:47,050 --> 00:01:52,026 So first, I'm gonna split this into a real tag with a start and end activity tag. 39 00:01:54,624 --> 00:01:56,568 Make this line up. 40 00:01:56,568 --> 00:01:58,510 I'm also gonna add a little bit of space at the bottom so 41 00:01:58,510 --> 00:02:00,590 that this bumps up on my screen. 42 00:02:00,590 --> 00:02:02,340 And we wanna add an intent filter. 43 00:02:04,400 --> 00:02:06,160 Now, how should we set this? 44 00:02:07,650 --> 00:02:11,390 Let's take a look at the documentation for intents and intent filters. 45 00:02:11,390 --> 00:02:12,910 In the navigation here on the right, 46 00:02:12,910 --> 00:02:15,820 we see a section called receiving an implicit intent. 47 00:02:15,820 --> 00:02:16,760 That's just what we're looking for. 48 00:02:17,770 --> 00:02:19,210 If we scroll down a little bit, 49 00:02:19,210 --> 00:02:23,460 we come to a section that lists the three things we may need for an intent-filter. 50 00:02:23,460 --> 00:02:27,300 To be specific, we need one or more of action, data and 51 00:02:27,300 --> 00:02:29,210 category, depending on what we're trying to do. 52 00:02:30,580 --> 00:02:33,400 Pay attention to this important note right here. 53 00:02:33,400 --> 00:02:35,650 In order to receive implicit intents, 54 00:02:35,650 --> 00:02:40,590 you must include the category default category in the intent-filter. 55 00:02:40,590 --> 00:02:43,930 We are trying to receive an implicit intent, so we need to add that. 56 00:02:43,930 --> 00:02:47,260 We also need an action and hey, [LAUGH] look at that. 57 00:02:47,260 --> 00:02:49,850 Down here is an example of sharing text data. 58 00:02:51,000 --> 00:02:54,970 It says we need to use the ACTION_SEND as the action, and 59 00:02:54,970 --> 00:03:00,340 we can also specify the mime type of the data with text/plain. 60 00:03:00,340 --> 00:03:04,804 Mime types can specify text, images and other types of data. 61 00:03:04,804 --> 00:03:06,152 So let's add the action. 62 00:03:06,152 --> 00:03:11,247 Action and then we say android:name equals, 63 00:03:11,247 --> 00:03:15,302 android.intent.action.SEND. 64 00:03:15,302 --> 00:03:18,560 And close this tag and next we need the default category. 65 00:03:18,560 --> 00:03:21,980 So we say category, then we add android:name, and 66 00:03:21,980 --> 00:03:25,010 this time I will pick from the list, we want the default category. 67 00:03:26,598 --> 00:03:32,530 Close this tag and finally for text, we can add the data, element. 68 00:03:32,530 --> 00:03:40,060 Then we wanna use android:mimeType and type text/plain. 69 00:03:40,060 --> 00:03:40,630 All right. 70 00:03:40,630 --> 00:03:41,860 Our intent-filter is set. 71 00:03:41,860 --> 00:03:42,830 At this point, 72 00:03:42,830 --> 00:03:46,690 if a user wants to share text, our app will show up as an option for sharing. 73 00:03:46,690 --> 00:03:48,750 It will launch the DetailActivity but 74 00:03:48,750 --> 00:03:51,990 nothing would happen because we haven't actually handled the intent yet. 75 00:03:51,990 --> 00:03:54,190 All we've done is enable the routing essentially. 76 00:03:54,190 --> 00:03:57,030 Let's take a short break and then we'll add the code to handle the intent.