Presentation雙屏異顯簡述

0.前言

Android 從4.2開始支持雙屏顯示,請確保minSdkVersion >= 17

Android 雙屏默認(rèn)是鏡像模式,投射主屏UI,如果要在副屏上顯示不同內(nèi)容,需要自定義一個Presentation類。

1.Presentation

Presentation是一種特殊的對話框,它是Dialog的子類,在創(chuàng)建的時候需要和特定的Display相關(guān)聯(lián)。

1.1 創(chuàng)建Presentation

public class MyPresentation extends Presentation {

    /**
     * 重寫構(gòu)造函數(shù)
     *
     * @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顯示在哪個設(shè)備上。要選擇顯示在哪個設(shè)備可能是一件非常困難的事情,因?yàn)榭赡艽藭r系統(tǒng)中有多個顯示設(shè)備。應(yīng)用程序應(yīng)該讓系統(tǒng)選擇合適的Display,而不是試圖猜測哪個顯示最佳。Android系統(tǒng)為我們提供了兩種方式選擇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.副屏不隨主屏幕退出

通常創(chuàng)建Presentation傳入的是Activity的上下文,副屏?xí)S創(chuàng)建它的Activity顯示或隱藏,這里討論創(chuàng)建全局副屏。

用的技術(shù)也不復(fù)雜,只是給Presentation添加系統(tǒng)彈窗權(quán)限。

前面說了傳入Presentation的上下文可以是ApplicationService,前提是必須加入系統(tǒng)彈窗權(quán)限,否則會崩潰;這樣就可以在任何地方新建顯示Presentation

2.1 加入權(quán)限

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

2.2 在Presentation中添加如下代碼:

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

2.3 補(bǔ)充

  • 系統(tǒng)彈窗權(quán)限的Presentation可以做全局的廣告頁面;

  • 系統(tǒng)彈窗權(quán)限的Presentation會覆蓋普通的Presentation,造成其無法顯示;請先dismiss全局Presentation后再顯示普通副屏,為了使切換流暢,建議如下代碼:

    normalPresentation.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            globalPresentation.dismiss();
        }
    });
    
  • Presentation實(shí)際上是一個Dialog,所以里面無法彈出Dialog、PopupWindow等依賴于Activity的小窗口,同樣也無法使用Fragment。

參考

Presentation官方文檔

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容