【发布时间】:2020-05-07 01:58:47
【问题描述】:
在我的场景中,我想在我的应用中镜像另一个应用。
我使用DisplayManager 到createVirtualDisplay 作为我的SurfaceView。然后我在开始活动时将DisplayId 设置为VirtualDisplay 中的AcitivityOptions。
通过这个过程,可以在我的surfaceview中成功启动另一个应用程序。但是有一些显示问题。根据我的测试,只有图像、背景颜色和GLSurfaceView.Renderer 被正确渲染。 TextView 和 Button 等 ui 组件不会在 SurfaceView 中呈现。
从上面的测试结果,我猜可能和渲染的深度或者顺序有关。
示例代码如下:
SurfaceView surfaceView = findViewById(R.id.surface_view);
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
VirtualDisplay virtualDisplay = displayManager.createVirtualDisplay("MyVirtualDisplay",
surfaceView.getWidth(), surfaceView.getHeight(), 1, surfaceView.getHolder().getSurface(),
flags);
int displayId = virtualDisplay.getDisplay().getDisplayId();
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(displayId);
Intent intent = getPackageManager().getLaunchIntentForPackage(appName);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent, options.toBundle());
相比之下,如果我将相同的活动镜像到Display 而不是VirtualDisplay,则可以由MediaRouter 或DisplayManager 捕获。这样,一切都显示正确(在我的测试中,我通过“开发人员选项”->“模拟辅助显示器”模拟第二个显示器):
MediaRouter mediaRouter = (MediaRouter)getSystemService(Context.MEDIA_ROUTER_SERVICE);
MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
Display presentationDisplay = route.getPresentationDisplay();
int displayId = presentationDisplay.getDisplayId();
Intent intent = getPackageManager().getLaunchIntentForPackage(appName);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(displayId);
startActivity(intent, options.toBundle());
在虚拟显示模式下是否有我错过的任何进程或我错误配置的任何标志?感谢您的帮助。
附:为了镜像另一个应用程序,我已经root并作为系统应用程序启动。
【问题讨论】:
标签: android api user-interface display android-virtualdisplay