presentation

Presentation双屏异显简述

2018-12-12  本文已影响168人  三生明理

0.前言

Android 从4.2开始支持双屏显示,请确保minSdkVersion >= 17

Android 双屏默认是镜像模式,投射主屏UI,如果要在副屏上显示不同内容,需要自定义一个Presentation类。

1.Presentation

Presentation是一种特殊的对话框,它是Dialog的子类,在创建的时候需要和特定的Display相关联。

1.1 创建Presentation

public class MyPresentation extends Presentation {

    /**
     * 重写构造函数
     *
     * @param outerContext 上下文不限于Activity,也可以是ApplicationContext或Service等
     * @param display      副屏的Display
     */
    public MyPresentation(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xxx);
        TextView textView = findViewById(R.id.text);
    }
}

1.2 选择Display

在将presentation显示出来之前,最重要的事情就是选择要将presentation显示在哪个设备上。要选择显示在哪个设备可能是一件非常困难的事情,因为可能此时系统中有多个显示设备。应用程序应该让系统选择合适的Display,而不是试图猜测哪个显示最佳。Android系统为我们提供了两种方式选择Display

1.2.1 MediaRouter
MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(
                MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;

// Dismiss the current presentation if the display has changed.
if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {
    mPresentation.dismiss();
    mPresentation = null;
}

// Show a new presentation if needed.
if (mPresentation == null && presentationDisplay != null) {
    mPresentation = new MyPresentation(context, presentationDisplay);
    try {
        mPresentation.show();
    } catch (WindowManager.InvalidDisplayException ex) {
        mPresentation = null;
    }
}
1.2.2 DisplayManager
DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
if (presentationDisplays.length > 0) {
    Display display = presentationDisplays[0];
    if (mPresentation != null && mPresentation.getDisplay() != display) {
        mPresentation.dismiss();
        mPresentation = null;
    }
    if (mPresentation == null) {
        mPresentation = new MyPresentation(context, display);
        try {
            mPresentation.show();
        } catch (WindowManager.InvalidDisplayException e) {
            mPresentation = null;
        }
    }
}

2.副屏不随主屏幕退出

通常创建Presentation传入的是Activity的上下文,副屏会随创建它的Activity显示或隐藏,这里讨论创建全局副屏。

用的技术也不复杂,只是给Presentation添加系统弹窗权限。

前面说了传入Presentation的上下文可以是ApplicationService,前提是必须加入系统弹窗权限,否则会崩溃;这样就可以在任何地方新建显示Presentation

2.1 加入权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

2.2 在Presentation中添加如下代码:

getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

2.3 补充

参考

Presentation官方文档

上一篇 下一篇

猜你喜欢

热点阅读