♪ (music) ♪ Hi, everyone! I'm Lalit Singh. So now that we have seen
the life cycle of activities and explicit intent, now we are going to go
deep dive in implicit intents and how exactly activities
work along with implicit intents. In this particular session we are going to go through
with the intents again, just a small recap, and then we are going
to learn about implicit intent more and how exactly we can send
different implicit intents across the Android system, and receive the implicit intents. So let's just do a small recap. We all know what is intent. Intent are the classes which are used
to invoke the fundamental components, like activities, services,
broadcast receivers. We also looked at that block diagram where, whenever you click
on any application intent is getting fired,
and the Android system then checks out
for that particular action, and then the app component turns on. So, where exactly can we use intent? To start an activity, to start a service, and to start a broadcast receiver. How exactly we use intent
and services in broadcast will be covered in later sessions. Types of intents: there are basically two types of intents-- explicit and implicit. Explicit-- we already know
when we have a specific class to cover, We know in which class
we really want to transfer. and we already have seen
the practical demo of it, and now we are going
to learn about implicit intent. In implicit intent,
we don't have source and destination, we all know that much, and all we [learn] the implicit intent
works with actions and data. Let's say I want to open up a map and in that I want to open up
a particular location. Now, for that, I need a map application. I need a map activity that can handle it. That can only be done
by a map application. So, from my application, I will invoke, or I will create an implicit intent. With the help of that I will be able
to transfer to that map application, and in turn it will
open up my map activity. So, you see, I didn't even have
to know the name of my activity which was there in my map application, I was still able to open up my location. In the same way a camera works. >From my own application
I can invoke the systems camera or the camera application which comes
inbuilt in your Android device. You can take a picture and you can just get back the picture
from that particular application. It is true that there might be a chance the same action can be handled
by different applications. For example, let's say
you want to open up a website and inside your Android device
there are more than one web browser. So, at this point of time,
whenever you will open up a link your app will provide you
with the different options. Let's say you have Chrome browser and you have your Opera also installed
in your Android device. So your Android device will ask you
in which one you want to open up. This process is known as <i>intent resolution</i> and it is being done
at the Android runtime. It matches with what
the registered intent handlers have been registered
for that particular action. So how exactly your Android runtime
matches the correct application, to open up? Well, that happens
with the help of intent filters. We will see the usage
of intent filters as we move on. And if there is more than one application, then your App Chooser will give you
the option to choose one application. This is what your App Chooser looks like. You must have come across
this kind of situation where you have to choose
one of the applications to execute that particular action. So how do you send your implicit intent? Well, you can send an implicit intent
by calling an object of intent, and inside that you put your action. It's not necessary that you have
to put the data too. An implicit intent can only
be created by just action too. Let's say I want to create an intent
which will allow me to call. For that, I will provide an action: <i>Intent.ACTION_CALL_BUTTON</i> Now, before I fire this intent, I need to check if there is a package
that can handle this particular request. So, for that, I use a condition<i>
intent.resolveActivity</i>. And inside that I do what?-- <i>get packageManager</i>. If this whole thing
doesn't return me a null, then only fire the intent. Now how exactly can I pass the data
along with my implicit intent? Well, for that, again,
there will be an action, and to set the data on data [inaudible] you can always use <i>intent.setData</i> and your data <i>Uri</i>. Let's say if it is a dial action
you want to perform, the data <i>Uri</i> will be <i>Uri.parse tel</i>
and your phone number. Let's say you want
to connect to a website. At that point of time <i>Uri.parse</i>,
STTP and your website link. For this also to execute
you will again check out it this kind of application
is actually present inside your Android device or not. If it is present, then only fire your intent. So how exactly do I provide my data <i>Uri</i>? To provide my data <i>Uri</i> I use <i>Uri.parse</i> and then I provide the data scheme. Now, data scheme depends upon
which kind of data you are parsing. Let's say I'm parsing a telephone number. At that point of time
my data scheme will become <i>tel</i>. If I'm parsing a longitude and latitude
for maps, it will become <i>geo</i>. And for your website it will become STTP. So these are a few examples
which we can take a look at when we are using implicit intent. So I have my <i>Uri</i> for Google, and to view this particular website
in an application I'm using an action known as<i>
Intent.ACTION_VIEW</i>, which is a generalized action. and along with that, I'm parsing <i>Uri</i> too. I can parse <i>Uri</i> as a second argument right when we are declaring
my intent object also, or I could have used <i>it.setData</i> and then parse <i>Uri</i>
at that point of time too. And then I need to trigger my intent. That I do with the help of <i>startActivity</i>. If I'm creating a dial or phone number. If I want to perform a dial action now, at that point of time
the data <i>Uri</i> will be <i>tel</i>, and then the phone number. And again, trigger the activity. How can I pass my extra data
with the implicit intent? It's just like we do
in our explicit intent. Here also I will create my intent object and I will use <i>putExtra</i> method
to put the data. I will put the key-- key can be anything-- and I will put whatever data I want. I can put the data
by getting from the user. If I have an <i>edittext</i>
defined in my layout, I could have just done<i>
edittext.getText().toString()</i>, and then pass that string as a data. And then to fire it I will check
if there is a particular application to handle that particular action, then only file it. You can add more additional information
into your implicit intent by providing categories to them. For example, when
we open up any website. Now, how exactly do they know
that there is a particular activity which can open up a website? Because that activity has one more
additional information known as <i>BROWSABLE</i>. In the same way, if you are
creating your own browser, or if you are creating your customized
browser for your Android device, you also need to add, inside the activity, or you also need to define
inside the activity tag that this activity
is a <i>BROWSABLE</i> category. The category gives
you additional information to help the intent filter
to find your activity better. If I want to add a type
and category, programmatically, I can use the intent object
and then set the type. For example, I want to set the type
for text or HTML file, or application or pdf file. And then I need to add a category. If I'm using a file then it will be<i>
intent.CATEGORY_OPENABLE</i>, because it's a file it should be openable. Or it can be, let's say
I'm creating a browser, at that point of time it will be
a category <i>intent.CATEGORY_BROWSABLE</i>. And when I'm done with that
I need to check if there is an application which can handle that request, and then fire that intent
by using <i>startActivityForResult</i>. Now this time I'm expecting
result from my second activity back to my first activity. And that we have already discussed
in my explicit intent. Now, there are some common actions
for implicit intents. For example, <i>ACTION_SET_ALARM</i>, <i>ACTION_IMAGE_CAPTURE</i>
or <i>ACTION_CREATE_DOCUMENT</i>. So these are some actions
which you can use along the way while you are using your implicit intent. You can use <i>ACTION_SET_ALARM</i>
for setting an alarm, or <i>ACTION_IMAGE_CAPTURE</i>
for opening up a camera, or create a document for creating a file. There are apps which can handle
our common actions, for example, alarm clock,
calendar, camera, contacts. They all can handle your alarm, set an alarm action. And similarly, we can use emails, file storage, maps,
or music, or video files, to access your external storage. So, for that, there will be an action where you can access
your external storage, as common. How exactly implicit intent receives? To receive an implicit intent
you need to know about intent filters. Whenever you declare any component, whether it is activity, service,
or broadcast receiver, an you want them to receive
a particular request in the Android manifest file; while you are declaring your component, inside that tag you need
to put an intent filter tag too which will actually
distinguish between them with different other applications. For example, let's say
I have an application. Inside that application
there are two activities. One activity will be a <i>LAUNCHER</i> activity and another activity
will be a <i>BROWSABLE</i> activity. So how do I do that? Inside the Android manifest file I will put an intent filter tag, and inside that intent filter tag,
in the first activities, I will put a <i>LAUNCHER</i> category, and in the second one
I will put a <i>BROWSABLE</i> category. And, of course, there will be
different actions also. So this is how we actually define it. If there is an activity tag there will be an intent filter
and action name and this time around this activity
will be used for sharing. So the action can be an inbuilt action or a user-defined action can be also used. The category I'm using is default, and the <i>mimeType</i> I am using is <i>text/plain</i>. So it will be sending or it will
be sharing only <i>text/plain</i> data. How exactly do we define the actions? Now actions may be a constant,
and these constants will be system-defined like <i>action.VIEW</i>, a generalized action; <i>action.SEND</i>, for sending something; or <i>action.DIAL</i>, for getting to a dialer. And categories like <i>BROWSABLE</i> for making your activity
behave as a browser, or <i>LAUNCHER</i> for making it
a launcher activity, or <i>OPENABLE</i> for making it as a file. Only action and category
doesn't help you to find your activity. Sometimes you have to provide
the additional information with the data too. What kind of data
are we talking about here? It can be a data scheme like <i>https</i>, it will be a <i>http</i>. Now that particular activity will be able
to handle all the <i>http</i> requests. If I put data scheme as <i>tel</i>, now that activity is perfectly capable
to handle all your <i>tel</i> requests or dial or call requests. In the same way, you could have used
attribute <i>android:host</i>. Now, in that host, if you put
a particular host in there, your activity will only accept the intent
from that particular host. In the same way, you can
always define mime type. What kind of types are acceptable
to your documents? An activity can have
multiple intent filters. So it is completely acceptable
that in a particular activity you are defining
two different intent filters. One is telling your activity
to behave like a <i>send</i> that is a normal <i>send</i> action, and another intent filter
you are defining for a multiple <i>send</i>. that you can send more than one file. So both are accepted. Or, you can have more than one action
inside a single intent filter also. So that also is allowed inside-- While you are defining your intent filter you can define more than one action
or more than one category, or more than one data
inside a single intent filter too. To learn more about implicit intent, the categories and the data schemes, you can follow the following links. Now we are going to see
the practical for implicit intent. ♪ (music) ♪