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