js-apis-screen-lock.md 15.9 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
uu  
ester.zhou 已提交
91
**Example**
G
ge-yafang 已提交
92

E
ester.zhou 已提交
93
```js
94
screenlock.unlock((err, data) => {      
E
ester.zhou 已提交
95
    if (err) {
96
        console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
97 98
        return;    
    }
99
    console.info(`unlock the screen successfully. result: ${data}`);
E
ester.zhou 已提交
100 101
});
```
G
ge-yafang 已提交
102

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

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

107
Unlocks the screen. This API uses a promise to return the result.
G
ge-yafang 已提交
108 109 110

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

E
ester.zhou 已提交
111 112
**System API**: This is a system API.

E
uu  
ester.zhou 已提交
113
**Return value**
E
ester.zhou 已提交
114

115 116 117 118
| 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
uu  
ester.zhou 已提交
119
**Example**
G
ge-yafang 已提交
120

E
ester.zhou 已提交
121
```js
122 123
screenlock.unlock().then((data) => {
    console.info(`unlock the screen successfully. result: ${data}`);
E
ester.zhou 已提交
124
}).catch((err) => {
125
    console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
126 127 128
});
```

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

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

133
Locks the screen. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
134 135 136

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

137
**System API**: This is a system API.
E
ester.zhou 已提交
138

139 140 141 142 143 144
**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 已提交
145 146 147
**Example**

```js
148 149 150 151 152 153 154
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 已提交
155
```
G
ge-yafang 已提交
156

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

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

161
Locks the screen. This API uses a promise to return the result.
G
ge-yafang 已提交
162 163 164

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

165
**System API**: This is a system API.
E
ester.zhou 已提交
166

167 168 169 170 171 172
**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.|

G
ge-yafang 已提交
173

E
uu  
ester.zhou 已提交
174
**Example**
G
ge-yafang 已提交
175

E
ester.zhou 已提交
176
```js
177 178 179 180
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 已提交
181 182
});
```
G
ge-yafang 已提交
183

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

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

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

G
ge-yafang 已提交
190 191
**System capability**: SystemCapability.MiscServices.ScreenLock

192 193 194 195 196 197
**System API**: This is a system API.

**Parameters**

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

E
uu  
ester.zhou 已提交
200
**Return value**
E
ester.zhou 已提交
201

202 203 204 205
| Type   | Description                                             |
| ------- | ------------------------------------------------- |
| boolean | Returns **true** if the callback is registered successfully; returns **false** otherwise.|

E
uu  
ester.zhou 已提交
206 207

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

E
ester.zhou 已提交
209
```js
210 211 212 213 214 215 216
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 已提交
217
```
E
uu  
ester.zhou 已提交
218

219
## screenlock.sendScreenLockEvent<sup>9+</sup>
220

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

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

E
ester.zhou 已提交
225
**System capability**: SystemCapability.MiscServices.ScreenLock
226

227 228
**System API**: This is a system API.

E
ester.zhou 已提交
229 230
**Parameters**

231 232
| Name   | Type           | Mandatory| Description                            |
| --------- | ------------------------ | ---- | -------------------- |
E
ester.zhou 已提交
233
| event     | String                   | Yes  | Event type.<br>- **"unlockScreenResult"**: Screen unlock result.<br>- **"lockScreenResult"**: Screen lock result.<br>- **"screenDrawDone"**: Screen drawing is complete.|
234 235 236
| 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 已提交
237 238 239 240

**Example**

```js
241
screenlock.sendScreenLockEvent('unlockScreenResult', 0, (err, result) => {
E
ester.zhou 已提交
242
    if (err) {
243 244
        console.error(`Failed to send screenlock event, because: ${err.message}`);
        return;
E
ester.zhou 已提交
245
    }
246
    console.info(`Send screenlock event successfully. result: ${result}`);
E
ester.zhou 已提交
247 248 249
});
```

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

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

254
Sends an event to the screen lock service. This API uses a promise to return the result.
E
ester.zhou 已提交
255 256 257

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

258 259 260 261 262 263
**System API**: This is a system API.

**Parameters**

| Name   | Type  | Mandatory| Description                                      |
| --------- | ------ | ---- | --------------------------------------- |
E
ester.zhou 已提交
264
| event     | String | Yes  | Event type.<br>- **"unlockScreenResult"**: Screen unlock result.<br>- **"lockScreenResult"**: Screen lock result.<br>- **"screenDrawDone"**: Screen drawing is complete.|
265 266
| 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 已提交
267
**Return value**
268

269 270 271
| 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 已提交
272 273 274 275

**Example**

```js
276 277
screenlock.sendScreenLockEvent('unlockScreenResult', 0).then((result) => {
    console.info(`Send screenlock event successfully. result: ${result}`);
E
ester.zhou 已提交
278
}).catch((err) => {
279
    console.error(`Failed to send screenlock event, because: ${err.message}`);
E
ester.zhou 已提交
280 281 282
});
```

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

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

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

289 290
> **NOTE**
> 
E
ester.zhou 已提交
291
> This API is supported since API version 7 and deprecated since API version 9.
292

293
**System capability**: SystemCapability.MiscServices.ScreenLock
294 295

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

297 298 299
| 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.|
300 301 302

**Example**

E
ester.zhou 已提交
303
```js
304
screenlock.isScreenLocked((err, data)=>{      
E
ester.zhou 已提交
305
    if (err) {
306
        console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`);
E
ester.zhou 已提交
307 308
        return;    
    }
309
    console.info(`Obtain whether the screen is locked successfully. result: ${data}`);
E
ester.zhou 已提交
310 311
});
```
312

313
## screenlock.isScreenLocked<sup>(deprecated)</sup>
314

315
isScreenLocked(): Promise&lt;boolean&gt;
316

317
Checks whether the screen is locked. This API uses a promise to return the result.
318

319 320
> **NOTE**
> 
E
ester.zhou 已提交
321
> This API is supported since API version 7 and deprecated since API version 9.
322

323
**System capability**: SystemCapability.MiscServices.ScreenLock
324 325 326

**Return value**

327 328 329
| 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.|
330 331 332

**Example**

E
ester.zhou 已提交
333
```js
334 335
screenlock.isScreenLocked().then((data) => {
    console.info(`Obtain whether the screen is locked successfully. result: ${data}`);
E
ester.zhou 已提交
336
}).catch((err) => {
337
    console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`);
E
ester.zhou 已提交
338 339
});
```
340

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

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

345
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 已提交
346

347 348
> **NOTE**
> 
E
ester.zhou 已提交
349
> This API is supported since API version 7 and deprecated since API version 9.
E
uu  
ester.zhou 已提交
350 351 352

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

353
**Parameters**
E
uu  
ester.zhou 已提交
354

355 356 357
| 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 已提交
358

359
**Example**
E
uu  
ester.zhou 已提交
360

361 362 363 364 365 366 367 368 369
```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 已提交
370

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

373
isSecureMode(): Promise&lt;boolean&gt;
374

375
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 已提交
376

377 378
> **NOTE**
> 
E
ester.zhou 已提交
379
> This API is supported since API version 7 and deprecated since API version 9.
380 381

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

E
ester.zhou 已提交
383
**Return value**
E
ester.zhou 已提交
384

385 386 387
| 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 已提交
388 389 390

**Example**

E
ester.zhou 已提交
391
```js
392 393 394 395
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 已提交
396 397
});
```
398
## screenlock.unlockScreen<sup>(deprecated)</sup>
E
uu  
ester.zhou 已提交
399

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

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

404 405
> **NOTE**
> 
E
ester.zhou 已提交
406
> This API is supported since API version 7 and deprecated since API version 9.
E
uu  
ester.zhou 已提交
407 408 409 410

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

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

412 413 414
| 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 已提交
415 416 417

**Example**

E
ester.zhou 已提交
418
```js
419 420 421 422 423 424
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 已提交
425 426
});
```
E
uu  
ester.zhou 已提交
427

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

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

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

434 435
> **NOTE**
> 
E
ester.zhou 已提交
436
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
437

438
**System capability**: SystemCapability.MiscServices.ScreenLock
E
uu  
ester.zhou 已提交
439 440

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

442 443 444
| Type               | Description                     |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
E
uu  
ester.zhou 已提交
445 446 447

**Example**

E
ester.zhou 已提交
448
```js
449 450 451 452
screenlock.unlockScreen().then(() => {
    console.info('unlock the screen successfully.');
}).catch((err) => {
    console.error(`Failed to unlock the screen, because: ${err.message}`);
E
ester.zhou 已提交
453 454
});
```