我正在开发一个同时支持 Chromecast 和 ActionbarSherlock 的应用程序。
在每个活动中,我都会在 ActionBar 中创建一个 MediaRouteButton。
我的问题是,我在第一个 Activity 中没有连接到 Chromecast 设备,然后转到第二个 Activity。在第二个中,我连接到 Chromecast 设备。它跑进了onRouteSelected()
第二个Activity,然后跑进了onDeviceAvailable()
->openSession()
第一个Activity。
我不知道为什么它没有onDeviceAvailable()
在第二个活动中遇到。
谁能帮我?
提前致谢
这是我在这两个活动中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cast_sample);
mMetaData = new ContentMetadata();
mMediaSelectionDialog = new MediaSelectionDialog(this);
mCastContext = new CastContext( getApplicationContext());
MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this );
mMediaRouter = MediaRouter.getInstance( getApplicationContext() );
mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST );
mMediaRouterCallback = new MyMediaRouterCallback();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.cast_sample, menu);
com.actionbarsherlock.view.MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
mMediaRouteButton = (MediaRouteButton) mediaRouteMenuItem.getActionView();
mMediaRouteButton.setRouteSelector( mMediaRouteSelector );
return true;
}
@Override
protected void onStart() {
super.onStart();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
@Override
protected void onStop() {
mMediaRouter.removeCallback(mMediaRouterCallback);
super.onStop();
}
private class MyMediaRouterCallback extends MediaRouter.Callback {
@Override
public void onRouteSelected(MediaRouter router, RouteInfo route) {
MediaRouteHelper.requestCastDeviceForRoute(route);
}
@Override
public void onRouteUnselected(MediaRouter router, RouteInfo route) {
try {
if (mSession != null) {
logVIfEnabled(TAG, "Ending session and stopping application");
mSession.setStopApplicationWhenEnding(true);
mSession.endSession();
} else {
Log.e(TAG, "onRouteUnselected: mSession is null");
}
} catch (IllegalStateException e) {
Log.e(TAG, "onRouteUnselected:");
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "onRouteUnselected:");
e.printStackTrace();
}
mMessageStream = null;
mSelectedDevice = null;
}
}
@Override
public void onDeviceAvailable(CastDevice device, String myString,
MediaRouteStateChangeListener listener) {
mSelectedDevice = device;
logVIfEnabled(TAG, "Available device found: " + myString);
openSession();
}
private void openSession() {
mSession = new ApplicationSession(mCastContext, mSelectedDevice);
.....
}