• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Running Lock
2
3> **NOTE**<br>
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.
5
6The Running Lock module provides APIs for creating, querying, holding, and releasing running locks.
7
8
9## Modules to Import
10
11```
12import runningLock from '@ohos.runningLock';
13```
14
15
16## RunningLockType
17
18Enumerates the types of **RunningLock** objects.
19
20**System capability:** SystemCapability.PowerManager.PowerManager.Core
21
22| Name                      | Default Value | Description                 |
23| ------------------------ | ---- | ------------------- |
24| BACKGROUND               | 1    | A lock that prevents the system from hibernating when the screen is off.          |
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.|
26
27
28## isRunningLockTypeSupported
29
30isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback&lt;boolean&gt;): void
31
32Checks whether a specified type of **RunningLock** is supported. This function uses an asynchronous callback to return the result.
33
34**System capability:** SystemCapability.PowerManager.PowerManager.Core
35
36**Parameters**
37
38| Name     | Type                          | Mandatory  | Description                                      |
39| -------- | ---------------------------- | ---- | ---------------------------------------- |
40| type     | RunningLockType              | Yes   | Type of the **RunningLock** object.                              |
41| 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.|
42
43**Example**
44
45```
46runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (error, supported) => {
47    if (typeof error === "undefined") {
48        console.info('BACKGROUND support status is ' + supported);
49    } else {
50        console.log('error: ' + error);
51    }
52})
53```
54
55
56## isRunningLockTypeSupported
57
58isRunningLockTypeSupported(type: RunningLockType): Promise&lt;boolean&gt;
59
60Checks whether a specified type of **RunningLock** is supported. This function uses an asynchronous callback to return the result.
61
62**System capability:** SystemCapability.PowerManager.PowerManager.Core
63
64**Parameters**
65
66| Name | Type             | Mandatory  | Description        |
67| ---- | --------------- | ---- | ---------- |
68| type | RunningLockType | Yes   | Type of the **RunningLock** object.|
69
70**Return Value**
71
72| Type                    | Description                                      |
73| ---------------------- | ---------------------------------------- |
74| 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.|
75
76**Example**
77
78```
79runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL)
80.then(supported => {
81    console.info('PROXIMITY_SCREEN_CONTROL support status is ' + supported);
82})
83.catch(error => {
84    console.log('error: ' + error);
85});
86```
87
88
89## createRunningLock
90
91createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback&lt;RunningLock&gt;): void
92
93Creates a **RunningLock** object.
94
95**System capability:** SystemCapability.PowerManager.PowerManager.Core
96
97**Required permission:** ohos.permission.RUNNING_LOCK
98
99**Parameters**
100
101| Name     | Type                                      | Mandatory  | Description                                    |
102| -------- | ---------------------------------------- | ---- | -------------------------------------- |
103| name     | string                                   | Yes   | Name of the **RunningLock** object.                                 |
104| type     | RunningLockType                          | Yes   | Type of the **RunningLock** object to be created.                             |
105| callback | AsyncCallback&lt;[RunningLock](#runninglock)&gt; | Yes   | Callback used to obtain the return value.|
106
107**Example**
108
109```
110runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND, (error, lockIns) => {
111    if (typeof error === "undefined") {
112        var used = lockIns.isUsed();
113        console.info('runninglock is used: ' + used);
114        lockIns.lock(500);
115        used = lockIns.isUsed();
116        console.info('after lock runninglock is used ' + used);
117    } else {
118        console.log('create runningLock test error: ' + error);
119    }
120})
121```
122
123
124## createRunningLock
125
126createRunningLock(name: string, type: RunningLockType): Promise&lt;RunningLock&gt;
127
128Creates a **RunningLock** object.
129
130**System capability:** SystemCapability.PowerManager.PowerManager.Core
131
132**Required permission:** ohos.permission.RUNNING_LOCK
133
134**Parameters**
135
136| Name | Type             | Mandatory  | Description       |
137| ---- | --------------- | ---- | --------- |
138| name | string          | Yes   | Name of the **RunningLock** object.    |
139| type | RunningLockType | Yes   | Type of the **RunningLock** object to be created.|
140
141**Return Value**
142
143| Type                                      | Description                                |
144| ---------------------------------------- | ---------------------------------- |
145| Promise&lt;[RunningLock](#runninglock)&gt; | Promise used to asynchronously obtain the returned **RunningLock** object.|
146
147**Example**
148
149```
150runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
151.then(runninglock => {
152    console.info('create runningLock success');
153})
154.catch(error => {
155    console.log('create runningLock test error: ' + error);
156})
157```
158
159
160## RunningLock
161
162Defines a **RunningLock** object.
163
164
165### lock
166
167lock(timeout: number): void
168
169Locks and holds a **RunningLock** object.
170
171**System capability:** SystemCapability.PowerManager.PowerManager.Core
172
173**Required permission:** ohos.permission.RUNNING_LOCK
174
175**Parameters**
176
177| Name    | Type    | Mandatory  | Description                        |
178| ------- | ------ | ---- | -------------------------- |
179| timeout | number | No   | Duration for locking and holding the **RunningLock** object, in ms.|
180
181**Example**
182
183```
184runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
185.then(runningLock => {
186    runningLock.lock(100)
187    console.info('create runningLock success')
188})
189.catch(error => {
190    console.log('create runningLock test error: ' + error)
191});
192```
193
194
195### unlock
196
197unlock(): void
198
199Releases a **Runninglock** object.
200
201**System capability:** SystemCapability.PowerManager.PowerManager.Core
202
203**Required permission:** ohos.permission.RUNNING_LOCK
204
205**Example**
206
207```
208runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
209.then(runningLock => {
210    runningLock.unlock()
211    console.info('create and unLock runningLock success')
212})
213.catch(error => {
214    console.log('create runningLock test error: ' + error)
215});
216```
217
218
219### isUsed
220
221isUsed(): boolean
222
223Checks the status of the **Runninglock** object.
224
225**System capability:** SystemCapability.PowerManager.PowerManager.Core
226
227**Return Value**
228| Type     | Description                                   |
229| ------- | ------------------------------------- |
230| boolean | Returns **true** if the **Runninglock** object is held; returns **false** if the **Runninglock** object is released.|
231
232**Example**
233
234```
235runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND)
236.then(runningLock => {
237    var used = runningLock.isUsed()
238    console.info('runningLock used status: ' + used)
239})
240.catch(error => {
241    console.log('runningLock isUsed test error: ' + error)
242});
243```
244