Table of contents
2. Create the secondary screen DisplayPresentation class
3. Create a PresentationActivity startup class
4. The main screen controls the secondary screen
foreword
Dual-screen different display can be used in various window service systems, information release systems, such as cash registers, advertising machines, etc. Record how to use it~
1. Environmental introduction
- Android
System version: Android 7.1 Android 12
Android Studio Electric Eel | 2022.1.1jdk-18.0.2
2. Equipment introduction
scenes to be used
The Android tablet is connected to an HDMI display, and the display is a secondary screen~
3. Write code
First create an android program, so I won’t talk about the creation process~
1. Add permissions
Add permission in AndroidManifest, the code is as follows:
<!-- 申请悬浮权限 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
Dynamic application permissions:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
//启动Activity让用户授权
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1000);
return;
}
}
2. Create the secondary screen DisplayPresentation class
Inheriting Presentation, Presentation is actually a Dialog
DisplayPresentation simply implements a full-screen webview for loading web pages~
code show as below:
package com.armt.sdktest_armt.displays;
import android.app.Presentation;
import android.content.Context;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.Display;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.armt.sdktest_armt.R;
public class DisplayPresentation extends Presentation {
public WebView webView;
public DisplayPresentation(Context outerContext, Display display) {
super(outerContext, display);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_presentation);
//此处URI可能失效
addWeb("https://haokan.baidu.com/v?vid=3742103441181501443&");
}
public void setVideoUrl(String url) {
webView.loadUrl(url);
}
public void addWeb(String url) {
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setSupportZoom(true);
//设置自适应屏幕,两者合用
webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
// 设置支持JavaScript激活,可用等
webSettings.setJavaScriptEnabled(true);
/*
* 设置自身浏览器,注意:可用把WebView理解为浏览器
* 设置setWebViewClient(new WebViewClient());后,手机就不会跳转其他的浏览器
*/
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// 注掉 super
// super.onReceivedSslError(view, handler, error);
// 接受所有网站的证书,忽略SSL错误,执行访问网页,解决网页空白的问题
handler.proceed();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
}
}
display_presentation.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. Create a PresentationActivity startup class
1. Get the screen management class 2. Obtain the display data of the secondary screen 3. Create the secondary screen interface DisplayPresentation 4.show() displays the secondary screen The code is as follows (example):
//屏幕管理类
DisplayManager mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
//获取副屏
Display[] presentationDisplays = mDisplayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
if (presentationDisplays.length > 0) {
mPresentation = new DisplayPresentation(this, presentationDisplays[presentationDisplays.length - 1]);
}
//获取全部显示屏
//Display[] displays = mDisplayManager.getDisplays();
//mPresentation = new DisplayPresentation(this, displays[displays.length - 1]);
mPresentation.show();
//另一种方法
/*MediaRouter mediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);
MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
if (route != null) {
Display presentationDisplay = route.getPresentationDisplay();
if (presentationDisplay != null) {
mPresentation = new DisplayPresentation(this, presentationDisplay);
mPresentation.show();
}
}
*/
4. The main screen controls the secondary screen
After obtaining the DisplayPresentation object, you can modify and change data on the main screen interface, or use a framework such as EventBus to transfer data
Called in the PresentationActivity class:
mPresentation.setVideoUrl("https://www.qq.com");
Functions implemented in DisplayPresentation:
public void setVideoUrl(String url) {
webView.loadUrl(url);
}
5. Complete code link:
Dual screen different display code
Under the displays package~