You need to sign in or sign up before continuing.
js-apis-workScheduler.md 12.6 KB
Newer Older
E
esterzhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Work Scheduler

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.


## Modules to Import

```
import workScheduler from '@ohos.workScheduler' 
```

## workScheduler.startWork
E
ester.zhou 已提交
14
startWork(work: WorkInfo): boolean
E
esterzhou 已提交
15 16 17

Instructs the **WorkSchedulerService** to add the specified task to the execution queue.

E
ester.zhou 已提交
18
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
19

E
ester.zhou 已提交
20
**Parameters**
E
esterzhou 已提交
21

E
ester.zhou 已提交
22 23 24
| Name | Type                   | Mandatory  | Description            |
| ---- | --------------------- | ---- | -------------- |
| work | [WorkInfo](#workinfo) | Yes   | Task to be added to the execution queue.|
E
esterzhou 已提交
25

E
ester.zhou 已提交
26
**Return value**
E
esterzhou 已提交
27

E
ester.zhou 已提交
28 29 30
| Type     | Description                              |
| ------- | -------------------------------- |
| boolean | Returns **true** if the task is added to the execution queue; returns **false** otherwise.|
E
esterzhou 已提交
31

E
ester.zhou 已提交
32
**Example**
E
esterzhou 已提交
33

E
ester.zhou 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
```
  let workInfo = {
      workId: 1,
      batteryLevel:50,
      batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW,
      isRepeat: false,
      isPersisted: true,
      bundleName: "com.example.myapplication",
      abilityName: "MyExtension"
  }
  var res = workScheduler.startWork(workInfo);
  console.info("workschedulerLog res:" + res);
```
E
esterzhou 已提交
47

E
ester.zhou 已提交
48
## workScheduler.stopWork
E
esterzhou 已提交
49 50 51 52
stopWork(work: WorkInfo, needCancel?: boolean): boolean

Instructs the **WorkSchedulerService** to stop the specified task.

E
ester.zhou 已提交
53
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
54

E
ester.zhou 已提交
55
**Parameters**
E
esterzhou 已提交
56

E
ester.zhou 已提交
57 58 59 60
| Name       | Type                   | Mandatory  | Description        |
| ---------- | --------------------- | ---- | ---------- |
| work       | [WorkInfo](#workinfo) | Yes   | Task to stop. |
| needCancel | boolean               | Yes   | Whether to cancel the task.|
E
esterzhou 已提交
61

E
ester.zhou 已提交
62
**Return value**
E
esterzhou 已提交
63

E
ester.zhou 已提交
64 65 66
| Type     | Description                     |
| ------- | ----------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
E
esterzhou 已提交
67

E
ester.zhou 已提交
68
**Example**
E
esterzhou 已提交
69

E
ester.zhou 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
```
  let workInfo = {
      workId: 1,
      batteryLevel:50,
      batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW,
      isRepeat: false,
      isPersisted: true,
      bundleName: "com.example.myapplication",
      abilityName: "MyExtension"
     }
  var res = workScheduler.stopWork(workInfo, false);
  console.info("workschedulerLog res:" + res);
```
E
esterzhou 已提交
83

E
ester.zhou 已提交
84 85
## workScheduler.getWorkStatus
getWorkStatus(workId: number, callback : AsyncCallback\<WorkInfo>): void
E
esterzhou 已提交
86

E
ester.zhou 已提交
87
Obtains the latest task status. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
88

E
ester.zhou 已提交
89
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
90

E
ester.zhou 已提交
91
**Parameters**
E
esterzhou 已提交
92

E
ester.zhou 已提交
93 94 95 96
| Name     | Type                                   | Mandatory  | Description                                      |
| -------- | ------------------------------------- | ---- | ---------------------------------------- |
| workId   | number                                | Yes   | Task ID.                                |
| callback | AsyncCallback\<[WorkInfo](#workinfo)> | Yes   | Callback used to return the result. Returns the task status obtained from the **WorkSchedulerService** if the specified task ID is valid; returns **null** otherwise.|
E
esterzhou 已提交
97

E
ester.zhou 已提交
98
**Example**
E
esterzhou 已提交
99

E
ester.zhou 已提交
100 101 102 103 104 105 106
```
  workScheduler.getWorkStatus(50, (err, res) => {
    if (err) {
      console.info('workschedulerLog getWorkStatus failed, because:' + err.data);
    } else {
      for (let item in res) {
        console.info('workschedulerLog getWorkStatus success,' + item + ' is:' + res[item]);
E
esterzhou 已提交
107
      }
E
ester.zhou 已提交
108 109 110
    }
  });
```
E
esterzhou 已提交
111 112

## workScheduler.getWorkStatus
E
ester.zhou 已提交
113
getWorkStatus(workId: number): Promise\<WorkInfo>
E
esterzhou 已提交
114

E
ester.zhou 已提交
115
Obtains the latest task status. This API uses a promise to return the result.
E
esterzhou 已提交
116

E
ester.zhou 已提交
117
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
118

E
ester.zhou 已提交
119
**Parameters**
E
esterzhou 已提交
120

E
ester.zhou 已提交
121 122 123
| Name   | Type    | Mandatory  | Description      |
| ------ | ------ | ---- | -------- |
| workId | number | Yes   | Task ID.|
E
esterzhou 已提交
124

E
ester.zhou 已提交
125
**Return value**
E
esterzhou 已提交
126

E
ester.zhou 已提交
127 128 129
| Type                             | Description                                      |
| ------------------------------- | ---------------------------------------- |
| Promise\<[WorkInfo](#workinfo)> | Promise used to return the result. Returns the task status obtained from the **WorkSchedulerService** if the specified task ID is valid; returns **null** otherwise.|
E
esterzhou 已提交
130

E
ester.zhou 已提交
131
**Example**
E
esterzhou 已提交
132

E
ester.zhou 已提交
133 134 135 136 137 138 139 140 141
```
  workScheduler.getWorkStatus(50).then((res) => {
    for (let item in res) {
      console.info('workschedulerLog getWorkStatus success,' + item + ' is:' + res[item]);
    }
  }).catch((err) => {
    console.info('workschedulerLog getWorkStatus failed, because:' + err.data);
  })
```
E
esterzhou 已提交
142 143

## workScheduler.obtainAllWorks
E
ester.zhou 已提交
144
obtainAllWorks(callback : AsyncCallback\<void>): Array\<WorkInfo>
E
esterzhou 已提交
145

E
ester.zhou 已提交
146
Obtains all tasks associated with this application. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
147

E
ester.zhou 已提交
148
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
149

E
ester.zhou 已提交
150
**Parameters**
E
esterzhou 已提交
151

E
ester.zhou 已提交
152 153 154
| Name     | Type                  | Mandatory  | Description                             |
| -------- | -------------------- | ---- | ------------------------------- |
| callback | AsyncCallback\<void> | Yes   | Callback used to return all tasks associated with the current application.|
E
esterzhou 已提交
155

E
ester.zhou 已提交
156
**Return value**
E
esterzhou 已提交
157

E
ester.zhou 已提交
158 159 160
| Type                           | Description             |
| ----------------------------- | --------------- |
| Array\<[WorkInfo](#workinfo)> | All tasks associated with the current application.|
E
esterzhou 已提交
161

E
ester.zhou 已提交
162
**Example**
E
esterzhou 已提交
163

E
ester.zhou 已提交
164 165 166 167 168 169 170 171 172
```
  workScheduler.obtainAllWorks((err, res) =>{
    if (err) {
      console.info('workschedulerLog obtainAllWorks failed, because:' + err.data);
    } else {
      console.info('workschedulerLog obtainAllWorks success, data is:' + JSON.stringify(res));
    }
  });
```
E
esterzhou 已提交
173 174

## workScheduler.obtainAllWorks
E
ester.zhou 已提交
175
obtainAllWorks(): Promise<Array\<WorkInfo>>
E
esterzhou 已提交
176

E
ester.zhou 已提交
177
Obtains all tasks associated with this application. This API uses a promise to return the result.
E
esterzhou 已提交
178

E
ester.zhou 已提交
179
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
180

E
ester.zhou 已提交
181
**Return value**
E
esterzhou 已提交
182

E
ester.zhou 已提交
183 184 185
| Type                                    | Description                            |
| -------------------------------------- | ------------------------------ |
| Promise<Array\<[WorkInfo](#workinfo)>> | Promise used to return all tasks associated with the current application.|
E
esterzhou 已提交
186

E
ester.zhou 已提交
187
**Example**
E
esterzhou 已提交
188

E
ester.zhou 已提交
189 190 191 192 193 194 195
```
  workScheduler.obtainAllWorks().then((res) => {
    console.info('workschedulerLog obtainAllWorks success, data is:' + JSON.stringify(res));
  }).catch((err) => {
    console.info('workschedulerLog obtainAllWorks failed, because:' + err.data);
  })
```
E
esterzhou 已提交
196 197 198 199 200 201

## workScheduler.stopAndClearWorks
stopAndClearWorks(): boolean

Stops and cancels all tasks associated with the current application.

E
ester.zhou 已提交
202 203 204
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler

**Example**
E
esterzhou 已提交
205

E
ester.zhou 已提交
206 207 208 209
```
  let res = workScheduler.stopAndClearWorks();
  console.info("workschedulerLog res:" + res);
```
E
esterzhou 已提交
210 211

## workScheduler.isLastWorkTimeOut
E
ester.zhou 已提交
212
isLastWorkTimeOut(workId: number, callback : AsyncCallback\<void>): boolean
E
esterzhou 已提交
213

E
ester.zhou 已提交
214
Checks whether the last execution of the specified task timed out. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
215

E
ester.zhou 已提交
216
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
217

E
ester.zhou 已提交
218
**Parameters**
E
esterzhou 已提交
219

E
ester.zhou 已提交
220 221 222 223
| Name     | Type                  | Mandatory  | Description                                      |
| -------- | -------------------- | ---- | ---------------------------------------- |
| workId   | number               | Yes   | Task ID.                                |
| callback | AsyncCallback\<void> | Yes   | Callback used to return the result.|
E
esterzhou 已提交
224

E
ester.zhou 已提交
225
**Return value**
E
esterzhou 已提交
226

E
ester.zhou 已提交
227 228 229
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the last execution of the specified task timed out; returns **false** otherwise.|
E
esterzhou 已提交
230

E
ester.zhou 已提交
231
**Example**
E
esterzhou 已提交
232

E
ester.zhou 已提交
233 234 235 236 237 238 239 240 241
```
  workScheduler.isLastWorkTimeOut(500, (err, res) =>{
    if (err) {
      console.info('workschedulerLog isLastWorkTimeOut failed, because:' + err.data);
    } else {
      console.info('workschedulerLog isLastWorkTimeOut success, data is:' + res);
    }
  });
```
E
esterzhou 已提交
242 243

## workScheduler.isLastWorkTimeOut
E
ester.zhou 已提交
244
isLastWorkTimeOut(workId: number): Promise\<boolean>
E
esterzhou 已提交
245

E
ester.zhou 已提交
246
Checks whether the last execution of the specified task timed out. This API uses a promise to return the result.
E
esterzhou 已提交
247

E
ester.zhou 已提交
248
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
249

E
ester.zhou 已提交
250
**Parameters**
E
esterzhou 已提交
251

E
ester.zhou 已提交
252 253 254
| Name   | Type    | Mandatory  | Description      |
| ------ | ------ | ---- | -------- |
| workId | number | Yes   | Task ID.|
E
esterzhou 已提交
255

E
ester.zhou 已提交
256
**Return value**
E
esterzhou 已提交
257

E
ester.zhou 已提交
258 259 260
| Type               | Description                                      |
| ----------------- | ---------------------------------------- |
| Promise\<boolean> | Promise used to return the result. Returns **true** if the last execution of the specified task timed out; returns **false** otherwise.|
E
esterzhou 已提交
261

E
ester.zhou 已提交
262
**Example**
E
esterzhou 已提交
263

E
ester.zhou 已提交
264 265 266 267
```
  workScheduler.isLastWorkTimeOut(500)
    .then(res => {
      console.info('workschedulerLog isLastWorkTimeOut success, data is:' + res);
E
esterzhou 已提交
268
    })
E
ester.zhou 已提交
269 270 271 272
    .catch(err =>  {
      console.info('workschedulerLog isLastWorkTimeOut failed, because:' + err.data);
    });
```
E
esterzhou 已提交
273

E
ester.zhou 已提交
274
## WorkInfo
E
esterzhou 已提交
275 276
Provides detailed information about the task.

E
ester.zhou 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler

| Name            | Type                               | Mandatory  | Description              |
| --------------- | --------------------------------- | ---- | ---------------- |
| workId          | number                            | Yes   | Task ID.         |
| bundleName      | string                            | Yes   | Name of the Work Scheduler task bundle.          |
| abilityName     | string                            | Yes   | Name of the component to be notified by a Work Scheduler callback.|
| networkType     | [NetworkType](#networktype)       | No   | Network type.            |
| isCharging      | boolean                           | No   | Whether the device is charging.            |
| chargerType     | [ChargingType](#chargingtype)     | No   | Charging type.            |
| batteryLevel    | number                            | No   | Battery level.              |
| batteryStatus   | [BatteryStatus](#batterystatus)   | No   | Battery status.            |
| storageRequest  | [StorageRequest](#storagerequest) | No   | Storage status.            |
| isRepeat        | boolean                           | No   | Whether the task is repeated.          |
| repeatCycleTime | number                            | No   | Repeat interval.            |
| repeatCount     | number                            | No   | Number of repeat times.            |

## NetworkType
E
esterzhou 已提交
295 296
Enumerates the network types that can trigger the task.

E
ester.zhou 已提交
297
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
298

E
ester.zhou 已提交
299 300 301 302 303 304 305 306
| Name                    | Default Value | Description                     |
| ---------------------- | ---- | ----------------------- |
| NETWORK_TYPE_ANY       | 0    | Any network type.    |
| NETWORK_TYPE_MOBILE    | 1    | Mobile network.   |
| NETWORK_TYPE_WIFI      | 2    | Wi-Fi network.  |
| NETWORK_TYPE_BLUETOOTH | 3    | Bluetooth network.|
| NETWORK_TYPE_WIFI_P2P  | 4    | Wi-Fi P2P network. |
| NETWORK_TYPE_ETHERNET  | 5    | Ethernet.       |
E
esterzhou 已提交
307

E
ester.zhou 已提交
308
## ChargingType
E
esterzhou 已提交
309 310
Enumerates the charging types that can trigger the task.

E
ester.zhou 已提交
311
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
312

E
ester.zhou 已提交
313 314 315 316 317 318
| Name                       | Default Value | Description                  |
| ------------------------- | ---- | -------------------- |
| CHARGING_PLUGGED_ANY      | 0    | Any charging type.|
| CHARGING_PLUGGED_AC       | 1    | DC charging.   |
| CHARGING_PLUGGED_USB      | 2    | USB charging.    |
| CHARGING_PLUGGED_WIRELESS | 3    | Wireless charging.   |
E
esterzhou 已提交
319

E
ester.zhou 已提交
320
## BatteryStatus
E
esterzhou 已提交
321 322
Enumerates the battery status that can trigger the task.

E
ester.zhou 已提交
323
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
E
esterzhou 已提交
324

E
ester.zhou 已提交
325 326 327 328 329
| Name                        | Default Value | Description                        |
| -------------------------- | ---- | -------------------------- |
| BATTERY_STATUS_LOW         | 0    | A low battery alert is displayed.            |
| BATTERY_STATUS_OKAY        | 1    | The battery level is restored from low to normal.      |
| BATTERY_STATUS_LOW_OR_OKAY | 2    | The battery level is restored from low to normal, or a low battery alert is displayed.|
E
esterzhou 已提交
330

E
ester.zhou 已提交
331
## StorageRequest
E
esterzhou 已提交
332 333
Enumerates the storage status that can trigger the task.

E
ester.zhou 已提交
334 335 336
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler

  |Name   |Default Value   |Description|
E
esterzhou 已提交
337 338 339
  | -------- | -------- | -------- |
  |STORAGE_LEVEL_LOW    |0    |The storage space is insufficient.
  |STORAGE_LEVEL_OKAY    |1    |The storage space is restored from insufficient to normal.
E
ester.zhou 已提交
340
  |STORAGE_LEVEL_LOW_OR_OKAY    |2    |The storage space is restored from insufficient to normal, or the storage space is insufficient.