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 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 41 42 43 44 45 46 47 48 49 50
| 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.|
| state | [DisplayState](#DisplayState) | Yes| No| State of the display.|
| 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|
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
64
  | callback | AsyncCallback<[Display](#Display)> | Yes| Callback used to return the default display object.|
W
wusongqing 已提交
65 66 67 68 69

- Example
  ```
  var displayClass = null;
  display.getDefaultDisplay((err, data) => {
W
wusongqing 已提交
70
      if (err.code) {
W
wusongqing 已提交
71 72 73 74 75 76 77 78
          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 已提交
79 80 81 82 83 84 85 86 87 88
## display.getDefaultDisplay

getDefaultDisplay(): Promise<Display>

Obtains the default display object.

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

- Return value

W
wusongqing 已提交
89
  | Type                              | Description                                          |
W
wusongqing 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
  | ---------------------------------- | ---------------------------------------------- |
  | Promise<[Display](#Display)> | 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 已提交
103 104 105

## display.getAllDisplay

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

Obtains all the display objects.

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

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

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

- Example
W
wusongqing 已提交
119

W
wusongqing 已提交
120 121
  ```
  display.getAllDisplay((err, data) => {
W
wusongqing 已提交
122
      if (err.code) {
W
wusongqing 已提交
123 124 125 126 127 128 129
          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 已提交
130 131 132 133 134 135 136 137 138 139
## display.getAllDisplay

getAllDisplay(): Promise<Array<Display>>

Obtains all the display objects.

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

- Return value

W
wusongqing 已提交
140
  | Type                                           | Description                                                   |
W
wusongqing 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
  | ----------------------------------------------- | ------------------------------------------------------- |
  | Promise<Array<[Display](#Display)>> | 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 已提交
154 155 156

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

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

Enables listening.

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

W
wusongqing 已提交
163 164 165
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
166
  | 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 已提交
167 168 169 170 171 172 173 174 175 176
  | 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 已提交
177 178


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

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

Disables listening.
Z
zengyawen 已提交
184

W
wusongqing 已提交
185 186
**System capabilities**: SystemCapability.WindowManager.WindowManager.Core

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

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