Preface
For dual-screen differential display, it means that when the application is opened on screen A, it must be displayed on screen B, or that while the application is displayed on screen A, some additional pages must be displayed on screen B.
The normal development solution for dual-screen differential display is to use Presentation
. This is a class provided by Android specifically for secondary screen display. It inherits from Dialog
, so the creation and usage are Dialog
close to the usage.
I first used activity to try the secondary screen display, so I encountered many problems, so I will record them here to give everyone a clear idea.
Note : To develop a secondary screen, please use Android 8.0 or Android 9.0 for testing, because later Android versions have updated a lot of things, and the functions that can be passed by lower versions may require additional settings to be effective.
Android emulator turns on secondary screen display
There are many online tutorials on this, so I’ll briefly describe it. In the developer options, there is "Simulate auxiliary display device". You can open a new screen by selecting any resolution.
How to get information from other screens?
Using the interface provided by android: DisplayManager
, with a few simple lines of code, you can get all screen information.
DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = displayManager.getDisplays();
Log.d(TAG, "onCreate: displays length: " + displays.length);
Add permissions and configuration
AndroidManifest.xml
Add the following permissions:
<uses-permission android:name= "android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name= "android.permission.SYSTEM_OVERLAY_WINDOW"/>
There is a floating window permission, which needs to be turned on manually on the application's settings page. You can use dynamic application to jump to this page, or you can wait for the application to be installed, open the application's permission settings page, and allow Floating window function.
Then AndroidManifest.xml
you need to add it to the activity inandroid:resizeableActivity="true"
<activity
android:name=".SecondActivity"
android:exported="true"
android:launchMode="singleTask"
android:resizeableActivity="true" />
Use Activity to display a screen on the secondary screen
First show how to open an activity on the secondary screen:
DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = displayManager.getDisplays();
Log.d(TAG, "onCreate: displays length: " + displays.length);
//在副屏打开一个activity
if (displays.length > 1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent();
intent.setClassName(getPackageName(), getPackageName() + ".SecondActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(displays[1].getDisplayId());
startActivity(intent, options.toBundle());
}
This function in the above code options.setLaunchDisplayId()
controls which screen the new activity is displayed on (this API is applicable to 8.0+), and then passing this parameter into startActivity()
, the activity will be opened on the new screen. Of course, there is another thing to note: you need to set the intent flag to FLAG_ACTIVITY_NEW_TASK
, because the secondary screen currently does not have a display stack for your app, so if you want to create an activity on the secondary screen, you need to open a new display stack. .
App does not support display on secondary screen
Here is the crux of the matter. If everything goes well, your activity will still not be displayed on the secondary screen of the simulator, and the system will pop up a Toast display: The application does not support display on the secondary screen .
This is the big pitfall of using activities for auxiliary screen display. The cause of this problem is almost difficult to find on the Internet. Fortunately, there is still a post that proposes a solution to this phenomenon:
the application does not support display on the secondary screen.
In the article, the author mentioned that files need to be created in the system directory android.software.activities_on_secondary_displays.xml
, so I I searched for this keyword and finally found a section in the android development documentation: Requirements for displaying android on the secondary screen.
As you can see, the first article states that android.software.activities_on_secondary_displays.xml
this file flag needs to exist to allow an activity to be opened on the secondary screen. (At the same time, through this content, we can also see that many conditions need to be met to display activities in the auxiliary screen, so when you see this, you should consider giving up this plan)
创建activities_on_secondary_displays.xml
First of all, our emulator does not have root permissions by default. Even if you use adb root
or use su
commands, you cannot obtain root, so you cannot directly create new files in the permissions folder of the system. Fortunately, Android provides us with a feasible solution. Find the folder
under Sdk (the default path is: ), open cmd in the folder, and run . This will display the list of simulators you have: Then use to open the specified simulator, where the parameter is to enable the writing function of the system directory . Then you can rewrite the contents of the folder.emulator
C:\Users\[你的用户名]\AppData\Local\Android\Sdk\emulator
emulator
emulator -list-avds
emulator -avd [模拟器名称] -writable-system
-writable-system
/system/etc/permissions/
end
Through creation activities_on_secondary_displays.xml
, I tried to create the activity on the auxiliary screen again, but the Toast of "The application does not support display on the auxiliary screen" was still displayed. At this point, I was unable to solve the problem .
But there is another way to verify whether the xml file we created is valid. Use the: am start -W -n com.example.xxx/.SecondActivity --display 1
command in adb shell to open our activity on the specified screen.
Through my personal experience, it can be seen that if you want to use activities to achieve dual-screen differential display in the native Android system, it will not work, so you should use the officially provided Presentation
classes for multi-screen display.