js-apis-display.md 6.7 KB
Newer Older
W
wusongqing 已提交
1
# Display
Z
zengyawen 已提交
2

W
wusongqing 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
W
wusongqing 已提交
4
> 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.
Z
zengyawen 已提交
5

W
wusongqing 已提交
6
## Modules to Import
Z
zengyawen 已提交
7

8
```js
Z
zengyawen 已提交
9 10 11
import display from '@ohos.display';
```

W
wusongqing 已提交
12 13

## DisplayState
Z
zengyawen 已提交
14 15 16

Provides the state of a display.

W
wusongqing 已提交
17 18
**System capability**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
19 20
| Name| Default Value| Description|
| -------- | -------- | -------- |
W
wusongqing 已提交
21 22 23 24 25 26 27
| STATE_UNKNOWN | 0 | Unknown.|
| STATE_OFF | 1 | The display is shut down.|
| STATE_ON | 2 | The display is powered on.|
| STATE_DOZE | 3 | The display is in sleep mode.|
| STATE_DOZE_SUSPEND | 4 | The display is in sleep mode, and the CPU is suspended.|
| STATE_VR | 5 | The display is in VR mode.|
| STATE_ON_SUSPEND | 6 | The display is powered on, and the CPU is suspended.|
W
wusongqing 已提交
28 29 30


## Display
Z
zengyawen 已提交
31 32 33

Describes the attributes of a display.

W
wusongqing 已提交
34 35
**System capability**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
36 37
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
38 39 40
| id | number | Yes| No| ID of the display.|
| name | string | Yes| No| Name of the display.|
| alive | boolean | Yes| No| Whether the display is alive.|
41
| state | [DisplayState](#displaystate) | Yes| No| State of the display.|
W
wusongqing 已提交
42 43 44 45 46 47 48 49 50
| refreshRate | number | Yes| No| Refresh rate of the display.|
| rotation | number | Yes| No| Screen rotation angle of the display.|
| width | number | Yes| No| Width of the display, in pixels.|
| height | number | Yes| No| Height of the display, in pixels.|
| densityDPI | number | Yes| No| Screen density of the display, in DPI.|
| densityPixels | number | Yes| No| Screen density of the display, in pixels.|
| scaledDensity | number | Yes| No| Scaling factor for fonts displayed on the display.|
| xDPI | number | Yes| No| Exact physical dots per inch of the screen in the horizontal direction.|
| yDPI | number | Yes| No| Exact physical dots per inch of the screen in the vertical direction.|
W
wusongqing 已提交
51 52 53 54


## display.getDefaultDisplay

W
wusongqing 已提交
55
getDefaultDisplay(callback: AsyncCallback<Display>): void
Z
zengyawen 已提交
56 57 58

Obtains the default display object.

W
wusongqing 已提交
59 60
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
61 62 63
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
64
  | callback | AsyncCallback<[Display](#display)> | Yes| Callback used to return the default display object.|
W
wusongqing 已提交
65 66

- Example
67 68

  ```js
W
wusongqing 已提交
69 70
  var displayClass = null;
  display.getDefaultDisplay((err, data) => {
W
wusongqing 已提交
71
      if (err.code) {
W
wusongqing 已提交
72 73 74 75 76 77 78 79
          console.error('Failed to obtain the default display object. Code:  ' + JSON.stringify(err));
          return;
      }
      console.info('Succeeded in obtaining the default display object. Data:' + JSON.stringify(data));
      displayClass = data;
  });
  ```

W
wusongqing 已提交
80 81 82 83 84 85 86 87 88 89
## display.getDefaultDisplay

getDefaultDisplay(): Promise<Display>

Obtains the default display object.

**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

- Return value

W
wusongqing 已提交
90
  | Type                              | Description                                          |
W
wusongqing 已提交
91
  | ---------------------------------- | ---------------------------------------------- |
92
  | Promise<[Display](#display)> | Promise used to return the default display object.|
W
wusongqing 已提交
93 94 95

- Example

96
  ```js
W
wusongqing 已提交
97 98 99 100 101 102 103
  let promise = display.getDefaultDisplay();
  promise.then(() => {
      console.log('getDefaultDisplay success');
  }).catch((err) => {
      console.log('getDefaultDisplay fail: ' + JSON.stringify(err));
  });
  ```
W
wusongqing 已提交
104 105 106

## display.getAllDisplay

W
wusongqing 已提交
107
getAllDisplay(callback: AsyncCallback<Array<Display>>): void
Z
zengyawen 已提交
108 109 110

Obtains all the display objects.

W
wusongqing 已提交
111 112
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
113
- Parameters
W
wusongqing 已提交
114

W
wusongqing 已提交
115
  | Name  | Type                                                | Mandatory| Description                           |
W
wusongqing 已提交
116
  | -------- | ---------------------------------------------------- | ---- | ------------------------------- |
117
  | callback | AsyncCallback<Array<[Display](#display)>> | Yes  | Callback used to return all the display objects.|
W
wusongqing 已提交
118 119

- Example
W
wusongqing 已提交
120

121
  ```js
W
wusongqing 已提交
122
  display.getAllDisplay((err, data) => {
W
wusongqing 已提交
123
      if (err.code) {
W
wusongqing 已提交
124 125 126 127 128 129 130
          console.error('Failed to obtain all the display objects. Code: ' + JSON.stringify(err));
          return;
      }
      console.info('Succeeded in obtaining all the display objects. Data: ' + JSON.stringify(data))
  });
  ```

W
wusongqing 已提交
131 132 133 134 135 136 137 138 139 140
## display.getAllDisplay

getAllDisplay(): Promise<Array<Display>>

Obtains all the display objects.

**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

- Return value

W
wusongqing 已提交
141
  | Type                                           | Description                                                   |
W
wusongqing 已提交
142
  | ----------------------------------------------- | ------------------------------------------------------- |
143
  | Promise<Array<[Display](#display)>> | Promise used to return an array containing all the display objects.|
W
wusongqing 已提交
144 145 146

- Example

147
  ```js
W
wusongqing 已提交
148 149 150 151 152 153 154
  let promise = display.getAllDisplay();
  promise.then(() => {
      console.log('getAllDisplay success');
  }).catch((err) => {
      console.log('getAllDisplay fail: ' + JSON.stringify(err));
  });
  ```
W
wusongqing 已提交
155 156 157

## display.on('add'|'remove'|'change')

W
wusongqing 已提交
158
on(type: 'add'|'remove'|'change', callback: Callback<number>): void
Z
zengyawen 已提交
159 160 161

Enables listening.

W
wusongqing 已提交
162 163
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
164 165 166
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
167
  | type | string | Yes| Listening type. The available values are as follows:<br>-&nbsp;**add**: listening for whether a display is added<br>-&nbsp;**remove**: listening for whether a display is removed<br>-&nbsp;**change**: listening for whether a display is changed|
W
wusongqing 已提交
168 169 170
  | callback | Callback&lt;number&gt; | Yes| Callback used to return the ID of the display.|

- Example
171 172

  ```js
W
wusongqing 已提交
173 174 175 176 177 178
  var type = "add";
  var callback = (data) => {
      console.info('Listening enabled. Data: ' + JSON.stringify(data))
  }
  display.on(type, callback);
  ```
Z
zengyawen 已提交
179 180


W
wusongqing 已提交
181
## display.off('add'|'remove'|'change')
Z
zengyawen 已提交
182

W
wusongqing 已提交
183
off(type: 'add'|'remove'|'change', callback?: Callback&lt;number&gt;): void
W
wusongqing 已提交
184 185

Disables listening.
Z
zengyawen 已提交
186

W
wusongqing 已提交
187 188
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
189 190 191
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
192
  | type | string | Yes| Listening type. The available values are as follows:<br>-&nbsp;**add**: listening for whether a display is added<br>-&nbsp;**remove**: listening for whether a display is removed<br>-&nbsp;**change**: listening for whether a display is changed|
W
wusongqing 已提交
193 194 195
  | callback | Callback&lt;number&gt; | No| Callback used to return the ID of the display.|

- Example
196 197

  ```js
W
wusongqing 已提交
198 199 200
  var type = "remove";
  display.off(type);
  ```