Android Presentation实现双屏异显

 更新时间:2020年01月21日 14:54:52   作者:Leon_Jcy  
这篇文章主要为大家详细介绍了Android Presentation实现双屏异显,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、概述

现在越来越多的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,不过是可以显示在多个屏幕上。当然上述分析深度尚浅,更深入的理解和一些问题要在后续工作中多使用再继续观察。

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

相关文章

  • Android 实现右滑返回功能

    Android 实现右滑返回功能

    右滑返回功能在ios上非常实用,因为它的返回键在左上角,下面脚本之家小编给大家带来了Android 实现右滑返回功能,感兴趣的朋友一起看看吧
    2018-04-04
  • Android中使用imageviewswitcher 实现图片切换轮播导航的方法

    Android中使用imageviewswitcher 实现图片切换轮播导航的方法

    ImageSwitcher是Android中控制图片展示效果的一个控件。本文给大家介绍Android中使用imageviewswitcher 实现图片切换轮播导航的方法,需要的朋友参考下吧
    2016-12-12
  • android控件Spinner(下拉列表)的使用例子

    android控件Spinner(下拉列表)的使用例子

    这篇文章主要给大家介绍了关于android控件Spinner(下拉列表)的使用例子,在Android开发中下拉框(Spinner)是常用的UI控件之一,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • 自定义Android圆形进度条(附源码)

    自定义Android圆形进度条(附源码)

    这篇文章主要介绍了自定义Android圆形进度条,本文设计的进度条是圆形的,对进度条感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Android ViewFlipper的详解及实例

    Android ViewFlipper的详解及实例

    这篇文章主要介绍了Android ViewFlipper的详解及实例的相关资料,通过本文希望能帮助大家理解这部分内容,需要的朋友可以参考下
    2017-08-08
  • Android下hook点击事件的示例

    Android下hook点击事件的示例

    这篇文章主要介绍了Android下hook点击事件的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • android RadioGroup的使用方法

    android RadioGroup的使用方法

    android RadioGroup的使用方法,需要的朋友可以参考下
    2012-11-11
  • android 判断横竖屏问题的详解

    android 判断横竖屏问题的详解

    本篇文章是对android中如何判断横竖屏的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • 解析android中include标签的使用

    解析android中include标签的使用

    本篇文章是对android中include标签的使用进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • Android实现界面的自动跳转功能

    Android实现界面的自动跳转功能

    界面自动跳转是指在应用启动或某个特定界面显示后,经过预定的时间或者满足某些条件后,自动跳转到另一个目标界面,本文小编给大家讲解了Android实现界面的自动跳转功能,感兴趣的小伙伴跟着小编一起来看看吧
    2025-04-04

最新评论