android 副屏连接

文章目录

Presentation承自 Dialog,专门用于多屏异显场景。用来在副屏 / 外部显示器上显示独立界面的组件。


文章目录


前言

最近整理过去项目,有副屏相关内容,现在单独拿出来


一、Presentation是什么?

承自 Dialog,专门用于多屏异显场景。用来在副屏 / 外部显示器上显示独立界面的组件

二、使用步骤

1.找到要控制的副屏

c 复制代码
    private Display[] displays;
    private DisplayManager displayManager;
     void function()
     {
                displayManager=(DisplayManager)getSystemService(DISPLAY_SERVICE);
                displays=displayManager.getDisplays();
                Log.i(TAG,"屏幕数目:"+displays.length);
                if(displaye.length<2)
                return;
                //打开要控制的副屏
                 if(aboutPresentation==null)
                    aboutPresentation=new APresentation(this,displays[1]);
                aboutPresentation.show();
     }
 

2.创建 Presentation 子类

在子类里 onCreate() 里 setContentView()

c 复制代码
ublic abstract class BasePresentation extends Presentation {
    private Context context;
    public BasePresentation(Context outCon, Display display)
    {
        super(outCon,display);
        context=outCon;
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, @Nullable Menu menu, int deviceId) {
        super.onProvideKeyboardShortcuts(data, menu, deviceId);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = LayoutInflater.from(context).inflate(getLayoutResId(), null, false);
        setContentView(inflate);
        initView(inflate);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onDisplayRemoved() {
        super.onDisplayRemoved();
    }

    public abstract void initView(View inflate);

    public abstract int getLayoutResId();

}

3.调用 Presentation 子类

在副屏打开网页

csharp 复制代码
public class APresentation extends BasePresentation{
    private static final String TAG="APresentation";
    private static final Logger log = LoggerFactory.getLogger(PresentationIniti.class);
    public static AndroidWebView web;
    public static WebView view;
    private TextView textView;
    public static MainPresentation mainPresentation;
    public APresentation(Context outCon, Display display) {
        super(outCon, display);
        mContest=outCon;
        setContentView(R.layout.start_main_layout);
    }

    @Override
    public  void initView( View inflate)
    {
        view=(WebView) findViewById(R.id.web_tbs);
        web=new AndroidWebView();
        String web_url= "https://chat.deepseek.com/";
        if(web_url!=null)
        {
            web.onShowWebView(view,web_url);
        }
        else
        {
            String url="https://cn.bing.com/";
            web.onShowWebView(view,url);
        }

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
    }

    @Override
    public  int getLayoutResId()
    {   //加载layout
        return R.layout.webview_layout;
    }

  
}

4.副屏界面


csharp 复制代码
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.tencent.smtt.sdk.WebView
        android:id="@+id/web_tbs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
    </com.tencent.smtt.sdk.WebView>
</RelativeLayout>

总结

可以调用副屏

相关推荐
慕容雪_19 小时前
Capacitor8-Android打包完整手册
android·打包·capacitor
阿杰m1 天前
Android源码第十三章:Bluetooth——沿一次发现走到控制器
android
booboosui1 天前
派活给另一个 AI:前后两次文件夹快照,就是交付清单
android
普通人61 天前
MySQL_FIND_IN_SET函数详解
android·数据库·mysql
好的佩奇2 天前
Dart 之集合元素
android·flutter·dart
Easyhood2 天前
Android 开发入门实践(十四):Compose 副作用入门与四个常用 API
android·开发语言·kotlin·android jetpack·compose
FlightYe2 天前
音视频修炼之容器与协议(四):RTSP详解
android·linux·ffmpeg·音视频·dash
疯华整猫2 天前
删掉 LeakCanary 那天,我用 Trae 自己撸了一套:最难的不是分析堆,是决定什么时候 dump
android·人工智能·性能优化
mmsx2 天前
耳切法切带洞多边形:论文能跑通,真实数据为什么崩
android·kotlin
洞见前行2 天前
你的 Activity 关了,为什么还活着?Android 内存泄漏从发现、定位到线上治理
android·性能优化·leakcanary