Now let's take a look at the life of a Cast sender application.
First off you should know the Cast SDK uses an asynchronous callback design
to inform the sender applications of events, like connects and
disconnects to the Cast device, as well as whether commands were received.
For example, let's say a Sender app sends a message to a receiver.
To do so, you'll implement a couple of callbacks provided by the SDK so
your app can be informed of whether it was received.
We'll go into each of the steps of a sender life cycle by looking at
the CastHelloText example which you can find in the instructor notes.
The reason I like this sample as a teaching tool is because it contains
a one file sender app inside of main activity.
The only other files we need to look at are manifest and our menu XML.
And again, you can find the corresponding receiver app
inside the receiver folder.
Now off the bat,
you should know that some of the code we'll cover is going to be simplified by
the CastCompanionLibrary which we'll get to in the next section of the course.
But it's important to see how it works from the ground up when you get started.
Also, just a reminder,
don't use this project as the starting point for your integration.
Use Cast videos instead.
Let's start with the manifest.
The minimum Android SDK version supported by Cast is 9,
which is Gingerbread, or version 2.3.
Next the applications theme needs to be correctly set based
on the minimum Android SDK version.
For example you might need to use a variant of Theme.AppCompat.
Believe it or not that's it for the manifest.
Of course in a production app with the CastCompanionLibrary you'll need
a couple of permissions.
And we'll get to those later on.
Now on to the sender application inside of main activity.
Our first job is to initialize the Cast API, and to do so
we'll obtain an instance of the media router.
You'll need to hold on to this instance for
the lifetime of you sender application.
And a good place to do it is inside your onCreate.
At this point, the user has launched her sender application and
we've initialized the API.
Now it's time to discover available Cast devices.
Behind the scenes the media router detects Cast devices,
both of the local network and by proximity.
We'll need to filter those Cast devices for just ones that can run your
application, and to do so we'll use the MediaRouteSelector.
This is important because if you have a video playing application,
you wouldn't want to show the user an option to Cast to a stereo.
Our application ID by the way is defined in our strings.xml file, but
here it's displayed by Android Studio for convenience.
And remember this ID corresponds to the customer receiver app
already published for this application.
But in our own app of course we have our own.
Next up we'll need a MediaRouterCallback.
This is used to inform our sender app when the user selects a device they
want to cast to.
And we'll see how they do that with Cast menu in just a sec.
First notice that our activity extends ActionBarActivity.
There are a couple of different ways to add the Cast button but
the easiest is to use the MediaRouter ActionBar provider.
Here's how we add the Cast button to the ActionBarCompat using
the MediaRouterAction bar provider in our menu XML.
And inside onCreateOptionsMenu, we assign the MediaRouteSelector to
the MediaRouteActionProvider in the ActionBar menu.
Now to trigger the discovery of Cast devices,
we add the MediaRouterCallback to the MediaRouter instance.
Note that we're doing this inside of our activities onStart method.
To conserve battery power, we remove the callback when our activity has stopped.
At this point, the user can select a Cast device using the menu in their app.
Once they do, our sender application's informed via the MediaRouterCallback.
And you can see that happening here inside the onRouteSelected method.
Now it's time to launch the receiver app.
To do so, we'll need an instance of the Google API client,
which contains the Cast APIs.
We use the connection callbacks to be notified whether or
not the connection was successful.
And once the connection is confirmed,
our sender application can launch the receiver app by specifying
the application ID we found on the Cast developer console.
At this time we'll set a couple of callbacks.
And if you see how far to the right we're getting indented,
you'll know this is never a good sign.
Fortunately this task is greatly simplified by the CastCompanionLibrary
we'll get to in a sec.
Now that the receiver's launched it's time to control it by sending messages.
To do so, we'll need to set up a custom message channel.
The sender can use this channel to send string messages to the receiver.
Each custom channel is defined by unique namespace.
It's possible to create multiple channels,
just make sure they all have unique names.
There's some rules about this namespace
that you can find in the instructor notes.
Once the sender application is connected to the receiver the custom
channel can be set up.
And as usual we create some callbacks being informed if there
are messages we've received.
Again this is simplified by the CCL.
Now that our channel's set up,
the sender application can use it to send string messages to the receiver.
A good trick here, if you need to send a complex message,
is to simply encode it in JSON, and then decode it on the receiver side.
And the response from
the receiver is then captured using the callback we set up.
Now it's time to handle disconnecting from the receiver,
when the user selects this option on the Cast menu.
You can see how that's done inside our teardown method.
That was a quick tour of the basics of the sender lifestyle.
Next up we'll learn about the CastCompanionLibrary or CCL for
short, which greatly simplifies working with Cast on Android.
Here's an optional learning exercise you can do if you really want to learn how
to write Cast applications.
Now you know the basics of senders and receivers.
The best next step is to try and write your own sender.
What I'd recommend doing is creating a new Android Studio project
with a blank activity for a phone or a tablet.
Next, add simple Cast support to it so you can send a message to the custom
receiver we already have from our HelloCast example.
To help you with this,
you can find links to documentation in the instructor notes, and
if you can do this successfully, you should feel really good.