# Running Lock
>![](../../public_sys-resources/icon-note.gif) **NOTE:**
>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.
## Modules to Import
```
import runninglock from '@ohos.runningLock';
```
## Required Permissions
To request or use a running lock, you must declare the **ohos.permission.RUNNING\_LOCK** permission.
## RunningLockType
Enumerates the types of running locks.
Description
|
Default Value
|
Description
|
BACKGROUND
|
1
|
A lock that prevents the system from hibernating.
|
PROXIMITY_SCREEN_CONTROL
|
2
|
A lock that determines whether to turn on or off the screen based on the distance away from the screen.
|
## runninglock.isRunningLockTypeSupported
isRunningLockTypeSupported\(type: RunningLockType, callback: AsyncCallback\): void
Checks whether a specified type of **RunningLock** is supported. This function uses an asynchronous callback to return the result.
- Parameters
Name
|
Type
|
Mandatory
|
Description
|
type
|
RunningLockType
|
Yes
|
Type of the RunningLock object.
|
callback
|
AsyncCallback<boolean>
|
Yes
|
Callback used to obtain the return value.
The value true indicates that the specified type of RunningLock is supported, and value false indicates the opposite.
|
- Example
```
runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (error, supported) => {
if (typeof error === "undefined") {
console.info('BACKGROUND support status is ' + supported);
} else {
console.log('error: ' + error);
}
})
```
## runninglock.isRunningLockTypeSupported
isRunningLockTypeSupported\(type: RunningLockType\): Promise
Checks whether a specified type of **RunningLock** is supported. This function uses a promise to return the result.
- Parameters
Name
|
Type
|
Mandatory
|
Description
|
type
|
RunningLockType
|
Yes
|
Type of the RunningLock object.
|
- Return values
Type
|
Description
|
Promise<boolean>
|
Promise used to asynchronously obtain the return value. The value true indicates that the specified type of RunningLock is supported, and value false indicates the opposite.
|
- Example
```
runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL)
.then(supported => {
console.info('PROXIMITY_SCREEN_CONTROL support status is ' + supported);
})
.catch(error => {
console.log('error: ' + error);
});
```
## runninglock.createRunningLock
createRunningLock\(name: string, type: RunningLockType, callback: AsyncCallback\): void
Creates a **RunningLock** object.
- Parameters
Name
|
Type
|
Mandatory
|
Description
|
name
|
string
|
Yes
|
Name of the RunningLock object.
|
type
|
RunningLockType
|
Yes
|
Type of the RunningLock object to be created.
|
callback
|
AsyncCallback<RunningLock>
|
Yes
|
Callback used to obtain the return value.
|
- Example
```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runninglock => {
var used = runninglock.isUsed();
console.info('runninglock is used: ' + used);
runninglock.lock(500);
used = runninglock.isUsed();
console.info('after lock runninglock is used ' + used);
})
.catch(error => {
console.log('create runningLock test error: ' + error);
})
```
## runninglock.createRunningLock
createRunningLock\(name: string, type: RunningLockType\): Promise
Creates a **RunningLock** object.
- Parameters
Name
|
Type
|
Mandatory
|
Description
|
name
|
string
|
Yes
|
Name of the RunningLock object.
|
type
|
RunningLockType
|
Yes
|
Type of the RunningLock object to be created.
|
- Return values
Type
|
Description
|
Promise<RunningLock>
|
Promise used to asynchronously obtain the returned RunningLock object.
|
- Example
```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runninglock => {
console.info('create runningLock success');
})
.catch(error => {
console.log('create runningLock test error: ' + error);
})
```
## RunningLock
Defines a **RunningLock** object.
### lock
lock\(timeout: number\): void
Locks and holds a **RunningLock** object.
- Parameters
Name
|
Type
|
Mandatory
|
Description
|
timeout
|
number
|
No
|
Duration for locking and holding the RunningLock object.
|
- Example
```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runningLock => {
runningLock.lock(100)
console.info('create runningLock success')
})
.catch(error => {
console.log('Lock runningLock test error: ' + error)
});
```
### unlock
unlock\(\): void
Releases a **Runninglock** object.
- Example
```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runningLock => {
runningLock.unlock()
console.info('unLock runningLock success')
})
.catch(error => {
console.log('unLock runningLock test error: ' + error)
});
```
### isUsed
isUsed\(\): boolean
Checks the status of the **Runninglock** object.
- Return values
Type
|
Description
|
boolean
|
Returns true if the Runninglock object is held; returns false if the Runninglock object is released.
|
- Example
```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runningLock => {
var used = runningLock.isUsed()
console.info('runningLock used status: ' + used)
})
.catch(error => {
console.log('runningLock isUsed test error: ' + error)
});
```