A singleton class containing global context for the Cast SDK. The shared instance can be
accessed by calling
getSharedInstance(Context)
.To properly initializeCastContext
,
the application must have a class that implementsOptionsProvider
interface:
package com.example.app; public class CastOptionsProvider implements OptionsProvider { @Override public CastOptions getCastOptions(Context appContext) { ... } }and specify its fully qualified class name in the
AndroidManifest.xml
with the
key
OPTIONS_PROVIDER_CLASS_NAME_KEY
:
... <meta-data android:name= "com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME" android:value= "com.example.app.CastOptionsProvider" /> ...All public methods must be called from the main thread.
Constant Summary
String | OPTIONS_PROVIDER_CLASS_NAME_KEY | The metadata key to specify the fully qualified
name of theOptionsProvider
implementation in theAndroidManifest.xml . |
Public Method Summary
void |
addAppVisibilityListener(AppVisibilityListener
listener)
This method is deprecated. Don't use the SDK to listen to app visibility
changes. Use alternative ways for watching activity lifecycle changes such as
by overriding
Activity
lifecycle change methods, or by using
Application.ActivityLifecycleCallbacks .This deprecated method is
now a no-op and will be removed in the next version.
|
void |
addCastStateListener(CastStateListener
listener)
Adds a
CastStateListener to monitor changes of the currentCastState .
|
void |
addSessionTransferCallback(SessionTransferCallback
callback)
Adds a
SessionTransferCallback to monitor the transfer between different
endpoints.
|
CastOptions |
getCastOptions()
Returns the
CastOptions .
|
int |
getCastReasonCodeForCastStatusCode(int castStatusCode)
Returns the
CastReasonCodes for the givenCastStatusCodes .
|
int |
getCastState()
Returns the current
CastState .
|
MediaNotificationManager |
getMediaNotificationManager()
Returns the
MediaNotificationManager .
|
MediaRouteSelector |
getMergedSelector()
Returns the merged
MediaRouteSelector
used for discovering devices and deciding whether to show the MediaRouteButton.
|
PrecacheManager |
getPrecacheManager()
Returns the
PrecacheManager .
|
SessionManager |
getSessionManager()
Returns the
SessionManager ,
never returnsnull .
|
staticCastContext | |
staticCastContext |
getSharedInstance()
Returns the shared instance of
CastContext .
|
static Task<CastContext> |
getSharedInstance(Context
context,Executor
executor)
Returns an asynchronousTask APIcall on the shared
instance of
CastContext ,
which will not block the caller thread.
|
boolean |
isAppVisible()
This method is deprecated. Don't use the SDK to listen to app visibility
changes. Use alternative ways for watching activity lifecycle changes such as
by overriding
Activity
lifecycle change methods, or by using
Application.ActivityLifecycleCallbacks .This deprecated method
always return false now and will be removed in the next version.
|
boolean |
onDispatchVolumeKeyEventBeforeJellyBean(KeyEvent
event)
This method is deprecated. Google Play services discontinued updates for
Jelly Bean (API levels 16, 17 & 18) since July 2021. This method always returns
false and will be removed in the next release.
|
void |
removeAppVisibilityListener(AppVisibilityListener
listener)
This method is deprecated. Don't use the SDK to listen to app visibility
changes. Use alternative ways for watching activity lifecycle changes such as
by overriding
Activity
lifecycle change methods, or by using
Application.ActivityLifecycleCallbacks .This deprecated method is
now a no-op and will be removed in the next version.
|
void | |
void |
removeSessionTransferCallback(SessionTransferCallback
callback)
Removes a
SessionTransferCallback from monitoring endpoint transferring
events.
|
void |
setLaunchCredentialsData(CredentialsData credentialsData)
Sets the credentials data of the current user.
|
void |
Inherited Method Summary
Constants
public static finalString OPTIONS_PROVIDER_CLASS_NAME_KEY
The metadata key to specify the fully qualified name of theOptionsProvider
implementation in theAndroidManifest.xml
.
Public Methods
public voidaddAppVisibilityListener(AppVisibilityListener listener)
This method is deprecated.
Don't use the SDK to listen to app visibility changes. Use alternative ways for watching
activity lifecycle changes such as by overridingActivity
lifecycle change methods, or by using
Application.ActivityLifecycleCallbacks
.This deprecated method is now a no-op
and will be removed in the next version.
public voidaddCastStateListener(CastStateListener listener)
Adds aCastStateListener
to monitor changes of the currentCastState
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|---|
NullPointerException | If listener isnull . |
public voidaddSessionTransferCallback(SessionTransferCallback callback)
Adds a
SessionTransferCallback
to monitor the transfer between different
endpoints.
Parameters
callback | a
SessionTransferCallback to monitor endpoint transferring |
---|
publicCastOptions getCastOptions()
Returns theCastOptions
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public intgetCastReasonCodeForCastStatusCode(int castStatusCode)
Returns theCastReasonCodes
for the givenCastStatusCodes
.
Some of theCastStatusCodes
are used by Cast SDK internally. This method translates thoseCastStatusCodes
toCastReasonCodes
which is developer-friendly.
Parameters
castStatusCode | theCastStatusCodes
for checking the detailed explanation |
---|
public intgetCastState()
Returns the currentCastState
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
publicMediaNotificationManager getMediaNotificationManager()
Returns the
MediaNotificationManager
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public MediaRouteSelector getMergedSelector()
Returns the mergedMediaRouteSelector
used for discovering devices and deciding whether to show the MediaRouteButton. This
selector is merged with any additionalSessionProvider
s
that might be registered with the CastContext.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
publicPrecacheManager getPrecacheManager()
Returns thePrecacheManager
.
publicSessionManager getSessionManager()
Returns theSessionManager
,
never returnsnull
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public staticCastContext getSharedInstance(Context context)
Returns a shared instance ofCastContext
.
The shared instance will be initialized on the first time this method is called.
Parameters
context | An applicationContext .
If this is not an applicationContext ,
Context.getApplicationContext() will be called on the given context, to
retrieve it. |
---|
Throws
IllegalStateException |
If any of the following:
|
---|
public staticCastContext getSharedInstance()
Returns the shared instance ofCastContext
.
This method must be called afterCastContext
is initialized through
getSharedInstance(Context, Executor)
.Otherwise, this method will return
null
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public static Task<CastContext> getSharedInstance(Contextcontext, Executor executor)
Returns an asynchronousTask
APIcall on the shared instance ofCastContext
,
which will not block the caller thread. The shared instance will be initialized on the
first time this method is called.
Note that
getSharedInstance(Context, Executor)
should be called in the
Activity.onCreate(Bundle)
method of the activities that might display a Cast
button. The Cast SDK providesCastButtonFactory
to set up a Cast button.
Note that other activity methods, e.g.Activity.onResume()
,
need to check the availability of theCastContext
object before using it, because theCastContext
is initialized on theExecutor
.
Note that
ModuleUnavailableException
could be thrown when the SDK fails to load the
internal Cast module. The caller will get the exception fromTask.getException()
when the task completes.
Parameters
context | An applicationContext .
If this is not an applicationContext ,
Context.getApplicationContext() will be called on the given context, to
retrieve it. |
---|---|
executor | AnExecutor
to load the internal Cast module. |
Throws
IllegalStateException |
If any of the following:
|
---|
public booleanisAppVisible()
This method is deprecated.
Don't use the SDK to listen to app visibility changes. Use alternative ways for watching
activity lifecycle changes such as by overridingActivity
lifecycle change methods, or by using
Application.ActivityLifecycleCallbacks
.This deprecated method always return
false now and will be removed in the next version.
Throws
IllegalStateException |
---|
public booleanonDispatchVolumeKeyEventBeforeJellyBean(KeyEvent event)
This method is deprecated.
Google Play services discontinued updates for Jelly Bean (API levels 16, 17 & 18) since
July 2021. This method always returns false and will be removed in the next release.
If your application supports SDK versions before JELLY BEAN, you should call this
method to delegate the handling of physical volume key events to the SDK so that users
can use the physical volume keys to control device volume of the Cast receiver. Clients
should override
Activity.dispatchKeyEvent(KeyEvent)
and call this method:
@Override public boolean dispatchKeyEvent(KeyEvent event) { return CastContext.getSharedInstance().onDispatchVolumeKeyEventBeforeJellyBean(event) || super.dispatchKeyEvent(event); }The amount of volume delta to increase or decrease is set through
CastOptions.Builder.setVolumeDeltaBeforeIceCreamSandwich(double)
.This
method is a no-op on platform JELLY BEAN and above.
Parameters
event | the dispatched event |
---|
public voidremoveAppVisibilityListener(AppVisibilityListener listener)
This method is deprecated.
Don't use the SDK to listen to app visibility changes. Use alternative ways for watching
activity lifecycle changes such as by overridingActivity
lifecycle change methods, or by using
Application.ActivityLifecycleCallbacks
.This deprecated method is now a no-op
and will be removed in the next version.
Throws
IllegalStateException |
---|
public voidremoveCastStateListener(CastStateListener listener)
Removes aCastStateListener
.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public voidremoveSessionTransferCallback(SessionTransferCallback callback)
Removes a
SessionTransferCallback
from monitoring endpoint transferring events.
Parameters
callback | the
SessionTransferCallback to be unregistered |
---|
public voidsetLaunchCredentialsData(CredentialsData credentialsData)
Sets the credentials data of the current user. You should call this API with the
current user information right after
getSharedInstance()
and whenever user account is changed.
Currently it serves two purposes:
- The credentials data will be embedded in the launch request. If it is to launch an Android TV app, the app can use the credentials data to determine if the app supports this specific user. If not, the cast app will be launched instead.
- If an Android TV app is launched, the credential data will be passed to the app, within the launch intent. The app can use this data to personalize the user experience.
public voidsetReceiverApplicationId(String applicationId)
Sets the receiver application ID. This overrides the receiver application ID
specified inCastOptions
.
Calling this method will result in the receiver application ID used for discovery and application launch being changed. The Cast button status will also changed accordingly. If the new application ID is different than the current one, the current session will be ended if there is any.
Specifying null or an empty string indicates that the app does not want to discover receiver devices or launch a receiver application.
For discovering for multiple application IDs at the same time, or for discovering
for other application ID without affect the current session, useMediaRouter
APIs instead.
Parameters
applicationId | The receiver application ID that will be used to discover the receiver device and launch the receiver application. |
---|
Throws
IllegalStateException | If this method is not called on the main thread. |
---|