Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Android uses something called intent filters to track and register apps that can handle certain kinds of intents. Let's see how to specify an intent filter in the Android Manifest.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
At this point, you might be wondering
how the heck does the Google Maps
0:00
app know that we want to show a location.
0:03
And how does the Android system
know if any app can show a location
0:06
on a map or not?
0:09
Well, Android uses something
called intent filters to track and
0:10
register apps that can handle
certain kinds of intents.
0:13
The idea is pretty simple.
0:17
If you want your app to handle
a certain kind of intent,
0:18
either one of the system ones
like this location example, or
0:20
a custom one that you or
another developer creates.
0:23
Then you need to say so
in your apps manifest.
0:26
The Android manifest file contains
all that juicy metadata about
0:28
your app that the operating system and
0:32
Google Play can use to keep track
of which apps do certain things.
0:35
You've actually already seen
intent filters in a manifest.
0:39
We just haven't really
talked in detail about them.
0:42
Let's open up AndroidManifest.xml.
0:45
So it's up here in the manifest folder.
0:47
Double click on it.
0:49
And check out the registration for
MainActivity here.
0:50
It has an element called intent-filter.
0:54
And inside are two children,
action and category.
0:57
This is the standard mean
launcher intent filter
1:02
that specifies which activity
is used to start an app.
1:05
Wait a minute.
1:09
That means that apps
are started by intents.
1:10
Yeah, these things are really
used all over the place.
1:12
Since we don't have a map
viewer in this app,
1:16
let's learn about intent filters
with a different example.
1:18
One of the most common things
to share is simple text data.
1:21
Many apps allow you to share data like,
select text, names, URLs or whatever.
1:24
And many other apps use intent
filters to accept that data and
1:29
do something with it,
like save it or post online.
1:32
For demonstration purposes,
1:35
let's just display the text in
the detail activity somewhere.
1:36
So detail activity is listed
down here at the bottom.
1:40
And inside,
we can add an intent filter element.
1:44
So first, I'm gonna split this into a real
tag with a start and end activity tag.
1:47
Make this line up.
1:54
I'm also gonna add a little
bit of space at the bottom so
1:56
that this bumps up on my screen.
1:58
And we wanna add an intent filter.
2:00
Now, how should we set this?
2:04
Let's take a look at the documentation for
intents and intent filters.
2:07
In the navigation here on the right,
2:11
we see a section called
receiving an implicit intent.
2:12
That's just what we're looking for.
2:15
If we scroll down a little bit,
2:17
we come to a section that lists the three
things we may need for an intent-filter.
2:19
To be specific, we need one or
more of action, data and
2:23
category, depending on
what we're trying to do.
2:27
Pay attention to this
important note right here.
2:30
In order to receive implicit intents,
2:33
you must include the category default
category in the intent-filter.
2:35
We are trying to receive an implicit
intent, so we need to add that.
2:40
We also need an action and
hey, [LAUGH] look at that.
2:43
Down here is an example
of sharing text data.
2:47
It says we need to use
the ACTION_SEND as the action, and
2:51
we can also specify the mime type
of the data with text/plain.
2:54
Mime types can specify text,
images and other types of data.
3:00
So let's add the action.
3:04
Action and
then we say android:name equals,
3:06
android.intent.action.SEND.
3:11
And close this tag and
next we need the default category.
3:15
So we say category,
then we add android:name, and
3:18
this time I will pick from the list,
we want the default category.
3:21
Close this tag and finally for
text, we can add the data, element.
3:26
Then we wanna use android:mimeType and
type text/plain.
3:32
All right.
3:40
Our intent-filter is set.
3:40
At this point,
3:41
if a user wants to share text, our app
will show up as an option for sharing.
3:42
It will launch the DetailActivity but
3:46
nothing would happen because we haven't
actually handled the intent yet.
3:48
All we've done is enable
the routing essentially.
3:51
Let's take a short break and then we'll
add the code to handle the intent.
3:54
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up