Operation of Android Presentation dual screen display and sub screen

Recently, there is a demand for dual screen display. At that time, I didn't know how to proceed with it. I did hope to find a solution. After Android version 4.2, the Presentation class was provided, which can easily display different contents on two screens at the same time. Take notes.

Presentation is a special dialog for displaying content to the second screen. When creating a presentation, associate a target device, determine which device the presentation will be displayed on, and configure the context and resources information of the presentation according to the information of the device.

Upper code

public class MainActivity extends AppCompatActivity {

private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webView);
    webView.loadUrl("https://www.baidu.com/");
    webView.getSettings().setJavaScriptEnabled(true);
	//Here's the main code
    setCustomerProductList();
}
@SuppressLint("NewApi")
private void setCustomerProductList() {
    DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
    Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
    if (presentationDisplays.length > 0) {
        Display display = presentationDisplays[0];
        Presentation presentation = new DifferentDislay(this, display);
        presentation.show();
    }
}
}

Create a new class to inherit Presentation

public class DifferentDislay extends Presentation {

    public DifferentDislay(Context outerContext, Display display) {
        super(outerContext,display);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        
    }
}

Operate on the secondary screen, or jump to the page

public class DifferentDislay extends Presentation {
private Context context;
private TextView img;
private Display display;

    public DifferentDislay(Context outerContext, Display display) {
        super(outerContext,display);
        this.context = outerContext;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        img = findViewById(R.id.img);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Presentation presentation = new PresentationActivity(context, display);
                presentation.show();
            }
        });
    }
}

Sub screen jump page is to jump to a new Presentation

Don't forget to add authority

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<!-- Display at the top of the screen addview-->
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

Tags: Android

Posted on Fri, 22 May 2020 08:43:13 -0700 by Sxooter