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() {
        //关闭页面前注销资源
    }
}
相关推荐
allk5517 分钟前
OkHttp源码解析(一)
android·okhttp
allk5518 分钟前
OkHttp源码解析(二)
android·okhttp
2501_915909063 小时前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
2501_915909064 小时前
苹果软件混淆与 iOS 代码加固趋势,IPA 加密、应用防反编译与无源码保护的工程化演进
android·ios·小程序·https·uni-app·iphone·webview
2501_916007474 小时前
苹果软件混淆与 iOS 应用加固实录,从被逆向到 IPA 文件防反编译与无源码混淆解决方案
android·ios·小程序·https·uni-app·iphone·webview
介一安全4 小时前
【Frida Android】基础篇6:Java层Hook基础——创建类实例、方法重载、搜索运行时实例
android·java·网络安全·逆向·安全性测试·frida
沐怡旸7 小时前
【底层机制】【Android】深入理解UI体系与绘制机制
android·面试
啊森要自信7 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
下位子9 小时前
『AI 编程』用 Codex 开发识字小帮手应用
android·openai·ai编程
Zender Han9 小时前
Flutter 实现人脸检测 — 使用 google_mlkit_face_detection
android·flutter·ios