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() {
        //关闭页面前注销资源
    }
}
相关推荐
lxysbly12 分钟前
安卓PCE模拟器下载指南:用手机玩 PC-Engine / TurboGrafx 经典
android·智能手机
Digitally2 小时前
哪款应用最适合将数据从安卓手机传输到 iPhone?
android·智能手机·iphone
Java小白,一起学习3 小时前
新版onenet云平台数据流对接,包括设备端MQTT和应用端API
android·物联网
走在路上的菜鸟4 小时前
Android学Flutter学习笔记 第一节 Android视角认知Flutter(View,intent,Async UI)
android·学习·flutter
一起搞IT吧4 小时前
相机Camera日志实例分析之十二:相机Camx【萌拍后置zoom拍照】单帧流程日志详解
android·c++·数码相机·智能手机
冬奇Lab4 小时前
一次 Android 车机黑屏问题的深度剖析:当显示驱动遇上中断风暴
android·性能优化·debug
兮动人4 小时前
Fatal error: Uncaught think\exception\ErrorException: SourceGuardian Loade
android·php
笔夏4 小时前
【安卓学习之myt】adb常用命令
android·学习·adb
lxysbly4 小时前
安卓gba模拟器下载
android
bst@微胖子5 小时前
CrewAI+FastAPI实现多Agent协作完成软件编码项目
android·fastapi