Android -- 双屏异显之方法一

Android -- 双屏异显之方法一:MediaRouter

复制代码
PS:
1. 部分主板可能不支持,得验证;
2. 副屏输出可以不用连接显示屏也能正常后台运行;
3. 主屏Activity内控制副屏;
4. 副屏截图命令:
	screencap -p -d 1 <path_name>;(-d 1 副屏截屏)

使用方法:

java 复制代码
//主屏activity内
private MediaRouter mMediaRouter;
private SecondDisplay secondDisplay;

//控制副屏显隐
public void onSecondChange(View view) {
    if (secondDisplay == null) {
        showSecondScreen();
    } else {
        closeSecondScreen();
    }
}
//====================================
private void showSecondScreen() {
    if (secondDisplay != null) {
        return;
    }

    mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);
    updatePresentation();
}

private void closeSecondScreen() {
    if (secondDisplay != null) {
        secondDisplay.release();
        secondDisplay.dismiss();
        secondDisplay = null;
    }
}

private void updatePresentation() {
    // Get the current route and its presentation display.
    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 (secondDisplay != null && secondDisplay.getDisplay() != presentationDisplay) {
        secondDisplay.dismiss();
        secondDisplay = null;
    }

    // Show a new presentation if needed.
    if (secondDisplay == null && presentationDisplay != null) {
        secondDisplay = new SecondDisplay(this, presentationDisplay);
        secondDisplay.setOnDismissListener(mOnDismissListener);
        try {
            secondDisplay.show();
        } catch (WindowManager.InvalidDisplayException ex) {
            secondDisplay = null;
            ex.printStackTrace();
        }
    }
}

//副屏关闭监听
private final DialogInterface.OnDismissListener mOnDismissListener = new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        if (dialog == secondDisplay) {
            secondDisplay = null;
        }
    }
};

SecondDisplay.java (副屏类)

java 复制代码
//主要继承Presentation类
public class SecondDisplay extends Presentation {
    private static final String TAG = "SecondDisplay";

    private Context mContext;

    //构造函数
    public SecondDisplay(Context outerContext, Display display) {
        super(outerContext, display);
        this.mContext = outerContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //副屏布局,与activity内一样
        setContentView(R.layout.layout_second);

        initUI();
    }

    private void initUI() {
        //绑定控件
    }

    public void release() {
        //关闭页面前注销资源
    }
}
相关推荐
%xiao Q6 小时前
GESP C++五级-202406
android·开发语言·c++
二哈喇子!7 小时前
JavaWeb+Vue分离项目实现增删改查讲解
android
2501_944521598 小时前
Flutter for OpenHarmony 微动漫App实战:推荐动漫实现
android·开发语言·前端·javascript·flutter·ecmascript
2501_944521599 小时前
Flutter for OpenHarmony 微动漫App实战:图片加载实现
android·开发语言·前端·javascript·flutter·php
新镜10 小时前
【Flutter】LTR/RTL 阿拉伯语言/希伯来语言
android·flutter·ios·客户端
初级代码游戏12 小时前
android开发:获取手机IP和UDP广播
android·udp·获取ip
阿杰 AJie13 小时前
MySQL 聚合函数
android·数据库·mysql
孟秋与你15 小时前
【安卓】开发一个读取文件信息的简易apk
android
42nf15 小时前
Android Launcher3添加负一屏
android·launcher3·android负一屏
LcVong15 小时前
老版本Android源码在新版本IDE打开的常规报错及解决方案
android·ide