js-apis-screen-lock.md 18.4 KB
Newer Older
1
# @ohos.screenLock (Screenlock)
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';
```

15
## EventType
G
ge-yafang 已提交
16

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

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

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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 已提交
37

38
## SystemEvent
E
uu  
ester.zhou 已提交
39

40
Defines the structure of the system event callback.
G
ge-yafang 已提交
41

42
**System capability**: SystemCapability.MiscServices.ScreenLock
G
ge-yafang 已提交
43

44 45 46 47
| Name   | Type  | Mandatory|       Description       |
| --------- | ------ | ---- | ------------- |
| eventType   | [EventType](#eventtype) | Yes  | System event type.|
| params | string | Yes  | System event parameters.|
G
ge-yafang 已提交
48

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

51
isLocked(): boolean
G
ge-yafang 已提交
52

53
Checks whether the screen is locked.
E
ester.zhou 已提交
54

G
ge-yafang 已提交
55 56
**System capability**: SystemCapability.MiscServices.ScreenLock

E
uu  
ester.zhou 已提交
57
**Return value**
E
ester.zhou 已提交
58

59 60 61
| Type   | Description                                             |
| ------- | ------------------------------------------------- |
| boolean | Returns **true** if the screen is locked; returns **false** otherwise.|
E
uu  
ester.zhou 已提交
62 63

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

E
ester.zhou 已提交
65
```js
66
let isLocked = screenlock.isLocked();
E
ester.zhou 已提交
67
```
G
ge-yafang 已提交
68

69
## screenlock.isSecure<sup>9+</sup>
E
ester.zhou 已提交
70

71
isSecure(): boolean
E
ester.zhou 已提交
72

73
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.
E
ester.zhou 已提交
74 75 76 77 78

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

**Return value**

79 80 81
| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the device is in secure mode; returns **false** otherwise.|
E
ester.zhou 已提交
82 83 84 85

**Example**

```js
86
let isSecure = screenlock.isSecure();
E
ester.zhou 已提交
87
```
G
ge-yafang 已提交
88

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

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

93
Unlocks the screen. This API uses an asynchronous callback to return the result.
G
ge-yafang 已提交
94 95 96

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

E
uu  
ester.zhou 已提交
97
**Parameters**
E
ester.zhou 已提交
98

99 100 101 102 103 104 105 106 107 108 109
| 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.|

**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 已提交
110

E
uu  
ester.zhou 已提交
111
**Example**
G
ge-yafang 已提交
112

E
ester.zhou 已提交
113
```js
114
screenlock.unlock((err, data) => {      
E
ester.zhou 已提交
115
    if (err) {
116
        console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
117 118
        return;    
    }
119
    console.info(`unlock the screen successfully. result: ${data}`);
E
ester.zhou 已提交
120 121
});
```
G
ge-yafang 已提交
122

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

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

127
Unlocks the screen. This API uses a promise to return the result.
G
ge-yafang 已提交
128 129 130

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

E
uu  
ester.zhou 已提交
131
**Return value**
E
ester.zhou 已提交
132

133 134 135 136 137 138 139 140 141 142 143
| 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.|

**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 已提交
144 145

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

E
ester.zhou 已提交
147
```js
148 149
screenlock.unlock().then((data) => {
    console.info(`unlock the screen successfully. result: ${data}`);
E
ester.zhou 已提交
150
}).catch((err) => {
151
    console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
152 153 154
});
```

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

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

159
Locks the screen. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
160 161 162

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

163
**System API**: This is a system API.
E
ester.zhou 已提交
164

165 166 167 168 169 170 171 172 173 174 175 176 177
**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.|

**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 已提交
178 179 180 181

**Example**

```js
182 183 184 185 186 187 188
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 已提交
189
```
G
ge-yafang 已提交
190

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

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

195
Locks the screen. This API uses a promise to return the result.
G
ge-yafang 已提交
196 197 198

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

199
**System API**: This is a system API.
E
ester.zhou 已提交
200

201 202 203 204 205 206 207 208 209 210 211 212 213
**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.|

**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 已提交
214

E
uu  
ester.zhou 已提交
215
**Example**
G
ge-yafang 已提交
216

E
ester.zhou 已提交
217
```js
218 219 220 221
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 已提交
222 223
});
```
G
ge-yafang 已提交
224

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

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

229
Registers a callback for system events related to screen locking.
E
ester.zhou 已提交
230

G
ge-yafang 已提交
231 232
**System capability**: SystemCapability.MiscServices.ScreenLock

233 234 235 236 237 238 239 240
**System API**: This is a system API.

**Parameters**

| Name  | Type                       | Mandatory| Description              |
| -------- | ------------------------- | ---- | ----------------- |
| callback | Callback\<[SystemEvent](#systemevent)> | Yes  | Callback for system events related to screen locking.|

E
uu  
ester.zhou 已提交
241
**Return value**
E
ester.zhou 已提交
242

243 244 245 246 247 248 249 250 251 252 253
| Type   | Description                                             |
| ------- | ------------------------------------------------- |
| boolean | Returns **true** if the callback is registered successfully; returns **false** otherwise.|

**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 已提交
254 255

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

E
ester.zhou 已提交
257
```js
258 259 260 261 262 263 264
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 已提交
265
```
E
uu  
ester.zhou 已提交
266

267
## screenlock.sendScreenLockEvent<sup>9+</sup>
268

269
sendScreenLockEvent(event: string, parameter: number, callback: AsyncCallback&lt;boolean&gt;): void
270

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

E
ester.zhou 已提交
273
**System capability**: SystemCapability.MiscServices.ScreenLock
274

275 276
**System API**: This is a system API.

E
ester.zhou 已提交
277 278
**Parameters**

279 280 281 282 283 284 285 286 287 288 289 290 291
| Name   | Type           | Mandatory| Description                            |
| --------- | ------------------------ | ---- | -------------------- |
| event     | string                   | Yes  | Event type.<br>- **"unlockScreenResult"**: Screen unlock result.<br>- **"lockScreenResult"**: Screen lock result.<br>- **"screenDrawDone"**: Screen drawing is complete.|
| 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.                |

**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 已提交
292 293 294 295

**Example**

```js
296
screenlock.sendScreenLockEvent('unlockScreenResult', 0, (err, result) => {
E
ester.zhou 已提交
297
    if (err) {
298 299
        console.error(`Failed to send screenlock event, because: ${err.message}`);
        return;
E
ester.zhou 已提交
300
    }
301
    console.info(`Send screenlock event successfully. result: ${result}`);
E
ester.zhou 已提交
302 303 304
});
```

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

307
sendScreenLockEvent(event: string, parameter: number): Promise&lt;boolean&gt;
E
ester.zhou 已提交
308

309
Sends an event to the screen lock service. This API uses a promise to return the result.
E
ester.zhou 已提交
310 311 312

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

313 314 315 316 317 318 319 320 321
**System API**: This is a system API.

**Parameters**

| Name   | Type  | Mandatory| Description                                      |
| --------- | ------ | ---- | --------------------------------------- |
| event     | string | Yes  | Event type.<br>- **"unlockScreenResult"**: Screen unlock result.<br>- **"lockScreenResult"**: Screen lock result.<br>- **"screenDrawDone"**: Screen drawing is complete.|
| 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 已提交
322
**Return value**
323

324 325 326
| 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 已提交
327 328 329 330

**Example**

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

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

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

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

344 345 346
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [screenlock.isLocked<sup>9+</sup>](#screenlockislocked9) instead.
347

348
**System capability**: SystemCapability.MiscServices.ScreenLock
349 350

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

352 353 354
| 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.|
355 356 357

**Example**

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

368
## screenlock.isScreenLocked<sup>(deprecated)</sup>
369

370
isScreenLocked(): Promise&lt;boolean&gt;
371

372
Checks whether the screen is locked. This API uses a promise to return the result.
373

374 375 376
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [screenlock.isLocked<sup>9+</sup>](#screenlockislocked9) instead.
377

378
**System capability**: SystemCapability.MiscServices.ScreenLock
379 380 381

**Return value**

382 383 384
| 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.|
385 386 387

**Example**

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

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

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

400
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 已提交
401

402 403 404
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [screenlock.isSecure<sup>9+</sup>](#screenlockissecure9) instead.
E
uu  
ester.zhou 已提交
405 406 407

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

408
**Parameters**
E
uu  
ester.zhou 已提交
409

410 411 412
| 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 已提交
413

414
**Example**
E
uu  
ester.zhou 已提交
415

416 417 418 419 420 421 422 423 424
```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 已提交
425

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

428
isSecureMode(): Promise&lt;boolean&gt;
429

430
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 已提交
431

432 433 434 435 436
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [screenlock.isSecure<sup>9+</sup>](#screenlockissecure9) instead.

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

E
ester.zhou 已提交
438
**Return value**
E
ester.zhou 已提交
439

440 441 442
| 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 已提交
443 444 445

**Example**

E
ester.zhou 已提交
446
```js
447 448 449 450
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 已提交
451 452
});
```
453
## screenlock.unlockScreen<sup>(deprecated)</sup>
E
uu  
ester.zhou 已提交
454

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

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

459 460 461
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [screenlock.unlock<sup>9+</sup>](#screenlockunlock9) instead.
E
uu  
ester.zhou 已提交
462 463 464 465

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

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

467 468 469
| 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 已提交
470 471 472

**Example**

E
ester.zhou 已提交
473
```js
474 475 476 477 478 479
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 已提交
480 481
});
```
E
uu  
ester.zhou 已提交
482

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

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

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

489 490 491
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [screenlock.unlock<sup>9+</sup>](#screenlockunlock9) instead.
E
ester.zhou 已提交
492

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

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

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

**Example**

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