A class that manages Session
instances. The application can attach a SessionManagerListener
to be notified of session events.
SessionManager
works with Android MediaRouter on
managing session lifecycle. The current session always uses the current selected route (which
corresponds to MediaRouter.getSelectedRoute()).
SessionManager
listens to route changes from MediaRouter via
MediaRouter.addCallback(MediaRouteSelector, MediaRouter.Callback). For each
session type it supports, either a Cast session type or a custom session type as specified by
a SessionProvider,
a MediaRouter.Callback
will be added to MediaRouter with
a corresponding MediaRouteSelector
generated using the category from
SessionProvider.getCategory(). For Cast sessions, the category is generated
according to
CastOptions.getReceiverApplicationId().
When a route is selected in MediaRouter, the
corresponding callback will be executed and SessionManager
will call
SessionProvider.createSession(String) to create a session. Then it will try to
start a new session or resume the session that was shutdown incorrectly. When a session has
ended, failed on start or failed on resume, SessionManager
will unselect the corresponding route.
When a route is unselected in MediaRouter, the
corresponding callback will be executed and SessionManager
will end the current session if it is using the unselected route.
Because SessionManager
listens to route selection signals to start and end sessions, Cast dialog must make sure to
update the route selection state in Android MediaRouter. The
device chooser dialog must call
MediaRouter.selectRoute(MediaRouter.RouteInfo) or MediaRouter.RouteInfo.select()
when the user selects a device. The route controller dialog must call MediaRouter.unselect(int)
(or
endCurrentSession(boolean) when the user clicks the "Stop Casting" (or
"Disconnect") button.
Based on how an app creates Cast dialogs, the following actions needs to be done:
MediaRouteChooserDialog
and MediaRouteControllerDialog,
then these dialogs will update route selection in MediaRouter
automatically, so nothing needs to be done.
CastButtonFactory.setUpMediaRouteButton(Context, Menu, int) or
CastButtonFactory.setUpMediaRouteButton(Context, MediaRouteButton), then the
dialogs are actually created using MediaRouteChooserDialog
and MediaRouteControllerDialog,
so nothing needs to be done.MediaRouter.| void |
addSessionManagerListener(SessionManagerListener<Session>
listener)
Adds a
SessionManagerListener to monitor events from any type of
Session
instance.
|
| <T extends Session> void |
addSessionManagerListener(SessionManagerListener<T>
listener, Class<T>
sessionClass)
Adds a
SessionManagerListener to monitor events from a Session
instance whose class is sessionClass.
|
| void |
endCurrentSession(boolean stopCasting)
Ends the current session.
|
| CastSession |
getCurrentCastSession()
Returns the current session if it is an instance of
CastSession,
otherwise returns null.
|
| Session |
getCurrentSession()
Returns the currently active session.
|
| void |
removeSessionManagerListener(SessionManagerListener<Session>
listener)
Removes the
SessionManagerListener.
|
| <T extends Session> void |
removeSessionManagerListener(SessionManagerListener<T>
listener, Class<T>
sessionClass)
Removes the
SessionManagerListener.
|
| void |
Adds a SessionManagerListener
to monitor events from any type of Session
instance.
| NullPointerException | If listener is null. |
|---|---|
| IllegalStateException | If this method is not called on the main thread. |
Adds a SessionManagerListener
to monitor events from a Session
instance whose class is sessionClass.
| NullPointerException | If listener or sessionClass are
null. |
|---|---|
| IllegalStateException | If this method is not called on the main thread. |
Ends the current session.
| stopCasting | Should the receiver application be stopped when ending the current Session. |
|---|
| IllegalStateException | If this method is not called on the main thread. |
|---|
Returns the current session if it is an instance of CastSession,
otherwise returns null.
| IllegalStateException | If this method is not called on the main thread. |
|---|
Returns the currently active session. Returns null if no session is
active.
| IllegalStateException | If this method is not called on the main thread. |
|---|
Removes the SessionManagerListener.
| listener | The
SessionManagerListener to be removed. |
|---|
| IllegalStateException | If this method is not called on the main thread. |
|---|
Removes the SessionManagerListener.
| listener | The
SessionManagerListener to be removed. |
|---|---|
| sessionClass |
| NullPointerException | If sessionClass is null. |
|---|---|
| IllegalStateException | If this method is not called on the main thread. |
Starts a session. The sender app should call this method after the app is launched by an implicit intent fired by a component outside the app, such as Google Home app. We recommended calling this method in onResume() instead of onCreate() because onCreate() is called only when the app launches. If the app is running when the intent is fired, it will be brought to the foreground and onCreate() will not be called.
public class MyActivity extends Activity {
...
@Override
protected void onResume() {
...
Intent intent = getIntent();
// You need define your own intent filter and figure out how to check if the intent is
// for joining a cast session.
if (intentIsForStartingSession(intent)) {
CastContext castContext = CastContext.getSharedInstance(this);
castContext.getSessionManager().startSession(intent);
}
...
}
}
| startSessionIntent | The intent that is used to start the app and ask it to join a cast session. The intent contains necessary information for starting a session, such as the route ID, device name, session ID, etc. This information is set by the component that fires the intent. |
|---|