js-apis-display.md 7.9 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 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
| Name| Default Value| Description|
| -------- | -------- | -------- |
W
wusongqing 已提交
19 20 21 22 23 24 25
| STATE_UNKNOWN | 0 | Unknown. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
| STATE_OFF | 1 | The display is shut down. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
| STATE_ON | 2 | The display is powered on. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
| STATE_DOZE | 3 | The display is in sleep mode. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
| STATE_DOZE_SUSPEND | 4 | The display is in sleep mode, and the CPU is suspended. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
| STATE_VR | 5 | The display is in VR mode. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
| STATE_ON_SUSPEND | 6 | The display is powered on, and the CPU is suspended. <br/>**System capabilities**: SystemCapability.WindowManager.WindowManager.Core|
W
wusongqing 已提交
26 27 28


## Display
Z
zengyawen 已提交
29 30 31

Describes the attributes of a display.

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


## display.getDefaultDisplay

W
wusongqing 已提交
51
getDefaultDisplay(callback: AsyncCallback&lt;Display&gt;): void
Z
zengyawen 已提交
52 53 54

Obtains the default display object.

W
wusongqing 已提交
55 56
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
57 58 59
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
60
  | callback | AsyncCallback&lt;[Display](#Display)&gt; | Yes| Callback used to return the default display object.|
W
wusongqing 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74

- Example
  ```
  var displayClass = null;
  display.getDefaultDisplay((err, data) => {
      if (err) {
          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 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
## display.getDefaultDisplay

getDefaultDisplay(): Promise&lt;Display&gt;

Obtains the default display object.

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

- Return value

  | Type| Description|
  | ---------------------------------- | ---------------------------------------------- |
  | Promise&lt;[Display](#Display)&gt; | Promise used to return the default display object.|

- Example

  ```
  let promise = display.getDefaultDisplay();
  promise.then(() => {
      console.log('getDefaultDisplay success');
  }).catch((err) => {
      console.log('getDefaultDisplay fail: ' + JSON.stringify(err));
  });
  ```
W
wusongqing 已提交
99 100 101

## display.getAllDisplay

W
wusongqing 已提交
102
getAllDisplay(callback: AsyncCallback&lt;Array&lt;Display&gt;&gt;): void
Z
zengyawen 已提交
103 104 105

Obtains all the display objects.

W
wusongqing 已提交
106 107
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
108
- Parameters
W
wusongqing 已提交
109

W
wusongqing 已提交
110
  | Name| Type| Mandatory| Description|
W
wusongqing 已提交
111 112
  | -------- | ---------------------------------------------------- | ---- | ------------------------------- |
  | callback | AsyncCallback&lt;Array&lt;[Display](Display)&gt;&gt; | Yes| Callback used to return all the display objects.|
W
wusongqing 已提交
113 114

- Example
W
wusongqing 已提交
115

W
wusongqing 已提交
116 117 118 119 120 121 122 123 124 125
  ```
  display.getAllDisplay((err, data) => {
      if (err) {
          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 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
## display.getAllDisplay

getAllDisplay(): Promise&lt;Array&lt;Display&gt;&gt;

Obtains all the display objects.

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

- Return value

  | Type| Description|
  | ----------------------------------------------- | ------------------------------------------------------- |
  | Promise&lt;Array&lt;[Display](#Display)&gt;&gt; | Promise used to return an array containing all the display objects.|

- Example

  ```
  let promise = display.getAllDisplay();
  promise.then(() => {
      console.log('getAllDisplay success');
  }).catch((err) => {
      console.log('getAllDisplay fail: ' + JSON.stringify(err));
  });
  ```
W
wusongqing 已提交
150 151 152

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

W
wusongqing 已提交
153
on(type: 'add'|'remove'|'change', callback: Callback&lt;number&gt;): void
Z
zengyawen 已提交
154 155 156

Enables listening.

W
wusongqing 已提交
157 158
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | 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|
  | callback | Callback&lt;number&gt; | Yes| Callback used to return the ID of the display.|

- Example
  ```
  var type = "add";
  var callback = (data) => {
      console.info('Listening enabled. Data: ' + JSON.stringify(data))
  }
  display.on(type, callback);
  ```
Z
zengyawen 已提交
173 174


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

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

Disables listening.
Z
zengyawen 已提交
180

W
wusongqing 已提交
181 182
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

W
wusongqing 已提交
183 184 185 186 187 188 189 190 191 192 193
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | 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|
  | callback | Callback&lt;number&gt; | No| Callback used to return the ID of the display.|

- Example
  ```
  var type = "remove";
  display.off(type);
  ```