问题
I would like to only activate the Cast feature due to certain criteria so I don't want any Cast logic in my onCreate. I have a setupCast method that has the following code:
private void setupCast(String appId) {
if (appId != null) {
Log.d(TAG, "Setting up Cast...");
setupCastListener();
CastOptionsProvider.setAppId(appId);
mCastContext = CastContext.getSharedInstance(_movieActivityContext);
mCastContext.registerLifecycleCallbacksBeforeIceCreamSandwich(this, _savedInstanceState);
mCastSession = mCastContext.getSessionManager().getCurrentCastSession();
mCastContext.getSessionManager().addSessionManagerListener(
mSessionManagerListener, CastSession.class);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), _menuForChromecastButton, R.id.media_route_menu_item);
}
}
The application receiver id is passed in through an http call and if the appId is null, don't start Cast. The problem I'm having is that my Cast button does not show up when initializing mCastContext this way.
However, if I move only:
mCastContext = CastContext.getSharedInstance(_movieActivityContext);
Into my onCreate method and keep everything else the way it is, the Cast button shows up.
回答1:
You can use onStart(android.content.Intent, int, int). Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Note that the system calls this on your service's main thread. A service's main thread is the same thread where UI operations take place for Activities running in the same process. You should always avoid stalling the main thread's event loop. When doing long-running operations, network calls or heavy disk I/O, you should kick off a new thread or use AsyncTask.
To properly initialize CastContext, the application must have a class that implements OptionsProvider 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.
来源:https://stackoverflow.com/questions/38731002/how-to-initialize-castcontext-outside-of-oncreate-method