“This is the 23rd day of my participation in the First Challenge 2022. For details: First Challenge 2022”
How to implement the display function of Android devices
Everyone knows about wireless or wired screen casting, but how many of you know about the alien feature in Android? The purpose of this article is to give you a preliminary understanding of the visualization and how to implement simple visualization.
First, what is aberration
In wireless or wired screen casting, the image of the Android device is projected onto the monitor intact, which is called screen casting. In contrast, the screen of the Android device and the monitor are displayed separately. For example, the android device is chatting, and the monitor can also play movies. Is it a bit like the multi-window feeling? Of course, Android devices need to support VGA/HDMI displays. Which devices are supported? I found in a store that only some devices are supported.
In addition, some Android devices support both screen casting and Office mode, while some Android Settings only support screen casting. The office mode of a certain brand mobile phone
Two, implementation steps
2.1 permissions
- Static permissions
Before implementing the screen casting function, you should go to the permission to operate the screen casting function, which is actually the permission to display the system window in the configuration file
<! - window display system permissions - > < USES - permission android: name = "android. Permission. SYSTEM_ALERT_WINDOW" / > <! - in the top screen - > < USES - permission android: name = "android. Permission. SYSTEM_OVERLAY_WINDOW" / >Copy the code
- Dynamic permissions
if (Build.VERSION.SDK_INT >= 23) { if (! Settings.canDrawOverlays(MainActivity.this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 10); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 10) { if (Build.VERSION.SDK_INT >= 23) { if (! Settings.canDrawOverlays(this)) { // SYSTEM_ALERT_WINDOW permission not granted... Toast.makeText(MainActivity.this, "not granted", Toast.LENGTH_SHORT); } } } super.onActivityResult(requestCode, resultCode, data); }Copy the code
Why do you need these permissions, since the main subscreen class Presentation inherits from Dialog
2.2 Defining the Split Screen
public class DifferentDisplay extends Presentation { public DifferentDisplay(Context outerContext, Display display) { super(outerContext, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.diffrentdisplay); } public void setText(String text) { TextView textView = findViewById(R.id.tv_test); textView.setText(text); }}Copy the code
Interface layout
<? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout 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" android:background="@color/purple_200" tools:context=".MainActivity"> <TextView android:id="@+id/tv_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" Android :textSize="50sp" /> </RelativeLayout>Copy the code
2.3 Operating the Split Screen
I’ll show you the code first
public class MainActivity extends AppCompatActivity { final String TAG = "MainActivity"; DifferentDisplay mPresentation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Dual screen DisplayManager mDisplayManager; // Display[] displays; / / screen mDisplayManager = array (DisplayManager) MainActivity. Enclosing getSystemService (Context. DISPLAY_SERVICE); displays = mDisplayManager.getDisplays(); If (display.length == 0) {log.d (TAG, "no display "); return; } mPresentation = new DifferentDisplay(getApplicationContext(), displays[1]); //displays[1] // 8.0 requires TYPE_APPLICATION_OVERLAY instead mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); mPresentation.show(); } int index = 1; public void Test(View view) { if (mPresentation ! = null) mpresent.settext (++index + "click "); }}Copy the code
Display[] displays is the number of screens retrieved, including those on android devices themselves. For example, new DifferentDisplay(getApplicationContext(), displays[1]) is the first secondary screen.
There is also a permission issue
mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Copy the code
Appeal code is 6.0 and above can (not), but above 8.0 need to use 8.0 above need to be like this: WindowManager. LayoutParams. TYPE_SYSTEM_ALERT replaced by: WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY