• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Display
2The **Display** module provides APIs for managing displays, such as obtaining information about the default display, obtaining information about all displays, and listening for the addition and removal of displays.
3
4> **NOTE**
5>
6> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7
8## Modules to Import
9
10```js
11import display from '@ohos.display';
12```
13
14
15## DisplayState
16
17Enumerates the display states.
18
19**System capability**: SystemCapability.WindowManager.WindowManager.Core
20
21| Name| Value| Description|
22| -------- | -------- | -------- |
23| STATE_UNKNOWN | 0 | Unknown.|
24| STATE_OFF | 1 | The display is shut down.|
25| STATE_ON | 2 | The display is powered on.|
26| STATE_DOZE | 3 | The display is in sleep mode.|
27| STATE_DOZE_SUSPEND | 4 | The display is in sleep mode, and the CPU is suspended.|
28| STATE_VR | 5 | The display is in VR mode.|
29| STATE_ON_SUSPEND | 6 | The display is powered on, and the CPU is suspended.|
30
31
32## Display
33
34Describes the attributes of a display.
35
36**System capability**: SystemCapability.WindowManager.WindowManager.Core
37
38| Name| Type| Readable| Writable| Description|
39| -------- | -------- | -------- | -------- | -------- |
40| id | number | Yes| No| ID of the display.|
41| name | string | Yes| No| Name of the display.|
42| alive | boolean | Yes| No| Whether the display is alive.|
43| state | [DisplayState](#displaystate) | Yes| No| State of the display.|
44| refreshRate | number | Yes| No| Refresh rate of the display.|
45| rotation | number | Yes| No| Screen rotation angle of the display.|
46| width | number | Yes| No| Width of the display, in pixels.|
47| height | number | Yes| No| Height of the display, in pixels.|
48| densityDPI | number | Yes| No| Screen density of the display, that is, the number of dots per inch. Generally, the value is **160** or **480**.|
49| densityPixels | number | Yes| No| Logical density of the display, which is a scaling coefficient independent of the pixel unit. Generally, the value is **1** or **3**.|
50| scaledDensity | number | Yes| No| Scaling factor for fonts displayed on the display. Generally, the value is the same as that of **densityPixels**.|
51| xDPI | number | Yes| No| Exact physical dots per inch of the screen in the horizontal direction.|
52| yDPI | number | Yes| No| Exact physical dots per inch of the screen in the vertical direction.|
53
54
55## display.getDefaultDisplay
56
57getDefaultDisplay(callback: AsyncCallback<Display>): void
58
59Obtains the default display object. This API uses an asynchronous callback to return the result.
60
61**System capability**: SystemCapability.WindowManager.WindowManager.Core
62
63**Parameters**
64
65  | Name| Type| Mandatory| Description|
66  | -------- | -------- | -------- | -------- |
67  | callback | AsyncCallback<[Display](#display)> | Yes| Callback used to return the default display object.|
68
69**Example**
70
71```js
72var displayClass = null;
73display.getDefaultDisplay((err, data) => {
74    if (err.code) {
75        console.error('Failed to obtain the default display object. Code:  ' + JSON.stringify(err));
76        return;
77    }
78    console.info('Succeeded in obtaining the default display object. Data:' + JSON.stringify(data));
79    displayClass = data;
80});
81```
82
83## display.getDefaultDisplay
84
85getDefaultDisplay(): Promise<Display>
86
87Obtains the default display object. This API uses a promise to return the result.
88
89**System capability**: SystemCapability.WindowManager.WindowManager.Core
90
91**Return value**
92
93  | Type                              | Description                                          |
94  | ---------------------------------- | ---------------------------------------------- |
95  | Promise<[Display](#display)> | Promise used to return the default display object.|
96
97**Example**
98
99```js
100var displayClass = null;
101let promise = display.getDefaultDisplay();
102promise.then((data) => {
103    displayClass = data;
104    console.info('Succeeded in obtaining the default display object. Data:' + JSON.stringify(data));
105}).catch((err) => {
106    console.error('Failed to obtain the default display object. Code:  ' + JSON.stringify(err));
107});
108```
109
110## display.getAllDisplay
111
112getAllDisplay(callback: AsyncCallback<Array<Display>>): void
113
114Obtains all display objects. This API uses an asynchronous callback to return the result.
115
116**System capability**: SystemCapability.WindowManager.WindowManager.Core
117
118**Parameters**
119
120  | Name  | Type                                                | Mandatory| Description                           |
121  | -------- | ---------------------------------------------------- | ---- | ------------------------------- |
122  | callback | AsyncCallback<Array<[Display](#display)>> | Yes  | Callback used to return all the display objects.|
123
124**Example**
125
126```js
127display.getAllDisplay((err, data) => {
128    if (err.code) {
129        console.error('Failed to obtain all the display objects. Code: ' + JSON.stringify(err));
130        return;
131    }
132    console.info('Succeeded in obtaining all the display objects. Data: ' + JSON.stringify(data));
133});
134```
135
136## display.getAllDisplay
137
138getAllDisplay(): Promise<Array<Display>>
139
140Obtains all display objects. This API uses a promise to return the result.
141
142**System capability**: SystemCapability.WindowManager.WindowManager.Core
143
144**Return value**
145
146  | Type                                           | Description                                                   |
147  | ----------------------------------------------- | ------------------------------------------------------- |
148  | Promise<Array<[Display](#display)>> | Promise used to return all the display objects.|
149
150**Example**
151
152```js
153let promise = display.getAllDisplay();
154promise.then((data) => {
155    console.info('Succeeded in obtaining all the display objects. Data: ' + JSON.stringify(data));
156}).catch((err) => {
157    console.error('Failed to obtain all the display objects. Code: ' + JSON.stringify(err));
158});
159```
160
161## display.on('add'|'remove'|'change')
162
163on(type: 'add'|'remove'|'change', callback: Callback<number>): void
164
165Subscribes to display changes.
166
167**System capability**: SystemCapability.WindowManager.WindowManager.Core
168
169**Parameters**
170
171  | Name| Type| Mandatory| Description|
172  | -------- | -------- | -------- | -------- |
173  | type | string | Yes| Event type.<br>- **add**, indicating the display addition event.<br>- **remove**, indicating the display removal event.<br>- **change**, indicating the display change event.|
174  | callback | Callback&lt;number&gt; | Yes| Callback used to return the ID of the display.|
175
176**Example**
177
178```js
179var callback = (data) => {
180    console.info('Listening enabled. Data: ' + JSON.stringify(data))
181}
182display.on("add", callback);
183  ```
184
185## display.off('add'|'remove'|'change')
186
187off(type: 'add'|'remove'|'change', callback?: Callback&lt;number&gt;): void
188
189Unsubscribes from display changes.
190
191**System capability**: SystemCapability.WindowManager.WindowManager.Core
192
193**Parameters**
194
195  | Name| Type| Mandatory| Description|
196  | -------- | -------- | -------- | -------- |
197  | type | string | Yes| Event type.<br>- **add**, indicating the display addition event.<br>- **remove**, indicating the display removal event.<br>- **change**, indicating the display change event.|
198  | callback | Callback&lt;number&gt; | No| Callback used to return the ID of the display.|
199
200**Example**
201```js
202display.off("remove");
203```
204