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