1# @ohos.runningLock (RunningLock) 2 3<!--Kit: Basic Services Kit--> 4<!--Subsystem: PowerManager--> 5<!--Owner: @zhang-yinglie; @volcano_wang--> 6<!--Designer: @wangyantian0--> 7<!--Tester: @alien0208--> 8<!--Adviser: @w_Machine_cc--> 9 10The **runningLock** module provides APIs for creating, querying, holding, and releasing running locks. 11 12> **NOTE** 13> 14> 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. 15 16## Modules to Import 17 18```js 19import {runningLock} from '@kit.BasicServicesKit'; 20``` 21 22## runningLock.isSupported<sup>9+</sup> 23 24isSupported(type: RunningLockType): boolean 25 26Checks whether a specified type of **RunningLock** is supported. 27 28**System capability**: SystemCapability.PowerManager.PowerManager.Core 29 30**Parameters** 31 32| Name| Type | Mandatory| Description | 33| ------ | ----------------------------------- | ---- | -------------------- | 34| type | [RunningLockType](#runninglocktype) | Yes | Type of the running lock. The value must be an enum.| 35 36**Returns** 37 38| Type | Description | 39| ------- | --------------------------------------- | 40| boolean | The value **true** indicates that the specified type of the running lock is supported, and the value **false** indicates the opposite.| 41 42**Error codes** 43 44For details about the error codes, see [RunningLock Error Codes](errorcode-runninglock.md). 45 46| ID | Error Message | 47|---------|---------| 48| 401 | Parameter error. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 49 50**Example** 51 52```js 53try { 54 let isSupported = runningLock.isSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL); 55 console.info('BACKGROUND type supported: ' + isSupported); 56} catch(err) { 57 console.error('check supported failed, err: ' + err); 58} 59``` 60 61## runningLock.create<sup>9+</sup> 62 63create(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void 64 65Creates a **RunningLock** object. 66 67**System capability**: SystemCapability.PowerManager.PowerManager.Core 68 69**Required permission**: ohos.permission.RUNNING_LOCK 70 71**Parameters** 72 73| Name | Type | Mandatory| Description | 74| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 75| name | string | Yes | Name of the **RunningLock** object. The value must be a string. | 76| type | [RunningLockType](#runninglocktype) | Yes | Type of the **RunningLock** object. The value must be an enum. | 77| callback | AsyncCallback<[RunningLock](#runninglock)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and data is the created **RunningLock** object. Otherwise, **err** is an error object. **AsyncCallback** has encapsulated an API of the **RunningLock** class.| 78 79**Error codes** 80 81For details about the error codes, see [RunningLock Error Codes](errorcode-runninglock.md). 82 83| ID | Error Message | 84|---------|---------| 85| 401 | Parameter error. Possible causes: 1.Parameter verification failed. | 86| 201 | If the permission is denied.| 87 88**Example** 89 90```js 91 92runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => { 93 if (typeof err === 'undefined') { 94 console.info('created running lock: ' + lock); 95 } else { 96 console.error('create running lock failed, err: ' + err); 97 } 98}); 99``` 100 101## runningLock.create<sup>9+</sup> 102 103create(name: string, type: RunningLockType): Promise<RunningLock> 104 105Creates a **RunningLock** object. 106 107**System capability**: SystemCapability.PowerManager.PowerManager.Core 108 109**Required permission**: ohos.permission.RUNNING_LOCK 110 111**Parameters** 112 113| Name| Type | Mandatory| Description | 114| ------ | ----------------------------------- | ---- | ------------------ | 115| name | string | Yes | Name of the **RunningLock** object. The value must be a string.| 116| type | [RunningLockType](#runninglocktype) | Yes | Type of the **RunningLock** object. The value must be an enum.| 117 118**Returns** 119 120| Type | Description | 121| ------------------------------------------ | ------------------------------------ | 122| Promise<[RunningLock](#runninglock)> | Promise used to return the result.| 123 124**Error codes** 125 126For details about the error codes, see [RunningLock Error Codes](errorcode-runninglock.md). 127 128| ID | Error Message | 129|---------|---------| 130| 401 | Parameter error. Possible causes: 1.Parameter verification failed. | 131| 201 | If the permission is denied.| 132 133**Example** 134 135```js 136 137runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL) 138.then((lock: runningLock.RunningLock) => { 139 console.info('created running lock: ' + lock); 140}) 141.catch((err: Error) => { 142 console.error('create running lock failed, err: ' + err); 143}); 144``` 145 146## runningLock.isRunningLockTypeSupported<sup>(deprecated)</sup> 147 148isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void 149 150> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [runningLock.isSupported](#runninglockissupported9). 151 152Checks whether a specified type of **RunningLock** is supported. This API uses an asynchronous callback to return the result. 153 154**System capability**: SystemCapability.PowerManager.PowerManager.Core 155 156**Parameters** 157 158| Name | Type | Mandatory| Description | 159| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 160| type | [RunningLockType](#runninglocktype) | Yes | Type of the running lock. | 161| callback | AsyncCallback<boolean> | 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 the specified type of the running lock is supported and **false** indicates the opposite. Otherwise, **err** is an error object.| 162 163**Example** 164 165```js 166runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (err: Error, data: boolean) => { 167 if (typeof err === 'undefined') { 168 console.info('BACKGROUND lock support status: ' + data); 169 } else { 170 console.log('check BACKGROUND lock support status failed, err: ' + err); 171 } 172}); 173``` 174 175## runningLock.isRunningLockTypeSupported<sup>(deprecated)</sup> 176 177isRunningLockTypeSupported(type: RunningLockType): Promise<boolean> 178 179> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [runningLock.isSupported](#runninglockissupported9). 180 181Checks whether a specified type of **RunningLock** is supported. This API uses a promise to return the result. 182 183**System capability**: SystemCapability.PowerManager.PowerManager.Core 184 185**Parameters** 186 187| Name| Type | Mandatory| Description | 188| ------ | ----------------------------------- | ---- | -------------------- | 189| type | [RunningLockType](#runninglocktype) | Yes | Type of the running lock.| 190 191**Returns** 192 193| Type | Description | 194| ---------------------- | ---------------------------------------------------- | 195| Promise<boolean> | Promise used to return the result. The value **true** indicates that the specified type of the running lock is supported, and the value **false** indicates the opposite.| 196 197**Example** 198 199```js 200runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND) 201.then((data: boolean) => { 202 console.info('BACKGROUND lock support status: ' + data); 203}) 204.catch((err: Error) => { 205 console.log('check BACKGROUND lock support status failed, err: ' + err); 206}); 207``` 208 209## runningLock.createRunningLock<sup>(deprecated)</sup> 210 211createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void 212 213> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [runningLock.create](#runninglockcreate9). 214 215Creates a **RunningLock** object. 216 217**System capability**: SystemCapability.PowerManager.PowerManager.Core 218 219**Required permission**: ohos.permission.RUNNING_LOCK 220 221**Parameters** 222 223| Name | Type | Mandatory| Description | 224| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 225| name | string | Yes | Name of the **RunningLock** object. | 226| type | [RunningLockType](#runninglocktype) | Yes | Type of the **RunningLock** object to be created. | 227| 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.| 228 229**Example** 230 231```js 232runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND, (err: Error, lock: runningLock.RunningLock) => { 233 if (typeof err === 'undefined') { 234 console.info('created running lock: ' + lock); 235 } else { 236 console.error('create running lock failed, err: ' + err); 237 } 238}); 239``` 240 241## runningLock.createRunningLock<sup>(deprecated)</sup> 242 243createRunningLock(name: string, type: RunningLockType): Promise<RunningLock> 244 245> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [runningLock.create](#runninglockcreate9). 246 247Creates a **RunningLock** object. 248 249**System capability**: SystemCapability.PowerManager.PowerManager.Core 250 251**Required permission**: ohos.permission.RUNNING_LOCK 252 253**Parameters** 254 255| Name| Type | Mandatory| Description | 256| ------ | ----------------------------------- | ---- | ------------------ | 257| name | string | Yes | Name of the **RunningLock** object. | 258| type | [RunningLockType](#runninglocktype) | Yes | Type of the **RunningLock** object to be created.| 259 260**Returns** 261 262| Type | Description | 263| ------------------------------------------ | ------------------------------------ | 264| Promise<[RunningLock](#runninglock)> | Promise used to return the result.| 265 266**Example** 267 268```js 269runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND) 270.then((lock: runningLock.RunningLock) => { 271 console.info('created running lock: ' + lock); 272}) 273.catch((err: Error) => { 274 console.log('create running lock failed, err: ' + err); 275}); 276``` 277 278## RunningLock 279 280Defines a **RunningLock** object. 281 282### hold<sup>9+</sup> 283 284hold(timeout: number): void 285 286Locks and holds a **RunningLock** object. 287 288**System capability**: SystemCapability.PowerManager.PowerManager.Core 289 290**Required permission**: ohos.permission.RUNNING_LOCK 291 292**Parameters** 293 294| Name | Type | Mandatory| Description | 295| ------- | ------ | ---- | ----------------------------------------- | 296| timeout | number | Yes | Duration for locking and holding the **RunningLock** object, in ms.<br>The value must be a number:<br>**-1**: The lock is permanently held and needs to be released automatically.<br>**0**: The lock is released 3 seconds after the timer expires by default.<br>> 0: The lock is released based on the input value after the timer expires.| 297 298**Error codes** 299 300For details about the error codes, see [RunningLock Error Codes](errorcode-runninglock.md). 301 302| ID | Error Message | 303|---------|----------| 304| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 305| 201 | If the permission is denied.| 306 307**Example** 308 309```ts 310// RunningLockTest.ets 311class RunningLockTest { 312 public static recordLock: runningLock.RunningLock; 313 314 public static holdRunningLock(): void { 315 if (RunningLockTest.recordLock) { 316 RunningLockTest.recordLock.hold(500); 317 console.info('hold running lock success'); 318 } else { 319 runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => { 320 if (typeof err === 'undefined') { 321 console.info('create running lock: ' + lock); 322 RunningLockTest.recordLock = lock; 323 try { 324 lock.hold(500); 325 console.info('hold running lock success'); 326 } catch(err) { 327 console.error('hold running lock failed, err: ' + err); 328 } 329 } else { 330 console.error('create running lock failed, err: ' + err); 331 } 332 }); 333 } 334 } 335} 336``` 337 338### unhold<sup>9+</sup> 339 340unhold(): void 341 342Releases a **RunningLock** object. 343 344**System capability**: SystemCapability.PowerManager.PowerManager.Core 345 346**Required permission**: ohos.permission.RUNNING_LOCK 347 348**Error codes** 349 350For details about the error codes, see [RunningLock Error Codes](errorcode-runninglock.md). 351 352| ID | Error Message | 353|---------|----------| 354| 201 | If the permission is denied.| 355 356 357**Example** 358 359```ts 360// RunningLockTest.ets 361class RunningLockTest { 362 public static recordLock: runningLock.RunningLock; 363 364 public static unholdRunningLock(): void { 365 if (RunningLockTest.recordLock) { 366 RunningLockTest.recordLock.unhold(); 367 console.info('unhold running lock success'); 368 } else { 369 runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => { 370 if (typeof err === 'undefined') { 371 console.info('create running lock: ' + lock); 372 RunningLockTest.recordLock = lock; 373 try { 374 lock.unhold(); 375 console.info('unhold running lock success'); 376 } catch(err) { 377 console.error('unhold running lock failed, err: ' + err); 378 } 379 } else { 380 console.error('create running lock failed, err: ' + err); 381 } 382 }); 383 } 384 } 385} 386``` 387 388### isHolding<sup>9+</sup> 389 390isHolding(): boolean 391 392Checks the hold status of the **RunningLock** object. 393 394**System capability**: SystemCapability.PowerManager.PowerManager.Core 395 396**Returns** 397 398| Type | Description | 399| ------- | ------------------------------------------------------------ | 400| boolean | The value **true** indicates that the **RunningLock** object is held; and the value **false** indicates that the **RunningLock** object is released.| 401 402**Example** 403 404```ts 405// RunningLockTest.ets 406class RunningLockTest { 407 public static recordLock: runningLock.RunningLock; 408 409 public static isHoldingRunningLock(): void { 410 if (RunningLockTest.recordLock) { 411 let isHolding = RunningLockTest.recordLock.isHolding(); 412 console.info('check running lock holding status: ' + isHolding); 413 } else { 414 runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => { 415 if (typeof err === 'undefined') { 416 console.info('create running lock: ' + lock); 417 RunningLockTest.recordLock = lock; 418 let isHolding = lock.isHolding(); 419 console.info('check running lock holding status: ' + isHolding); 420 } else { 421 console.error('create running lock failed, err: ' + err); 422 } 423 }); 424 } 425 } 426} 427``` 428 429### lock<sup>(deprecated)</sup> 430 431lock(timeout: number): void 432 433> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [RunningLock.hold](#hold9). 434 435Locks and holds a **RunningLock** object. 436 437**System capability**: SystemCapability.PowerManager.PowerManager.Core 438 439**Required permission**: ohos.permission.RUNNING_LOCK 440 441**Parameters** 442 443| Name | Type | Mandatory| Description | 444| ------- | ------ | ---- | ----------------------------------------- | 445| timeout | number | Yes | Duration for locking and holding the **RunningLock** object, in ms.| 446 447**Example** 448 449```js 450runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND) 451.then((lock: runningLock.RunningLock) => { 452 lock.lock(500); 453 console.info('create running lock and lock success'); 454}) 455.catch((err: Error) => { 456 console.error('create running lock failed, err: ' + err); 457}); 458``` 459 460### unlock<sup>(deprecated)</sup> 461 462unlock(): void 463 464> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [RunningLock.unhold](#unhold9). 465 466Releases a **RunningLock** object. 467 468**System capability**: SystemCapability.PowerManager.PowerManager.Core 469 470**Required permission**: ohos.permission.RUNNING_LOCK 471 472**Example** 473 474```js 475runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND) 476.then((lock: runningLock.RunningLock) => { 477 lock.unlock(); 478 console.info('create running lock and unlock success'); 479}) 480.catch((err: Error) => { 481 console.error('create running lock failed, err: ' + err); 482}); 483``` 484 485### isUsed<sup>(deprecated)</sup> 486 487isUsed(): boolean 488 489> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [RunningLock.isHolding](#isholding9). 490 491Checks the hold status of the **RunningLock** object. 492 493**System capability**: SystemCapability.PowerManager.PowerManager.Core 494 495**Returns** 496| Type | Description | 497| ------- | ------------------------------------------------------------ | 498| boolean | The value **true** indicates that the **RunningLock** object is held; and the value **false** indicates that the **RunningLock** object is released.| 499 500**Example** 501 502```js 503runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND) 504.then((lock: runningLock.RunningLock) => { 505 let isUsed = lock.isUsed(); 506 console.info('check running lock used status: ' + isUsed); 507}) 508.catch((err: Error) => { 509 console.error('check running lock used status failed, err: ' + err); 510}); 511``` 512 513## RunningLockType 514 515Enumerates the types of **RunningLock** objects. 516 517**System capability**: SystemCapability.PowerManager.PowerManager.Core 518 519| Name | Value | Description | 520| --------------------------------- | ---- | ------------------------------------------------------------ | 521| BACKGROUND<sup>(deprecated)</sup> | 1 | A lock that prevents the system from hibernating when the screen is off.<br>**NOTE**<br>This parameter is supported since API version 7 and deprecated since API version 10.| 522| PROXIMITY_SCREEN_CONTROL | 2 | A lock that enables the proximity sensor and turns on or off the screen based on the distance between the sensor and the obstacle. | 523