Android9 双屏异显实现方式思路

Camille ·
更新时间:2024-09-20
· 1445 次阅读

目录

双屏异显的三种方式

1、通过adb命令将app启动在指定的屏幕

2、通过Presentation

3. 通过startActivity

双屏异显的三种方式 1、通过adb命令将app启动在指定的屏幕 adb shell am start -n com.android.demo/com.android.demo.MainActivity --display 1 adb shell am start -n com.android.demo/com.android.demo.MainActivity --user 0 --display 1

参数--display指定屏幕, display 0,表示第一块屏幕; display 1,表示第2块屏幕。

参数--user可以启动指定的用户,在多用户下有效,系统默认是--user 0。

2、通过Presentation

Presentation是一个特殊的dialog,它的目的是显示内容到第二屏幕。在Presentation创建的时候关联一个目标设备,确定Presentation要显示在哪个设备上,根据这个设备的信息来配置Presentation的context和resources信息。

获取辅助屏幕有两种方式:MediaRouter或者DisplayManager

MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute();---先获取RouteInfo,再获取Display //MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(ROUTE_TYPE_USER) if(route != null) { Display presentationDisplay = route.getPresentationDisplay(); if (presentationDisplay != null) { Presentation presentation = new MyPresentation(context, presentationDisplay); presentation.show(); } } DisplayManager mDisplayManager = getSystemService(Context.DISPLAY_SERVICE); Display displays = mDisplayManager.getDisplay(1); 3. 通过startActivity ActivityOptions options = ActivityOptions.makeBasic(); options.setLaunchDisplayId(1); //这里一直display0是第一块屏;display1是第二块屏 Intent secondIntent = new Intent(); ComponentName cn= new ComponentName("com.android.demo","com.android.demo.SecondActivity"); secondIntent .setComponent(cn); //该句很重要,不添加则无法推送到副屏 secondIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(secondIntent, options.toBundle()); 使用ActivityOptions指定显示屏幕 为Intent增加标志Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK

备注:通过Activity的方式显示在副屏上会带来一些问题

1、SoftInputWindow,Toast只显示在主屏

当在副屏的Activity中调用上面控件时,控件会显示到主屏上

PopupWindow、Dialog可以正常显示

但是传入的Context必须是当前Activity的

以上问题需要修改Framwork适配控件,更多关于Android9 双屏异显的资料请关注软件开发网其它相关文章!



Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号