This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Using an Implicit Intent 4:13
- Unmatched Implicit Intents 5:30
- Review: Implicit Intents 5 questions
- Intent Filters 3:58
- Handling an Intent from a Filter 4:24
- Creating Intent Filters 1 objective
- Sharing Songs with a Custom Intent 5:06
- Using a Custom Intent Filter 6:57
- Review: Custom Intent Filters 5 questions
Well done!
You have completed Intents and Broadcast Receivers!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we will see how to handle an intent in an Activity that has been successfully routed by an intent filter.
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
Let's go into the detail activity.
0:00
Remember this activity will be
started even from other apps but
0:05
that means that onCreate will be called
and we can check the intent in there.
0:08
What's important to note is that we now
have two different types of intents
0:13
that can start this activity.
0:16
The first is the regular navigation
here from within our app
0:18
which we're already handling.
0:21
The second is this new method.
0:22
So let's add a check here
after we get the intent.
0:25
We can easily check the actions of
an intent with intent.getaction.
0:29
So we'll say if intent.get
action equals and
0:35
then we can use some constants
from the intent class.
0:41
Intent.action_send, let's
add some curly braces.
0:44
And in here, we'll create a new method to
handle this that we'll define in a moment.
0:51
Handle, send intent and
we'll pass in the intent.
0:55
Let's select this, and hit Alt+Enter,
and create the new method, and
1:01
there it is here at the bottom.
1:05
Now back up here the rest of this
code can go in an else block
1:08
because we want to work like it did before
we worried about this text sharing.
1:11
So I'm going to add curly
brace here at the bottom and
1:16
there everything was
automatically indented.
1:21
Now there's one more thing here.
1:22
Notice that I've got a highlight
on these double equal signs.
1:24
That's because these
are actually string values and
1:27
remember we want to compare string
values with the DOT equals method.
1:30
So I'm going to change
this to dot equals and
1:33
put parentheses around intent.action send.
1:36
There we go, now we can get
back to our handle sended pin.
1:39
So in here let's follow the best
practice of checking for null first.
1:43
If intent.getStringExtra and
1:47
the name is intent.EXTRA_TEXT.
1:50
That is the name for
text data when it's shared.
1:56
If it's not equal to null
then we want to show it.
2:00
Let's just display it with a snack bar.
2:04
We will need the root element again which
I've already set with an ID in the starter
2:06
files so up here at the top,
let's add it as a field private
2:10
RelativeLayout layout.
2:16
And we'll set it here in the onCreate
method and root layout equals,
2:21
I'll cast to RelativeLayout,
findViewById(R.id.rootlayout).
2:26
Now back in our method,
we can make a snack bar.
2:34
SnackBar.make, and
then we pass an mRootLayout
2:36
and for our message we wanna
use the text data itself.
2:42
Let's call it the same thing,
intent.getStringExtra.
2:46
And the key we use is intent.EXTRA_TEXT.
2:51
I'm going to add a comma and
again going to drop down to a new line and
2:54
say Snackbar.LENGTH_LONG show at the end.
3:00
So let's run this and see what happens.
3:06
Okay, so the app has started and first
let's dismiss our app because we want to
3:11
share text data from somewhere else.
3:15
Now open the browser.
3:18
And let's just share the URL from up here
I'm going to click on the menu say share
3:21
page and look at that we see our
app down here in the share menu.
3:26
So we'll select it and
here comes detail activity and
3:31
there's our snack bar and
it's got the URL, cool right?
3:35
Sharing the text data is even
easier than location data.
3:39
We don't need to do it here but
3:42
you can check the Android
documentation for example.
3:43
Back on our Intents and filters page,
click on an example implicit intent.
3:46
And here we see some sample code
to share text via an intent.
3:52
Here's ActionSend just like we saw on
the other end, and the mimeType, and
3:56
the data itself with
the intent.EXTRA_TEXT key.
4:01
That's basically the same code that
was used to share the URL inside
4:04
the browser app.
4:07
Well done, so
far we've seen how to send and
4:10
receive data via two different
intents provided by Android.
4:12
But what if we want to send and receive
totally custom data between two apps?
4:16
As always, there's an intent for that.
4:20
Let's check it out after the break.
4:22
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