Android中Presentation如何实现双屏异显
创始人
2024-12-19 13:09:01
0次

一、概述

现在越来越多的Android设备有多个屏幕,双屏异显应用场景最多的应该就是类似于收银平台那种设备,在主屏上店员能够对点商品进行选择录入,副屏则是展示给我们的账单详情,但是它只通过了一个软件系统就实现了双屏异显这个功能,而Presentation正是这其中的关键。

二、Presentation分析

1.简述:首先从它的继承关系上来看Presentation是继承自Dialog的,就是说它其实就是一种特殊的Dialog用于在第二个屏幕上显示内容的,它在创建时会和它的目标展示屏幕相关联,包括它的context和一些配置参数等。

2.Context:然而这里提到的context和它的容器所处的context会有不同,它会用自身的context去加载presentation的布局和相关资源以此来确保在目标屏幕上能够展示正确的大小和获取合适的屏幕密度。

3.自动移除:虽说presentation和它相关联的Activity的context不同,但是他们也不是完全分离的关系,当和presentation相关联的屏幕被移除后,presentation也会自动的被移除,所以当Activity处于pause和resume的状态时Presentation也需要特别注意当前显示的内容的状态。

4.屏幕选择:因为有时候我们的Android设备有多个屏幕,所以选择合适的屏幕去展示就显得非常重要了,所以在我们显示Presentation的时候需要去让我们的系统去选择合适的屏幕来进行展示,以下是两种方式去选择一个合适的屏幕。

这是我们选择展示presentation最简单的一种方式,media router是一种系统层级的服务,它能够追踪到系统当中所有可用的音频和视屏route,当有路径被选中或取消选中,还有当适合用presentation进行显示的时候的route改变的时候它会发送一个通知,然后应用本身会监控这个通知自动的去选择presentation的展示或者隐藏,这里的推荐使用的presentation display其实只是media router推荐的,如果我们的应用需要在第二个屏幕上进行显示就使用,如果不用的话就用本地来展示内容。

利用media router去选择presentation的显示屏幕利用display manager去选择persentation的显示屏幕

DisplayManager能够监控到我们系统当中的所有连接上的显示设备,然而不是所有的设备都适用于作为副屏来进行内容展示的,比如当一个Activity想显示一个presentation在主屏幕上,其实效果就会相当于在主Activity当中显示了一个特殊的Dialog,所以当我们选择这种方式去选用Presentation去显示的时候就必须给它绑定一个可用的display。

三、源码分析

首先来看它的构造函数

public Presentation(Context outerContext, Display display, int theme) { super(createPresentationContext(outerContext, display, theme), theme, false); mDisplay = display; mDisplayManager = (DisplayManager)getContext().getSystemService(DISPLAY_SERVICE); final Window w = getWindow(); final WindowManager.LayoutParams attr = w.getAttributes(); attr.token = mToken; w.setAttributes(attr); w.setGravity(Gravity.FILL); w.setType(TYPE_PRESENTATION); setCanceledOnTouchOutside(false); }

在它的形参中第一个Context参数非常重要,这是应用正在展示presentation的一个context,它是Presentation自己创建的它自己的一个context,基于这个context才能正确的在它所关联的屏幕上展示合适的信息。然后代码里面设置了这个window的相关属性。

接着我们看看它自身的Context的创建

private static Context createPresentationContext( Context outerContext, Display display, int theme) { //首先判断传入的context和display是否为空,为空则抛出异常 if (outerContext == null) { throw new IllegalArgumentException("outerContext must not be null"); } if (display == null) { throw new IllegalArgumentException("display must not be null"); } Context displayContext = outerContext.createDisplayContext(display); //这里是对它的主题的判断,为0即为默认主题 if (theme == 0) { TypedValue outValue = new TypedValue(); displayContext.getTheme().resolveAttribute(  com.android.internal.R.attr.presentationTheme, outValue, true); theme = outValue.resourceId; } // Derive the display's window manager from the outer window manager. // We do this because the outer window manager have some extra information // such as the parent window, which is important if the presentation uses // an application window type. final WindowManagerImpl outerWindowManager = (WindowManagerImpl)outerContext.getSystemService(WINDOW_SERVICE); final WindowManagerImpl displayWindowManager = outerWindowManager.createPresentationWindowManager(displayContext); //因为ContextThemeWrapper的父类是我们的Context //所以这里最终返回的就是Presentation给我们创建好的Context return new ContextThemeWrapper(displayContext, theme) { //在这个方法中又返回的是Object对象 @Override public Object getSystemService(String name) { if (WINDOW_SERVICE.equals(name)) {  return displayWindowManager;  //如果和这个传入的name相同的话返回的就是上面windowManager创建出来的WindowManager的一个具体实现 } //否则就返回的是Context这个抽象类中的一种服务类型 return super.getSystemService(name); } }; }

接着我们继续看一下Presentation对屏幕增加、移除和改变的监听

private final DisplayListener mDisplayListener = new DisplayListener() { @Override public void onDisplayAdded(int displayId) { } @Override public void onDisplayRemoved(int displayId) { if (displayId == mDisplay.getDisplayId()) { handleDisplayRemoved(); } } @Override public void onDisplayChanged(int displayId) { if (displayId == mDisplay.getDisplayId()) { handleDisplayChanged(); } } };

这里我们看到它对add并没有进行处理,所以我们进一步去看一下onDisplayRemoved中的关键handlerDisplayRemoved和onDisplayChanged中的核心handlerDisplayChanged的实现

handlerDisplayChanged:

private void handleDisplayChanged() { onDisplayChanged(); // We currently do not support configuration changes for presentations // (although we could add that feature with a bit more work). // If the display metrics have changed in any way then the current configuration // is invalid and the application must recreate the presentation to get // a new context. if (!isConfigurationStillValid()) { Log.i(TAG, "Presentation is being dismissed because the "  + "display metrics have changed since it was created."); cancel(); } }

在这个方法中,我们遗憾的发现Google程序员并没有对当前屏幕配置发生改变后做特殊的处理,所以当我们的屏幕尺寸等信息改变时,我们的presentation必须重新Create去重新获取context,再重新进行显示。

@Override protected void onStart() { super.onStart(); mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); // Since we were not watching for display changes until just now, there is a // chance that the display metrics have changed. If so, we will need to // dismiss the presentation immediately. This case is expected // to be rare but surprising, so we'll write a log message about it. if (!isConfigurationStillValid()) { Log.i(TAG, "Presentation is being dismissed because the "  + "display metrics have changed since it was created."); mHandler.sendEmptyMessage(MSG_CANCEL); } }

同时在onStart中我们也能发现,因为它暂时还没有对display change进行监听,而这里又是有可能会改变的,所以在这种情况下它是打印了一个log去通知一下。

handlerDisplayRemoved这个方法的话是系统自动去发送一个消息,然后调用后把presentation自动取消掉。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...