Android双屏异显简单上手

获得 Display

1.一、使用MediaRouter选择演示显示

1
2
3
4
5
6
7
8
MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute();
if (route != null) {
Display presentationDisplay = route.getPresentationDisplay();
if (presentationDisplay != null) {
// ***
}
}

2.二、使用DisplayManager选择演示显示

1
2
3
4
5
6
7
8
DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
if (presentationDisplays.length > 0) {
// If there is more than one suitable presentation display, then we could consider
// giving the user a choice. For this example, we simply choose the first display
// which is the one the system recommends as the preferred presentation display.
///
}

将 View 展示到第二个 Display 上(Presention)

1
2
3
Presentation presentation = new Presentation(this, display);
presentation.setContentView(flutterView);
presentation.show();

将 Activity 展示到第二个 Display 上

1
2
3
4
5
6
7
8
9
10
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityOptions options = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
options = ActivityOptions.makeBasic().setLaunchDisplayId(display.getDisplayId());
}
ComponentName cName = new ComponentName(this, MainActivity.class);
intent.setComponent(cName);
startActivity(intent, options.toBundle());

Android 为了防止应用通过这种方式恶意获取其他 App 其他的 Display 是辅助屏才能正常跳转,如果另外的 Display 是虚拟显示器,则只能跳转本 App 中的 Activity

作者

梦魇兽

发布于

2023-02-02

更新于

2023-03-12

许可协议

评论