So now that we have learned about explicit intents, let's turn our focus to
implicit intents, which is one of the most powerful features in Android.
Let's just start with an example.
So let's go to the emulator, and we are going to launch the gallery
application here, which is basically the tool that you use to view the photos
that are on your phone.
So I will click Gallery, and I have a couple of pictures on there already, and
let's say I choose this photo.
We can see up here we have this button called Share.
Well, this allows you to send this photo to somebody else, but there could be
many applications on your phone that are capable of doing that.
The first one that comes to mind is a text message.
The next could be sharing it through Twitter or Facebook or through an email, so
you can have your application be added to that list of applications that can
actually handle that type of task, and that's what an implicit intent does.
So this Gallery application, when I click Share, it's actually activating
using an implicit intent.
It's basically saying, "Hey, any applications on the system that are capable of sending data,
here is an image that I want to send out."
It will then present the user with a list of options.
Well, here on the emulator right now, if I click Share, there is only one
option, which is to send it as a text message. And that's because nobody else on
the phone, no other applications, have actually registered to be able to handle that intent.
So one thing before we move forward is you are probably wondering how did I get
images onto the emulator?
Well, if we go back to Eclipse and go into DDMS, you will notice one of the
tools up here is File Explorer, and this gives you an Explorer view of the actual device.
So if we go into mnt, there we see our sdcard.
And I have put a couple of photos on here that you have in the exercise files in
the Assets folder, and all you do is to basically click sdcard and then click
this button over here, which is Push a file onto the device.
It then allows you to select any file, and it will upload it to the device.
Let's go back to the emulator here.
So again, when the user clicks Share, the Gallery application is sending out
this intent message.
So how do we know what the intent message is?
Well, that's easier to do because will just click it and then go back to
Eclipse and if we look here in LogCat, we can actually see what was sent out
when the user hit that.
And we can see here this Starting Intent, and the intent is
android.intent.action.SEND, which is an implicit intent.
We can see that with the actual intent there is a data type of image JPEG.
So again, the gallery is sending out this intent with the JPEG information for
any applications that are able to handle it.
So let's create an application that can handle that implicit intent.
So I have created a blank project here, just called Implicit_Intents, and I
haven't done anything with it yet.
So the first thing I want to do is I want to be able to register with Android
that I'm able to handle that intent, and we do that in the Android manifest file.
Now we can see for our one and only activity, which is called Main, we
already have an intent-filter, but this is the one I described to you in an earlier movie.
This lets the Android system know which activity to activate when our
application is launched, and that's using this action main intent filter.
So we are going to add another intent filter, and I am just going to copy this
block and paste it down here, so we can have multiple intent filters.
And this is basically advertising to Android that, hey, we are able to handle
that intent and that data.
So we are going to put in here, this is going to be action.SEND.
Now for category, we haven't really talked about categories here.
Category is a just a way to give additional information to Android about this intent.
Traditionally, or in most cases, it's best to just use the DEFAULT category,
unless you know that there's a different category that you should use.
We are just going to use DEFAULT here.
Now the next thing that we need to add is a data tag.
And this is basically telling Android the type of data that we are able to
handle with this intent.
So you can see one of the properties is mimeTYPE, and this we just pass in a
valid mimeTYPE value, which is image/. And remember we saw that in DDMS. We can
put explicitly JPEGs here, or we can just put star to say hey, we can handle any
image types that are on the system.
Okay, so just by adding that to our manifest file, now when on the phone or on
the device when the user clicks Share or Send, we are going to be able to
handle that file type, in addition to the text messaging application, and so on and so forth.
So when the image is actually sent from the Gallery application, let's go ahead
and do something just so we can actually display it.
So I am going to go to my main.xml file, which is my layout.
I am going to get rid of that TextView, and I am going to go to Images & Media,
and I am going to put in an ImageView.
So an ImageView is just that--it's a component that displays an image.
So I am just going to leave it there at the default location, and now I will
go into my Java code.
I am going to go into Main.java.
First thing I need to do, again, like we've done before, is to get a reference to
that ImageView component.
I am going to call it iv, and it's going to be findViewById R.Id.
There's that imageView.
Again, we want to cast it to imageView.
Okay, so now we actually want to set the ImageView to the image that was sent to
us from the gallery that's actually bundled with the intent.
So to do that we are going to do iv.setImageURI, which is essentially the
address to where that image is.
So to this, we need to go into the intent object itself to pull out that information.
So we are going to say getIntent().getExtras. And again, we covered this in the
movie about explicit intents.
And then, we are going to get--and here we are going to use a constant from the
intent class called intent.EXTRA_STREAM.
So basically, in the intent object that's sent to us, we are going to grab out that
stream of information, which is actually a URI to where that image file is.
Now one thing, we have a quick fix here, we just need to cast that argument to a URI.
Now remember, we are not doing any checking here that's basically when we come
in, if we launch this application not through the gallery, we are going to want
to check for that, but in this case we are not going to worry about it.
So we are going to run this, and let's actually go to the emulator, wait for our
application to launch up, and you can see we have an exception.
Now, this is actually expected because there is no intent when this was just
launched, because the system just launched it.
In a real-world case, you are going to want to be able to handle your
application launching without that intent information.
But for now, we are not going to worry about it.
Now, I am going to go back to my image.
There it is. And now I am going to click Share, and now you can see instead of
going right to the messaging application, we have an option.
We can share the pictures via my application that I created called Implicit
Intents, or use messaging.
Well, I am going to use Implicit Intents, which is our application, and now you
can see our application is displaying that photo.
So again, you can now understand the real power of Android. Whereas all
applications run in their own sandbox,
we are actually able to share data, to activate each other's components.
We just have to set in the manifest file that we are capable of handling different intents.
AndroidCode.ir Subtitle by : Mojtaba Haddadi