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
Well done!
You have completed Intents and Broadcast Receivers!
You have completed Intents and Broadcast Receivers!
Preview
Let's see how to send complex data from one app to another. We'll need to create a custom intent and a custom intent filter in a second app.
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
We've already seen how
to bundle up a song and
0:00
send it by an intent from
one activity to another.
0:02
We'll do the same thing here to
send it from one app to another but
0:05
we need to create a custom intent and
a custom intent filter in a second app.
0:08
Let's do this from detail activity, where
we already have a specific song chosen.
0:14
I already created a menu option for us.
0:19
So, all we need to do is
define our new intent and
0:21
share it when that menu option is tapped.
0:23
First we need a song though.
0:25
Right now it's only available
here in the onCreate method
0:27
because that's the only
place we are using it.
0:30
I've also already seated these starter
files with a member variable mSong.
0:33
So let's just switch that.
0:37
So we'll change song to mSong, and
we'll do the same thing down here,
0:39
mSong, and over here, msong.
0:44
Okay, now let's scroll down
to the methods for the menu.
0:47
First is the create method, and
then we have the onOptionsItemSelected,
0:51
which is called when
a menu item is Selected.
0:56
Now it's good practice to check the item
id and then perform actions on it,
1:00
we only have one item id but let's follow
the best practice and check it, so
1:04
if we expand menu and look at the detail
XML, we see that the id is action_ share.
1:08
Let's go back here at a test,
if ItemID = r.id.action_share.
1:15
Then we want to share the data.
1:23
Let's play it safe in here and add a check
to make sure that our song isn't null so
1:26
if Msong does not equal
null then we will continue.
1:30
Let's create a custom intent
CustomIntent equals a new Intent.
1:36
What we really need is a custom action.
1:43
It's just a string value, but we want it
to be unique so that it doesn't collide
1:46
with any other custom actions that any
other developers might have made up.
1:50
Common practice when we
need to use unique values
1:54
is to prefix them with the package name.
1:57
So let's create a constant
to use down here.
1:59
Up at the top let's add
public static final string.
2:01
And let's call this share_song.
2:06
Let's set this equal to the Packers name,
come.teamtreehouse.
2:12
And then I'll follow
the intent.ActionPattern used by Android.
2:17
We'll finish with .SHARE_SONG.
2:22
Let's go back down and
use this in our constructor.
2:25
So now we pass in SHARE_SONG.
2:33
And now we can add the song as
an extra since it's parsable.
2:36
customIntent.putExtra(mSong).
2:39
Whoops I forgot to pass in a key.
2:44
Let's add that.
2:47
We'll use the same one as before,
MainActivity.EXTRA_SONG, mSong.
2:48
There we go.
2:52
Finally lets start the activity.
2:54
But rather than just calling
regular old startActivity
2:57
let's follow another best practice and
use an activity chooser.
3:00
Let's revisit our handy intense and
filters page.
3:04
And now we wanna look at
Forcing an app chooser.
3:07
The app chooser is a nice feature because
it allows the user to pick a specific app
3:11
and even make that app the default for
that action for
3:15
given app We just need to create
a second intent to create a chooser for
3:17
the actual intent, which we see down here.
3:22
So before we call this,
let's create a second intent called intent
3:26
chooser equals intent.createchooser.
3:30
Then we pass in our custom intent and
a title for the chooser dialog.
3:36
Let's say Share song, and now we can
pass in chooser to start activity.
3:40
Now we can't try this yet because we don't
have anything that can receive it, but
3:48
still let's take a look.
3:51
So let's pick one of
the songs in our list and.
3:57
We got an error.
4:01
Let's take a look and see.
4:02
So, here in the stack trace we see
that it is a NullPointerException and
4:04
it links to the offending line and
I see what it is.
4:09
This is a common mistake I made it before.
4:13
We want to call the .equals method and
4:16
the Intent.ACTION said I want
to switch it around the order.
4:19
Now right now get action might be N o and
it's giving us a No pointer.
4:22
So instead we say intent.
4:26
ACTION_SEND.equals And
then we pass in intent.getAction.
4:28
There that's better let's try this again.
4:36
So, now if we click on a song
we're taken to the detail view.
4:40
And if we try the menu we
can click on share song.
4:45
But now it's hard to see it says
no apps can perform this action.
4:50
One of the good things about using
a chooser like this is that we avoid
4:54
a runtime exception.
4:57
If we weren't using a chooser we would
need to add another check like we did
4:58
before by calling result activity and
checking the package manager for handlers.
5:01
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