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() {
        //关闭页面前注销资源
    }
}
相关推荐
冰珊孤雪11 小时前
Android Studio Panda革命性升级:内存诊断、构建标准化与AI调试全解析
android·前端
_李小白12 小时前
【OSG学习笔记】Day 23: ClipNode(动态裁剪)
android·笔记·学习
Eagsen CEO12 小时前
如何让 Gemini 在 Android Studio 中顺利工作
android·ide·android studio
ywf121513 小时前
FlinkCDC实战:将 MySQL 数据同步至 ES
android·mysql·elasticsearch
鹏程十八少14 小时前
9. Android Shadow插件化如何解决资源冲突问题和实现tinker热修复资源(源码分析4)
android·前端·面试
gechunlian8814 小时前
MySQL - Navicat自动备份MySQL数据
android·数据库·mysql
快乐非自愿14 小时前
MySQL优化全攻略:索引、SQL与分库分表的最佳实践
android·sql·mysql
毕设源码-钟学长14 小时前
【开题答辩全过程】以 基于Android的收支记账管理系统为例,包含答辩的问题和答案
android
新小梦14 小时前
AOSP Android13 新增Product实践
android