# Virtual Screen

A virtual screen refers to creating one or more virtual displays on your phone, essentially an isolated background screen. Apps and automation scripts running on the background screen do not affect the device's main screen. This means that while using automation scripts to automatically reply to WeChat messages on a background screen, you can continue using the main screen to scroll through TikTok videos. Our WebUI also supports multiple screen displays, allowing you to select and view or operate on other screens via the remote desktop.

## Create a Virtual Screen

It's very easy to use. You can create a virtual screen with the following call, which will create a virtual display with the same resolution and DPI as the current screen.

```python
vd = d.create_virtual_display()
```

Of course, you can also specify the virtual screen's resolution and DPI yourself.

```python
vd = d.create_virtual_display(width=1080, height=1920, densityDpi=480)
```

## Launch an App on a Virtual Screen

By default, no app is running on the newly created virtual screen, so it appears as a black screen. Therefore, you need to launch an app on the virtual screen first. You can start an app on the virtual screen using the following method.

```python
app = vd.application("com.android.settings")
app.start()
```

```{tip}
As you can see, it works much like the standard `d.xxx` usage pattern. You can even switch your existing code from `d.xx` to `vd.xx` with minimal changes, allowing your script to seamlessly transition to the virtual screen.
```

## Automation on a Virtual Screen

You can directly refer to other automation interface sections such as [Basic Automation](./ui-basics.md), [Advanced UI](./ui-advanced.md), etc. The usage is exactly the same, and you only need to make minor adjustments to your existing code to fully migrate virtual screen automation capabilities into your scripts. Refer to the code example below – simply replace the original `d` instance with the one created by `create_virtual_display`.

```{attention}
The `vd` instance generated by the virtual screen only includes automation and related actions. Methods that produce global effects, such as setting proxies, still need to be accessed via the original `d` instance. For interfaces that belong to the automation category but would produce global effects, a WARNING message will be output.
```

```python
# Replace the original d instance with a virtual screen instance
_d = Device("example.local")
d = _d.create_virtual_display()

# Original logic code
app = d.application("com.android.settings")
app.start()
...
d(textContains="WLAN").click()
...

# The watcher defined here will only affect the virtual screen
d.set_watcher_loop_enabled(True)
d.register_none_op_watcher("RecordElementAppearTimes", [Selector(textContains="WLAN")])
d.set_watcher_enabled("RecordElementAppearTimes", True)
# ...
d.screenshot(100).save("screenshot.jpeg")
# ...

# Release the virtual screen
d.release_virtual_display()
```

You can also use a `with` context manager, which will automatically destroy related resources after the code block ends.

```python
with d.create_virtual_display() as vd:
    do_someting(vd)
```