對於雙屏異顯(lcd 和 hdmi 的雙屏異顯),android框架已經支持,但是底層接口功能還是要自己去實現,且需要底層驅動支持。
使用presentation 去畫第二個display就好了。
1 MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); 2 MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(); 3 if(route != null) { 4 Display presentationDisplay = route.getPresentationDisplay(); 5 if (presentationDisplay != null) { 6 Presentation presentation = new MyPresentation(context, presentationDisplay); 7 presentation.show(); 8 } 9 }
應用在輔助顯示器上顯示不同的內容程序,以有線或Wi-Fi將外接顯示輸出連接到用戶設備上,顯示獨特的內容。
-------------------------------------------------------------
注意:public Presentation (Context outerContext, Display display) 這個初始化函數,這里的outerContext必須要activity,一個activity的context,雖然presentation會創建自己的context,但是它是在這個參數context之上的,而且這個activity跳轉后presentation就消失了,如果說用getApplicationContext()就直接報錯,也不行。
-------------------------------------------------------------
要為輔助顯示屏創建獨特的內容:
1. 您需要擴展Presentation類,並實現onCreate()回調方法。在onCreate()中,調用setContentView()來指定您要在輔助顯示屏上顯示的UI。
2. 作為Dialog類的擴展,Presentation類提供了一個區域,在其中,您的應用可以在輔助顯示屏上顯示不同的UI。
獲取顯示Presentation的輔助顯示屏:
1. 可以使用DisplayManager或者MediaRouter的API。其中,使用DisplayManagerAPI可以使您獲得當前連接的所有顯示屏的枚舉,而MediaRouter則可以使您直接訪問系統為Presentation設置的默認顯示輸出。
2. 可以給MediaRouter.getSelectedRoute()傳一個ROUTE_TYPE_LIVE_VIDEO來獲得演示的默認顯示器。它將返回一個MediaRouter.RouteInfo對象,描述了系統為視頻演示所選擇的當前路由。如果MediaRouter.RouteInfo不空,調用getPresentationDisplay()即可獲取當前連接的顯示屏對象:Display。
3. 然后,您可以將這個Display對象傳入Presentation的構造函數。調用Presentation.show()方法,演示就會出現在輔助顯示屏上了。
為在運行時即時檢測新接入的顯示器,需要先創建一個MediaRouter.SimpleCallback的實例。在其中,您需要實現onRoutePresentationDisplayChanged()回調方法。當新的顯示器連接時,系統會調用這個回調方法。
1 private final MediaRouter.SimpleCallback mMediaRouterCallback = 2 new MediaRouter.SimpleCallback() { 3 @Override 4 public void onRouteSelected(MediaRouter router, int type, RouteInfo info) { 5 Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info); 6 updatePresentation(); 7 } 8 9 @Override 10 public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) { 11 Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info); 12 updatePresentation(); 13 } 14 15 @Override 16 public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) { 17 Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info); 18 updatePresentation(); 19 } 20 };
1 private void updatePresentation() { 2 // Get the current route and its presentation display. 3 MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute( 4 MediaRouter.ROUTE_TYPE_LIVE_VIDEO); 5 Display presentationDisplay = route != null ? route.getPresentationDisplay() : null; 6 7 // Dismiss the current presentation if the display has changed. 8 if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) { 9 Log.i(TAG, "Dismissing presentation because the current route no longer " 10 + "has a presentation display."); 11 mPresentation.dismiss(); 12 mPresentation = null; 13 } 14 15 // Show a new presentation if needed. 16 if (mPresentation == null && presentationDisplay != null) { 17 Log.i(TAG, "Showing presentation on display: " + presentationDisplay); 18 mPresentation = new DemoPresentation(this, presentationDisplay); 19 mPresentation.setOnDismissListener(mOnDismissListener); 20 try { 21 mPresentation.show(); 22 } catch (WindowManager.InvalidDisplayException ex) { 23 Log.w(TAG, "Couldn't show presentation! Display was removed in " 24 + "the meantime.", ex); 25 mPresentation = null; 26 } 27 } 28 29 // Update the contents playing in this activity. 30 updateContents(); 31 }
1 private void updateContents() { 2 // Show either the content in the main activity or the content in the presentation 3 // along with some descriptive text about what is happening. 4 if (mPresentation != null) { 5 mInfoTextView.setText(getResources().getString( 6 R.string.presentation_with_media_router_now_playing_remotely, 7 mPresentation.getDisplay().getName())); 8 mSurfaceView.setVisibility(View.INVISIBLE); 9 mSurfaceView.onPause(); 10 if (mPaused) { 11 mPresentation.getSurfaceView().onPause(); 12 } else { 13 mPresentation.getSurfaceView().onResume(); 14 } 15 } else { 16 mInfoTextView.setText(getResources().getString( 17 R.string.presentation_with_media_router_now_playing_locally, 18 getWindowManager().getDefaultDisplay().getName())); 19 mSurfaceView.setVisibility(View.VISIBLE); 20 if (mPaused) { 21 mSurfaceView.onPause(); 22 } else { 23 mSurfaceView.onResume(); 24 } 25 } 26 }
為針對輔助顯示進一步優化Presentation的UI,您需要為您的應用或activity指定標簽為android:presentationTheme主題。
請留意,連接到用戶移動設備的屏幕往往有較大的屏幕尺寸和不同的屏幕密度。由於屏幕特征可能不同,您需要為大型顯示設備提供特定優化的資源。如果您需要從Presentation中獲取額外的資源,調用getContext().getResources()來獲取針對這種顯示的資源對象。這樣,您的應用就可以根據輔助顯示器的尺寸和密度提供最合適的資源了。
關於 Presentation api:http://android.toolib.net/reference/android/app/Presentation.html
