js-apis-screen-lock.md 17.8 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.screenLock (Screen Lock)
G
ge-yafang 已提交
2

3 4
The **screenlock** module is a system module in OpenHarmony. It provides APIs for screen lock applications to subscribe to screen lock status changes as well as callbacks for them to receive the results. It also provides APIs for third-party applications to unlock the screen, obtain the screen locked status, and check whether a lock screen password has been set.

E
uu  
ester.zhou 已提交
5 6
> **NOTE**
>
7
> 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.
G
ge-yafang 已提交
8 9 10

## Modules to Import

S
shawn_he 已提交
11
```js
G
ge-yafang 已提交
12 13 14
import screenlock from '@ohos.screenLock';
```

E
ester.zhou 已提交
15
## EventType<sup>9+</sup>
G
ge-yafang 已提交
16

17
Defines the system event type.
E
ester.zhou 已提交
18

G
ge-yafang 已提交
19 20
**System capability**: SystemCapability.MiscServices.ScreenLock

E
ester.zhou 已提交
21 22
**System API**: This is a system API.

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| Event Type           | Description                    |
| ------------------ | ------------------------ |
| beginWakeUp        | Wakeup starts.|
| endWakeUp          | Wakeup ends.|
| beginScreenOn      | Screen turn-on starts.|
| endScreenOn        | Screen turn-on ends.|
| beginScreenOff     | Screen turn-off starts.|
| endScreenOff       | Screen turn-off ends.|
| unlockScreen       | The screen is unlocked.      |
| lockScreen         | The screen is locked.      |
| beginExitAnimation | Exit animation starts.      |
| beginSleep         | The device enters sleep mode.          |
| endSleep           | The device exits sleep mode.          |
| changeUser         | The user is switched.          |
| screenlockEnabled  | Screen lock is enabled.      |
| serviceRestart     | The screen lock service is restarted.  |
E
ester.zhou 已提交
39

E
ester.zhou 已提交
40
## SystemEvent<sup>9+</sup>
E
uu  
ester.zhou 已提交
41

42
Defines the structure of the system event callback.
G
ge-yafang 已提交
43

44
**System capability**: SystemCapability.MiscServices.ScreenLock
G
ge-yafang 已提交
45

E
ester.zhou 已提交
46 47
**System API**: This is a system API.

48 49
| Name   | Type  | Mandatory|       Description       |
| --------- | ------ | ---- | ------------- |
E
ester.zhou 已提交
50
| eventType   | [EventType](#eventtype9) | Yes  | System event type.|
51
| params | string | Yes  | System event parameters.|
G
ge-yafang 已提交
52

53
## screenlock.isLocked<sup>9+</sup>
G
ge-yafang 已提交
54

55
isLocked(): boolean
G
ge-yafang 已提交
56

57
Checks whether the screen is locked.
E
ester.zhou 已提交
58

G
ge-yafang 已提交
59 60
**System capability**: SystemCapability.MiscServices.ScreenLock

E
ester.zhou 已提交
61 62
**System API**: This is a system API.

E
uu  
ester.zhou 已提交
63
**Return value**
E
ester.zhou 已提交
64

65 66 67
| Type   | Description                                             |
| ------- | ------------------------------------------------- |
| boolean | Returns **true** if the screen is locked; returns **false** otherwise.|
E
uu  
ester.zhou 已提交
68 69

**Example**
G
ge-yafang 已提交
70

E
ester.zhou 已提交
71
```js
72
let isLocked = screenlock.isLocked();
E
ester.zhou 已提交
73
```
G
ge-yafang 已提交
74

75
## screenlock.unlock<sup>9+</sup>
G
ge-yafang 已提交
76

77
unlock(callback: AsyncCallback&lt;boolean&gt;): void
G
ge-yafang 已提交
78

79
Unlocks the screen. This API uses an asynchronous callback to return the result.
G
ge-yafang 已提交
80 81 82

**System capability**: SystemCapability.MiscServices.ScreenLock

E
ester.zhou 已提交
83 84
**System API**: This is a system API.

E
uu  
ester.zhou 已提交
85
**Parameters**
E
ester.zhou 已提交
86

87 88 89 90
| Name  | Type         | Mandatory| Description                                |
| -------- | --------------------- | ---- | ------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. The value **true** means that the screen is unlocked successfully, and **false** means the opposite.|

E
ester.zhou 已提交
91 92 93 94 95 96 97 98
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  | the screenlock management service is abnormal.         |

E
uu  
ester.zhou 已提交
99
**Example**
G
ge-yafang 已提交
100

E
ester.zhou 已提交
101
```js
102
screenlock.unlock((err, data) => {      
E
ester.zhou 已提交
103
    if (err) {
104
        console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
105 106
        return;    
    }
107
    console.info(`unlock the screen successfully. result: ${data}`);
E
ester.zhou 已提交
108 109
});
```
G
ge-yafang 已提交
110

111
## screenlock.unlock<sup>9+</sup>
G
ge-yafang 已提交
112

113
unlock(): Promise&lt;boolean&gt;
E
ester.zhou 已提交
114

115
Unlocks the screen. This API uses a promise to return the result.
G
ge-yafang 已提交
116 117 118

**System capability**: SystemCapability.MiscServices.ScreenLock

E
ester.zhou 已提交
119 120
**System API**: This is a system API.

E
uu  
ester.zhou 已提交
121
**Return value**
E
ester.zhou 已提交
122

123 124 125 126
| Type               | Description                                                        |
| ------------------- | ------------------------------------------------------------ |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means that the screen is unlocked successfully, and **false** means the opposite.|

E
ester.zhou 已提交
127 128 129 130 131 132 133 134
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  | the screenlock management service is abnormal.         |

E
uu  
ester.zhou 已提交
135
**Example**
G
ge-yafang 已提交
136

E
ester.zhou 已提交
137
```js
138 139
screenlock.unlock().then((data) => {
    console.info(`unlock the screen successfully. result: ${data}`);
E
ester.zhou 已提交
140
}).catch((err) => {
141
    console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
142 143 144
});
```

145
## screenlock.lock<sup>9+</sup>
E
ester.zhou 已提交
146

147
lock(callback: AsyncCallback&lt;boolean&gt;): void
G
ge-yafang 已提交
148

149
Locks the screen. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
150 151 152

**System capability**: SystemCapability.MiscServices.ScreenLock

153
**System API**: This is a system API.
E
ester.zhou 已提交
154

155 156 157 158 159 160
**Parameters**

| Name  | Type         | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. The value **true** means that the screen is locked successfully, and **false** means the opposite.|

E
ester.zhou 已提交
161 162 163 164 165 166 167 168
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  | the screenlock management service is abnormal.         |

E
ester.zhou 已提交
169 170 171
**Example**

```js
172 173 174 175 176 177 178
screenlock.lock((err, data) => {      
    if (err) {
        console.error(`Failed to lock the screen, because: ${err.message}`);
        return;    
    }
    console.info(`lock the screen successfully. result: ${data}`);
});
E
ester.zhou 已提交
179
```
G
ge-yafang 已提交
180

181
## screenlock.lock<sup>9+</sup>
G
ge-yafang 已提交
182

183
lock(): Promise&lt;boolean&gt;
G
ge-yafang 已提交
184

185
Locks the screen. This API uses a promise to return the result.
G
ge-yafang 已提交
186 187 188

**System capability**: SystemCapability.MiscServices.ScreenLock

189
**System API**: This is a system API.
E
ester.zhou 已提交
190

191 192 193 194 195 196
**Return value**

| Type                  | Description                                                        |
| ---------------------- | ------------------------------------------------------------ |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means that the screen is locked successfully, and **false** means the opposite.|

E
ester.zhou 已提交
197 198 199 200 201 202 203
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  | the screenlock management service is abnormal.         |
G
ge-yafang 已提交
204

E
uu  
ester.zhou 已提交
205
**Example**
G
ge-yafang 已提交
206

E
ester.zhou 已提交
207
```js
208 209 210 211
screenlock.lock().then((data) => {
    console.info(`lock the screen successfully. result: ${data}`);
}).catch((err) => {
    console.error(`Failed to lock the screen, because: ${err.message}`);
E
ester.zhou 已提交
212 213
});
```
G
ge-yafang 已提交
214

215
## screenlock.onSystemEvent<sup>9+</sup>
G
ge-yafang 已提交
216

217
onSystemEvent(callback: Callback&lt;SystemEvent&gt;): boolean
G
ge-yafang 已提交
218

E
ester.zhou 已提交
219
Registers a callback for system events related to screen locking. This API can be called only by system screen lock applications.
E
ester.zhou 已提交
220

G
ge-yafang 已提交
221 222
**System capability**: SystemCapability.MiscServices.ScreenLock

223 224 225 226 227 228
**System API**: This is a system API.

**Parameters**

| Name  | Type                       | Mandatory| Description              |
| -------- | ------------------------- | ---- | ----------------- |
E
ester.zhou 已提交
229
| callback | Callback\<[SystemEvent](#systemevent9)> | Yes  | Callback for system events related to screen locking.|
230

E
uu  
ester.zhou 已提交
231
**Return value**
E
ester.zhou 已提交
232

233 234 235 236
| Type   | Description                                             |
| ------- | ------------------------------------------------- |
| boolean | Returns **true** if the callback is registered successfully; returns **false** otherwise.|

E
ester.zhou 已提交
237 238 239 240 241 242 243
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  | the screenlock management service is abnormal.         |
E
uu  
ester.zhou 已提交
244 245

**Example**
G
ge-yafang 已提交
246

E
ester.zhou 已提交
247
```js
248 249 250 251 252 253 254
try {
    let isSuccess = screenlock.onSystemEvent((event) => {
        console.log(`Register the system event which related to screenlock successfully. eventType: ${event.eventType}`)
    });
} catch (err) {
    console.error(`Failed to register the system event which related to screenlock, because: ${err.message}`)
}
E
ester.zhou 已提交
255
```
E
uu  
ester.zhou 已提交
256

257
## screenlock.sendScreenLockEvent<sup>9+</sup>
258

E
ester.zhou 已提交
259
sendScreenLockEvent(event: String, parameter: number, callback: AsyncCallback&lt;boolean&gt;): void
260

261
Sends an event to the screen lock service. This API uses an asynchronous callback to return the result.
262

E
ester.zhou 已提交
263
**System capability**: SystemCapability.MiscServices.ScreenLock
264

265 266
**System API**: This is a system API.

E
ester.zhou 已提交
267 268
**Parameters**

269 270
| Name   | Type           | Mandatory| Description                            |
| --------- | ------------------------ | ---- | -------------------- |
E
ester.zhou 已提交
271
| event     | String                   | Yes  | Event type.<br>- **"unlockScreenResult"**: Screen unlock result.<br>- **"lockScreenResult"**: Screen lock result.<br>- **"screenDrawDone"**: Screen drawing is complete.|
272 273 274
| parameter | number                   | Yes  | Result.<br>- **0**: The operation is successful. For example, the screen is locked or unlocked successfully.<br>- **1**, the operation fails. For example, screen locking or unlocking fails.<br>- **2**: The operation is canceled. For example, screen locking or unlocking is canceled.|
| callback  | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The **value** true means that the event is sent successfully, and **false** means the opposite.                |

E
ester.zhou 已提交
275 276 277 278 279 280 281
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  |the screenlock management service is abnormal.         |
E
ester.zhou 已提交
282 283 284 285

**Example**

```js
286
screenlock.sendScreenLockEvent('unlockScreenResult', 0, (err, result) => {
E
ester.zhou 已提交
287
    if (err) {
288 289
        console.error(`Failed to send screenlock event, because: ${err.message}`);
        return;
E
ester.zhou 已提交
290
    }
291
    console.info(`Send screenlock event successfully. result: ${result}`);
E
ester.zhou 已提交
292 293 294
});
```

295
## screenlock.sendScreenLockEvent<sup>9+</sup>
E
ester.zhou 已提交
296

E
ester.zhou 已提交
297
sendScreenLockEvent(event: String, parameter: number): Promise&lt;boolean&gt;
E
ester.zhou 已提交
298

299
Sends an event to the screen lock service. This API uses a promise to return the result.
E
ester.zhou 已提交
300 301 302

**System capability**: SystemCapability.MiscServices.ScreenLock

303 304 305 306 307 308
**System API**: This is a system API.

**Parameters**

| Name   | Type  | Mandatory| Description                                      |
| --------- | ------ | ---- | --------------------------------------- |
E
ester.zhou 已提交
309
| event     | String | Yes  | Event type.<br>- **"unlockScreenResult"**: Screen unlock result.<br>- **"lockScreenResult"**: Screen lock result.<br>- **"screenDrawDone"**: Screen drawing is complete.|
310 311
| parameter | number | Yes  | Result.<br>- **0**: The operation is successful. For example, the screen is locked or unlocked successfully.<br>- **1**, the operation fails. For example, screen locking or unlocking fails.<br>- **2**: The operation is canceled. For example, screen locking or unlocking is canceled.|

E
ester.zhou 已提交
312
**Return value**
313

314 315 316
| Type              | Description                                          |
| ----------------- | ---------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The **value** true means that the event is sent successfully, and **false** means the opposite.|
E
ester.zhou 已提交
317

E
ester.zhou 已提交
318 319 320 321 322 323 324 325
**Error codes**

For details about the error codes, see [Screen Lock Management Error Codes](../errorcodes/errorcode-screenlock.md).

| ID| Error Message|
| -------- | ---------------------------------------- |
| 13200002  | the screenlock management service is abnormal.         |

E
ester.zhou 已提交
326 327 328
**Example**

```js
329 330
screenlock.sendScreenLockEvent('unlockScreenResult', 0).then((result) => {
    console.info(`Send screenlock event successfully. result: ${result}`);
E
ester.zhou 已提交
331
}).catch((err) => {
332
    console.error(`Failed to send screenlock event, because: ${err.message}`);
E
ester.zhou 已提交
333 334 335
});
```

336
## screenlock.isScreenLocked<sup>(deprecated)</sup>
E
ester.zhou 已提交
337

338
isScreenLocked(callback: AsyncCallback&lt;boolean&gt;): void
E
ester.zhou 已提交
339

340
Checks whether the screen is locked. This API uses an asynchronous callback to return the result.
341

342 343
> **NOTE**
> 
E
ester.zhou 已提交
344
> This API is supported since API version 7 and deprecated since API version 9.
345

346
**System capability**: SystemCapability.MiscServices.ScreenLock
347 348

**Parameters**
E
ester.zhou 已提交
349

350 351 352
| Name  | Type                        | Mandatory| Description                                                       |
| -------- | ---------------------------- | ---- | ----------------------------------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. The value **true** means that the screen is locked, and **false** means the opposite.|
353 354 355

**Example**

E
ester.zhou 已提交
356
```js
357
screenlock.isScreenLocked((err, data)=>{      
E
ester.zhou 已提交
358
    if (err) {
359
        console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`);
E
ester.zhou 已提交
360 361
        return;    
    }
362
    console.info(`Obtain whether the screen is locked successfully. result: ${data}`);
E
ester.zhou 已提交
363 364
});
```
365

366
## screenlock.isScreenLocked<sup>(deprecated)</sup>
367

368
isScreenLocked(): Promise&lt;boolean&gt;
369

370
Checks whether the screen is locked. This API uses a promise to return the result.
371

372 373
> **NOTE**
> 
E
ester.zhou 已提交
374
> This API is supported since API version 7 and deprecated since API version 9.
375

376
**System capability**: SystemCapability.MiscServices.ScreenLock
377 378 379

**Return value**

380 381 382
| Type                  | Description                                        |
| ---------------------- | ------------------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means that the screen is locked, and **false** means the opposite.|
383 384 385

**Example**

E
ester.zhou 已提交
386
```js
387 388
screenlock.isScreenLocked().then((data) => {
    console.info(`Obtain whether the screen is locked successfully. result: ${data}`);
E
ester.zhou 已提交
389
}).catch((err) => {
390
    console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`);
E
ester.zhou 已提交
391 392
});
```
393

394
## screenlock.isSecureMode<sup>(deprecated)</sup>
E
ester.zhou 已提交
395

396
isSecureMode(callback: AsyncCallback&lt;boolean&gt;): void
E
ester.zhou 已提交
397

398
Checks whether the device is in secure mode. When the device is in secure mode, its screen requires a password, unlock pattern, or other user credentials to unlock. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
399

400 401
> **NOTE**
> 
E
ester.zhou 已提交
402
> This API is supported since API version 7 and deprecated since API version 9.
E
uu  
ester.zhou 已提交
403 404 405

**System capability**: SystemCapability.MiscServices.ScreenLock

406
**Parameters**
E
uu  
ester.zhou 已提交
407

408 409 410
| Name  | Type           | Mandatory| Description                             |
| -------- | --------------------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. The value **true** means that the device is in secure mode, and **false** means the opposite.|
E
uu  
ester.zhou 已提交
411

412
**Example**
E
uu  
ester.zhou 已提交
413

414 415 416 417 418 419 420 421 422
```js
screenlock.isSecureMode((err, data)=>{
    if (err) {
        console.error(`Failed to obtain whether the device is in secure mode, because: ${err.message}`);
        return;
    }
    console.info(`Obtain whether the device is in secure mode successfully. result: ${data}`);
});
```
E
uu  
ester.zhou 已提交
423

424
## screenlock.isSecureMode<sup>(deprecated)</sup>
E
uu  
ester.zhou 已提交
425

426
isSecureMode(): Promise&lt;boolean&gt;
427

428
Checks whether the device is in secure mode. When the device is in secure mode, its screen requires a password, unlock pattern, or other user credentials to unlock. This API uses a promise to return the result.
E
ester.zhou 已提交
429

430 431
> **NOTE**
> 
E
ester.zhou 已提交
432
> This API is supported since API version 7 and deprecated since API version 9.
433 434

**System capability**: SystemCapability.MiscServices.ScreenLock
E
uu  
ester.zhou 已提交
435

E
ester.zhou 已提交
436
**Return value**
E
ester.zhou 已提交
437

438 439 440
| Type                  | Description                                                        |
| ---------------------- | ------------------------------------------------------------ |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means that the device is in secure mode, and **false** means the opposite.|
E
uu  
ester.zhou 已提交
441 442 443

**Example**

E
ester.zhou 已提交
444
```js
445 446 447 448
screenlock.isSecureMode().then((data) => {
    console.info(`Obtain whether the device is in secure mode successfully. result: ${data}`);
}).catch((err) => {
    console.error(`Failed to obtain whether the device is in secure mode, because: ${err.message}`);
E
ester.zhou 已提交
449 450
});
```
451
## screenlock.unlockScreen<sup>(deprecated)</sup>
E
uu  
ester.zhou 已提交
452

453
unlockScreen(callback: AsyncCallback&lt;void&gt;): void
E
uu  
ester.zhou 已提交
454

455
Unlocks the screen. This API uses an asynchronous callback to return the result.
E
uu  
ester.zhou 已提交
456

457 458
> **NOTE**
> 
E
ester.zhou 已提交
459
> This API is supported since API version 7 and deprecated since API version 9.
E
uu  
ester.zhou 已提交
460 461 462 463

**System capability**: SystemCapability.MiscServices.ScreenLock

**Parameters**
E
ester.zhou 已提交
464

465 466 467
| Name  | Type             | Mandatory| Description            |
| -------- | ------------- | ---- | --------------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the screen is unlocked successfully, **err** is **undefined**; otherwise, **err** is an error object.|
E
uu  
ester.zhou 已提交
468 469 470

**Example**

E
ester.zhou 已提交
471
```js
472 473 474 475 476 477
screenlock.unlockScreen((err) => {      
    if (err) {
        console.error(`Failed to unlock the screen, because: ${err.message}`);
        return;    
    }
    console.info('unlock the screen successfully.');
E
ester.zhou 已提交
478 479
});
```
E
uu  
ester.zhou 已提交
480

481
## screenlock.unlockScreen<sup>(deprecated)</sup>
E
uu  
ester.zhou 已提交
482

483
unlockScreen(): Promise&lt;void&gt;
E
uu  
ester.zhou 已提交
484

485
Unlocks the screen. This API uses a promise to return the result.
E
uu  
ester.zhou 已提交
486

487 488
> **NOTE**
> 
E
ester.zhou 已提交
489
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
490

491
**System capability**: SystemCapability.MiscServices.ScreenLock
E
uu  
ester.zhou 已提交
492 493

**Return value**
E
ester.zhou 已提交
494

495 496 497
| Type               | Description                     |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
E
uu  
ester.zhou 已提交
498 499 500

**Example**

E
ester.zhou 已提交
501
```js
502 503 504 505
screenlock.unlockScreen().then(() => {
    console.info('unlock the screen successfully.');
}).catch((err) => {
    console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
506 507
});
```