当前位置: 移动技术网 > IT编程>移动开发>Android > Android Presentation实现双屏异显

Android Presentation实现双屏异显

2020年03月09日  | 移动技术网IT编程  | 我要评论

杜拉拉升职记电子书下载,全国消防日是几月几日,马吉亮微博

一、概述

现在越来越多的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自动取消掉。

四、总结

在基本分析了presentation之后,主要要注意一下它的context以及和activity之间绑定的关系,其实从简单来看,它就是一个dialog,不过是可以显示在多个屏幕上。当然上述分析深度尚浅,更深入的理解和一些问题要在后续工作中多使用再继续观察。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网