//主屏activity内privateMediaRouter mMediaRouter;privateSecondDisplay secondDisplay;//控制副屏显隐publicvoidonSecondChange(View view){if(secondDisplay ==null){showSecondScreen();}else{closeSecondScreen();}}//====================================privatevoidshowSecondScreen(){if(secondDisplay !=null){return;}
mMediaRouter =(MediaRouter)getSystemService(Context.MEDIA_ROUTER_SERVICE);updatePresentation();}privatevoidcloseSecondScreen(){if(secondDisplay !=null){
secondDisplay.release();
secondDisplay.dismiss();
secondDisplay =null;}}privatevoidupdatePresentation(){// Get the current route and its presentation display.MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);Display presentationDisplay = route !=null? route.getPresentationDisplay():null;// Dismiss the current presentation if the display has changed.if(secondDisplay !=null&& secondDisplay.getDisplay()!= presentationDisplay){
secondDisplay.dismiss();
secondDisplay =null;}// Show a new presentation if needed.if(secondDisplay ==null&& presentationDisplay !=null){
secondDisplay =newSecondDisplay(this, presentationDisplay);
secondDisplay.setOnDismissListener(mOnDismissListener);try{
secondDisplay.show();}catch(WindowManager.InvalidDisplayException ex){
secondDisplay =null;
ex.printStackTrace();}}}//副屏关闭监听privatefinalDialogInterface.OnDismissListener mOnDismissListener =newDialogInterface.OnDismissListener(){@OverridepublicvoidonDismiss(DialogInterface dialog){if(dialog == secondDisplay){
secondDisplay =null;}}};