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

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> 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 已提交
6

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

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

W
wusongqing 已提交
13 14

## DisplayState
Z
zengyawen 已提交
15 16 17

Provides the state of a display.

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

W
wusongqing 已提交
20 21
| Name| Default Value| Description|
| -------- | -------- | -------- |
W
wusongqing 已提交
22 23 24 25 26 27 28
| 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 已提交
29 30 31


## Display
Z
zengyawen 已提交
32 33 34

Describes the attributes of a display.

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

W
wusongqing 已提交
37 38
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
39 40 41
| 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.|
42
| state | [DisplayState](#displaystate) | Yes| No| State of the display.|
W
wusongqing 已提交
43 44 45 46 47 48 49 50 51
| 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 已提交
52 53 54 55


## display.getDefaultDisplay

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

Obtains the default display object.

W
wusongqing 已提交
60
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Parameters**
W
wusongqing 已提交
63

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

W
wusongqing 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
**Example**

```js
var displayClass = null;
display.getDefaultDisplay((err, data) => {
    if (err.code) {
        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 已提交
81

W
wusongqing 已提交
82 83 84 85 86 87
## display.getDefaultDisplay

getDefaultDisplay(): Promise<Display>

Obtains the default display object.

W
wusongqing 已提交
88
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
89

W
wusongqing 已提交
90
**Return value**
W
wusongqing 已提交
91

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

W
wusongqing 已提交
96
**Example**
W
wusongqing 已提交
97

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

## display.getAllDisplay

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

Obtains all the display objects.

W
wusongqing 已提交
113
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
114

W
wusongqing 已提交
115
**Parameters**
W
wusongqing 已提交
116

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

W
wusongqing 已提交
121
**Example**
W
wusongqing 已提交
122

W
wusongqing 已提交
123 124 125 126 127 128 129 130 131
```js
display.getAllDisplay((err, data) => {
    if (err.code) {
        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 已提交
132

W
wusongqing 已提交
133 134 135 136 137 138
## display.getAllDisplay

getAllDisplay(): Promise<Array<Display>>

Obtains all the display objects.

W
wusongqing 已提交
139
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
140

W
wusongqing 已提交
141
**Return value**
W
wusongqing 已提交
142

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

W
wusongqing 已提交
147
**Example**
W
wusongqing 已提交
148

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

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

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

Enables listening.

W
wusongqing 已提交
164 165 166
**System capability**: SystemCapability.WindowManager.WindowManager.Core

**Parameters**
W
wusongqing 已提交
167

W
wusongqing 已提交
168 169 170 171
| 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.|
W
wusongqing 已提交
172

W
wusongqing 已提交
173
**Example**
174

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


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

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

Disables listening.
Z
zengyawen 已提交
189

W
wusongqing 已提交
190
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
191

W
wusongqing 已提交
192
**Parameters**
W
wusongqing 已提交
193

W
wusongqing 已提交
194 195 196 197
| 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.|
198

W
wusongqing 已提交
199 200 201 202 203 204
**Example**

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