我正在尝试使用 JellyBean 中的 Presenter 功能在额外的大屏幕上显示自定义图形。我连接了符合 MHL 标准的 HDMI 适配器(microUSB -> HDMI)。下面是我试图检测外部显示器的一段代码。出于某种原因 - 我确实在外接显示器上获得了输出,但 DisplayManager 不会检测到外接显示器。我想知道这是否与适配器有关,或者如果我直接使用 HDMI 电缆将手机连接到外接显示器会是这种情况?
我的日志猫:http: //postimg.org/image/sloflge1b/
我的代码片段:
@SuppressLint("NewApi")
private void updatePresentation() {
// Get the current route and its presentation display.
Log.d(EXTRA_DISPLAY_TAG, "Inside updatePresentation() call...");
MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
Log.d(EXTRA_DISPLAY_TAG, "Route name: " + route.getName() + " , route status: " + route.getStatus());
Display externalDisplay = null;
if(route != null && route.getName().equals("HDMI")){
externalDisplay = route.getPresentationDisplay();
if(externalDisplay == null){ //Maybe not ready...
Log.d(EXTRA_DISPLAY_TAG, "Waiting for external display to become ready...");
SystemClock.sleep(2000);
DisplayManager displayManager = (DisplayManager)getSystemService(DISPLAY_SERVICE);
Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
if(null == presentationDisplays || presentationDisplays.length == 0){
Log.d(EXTRA_DISPLAY_TAG, "Didn't find any presentation displays by category...");
}else{
externalDisplay = presentationDisplays[0]; //OK: Take first
}
//Try to manually select display...
if(externalDisplay == null){
Display[] allDisplays = displayManager.getDisplays();
for(int i=0; i<allDisplays.length; i++){
Log.d(EXTRA_DISPLAY_TAG, "Detected display "+(i+1)+ " : " + allDisplays[i].getName());
}
//externalDisplay = allDisplays[0]; //FIXME: Take by some criteria...
}
}
}
if(externalDisplay != null){
Log.i(EXTRA_DISPLAY_TAG, "Detected external display...");
Point size = new Point();
externalDisplay.getSize(size);
int width = size.x;
int height = size.y;
Log.i(EXTRA_DISPLAY_TAG, "External display resolution: " + width + "px x " + height + "px");
}
// Dismiss current presentation if display changes
if(quantumPresentation != null && quantumPresentation.getDisplay() != externalDisplay){
Log.i(EXTRA_DISPLAY_TAG, "Leaving presentation because current route no longer has a presentation display.");
quantumPresentation.dismiss();
quantumPresentation = null;
}
// Show new presentation if needed
if(quantumPresentation == null && externalDisplay != null) {
Log.i(EXTRA_DISPLAY_TAG, "Showing presentation on display: " + externalDisplay);
quantumPresentation = new QuantumPresentation(this, externalDisplay);
quantumPresentation.setOnDismissListener(onPresentationDismissListener);
try{
Log.i(EXTRA_DISPLAY_TAG, "Starting additional display presentation...");
quantumPresentation.show();
} catch (WindowManager.InvalidDisplayException ex){
Log.w(EXTRA_DISPLAY_TAG, "Couldn't show presentation! External display was removed in the meantime!", ex);
quantumPresentation = null;
}
}
// Update the contents playing in activity...
updateContents();
}