js-apis-screen-lock.md 17.5 KB
Newer Older
Z
zengyawen 已提交
1 2
# 锁屏管理

3
锁屏管理服务是OpenHarmony中的系统服务,为锁屏应用提供注册亮屏、灭屏、开启屏幕、结束休眠、退出动画、请求解锁结果监听,并提供回调结果给锁屏应用。锁屏管理服务向三方应用提供请求解锁、查询锁屏状态、查询是否设置锁屏密码的能力。
4

5 6
> **说明:**
>
Z
zengyawen 已提交
7 8 9 10
> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

A
aqxyjay 已提交
11
```js
Z
zengyawen 已提交
12 13 14
import screenlock from '@ohos.screenLock';
```

15
## EventType
Z
zengyawen 已提交
16

17
定义系统事件类型。
18

19
**系统能力:** SystemCapability.MiscServices.ScreenLock
Z
zengyawen 已提交
20

21 22
| 事件类型            | 说明                     |
| ------------------ | ------------------------ |
Q
qimeng 已提交
23 24 25 26 27 28 29 30 31 32 33
| beginWakeUp        | 表示设备开始唤醒。 |
| endWakeUp          | 表示设备结束唤醒。 |
| beginScreenOn      | 表示设备开始亮屏。 |
| endScreenOn        | 表示设备结束亮屏。 |
| beginScreenOff     | 表示设备开始灭屏。 |
| endScreenOff       | 表示设备结束灭屏。 |
| unlockScreen       | 表示请求屏幕解锁。       |
| lockScreen         | 表示请求屏幕锁定。       |
| beginExitAnimation | 表示开始退场动画。       |
| beginSleep         | 表示设备开始休眠。           |
| endSleep           | 表示设备结束休眠。           |
34 35 36
| changeUser         | 表示切换用户。           |
| screenlockEnabled  | 表示锁屏是否启用。       |
| serviceRestart     | 表示锁屏服务进行重启。   |
Z
zengyawen 已提交
37

38
## SystemEvent
Z
zengyawen 已提交
39

40
定义系统事件回调参数结构。
41

42
**系统能力:** SystemCapability.MiscServices.ScreenLock
Z
zengyawen 已提交
43

44 45 46
| 名称    | 类型   | 必填 |       说明        |
| --------- | ------ | ---- | ------------- |
| eventType   | [EventType](#eventtype) | 是   | 系统事件类型。 |
Q
qimeng 已提交
47
| params | string | 是   | 系统事件参数。 |
Z
zengyawen 已提交
48

49 50
## screenlock.isLocked<sup>9+</sup>

51
isLocked(): boolean
52

53
判断屏幕是否锁屏。
54

55
**系统能力:** SystemCapability.MiscServices.ScreenLock
56

57
**返回值:** 
58

59 60
| 类型    | 说明                                              |
| ------- | ------------------------------------------------- |
Q
qimeng 已提交
61
| boolean | 返回true表示屏幕已锁屏;返回false表示屏幕未锁屏。 |
62

63
**示例:** 
64

65 66 67
```js
let isLocked = screenlock.isLocked();
```
68 69 70

## screenlock.isSecure<sup>9+</sup>

71
isSecure(): boolean
72

Q
qimeng 已提交
73
判断当前设备的屏幕锁定是否安全(安全屏幕锁定意味着解锁屏幕需要密码、图案或其他用户身份识别)。
74

75
**系统能力:** SystemCapability.MiscServices.ScreenLock
76

77
**返回值:** 
78

79 80
| 类型    | 说明                                                         |
| ------- | ------------------------------------------------------------ |
Q
qimeng 已提交
81
| boolean | 返回true表示当前设备的屏幕锁定安全;返回false表示当前设备的屏幕锁定不安全。 |
82

83
**示例:** 
84

85 86 87
```js
let isSecure = screenlock.isSecure();
```
88 89

## screenlock.unlock<sup>9+</sup>
90

91
unlock(callback: AsyncCallback&lt;boolean&gt;): void
92

Q
qimeng 已提交
93
解锁屏幕。使用callback异步回调。
94

95
**系统能力:** SystemCapability.MiscServices.ScreenLock
96

97
**参数:** 
98

99 100
| 参数名   | 类型          | 必填 | 说明                                 |
| -------- | --------------------- | ---- | ------------------------- |
Q
qimeng 已提交
101 102 103 104 105 106 107 108 109
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回true表示屏幕解锁成功;返回false表示屏幕解锁失败。 |

**错误码**

以下错误码的详细介绍请参见[锁屏服务错误码](../errorcodes/errorcode-screenlock.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13200002  | The screenlock management service is abnormal.         |
110

111
**示例:** 
112

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

## screenlock.unlock<sup>9+</sup>
124

125 126
unlock(): Promise&lt;boolean&gt;

Q
qimeng 已提交
127
解锁屏幕。使用Promise异步回调。
128

129
**系统能力:** SystemCapability.MiscServices.ScreenLock
130

131
**返回值:** 
132

133 134
| 类型                | 说明                                                         |
| ------------------- | ------------------------------------------------------------ |
Q
qimeng 已提交
135 136 137 138 139 140 141 142 143
| Promise&lt;boolean&gt; | Promise对象。返回true表示屏幕解锁成功;返回false表示屏幕解锁失败。 |

**错误码**

以下错误码的详细介绍请参见[锁屏服务错误码](../errorcodes/errorcode-screenlock.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13200002  | The screenlock management service is abnormal.         |
144

145
**示例:** 
146

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

## screenlock.lock<sup>9+</sup>

lock(callback: AsyncCallback&lt;boolean&gt;): void

Q
qimeng 已提交
159
锁定屏幕。使用callback异步回调。
160

161
**系统能力:** SystemCapability.MiscServices.ScreenLock
162

163
**系统接口**:此接口为系统接口。
164

165
**参数:** 
B
bmeangel 已提交
166

167 168
| 参数名   | 类型          | 必填 | 说明                      |
| -------- | ---------------------- | ---- | ---------------- |
Q
qimeng 已提交
169 170 171 172 173 174 175 176 177
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回true表示屏幕锁定成功;返回false表示屏幕锁定失败。 |

**错误码**

以下错误码的详细介绍请参见[锁屏服务错误码](../errorcodes/errorcode-screenlock.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13200002  | The screenlock management service is abnormal.         |
178

179 180 181
**示例:** 

```js
Q
qimeng 已提交
182
screenlock.lock((err, data) => {      
183
    if (err) {
Q
qimeng 已提交
184
        console.error(`Failed to lock the screen, because: ${err.message}`);
185 186
        return;    
    }
Q
qimeng 已提交
187
    console.info(`lock the screen successfully. result: ${data}`);
188 189
});
```
190

191
## screenlock.lock<sup>9+</sup>
192

193
lock(): Promise&lt;boolean&gt;
194

Q
qimeng 已提交
195
锁定屏幕。使用Promise异步回调。
196

197
**系统能力:** SystemCapability.MiscServices.ScreenLock
198

199 200 201
**系统接口**:此接口为系统接口。

**返回值:** 
202

203 204
| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
Q
qimeng 已提交
205 206 207 208 209 210 211 212 213
| Promise&lt;boolean&gt; | Promise对象。返回true表示屏幕锁定成功;返回false表示屏幕锁定失败。 |

**错误码**

以下错误码的详细介绍请参见[锁屏服务错误码](../errorcodes/errorcode-screenlock.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13200002  | The screenlock management service is abnormal.         |
B
bmeangel 已提交
214

215
**示例:** 
216

217 218
```js
screenlock.lock().then((data) => {
Q
qimeng 已提交
219
    console.info(`lock the screen successfully. result: ${data}`);
220
}).catch((err) => {
Q
qimeng 已提交
221
    console.error(`Failed to lock the screen, because: ${err.message}`);
222 223
});
```
224

225
## screenlock.onSystemEvent<sup>9+</sup>
B
bmeangel 已提交
226

Q
qimeng 已提交
227
onSystemEvent(callback: Callback&lt;SystemEvent&gt;): boolean
228

229
注册锁屏相关的系统事件。
230

231
**系统能力:** SystemCapability.MiscServices.ScreenLock
232

233
**系统接口**:此接口为系统接口。
234

235
**参数:** 
B
bmeangel 已提交
236

237 238
| 参数名   | 类型                        | 必填 | 说明               |
| -------- | ------------------------- | ---- | ----------------- |
239
| callback | Callback\<[SystemEvent](#systemevent)> | 是   | 锁屏相关的系统事件回调函数。 |
240 241 242

**返回值:** 

243 244
| 类型    | 说明                                              |
| ------- | ------------------------------------------------- |
Q
qimeng 已提交
245 246 247 248 249 250 251 252 253
| boolean | 返回true表示锁屏相关系统事件注册成功;返回false表示锁屏相关系统事件注册失败。 |

**错误码**

以下错误码的详细介绍请参见[锁屏服务错误码](../errorcodes/errorcode-screenlock.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13200002  | The screenlock management service is abnormal.         |
254

255
**示例:** 
256

257
```js
Q
qimeng 已提交
258 259 260 261 262 263
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}`)
264 265
}
```
266 267 268

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

Q
qimeng 已提交
269
sendScreenLockEvent(event: String, parameter: number, callback: AsyncCallback&lt;boolean&gt;): void
270

Q
qimeng 已提交
271
应用发送事件到锁屏服务。使用callback异步回调。
272

273
**系统能力:** SystemCapability.MiscServices.ScreenLock
274

275
**系统接口**:此接口为系统接口。
276

277
**参数:** 
278

279 280
| 参数名    | 类型            | 必填 | 说明                             |
| --------- | ------------------------ | ---- | -------------------- |
Q
qimeng 已提交
281 282 283 284 285 286 287 288 289 290 291
| event     | String                   | 是   | 事件类型,支持如下取值:<br/>- "unlockScreenResult",表示解锁结果。<br/>- "lockScreenResult",表示锁屏结果。<br/>- "screenDrawDone",表示屏幕绘制完成。 |
| parameter | number                   | 是   | 事件结果。<br/>- parameter为0,表示成功。例如解锁成功或锁屏成功。<br/>- parameter为1,表示失败。例如解锁失败或锁屏失败。<br/>- parameter为2,表示取消。例如锁屏取消或解锁取消。 |
| callback  | AsyncCallback\<boolean> | 是   | 回调函数。返回true表示发送事件成功;返回false表示发送事件失败。                 |

**错误码**

以下错误码的详细介绍请参见[锁屏服务错误码](../errorcodes/errorcode-screenlock.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13200002  | The screenlock management service is abnormal.         |
B
bmeangel 已提交
292

293
**示例:** 
294

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

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

Q
qimeng 已提交
307
sendScreenLockEvent(event: String, parameter: number): Promise&lt;boolean&gt;
308

Q
qimeng 已提交
309
应用发送事件到锁屏服务。使用Promise异步回调。
310

311
**系统能力:** SystemCapability.MiscServices.ScreenLock
312

313
**系统接口**:此接口为系统接口。
314

315
**参数:** 
B
bmeangel 已提交
316

317 318
| 参数名    | 类型   | 必填 | 说明                                       |
| --------- | ------ | ---- | --------------------------------------- |
Q
qimeng 已提交
319 320
| event     | String | 是   | 事件类型,支持如下取值:<br/>- "unlockScreenResult",表示解锁结果。<br/>- "lockScreenResult",表示锁屏结果。<br/>- "screenDrawDone",表示屏幕绘制完成。 |
| parameter | number | 是   | 事件结果。<br/>- parameter为0,表示成功。例如解锁成功或锁屏成功。<br/>- parameter为1,表示失败。例如解锁失败或锁屏失败。<br/>- parameter为2,表示取消。例如锁屏取消或解锁取消。 |
321

322 323
**返回值:** 

324 325
| 类型               | 说明                                           |
| ----------------- | ---------------------------------------------- |
Q
qimeng 已提交
326
| Promise\<boolean> | Promise对象。返回true表示发送事件成功;返回false表示发送事件失败。 |
B
bmeangel 已提交
327

328
**示例:** 
329

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

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

isScreenLocked(callback: AsyncCallback&lt;boolean&gt;): void

Q
qimeng 已提交
342
判断屏幕是否锁屏。使用callback异步回调。
343 344 345

> **说明:**
> 
Q
qimeng 已提交
346
> 从API version 7开始支持,从API version 9开始不再维护。建议使用[screenlock.isLocked<sup>9+</sup>](#screenlockislocked9)代替。
347 348 349 350 351 352 353

**系统能力:** SystemCapability.MiscServices.ScreenLock

**参数:**

| 参数名   | 类型                         | 必填 | 说明                                                        |
| -------- | ---------------------------- | ---- | ----------------------------------------------------------- |
Q
qimeng 已提交
354
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回true表示屏幕已锁屏;返回false表示屏幕未锁屏。 |
355 356 357 358 359 360

**示例:**

```js
screenlock.isScreenLocked((err, data)=>{      
    if (err) {
Q
qimeng 已提交
361
        console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`);
362 363
        return;    
    }
Q
qimeng 已提交
364
    console.info(`Obtain whether the screen is locked successfully. result: ${data}`);
365 366 367 368 369 370 371
});
```

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

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

Q
qimeng 已提交
372
判断屏幕是否锁屏。使用Promise异步回调。
373 374 375

> **说明:**
> 
Q
qimeng 已提交
376
> 从API version 7开始支持,从API version 9开始不再维护。建议使用[screenlock.isLocked<sup>9+</sup>](#screenlockislocked9)代替。
377 378 379 380 381 382 383

**系统能力:** SystemCapability.MiscServices.ScreenLock

**返回值:** 

| 类型                   | 说明                                         |
| ---------------------- | ------------------------------------------- |
Q
qimeng 已提交
384
| Promise&lt;boolean&gt; | Promise对象。返回true表示屏幕已锁屏;返回false表示屏幕未锁屏。 |
385 386 387 388 389

**示例:** 

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

## screenlock.isSecureMode<sup>(deprecated)</sup>

isSecureMode(callback: AsyncCallback&lt;boolean&gt;): void

Q
qimeng 已提交
400
判断当前设备的屏幕锁定是否安全(安全屏幕锁定意味着解锁屏幕需要密码、图案或其他用户身份识别)。使用callback异步回调。
401 402 403

> **说明:**
> 
Q
qimeng 已提交
404
> 从API version 7开始支持,从API version 9开始不再维护。建议使用[screenlock.isSecure<sup>9+</sup>](#screenlockissecure9)代替。
405 406 407 408 409 410 411

**系统能力:** SystemCapability.MiscServices.ScreenLock

**参数:** 

| 参数名   | 类型            | 必填 | 说明                              |
| -------- | --------------------- | ---- | ------------------------ |
Q
qimeng 已提交
412
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回true表示当前设备的屏幕锁定安全;返回false表示当前设备的屏幕锁定不安全。 |
413 414 415 416

**示例:** 

```js
Q
qimeng 已提交
417
screenlock.isSecureMode((err, data)=>{
418
    if (err) {
Q
qimeng 已提交
419 420
        console.error(`Failed to obtain whether the device is in secure mode, because: ${err.message}`);
        return;
421
    }
Q
qimeng 已提交
422
    console.info(`Obtain whether the device is in secure mode successfully. result: ${data}`);
423 424 425 426 427 428 429
});
```

## screenlock.isSecureMode<sup>(deprecated)</sup>

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

Q
qimeng 已提交
430
判断当前设备的屏幕锁定是否安全(安全屏幕锁定意味着解锁屏幕需要密码、图案或其他用户身份识别)。使用Promise异步回调。
431 432 433

> **说明:**
> 
Q
qimeng 已提交
434
> 从API version 7开始支持,从API version 9开始不再维护。建议使用[screenlock.isSecure<sup>9+</sup>](#screenlockissecure9)代替。
435 436 437 438 439 440 441

**系统能力:** SystemCapability.MiscServices.ScreenLock

**返回值:** 

| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
Q
qimeng 已提交
442
| Promise&lt;boolean&gt; | Promise对象。返回true表示当前设备的屏幕锁定安全;返回false表示当前设备的屏幕锁定不安全。 |
443 444 445 446 447

**示例:** 

```js
screenlock.isSecureMode().then((data) => {
Q
qimeng 已提交
448
    console.info(`Obtain whether the device is in secure mode successfully. result: ${data}`);
449
}).catch((err) => {
Q
qimeng 已提交
450
    console.error(`Failed to obtain whether the device is in secure mode, because: ${err.message}`);
451 452 453 454 455 456
});
```
## screenlock.unlockScreen<sup>(deprecated)</sup>

unlockScreen(callback: AsyncCallback&lt;void&gt;): void

Q
qimeng 已提交
457
解锁屏幕。使用callback异步回调。
458 459 460

> **说明:**
> 
Q
qimeng 已提交
461
> 从API version 7开始支持,从API version 9开始不再维护。建议使用[screenlock.unlock<sup>9+</sup>](#screenlockunlock9)代替。
462 463 464 465 466 467 468

**系统能力:** SystemCapability.MiscServices.ScreenLock

**参数:** 

| 参数名   | 类型              | 必填 | 说明             |
| -------- | ------------- | ---- | --------------- |
Q
qimeng 已提交
469
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。解锁屏幕成功,err为undefined,否则为错误对象。 |
470 471 472 473 474 475

**示例:** 

```js
screenlock.unlockScreen((err) => {      
    if (err) {
Q
qimeng 已提交
476
        console.error(`Failed to unlock the screen, because: ${err.message}`);
477 478
        return;    
    }
Q
qimeng 已提交
479
    console.info('unlock the screen successfully.');
480 481 482 483 484 485 486
});
```

## screenlock.unlockScreen<sup>(deprecated)</sup>

unlockScreen(): Promise&lt;void&gt;

Q
qimeng 已提交
487
解锁屏幕。使用Promise异步回调。
488 489 490

> **说明:**
> 
Q
qimeng 已提交
491
> 从API version 7开始支持,从API version 9开始不再维护。建议使用[screenlock.unlock<sup>9+</sup>](#screenlockunlock9)代替。
492 493 494 495 496 497 498

**系统能力:** SystemCapability.MiscServices.ScreenLock

**返回值:** 

| 类型                | 说明                      |
| ------------------- | ------------------------- |
Q
qimeng 已提交
499
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
500 501 502 503 504

**示例:** 

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