js-apis-runninglock.md 7.7 KB
Newer Older
S
shawn_he 已提交
1
# Running Lock
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
S
shawn_he 已提交
4
> 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.
Z
zengyawen 已提交
5

S
shawn_he 已提交
6 7
The Running Lock module provides APIs for creating, querying, holding, and releasing running locks.

S
shawn_he 已提交
8 9

## Modules to Import
Z
zengyawen 已提交
10 11

```
S
shawn_he 已提交
12
import runningLock from '@ohos.runningLock';
Z
zengyawen 已提交
13 14
```

S
shawn_he 已提交
15 16 17 18 19

## RunningLockType

Enumerates the types of **RunningLock** objects.

S
shawn_he 已提交
20 21
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
22
| Name                      | Default Value | Description                 |
S
shawn_he 已提交
23
| ------------------------ | ---- | ------------------- |
S
shawn_he 已提交
24
| BACKGROUND               | 1    | A lock that prevents the system from hibernating when the screen is off.          |
S
shawn_he 已提交
25
| PROXIMITY_SCREEN_CONTROL | 2    | A lock that determines whether to turn on or off the screen based on the distance away from the screen.|
S
shawn_he 已提交
26 27 28 29 30 31


## isRunningLockTypeSupported

isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback&lt;boolean&gt;): void

S
shawn_he 已提交
32 33 34
Checks whether a specified type of **RunningLock** is supported. This function uses an asynchronous callback to return the result.

**System capability:** SystemCapability.PowerManager.PowerManager.Core
S
shawn_he 已提交
35 36 37

**Parameters**

S
shawn_he 已提交
38 39 40 41
| Name     | Type                          | Mandatory  | Description                                      |
| -------- | ---------------------------- | ---- | ---------------------------------------- |
| type     | RunningLockType              | Yes   | Type of the **RunningLock** object.                              |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to obtain the return value.<br>Return value: The value **true** indicates that the specified type of **RunningLock** is supported, and the value **false** indicates the opposite.|
S
shawn_he 已提交
42 43 44 45 46 47 48 49

**Example**

```
runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (error, supported) => {
    if (typeof error === "undefined") {
        console.info('BACKGROUND support status is ' + supported);
    } else {
Z
zengyawen 已提交
50
        console.log('error: ' + error);
S
shawn_he 已提交
51 52 53 54 55 56 57 58 59 60 61
    }
})
```


## isRunningLockTypeSupported

isRunningLockTypeSupported(type: RunningLockType): Promise&lt;boolean&gt;

Checks whether a specified type of **RunningLock** is supported. This function uses an asynchronous callback to return the result.

S
shawn_he 已提交
62 63
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
64 65
**Parameters**

S
shawn_he 已提交
66 67 68
| Name | Type             | Mandatory  | Description        |
| ---- | --------------- | ---- | ---------- |
| type | RunningLockType | Yes   | Type of the **RunningLock** object.|
S
shawn_he 已提交
69

S
shawn_he 已提交
70
**Return Value**
S
shawn_he 已提交
71

S
shawn_he 已提交
72 73 74
| Type                    | Description                                      |
| ---------------------- | ---------------------------------------- |
| Promise&lt;boolean&gt; | Promise used to asynchronously obtain the return value.<br/>Return value: The value **true** indicates that the specified type of **RunningLock** is supported, and the value **false** indicates the opposite.|
S
shawn_he 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

**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);
});
```


## createRunningLock

createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback&lt;RunningLock&gt;): void

Creates a **RunningLock** object.

S
shawn_he 已提交
95 96 97
**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK
S
shawn_he 已提交
98 99 100

**Parameters**

S
shawn_he 已提交
101 102 103 104 105
| 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&lt;[RunningLock](#runninglock)&gt; | Yes   | Callback used to obtain the return value.|
S
shawn_he 已提交
106 107 108 109

**Example**

```
S
shawn_he 已提交
110 111 112 113 114 115 116 117 118 119
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND, (error, lockIns) => {
    if (typeof error === "undefined") {
        console.log('create runningLock test error: ' + error);
    } else {
        var used = lockIns.isUsed();
        console.info('runninglock is used: ' + used);
        lockIns.lock(500);
        used = lockIns.isUsed();
        console.info('after lock runninglock is used ' + used);
    }
S
shawn_he 已提交
120 121 122 123 124
})
```


## createRunningLock
Z
zengyawen 已提交
125

S
shawn_he 已提交
126
createRunningLock(name: string, type: RunningLockType): Promise&lt;RunningLock&gt;
Z
zengyawen 已提交
127

S
shawn_he 已提交
128 129
Creates a **RunningLock** object.

S
shawn_he 已提交
130 131 132
**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Required permission:** ohos.permission.RUNNING_LOCK
S
shawn_he 已提交
133 134 135

**Parameters**

S
shawn_he 已提交
136 137 138 139
| Name | Type             | Mandatory  | Description       |
| ---- | --------------- | ---- | --------- |
| name | string          | Yes   | Name of the **RunningLock** object.    |
| type | RunningLockType | Yes   | Type of the **RunningLock** object to be created.|
S
shawn_he 已提交
140

S
shawn_he 已提交
141
**Return Value**
S
shawn_he 已提交
142

S
shawn_he 已提交
143 144
| Type                                      | Description                                |
| ---------------------------------------- | ---------------------------------- |
S
shawn_he 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| Promise&lt;[RunningLock](#runninglock)&gt; | 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.

S
shawn_he 已提交
171 172
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
173 174
**Required permission:** ohos.permission.RUNNING_LOCK

S
shawn_he 已提交
175 176
**Parameters**

S
shawn_he 已提交
177 178 179
| Name    | Type    | Mandatory  | Description                        |
| ------- | ------ | ---- | -------------------------- |
| timeout | number | No   | Duration for locking and holding the **RunningLock** object, in ms.|
S
shawn_he 已提交
180 181 182 183 184 185 186 187 188 189

**Example**

```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runningLock => {
    runningLock.lock(100)
    console.info('create runningLock success')
})
.catch(error => {
S
shawn_he 已提交
190
    console.log('create runningLock test error: ' + error)
S
shawn_he 已提交
191 192 193 194 195 196 197 198 199 200
});
```


### unlock

unlock(): void

Releases a **Runninglock** object.

S
shawn_he 已提交
201 202
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
203 204
**Required permission:** ohos.permission.RUNNING_LOCK

S
shawn_he 已提交
205 206 207 208 209 210
**Example**

```
runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
.then(runningLock => {
    runningLock.unlock()
S
shawn_he 已提交
211
    console.info('create and unLock runningLock success')
S
shawn_he 已提交
212 213
})
.catch(error => {
S
shawn_he 已提交
214
    console.log('create runningLock test error: ' + error)
S
shawn_he 已提交
215 216 217 218 219 220 221 222 223 224
});
```


### isUsed

isUsed(): boolean

Checks the status of the **Runninglock** object.

S
shawn_he 已提交
225 226 227 228 229
**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Return Value**
| Type     | Description                                   |
| ------- | ------------------------------------- |
S
shawn_he 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
| 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)
});
```