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

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

## Modules to Import
Z
zengyawen 已提交
8 9 10 11 12

```
import display from '@ohos.display';
```

W
wusongqing 已提交
13 14

## Required Permissions
Z
zengyawen 已提交
15 16 17

None

W
wusongqing 已提交
18 19

## DisplayState
Z
zengyawen 已提交
20 21 22

Provides the state of a display.

W
wusongqing 已提交
23 24 25 26 27 28 29 30 31 32 33 34
| Name| Default Value| Description|
| -------- | -------- | -------- |
| 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.|


## Display
Z
zengyawen 已提交
35 36 37

Describes the attributes of a display.

W
wusongqing 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|


## display.getDefaultDisplay

getDefaultDisplay(callback: AsyncCallback<Display>): void;
Z
zengyawen 已提交
58 59 60

Obtains the default display object.

W
wusongqing 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<[Display](#display)> | Yes| Callback used to return the attributes of the default display.|

- 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;
  });
  ```


## display.getAllDisplay

getAllDisplay(callback: AsyncCallback<Array<Display>>): void;
Z
zengyawen 已提交
83 84 85

Obtains all the display objects.

W
wusongqing 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<Array<[Display](#display)>> | Yes| Callback used to return the attributes of all displays.|

- Example
  ```
  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))
  });
  ```


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

on(type: 'add'|'remove'|'change', callback: Callback<number>): void;
Z
zengyawen 已提交
106 107 108

Enables listening.

W
wusongqing 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
- 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 已提交
123 124


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

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

Disables listening.
Z
zengyawen 已提交
130

W
wusongqing 已提交
131 132 133 134 135 136 137 138 139 140 141
- 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);
  ```