1 00:00:01,000 --> 00:00:05,000 So now that we have learned about explicit intents, let's turn our focus to 2 00:00:05,000 --> 00:00:09,000 implicit intents, which is one of the most powerful features in Android. 3 00:00:09,000 --> 00:00:11,000 Let's just start with an example. 4 00:00:11,000 --> 00:00:15,000 So let's go to the emulator, and we are going to launch the gallery 5 00:00:15,000 --> 00:00:19,000 application here, which is basically the tool that you use to view the photos 6 00:00:19,000 --> 00:00:21,000 that are on your phone. 7 00:00:21,000 --> 00:00:27,000 So I will click Gallery, and I have a couple of pictures on there already, and 8 00:00:27,000 --> 00:00:31,000 let's say I choose this photo. 9 00:00:31,000 --> 00:00:33,000 We can see up here we have this button called Share. 10 00:00:33,000 --> 00:00:38,000 Well, this allows you to send this photo to somebody else, but there could be 11 00:00:38,000 --> 00:00:42,000 many applications on your phone that are capable of doing that. 12 00:00:42,000 --> 00:00:46,000 The first one that comes to mind is a text message. 13 00:00:46,000 --> 00:00:51,000 The next could be sharing it through Twitter or Facebook or through an email, so 14 00:00:51,000 --> 00:00:56,000 you can have your application be added to that list of applications that can 15 00:00:56,000 --> 00:01:01,000 actually handle that type of task, and that's what an implicit intent does. 16 00:01:01,000 --> 00:01:05,000 So this Gallery application, when I click Share, it's actually activating 17 00:01:05,000 --> 00:01:08,000 using an implicit intent. 18 00:01:08,000 --> 00:01:14,000 It's basically saying, "Hey, any applications on the system that are capable of sending data, 19 00:01:14,000 --> 00:01:16,000 here is an image that I want to send out." 20 00:01:16,000 --> 00:01:19,000 It will then present the user with a list of options. 21 00:01:19,000 --> 00:01:24,000 Well, here on the emulator right now, if I click Share, there is only one 22 00:01:24,000 --> 00:01:29,000 option, which is to send it as a text message. And that's because nobody else on 23 00:01:29,000 --> 00:01:35,000 the phone, no other applications, have actually registered to be able to handle that intent. 24 00:01:35,000 --> 00:01:39,000 So one thing before we move forward is you are probably wondering how did I get 25 00:01:39,000 --> 00:01:41,000 images onto the emulator? 26 00:01:41,000 --> 00:01:46,000 Well, if we go back to Eclipse and go into DDMS, you will notice one of the 27 00:01:46,000 --> 00:01:53,000 tools up here is File Explorer, and this gives you an Explorer view of the actual device. 28 00:01:53,000 --> 00:01:58,000 So if we go into mnt, there we see our sdcard. 29 00:01:58,000 --> 00:02:02,000 And I have put a couple of photos on here that you have in the exercise files in 30 00:02:02,000 --> 00:02:08,000 the Assets folder, and all you do is to basically click sdcard and then click 31 00:02:08,000 --> 00:02:12,000 this button over here, which is Push a file onto the device. 32 00:02:12,000 --> 00:02:16,000 It then allows you to select any file, and it will upload it to the device. 33 00:02:16,000 --> 00:02:20,000 Let's go back to the emulator here. 34 00:02:20,000 --> 00:02:26,000 So again, when the user clicks Share, the Gallery application is sending out 35 00:02:26,000 --> 00:02:28,000 this intent message. 36 00:02:28,000 --> 00:02:30,000 So how do we know what the intent message is? 37 00:02:30,000 --> 00:02:36,000 Well, that's easier to do because will just click it and then go back to 38 00:02:36,000 --> 00:02:42,000 Eclipse and if we look here in LogCat, we can actually see what was sent out 39 00:02:42,000 --> 00:02:44,000 when the user hit that. 40 00:02:44,000 --> 00:02:49,000 And we can see here this Starting Intent, and the intent is 41 00:02:49,000 --> 00:02:55,000 android.intent.action.SEND, which is an implicit intent. 42 00:02:55,000 --> 00:03:02,000 We can see that with the actual intent there is a data type of image JPEG. 43 00:03:02,000 --> 00:03:07,000 So again, the gallery is sending out this intent with the JPEG information for 44 00:03:07,000 --> 00:03:10,000 any applications that are able to handle it. 45 00:03:10,000 --> 00:03:14,000 So let's create an application that can handle that implicit intent. 46 00:03:14,000 --> 00:03:18,000 So I have created a blank project here, just called Implicit_Intents, and I 47 00:03:18,000 --> 00:03:20,000 haven't done anything with it yet. 48 00:03:20,000 --> 00:03:26,000 So the first thing I want to do is I want to be able to register with Android 49 00:03:26,000 --> 00:03:31,000 that I'm able to handle that intent, and we do that in the Android manifest file. 50 00:03:31,000 --> 00:03:37,000 Now we can see for our one and only activity, which is called Main, we 51 00:03:37,000 --> 00:03:42,000 already have an intent-filter, but this is the one I described to you in an earlier movie. 52 00:03:42,000 --> 00:03:46,000 This lets the Android system know which activity to activate when our 53 00:03:46,000 --> 00:03:52,000 application is launched, and that's using this action main intent filter. 54 00:03:52,000 --> 00:03:56,000 So we are going to add another intent filter, and I am just going to copy this 55 00:03:56,000 --> 00:04:01,000 block and paste it down here, so we can have multiple intent filters. 56 00:04:01,000 --> 00:04:06,000 And this is basically advertising to Android that, hey, we are able to handle 57 00:04:06,000 --> 00:04:08,000 that intent and that data. 58 00:04:08,000 --> 00:04:12,000 So we are going to put in here, this is going to be action.SEND. 59 00:04:12,000 --> 00:04:16,000 Now for category, we haven't really talked about categories here. 60 00:04:16,000 --> 00:04:22,000 Category is a just a way to give additional information to Android about this intent. 61 00:04:22,000 --> 00:04:28,000 Traditionally, or in most cases, it's best to just use the DEFAULT category, 62 00:04:28,000 --> 00:04:31,000 unless you know that there's a different category that you should use. 63 00:04:31,000 --> 00:04:33,000 We are just going to use DEFAULT here. 64 00:04:33,000 --> 00:04:37,000 Now the next thing that we need to add is a data tag. 65 00:04:37,000 --> 00:04:41,000 And this is basically telling Android the type of data that we are able to 66 00:04:41,000 --> 00:04:44,000 handle with this intent. 67 00:04:44,000 --> 00:04:49,000 So you can see one of the properties is mimeTYPE, and this we just pass in a 68 00:04:49,000 --> 00:04:56,000 valid mimeTYPE value, which is image/. And remember we saw that in DDMS. We can 69 00:04:56,000 --> 00:05:01,000 put explicitly JPEGs here, or we can just put star to say hey, we can handle any 70 00:05:01,000 --> 00:05:03,000 image types that are on the system. 71 00:05:03,000 --> 00:05:10,000 Okay, so just by adding that to our manifest file, now when on the phone or on 72 00:05:10,000 --> 00:05:14,000 the device when the user clicks Share or Send, we are going to be able to 73 00:05:14,000 --> 00:05:20,000 handle that file type, in addition to the text messaging application, and so on and so forth. 74 00:05:20,000 --> 00:05:24,000 So when the image is actually sent from the Gallery application, let's go ahead 75 00:05:24,000 --> 00:05:27,000 and do something just so we can actually display it. 76 00:05:27,000 --> 00:05:31,000 So I am going to go to my main.xml file, which is my layout. 77 00:05:31,000 --> 00:05:36,000 I am going to get rid of that TextView, and I am going to go to Images & Media, 78 00:05:36,000 --> 00:05:39,000 and I am going to put in an ImageView. 79 00:05:39,000 --> 00:05:43,000 So an ImageView is just that--it's a component that displays an image. 80 00:05:43,000 --> 00:05:47,000 So I am just going to leave it there at the default location, and now I will 81 00:05:47,000 --> 00:05:48,000 go into my Java code. 82 00:05:48,000 --> 00:05:51,000 I am going to go into Main.java. 83 00:05:51,000 --> 00:05:55,000 First thing I need to do, again, like we've done before, is to get a reference to 84 00:05:55,000 --> 00:05:56,000 that ImageView component. 85 00:05:56,000 --> 00:06:06,000 I am going to call it iv, and it's going to be findViewById R.Id. 86 00:06:06,000 --> 00:06:08,000 There's that imageView. 87 00:06:08,000 --> 00:06:11,000 Again, we want to cast it to imageView. 88 00:06:11,000 --> 00:06:16,000 Okay, so now we actually want to set the ImageView to the image that was sent to 89 00:06:16,000 --> 00:06:19,000 us from the gallery that's actually bundled with the intent. 90 00:06:19,000 --> 00:06:27,000 So to do that we are going to do iv.setImageURI, which is essentially the 91 00:06:27,000 --> 00:06:30,000 address to where that image is. 92 00:06:30,000 --> 00:06:35,000 So to this, we need to go into the intent object itself to pull out that information. 93 00:06:35,000 --> 00:06:45,000 So we are going to say getIntent().getExtras. And again, we covered this in the 94 00:06:45,000 --> 00:06:49,000 movie about explicit intents. 95 00:06:49,000 --> 00:06:52,000 And then, we are going to get--and here we are going to use a constant from the 96 00:06:52,000 --> 00:07:02,000 intent class called intent.EXTRA_STREAM. 97 00:07:02,000 --> 00:07:06,000 So basically, in the intent object that's sent to us, we are going to grab out that 98 00:07:06,000 --> 00:07:12,000 stream of information, which is actually a URI to where that image file is. 99 00:07:12,000 --> 00:07:18,000 Now one thing, we have a quick fix here, we just need to cast that argument to a URI. 100 00:07:18,000 --> 00:07:22,000 Now remember, we are not doing any checking here that's basically when we come 101 00:07:22,000 --> 00:07:26,000 in, if we launch this application not through the gallery, we are going to want 102 00:07:26,000 --> 00:07:29,000 to check for that, but in this case we are not going to worry about it. 103 00:07:29,000 --> 00:07:34,000 So we are going to run this, and let's actually go to the emulator, wait for our 104 00:07:34,000 --> 00:07:39,000 application to launch up, and you can see we have an exception. 105 00:07:39,000 --> 00:07:44,000 Now, this is actually expected because there is no intent when this was just 106 00:07:44,000 --> 00:07:47,000 launched, because the system just launched it. 107 00:07:47,000 --> 00:07:50,000 In a real-world case, you are going to want to be able to handle your 108 00:07:50,000 --> 00:07:53,000 application launching without that intent information. 109 00:07:53,000 --> 00:07:56,000 But for now, we are not going to worry about it. 110 00:07:56,000 --> 00:08:02,000 Now, I am going to go back to my image. 111 00:08:02,000 --> 00:08:08,000 There it is. And now I am going to click Share, and now you can see instead of 112 00:08:08,000 --> 00:08:11,000 going right to the messaging application, we have an option. 113 00:08:11,000 --> 00:08:15,000 We can share the pictures via my application that I created called Implicit 114 00:08:15,000 --> 00:08:18,000 Intents, or use messaging. 115 00:08:18,000 --> 00:08:23,000 Well, I am going to use Implicit Intents, which is our application, and now you 116 00:08:23,000 --> 00:08:26,000 can see our application is displaying that photo. 117 00:08:26,000 --> 00:08:30,000 So again, you can now understand the real power of Android. Whereas all 118 00:08:30,000 --> 00:08:33,000 applications run in their own sandbox, 119 00:08:33,000 --> 00:08:38,000 we are actually able to share data, to activate each other's components. 120 00:08:38,000 --> 00:08:43,000 We just have to set in the manifest file that we are capable of handling different intents. 121 00:08:43,000 --> 00:08:45,000 AndroidCode.ir Subtitle by : Mojtaba Haddadi