js-apis-runninglock.md 16.6 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.runningLock
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The **runningLock** module provides APIs for creating, querying, holding, and releasing running locks.
S
shawn_he 已提交
4

S
shawn_he 已提交
5
> **NOTE**<br>
S
shawn_he 已提交
6
> 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 已提交
7

S
shawn_he 已提交
8
## Modules to Import
S
shawn_he 已提交
9

S
shawn_he 已提交
10 11 12
```js
import runningLock from '@ohos.runningLock';
```
S
shawn_he 已提交
13

S
shawn_he 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
## runningLock.isSupported<sup>9+</sup>

isSupported(type: RunningLockType): boolean;

Checks whether a specified type of **RunningLock** is supported.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Parameters**

| Name| Type                               | Mandatory| Description                |
| ------ | ----------------------------------- | ---- | -------------------- |
| type   | [RunningLockType](#runninglocktype) | Yes  | Type of the **RunningLock** object.|

**Return value**

| Type   | Description                                   |
| ------- | --------------------------------------- |
| boolean | The value **true** indicates that the specified type of **RunningLock** is supported, and the value **false** indicates the opposite.|

**Error codes**

For details about the error codes, see [RunningLock Error Codes](../errorcodes/errorcode-runninglock.md).

| Code  | Error Message   |
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**
Z
zengyawen 已提交
43

S
shawn_he 已提交
44 45 46 47 48 49 50
```js
try {
    var isSupported = runningLock.isSupported(runningLock.RunningLockType.BACKGROUND);
    console.info('BACKGROUND type supported: ' + isSupported);
} catch(err) {
    console.error('check supported failed, err: ' + err);
}
Z
zengyawen 已提交
51
```
S
shawn_he 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

## runningLock.create<sup>9+</sup>

create(name: string, type: RunningLockType, callback: AsyncCallback&lt;RunningLock&gt;): void

Creates a **RunningLock** object.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK

**Parameters**

| Name  | Type                                      | Mandatory| Description                                                        |
| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ |
| name     | string                                     | Yes  | Name of the **RunningLock** object.                                                  |
| type     | [RunningLockType](#runninglocktype)        | Yes  | Type of the **RunningLock** object to be created.                                          |
| callback | AsyncCallback<[RunningLock](#runninglock)> | Yes  | Callback used to return the result. If a lock is successfully created, **err** is **undefined** and **data** is the created **RunningLock**. Otherwise, **err** is an error object.|

**Error codes**

For details about the error codes, see [RunningLock Error Codes](../errorcodes/errorcode-runninglock.md).

| Code  | Error Message    |
|---------|----------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND, (err, lock) => {
    if (typeof err === 'undefined') {
        console.info('created running lock: ' + lock);
    } else {
        console.error('create running lock failed, err: ' + err);
    }
});
Z
zengyawen 已提交
89 90
```

S
shawn_he 已提交
91
## runningLock.create<sup>9+</sup>
S
shawn_he 已提交
92

S
shawn_he 已提交
93
create(name: string, type: RunningLockType): Promise&lt;RunningLock&gt;
S
shawn_he 已提交
94

S
shawn_he 已提交
95
Creates a **RunningLock** object.
S
shawn_he 已提交
96

S
shawn_he 已提交
97 98
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
99
**Required permission:** ohos.permission.RUNNING_LOCK
S
shawn_he 已提交
100

S
shawn_he 已提交
101 102 103 104 105 106 107 108 109 110 111 112
**Parameters**

| Name| Type                               | Mandatory| Description              |
| ------ | ----------------------------------- | ---- | ------------------ |
| name   | string                              | Yes  | Name of the **RunningLock** object.        |
| type   | [RunningLockType](#runninglocktype) | Yes  | Type of the **RunningLock** object to be created.|

**Return value**

| Type                                      | Description                                |
| ------------------------------------------ | ------------------------------------ |
| Promise&lt;[RunningLock](#runninglock)&gt; | Promise used to return the result.|
S
shawn_he 已提交
113

S
shawn_he 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
**Error codes**

For details about the error codes, see [RunningLock Error Codes](../errorcodes/errorcode-runninglock.md).

| Code  | Error Message    |
|---------|----------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    console.info('created running lock: ' + lock);
})
.catch(err => {
    console.error('create running lock failed, error: ' + err);
});
```

## runningLock.isRunningLockTypeSupported<sup>(deprecated)</sup>
S
shawn_he 已提交
135 136 137

isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback&lt;boolean&gt;): void

S
shawn_he 已提交
138 139 140
> This API is deprecated since API version 9. You are advised to use [runningLock.isSupported](#runninglockissupported9) instead.

Checks whether a specified type of **RunningLock** is supported. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
141 142

**System capability:** SystemCapability.PowerManager.PowerManager.Core
S
shawn_he 已提交
143 144 145

**Parameters**

S
shawn_he 已提交
146 147 148 149
| Name  | Type                               | Mandatory| Description                                                        |
| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| type     | [RunningLockType](#runninglocktype) | Yes  | Type of the **RunningLock** object.                                        |
| callback | AsyncCallback&lt;boolean&gt;        | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the query result obtained, where the value **true** indicates that **RunningLock** is supported and **false** indicates the opposite. Otherwise, **err** is an error object.|
S
shawn_he 已提交
150 151 152

**Example**

S
shawn_he 已提交
153 154 155 156
```js
runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (err, data) => {
    if (typeof err === 'undefined') {
        console.info('BACKGROUND lock support status: ' + data);
S
shawn_he 已提交
157
    } else {
S
shawn_he 已提交
158
        console.log('check BACKGROUND lock support status failed, err: ' + err);
S
shawn_he 已提交
159
    }
S
shawn_he 已提交
160
});
S
shawn_he 已提交
161 162
```

S
shawn_he 已提交
163
## runningLock.isRunningLockTypeSupported<sup>(deprecated)</sup>
S
shawn_he 已提交
164

S
shawn_he 已提交
165
isRunningLockTypeSupported(type: RunningLockType): Promise&lt;boolean>
S
shawn_he 已提交
166

S
shawn_he 已提交
167
> This API is deprecated since API version 9. You are advised to use [runningLock.isSupported](#runninglockissupported9) instead.
S
shawn_he 已提交
168

S
shawn_he 已提交
169
Checks whether a specified type of **RunningLock** is supported. This API uses a promise to return the result.
S
shawn_he 已提交
170

S
shawn_he 已提交
171 172
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
173 174
**Parameters**

S
shawn_he 已提交
175 176 177
| Name| Type                               | Mandatory| Description                |
| ------ | ----------------------------------- | ---- | -------------------- |
| type   | [RunningLockType](#runninglocktype) | Yes  | Type of the **RunningLock** object.|
S
shawn_he 已提交
178

S
shawn_he 已提交
179
**Return value**
S
shawn_he 已提交
180

S
shawn_he 已提交
181 182 183
| Type                  | Description                                                |
| ---------------------- | ---------------------------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the specified type of **RunningLock** is supported, and the value **false** indicates the opposite.|
S
shawn_he 已提交
184 185 186

**Example**

S
shawn_he 已提交
187 188 189 190
```js
runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND)
.then(data => {
    console.info('BACKGROUND lock support status: ' + data);
S
shawn_he 已提交
191
})
S
shawn_he 已提交
192 193
.catch(err => {
    console.log('check BACKGROUND lock support status failed, err: ' + err);
S
shawn_he 已提交
194 195 196
});
```

S
shawn_he 已提交
197
## runningLock.createRunningLock<sup>(deprecated)</sup>
S
shawn_he 已提交
198 199 200

createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback&lt;RunningLock&gt;): void

S
shawn_he 已提交
201 202
> This API is deprecated since API version 9. You are advised to use [runningLock.create](#runninglockcreate9) instead.

S
shawn_he 已提交
203 204
Creates a **RunningLock** object.

S
shawn_he 已提交
205 206 207
**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK
S
shawn_he 已提交
208 209 210

**Parameters**

S
shawn_he 已提交
211 212 213 214 215
| Name  | Type                                      | Mandatory| Description                                                        |
| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ |
| name     | string                                     | Yes  | Name of the **RunningLock** object.                                                  |
| type     | [RunningLockType](#runninglocktype)        | Yes  | Type of the **RunningLock** object to be created.                                          |
| callback | AsyncCallback<[RunningLock](#runninglock)> | Yes  | Callback used to return the result. If a lock is successfully created, **err** is **undefined** and **data** is the created **RunningLock**. Otherwise, **err** is an error object.|
S
shawn_he 已提交
216 217 218

**Example**

S
shawn_he 已提交
219 220 221 222
```js
runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND, (err, lock) => {
    if (typeof err === 'undefined') {
        console.info('created running lock: ' + lock);
223
    } else {
S
shawn_he 已提交
224
        console.error('create running lock failed, err: ' + err);
S
shawn_he 已提交
225
    }
S
shawn_he 已提交
226
});
S
shawn_he 已提交
227 228
```

S
shawn_he 已提交
229
## runningLock.createRunningLock<sup>(deprecated)</sup>
Z
zengyawen 已提交
230

S
shawn_he 已提交
231
createRunningLock(name: string, type: RunningLockType): Promise&lt;RunningLock&gt;
Z
zengyawen 已提交
232

S
shawn_he 已提交
233 234
> This API is deprecated since API version 9. You are advised to use [runningLock.create](#runninglockcreate9) instead.

S
shawn_he 已提交
235 236
Creates a **RunningLock** object.

S
shawn_he 已提交
237 238 239
**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK
S
shawn_he 已提交
240 241 242

**Parameters**

S
shawn_he 已提交
243 244 245 246
| Name| Type                               | Mandatory| Description              |
| ------ | ----------------------------------- | ---- | ------------------ |
| name   | string                              | Yes  | Name of the **RunningLock** object.        |
| type   | [RunningLockType](#runninglocktype) | Yes  | Type of the **RunningLock** object to be created.|
S
shawn_he 已提交
247

S
shawn_he 已提交
248
**Return value**
S
shawn_he 已提交
249

S
shawn_he 已提交
250
| Type                                      | Description                                |
S
shawn_he 已提交
251 252
| ------------------------------------------ | ------------------------------------ |
| Promise&lt;[RunningLock](#runninglock)&gt; | Promise used to return the result.|
S
shawn_he 已提交
253 254 255

**Example**

S
shawn_he 已提交
256 257 258 259 260 261 262 263
```js
runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    console.info('created running lock: ' + lock);
})
.catch(err => {
    console.log('create running lock failed, err: ' + err);
});
S
shawn_he 已提交
264
```
S
shawn_he 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

## RunningLock

Represents a **RunningLock** object.

### hold<sup>9+</sup>

hold(timeout: number): void

Locks and holds a **RunningLock** object.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK

**Parameters**

| Name | Type  | Mandatory| Description                                     |
| ------- | ------ | ---- | ----------------------------------------- |
| timeout | number | Yes  | Duration for locking and holding the **RunningLock** object, in ms.|

**Error codes**

For details about the error codes, see [RunningLock Error Codes](../errorcodes/errorcode-runninglock.md).

| Code  | Error Message    |
|---------|----------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    console.info('create running lock success');
    try {
        lock.hold(500);
        console.info('hold running lock success');
    } catch(err) {
        console.error('hold running lock failed, err: ' + err);
    }
S
shawn_he 已提交
306
})
S
shawn_he 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
.catch(err => {
    console.error('create running lock failed, err: ' + err);
});
```

### unhold<sup>9+</sup>

unhold(): void

Releases a **RunningLock** object.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK

**Error codes**

For details about the error codes, see [RunningLock Error Codes](../errorcodes/errorcode-runninglock.md).

| Code  | Error Message    |
|---------|----------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    console.info('create running lock success');
    try {
        lock.unhold();
        console.info('unhold running lock success');
    } catch(err) {
        console.error('unhold running lock failed, err: ' + err);
    }
S
shawn_he 已提交
342
})
S
shawn_he 已提交
343 344 345
.catch(err => {
    console.error('create running lock failed, err: ' + err);
});
S
shawn_he 已提交
346 347
```

S
shawn_he 已提交
348
### isHolding<sup>9+</sup>
S
shawn_he 已提交
349

S
shawn_he 已提交
350 351 352 353 354
isHolding(): boolean

Checks the hold status of the **Runninglock** object.

**System capability:** SystemCapability.PowerManager.PowerManager.Core
S
shawn_he 已提交
355

S
shawn_he 已提交
356
**Return value**
S
shawn_he 已提交
357

S
shawn_he 已提交
358 359 360
| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
| boolean | The value **true** indicates that the **Runninglock** object is held; and the value **false** indicates that the **Runninglock** object is released.|
S
shawn_he 已提交
361

S
shawn_he 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
**Error codes**

For details about the error codes, see [RunningLock Error Codes](../errorcodes/errorcode-runninglock.md).

| Code  | Error Message   |
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    console.info('create running lock success');
    try {
        var isHolding = lock.isHolding();
        console.info('check running lock holding status: ' + isHolding);
    } catch(err) {
        console.error('check running lock holding status failed, err: ' + err);
    }
})
.catch(err => {
    console.error('create running lock failed, err: ' + err);
});
```

### lock<sup>(deprecated)</sup>
S
shawn_he 已提交
389 390 391

lock(timeout: number): void

S
shawn_he 已提交
392 393
> This API is deprecated since API version 9. You are advised to use [RunningLock.hold](#hold9) instead.

S
shawn_he 已提交
394 395
Locks and holds a **RunningLock** object.

S
shawn_he 已提交
396 397
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
398 399
**Required permission:** ohos.permission.RUNNING_LOCK

S
shawn_he 已提交
400 401
**Parameters**

S
shawn_he 已提交
402 403 404
| Name | Type  | Mandatory| Description                                     |
| ------- | ------ | ---- | ----------------------------------------- |
| timeout | number | Yes  | Duration for locking and holding the **RunningLock** object, in ms.|
S
shawn_he 已提交
405 406 407

**Example**

S
shawn_he 已提交
408 409 410 411 412
```js
runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    lock.lock(500);
    console.info('create running lock and lock success');
S
shawn_he 已提交
413
})
S
shawn_he 已提交
414 415
.catch(err => {
    console.error('create running lock failed, err: ' + err);
S
shawn_he 已提交
416 417 418
});
```

S
shawn_he 已提交
419
### unlock<sup>(deprecated)</sup>
S
shawn_he 已提交
420 421 422

unlock(): void

S
shawn_he 已提交
423 424 425
> This API is deprecated since API version 9. You are advised to use [RunningLock.unhold](#unhold9) instead.

Releases a **RunningLock** object.
S
shawn_he 已提交
426

S
shawn_he 已提交
427 428
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
429 430
**Required permission:** ohos.permission.RUNNING_LOCK

S
shawn_he 已提交
431 432
**Example**

S
shawn_he 已提交
433 434 435 436 437
```js
runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    lock.unlock();
    console.info('create running lock and unlock success');
S
shawn_he 已提交
438
})
S
shawn_he 已提交
439 440
.catch(err => {
    console.error('create running lock failed, err: ' + err);
S
shawn_he 已提交
441 442 443
});
```

S
shawn_he 已提交
444
### isUsed<sup>(deprecated)</sup>
S
shawn_he 已提交
445 446 447

isUsed(): boolean

S
shawn_he 已提交
448 449 450
> This API is deprecated since API version 9. You are advised to use [RunningLock.isHolding](#isholding9) instead.

Checks the hold status of the **Runninglock** object.
S
shawn_he 已提交
451

S
shawn_he 已提交
452 453
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
454 455 456 457
**Return value**
| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
| boolean | The value **true** indicates that the **Runninglock** object is held; and the value **false** indicates that the **Runninglock** object is released.|
S
shawn_he 已提交
458 459 460

**Example**

S
shawn_he 已提交
461 462 463 464 465
```js
runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
.then(lock => {
    var isUsed = lock.isUsed();
    console.info('check running lock used status: ' + isUsed);
S
shawn_he 已提交
466
})
S
shawn_he 已提交
467 468
.catch(err => {
    console.error('check running lock used status failed, err: ' + err);
S
shawn_he 已提交
469 470
});
```
S
shawn_he 已提交
471 472 473 474 475 476 477 478 479 480 481

## RunningLockType

Enumerates the types of **RunningLock** objects.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

| Name                    | Value  | Description                                  |
| ------------------------ | ---- | -------------------------------------- |
| BACKGROUND               | 1    | A lock that prevents the system from hibernating when the screen is off.                    |
| PROXIMITY_SCREEN_CONTROL | 2    | A lock that determines whether to turn on or off the screen based on the distance away from the screen.|