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