js-apis-WorkSchedulerExtensionAbility.md 2.6 KB
Newer Older
G
Gloria 已提交
1
# @ohos.WorkSchedulerExtensionAbility (Deferred Task Scheduling Callbacks)
E
esterzhou 已提交
2

G
Gloria 已提交
3
The **WorkSchedulerExtensionAbility** module provides callbacks for deferred task scheduling.
E
ester.zhou 已提交
4 5 6 7 8 9 10

When developing an application, you can override the APIs of this module and add your own task logic to the APIs.

>  **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.
>  - The APIs of this module can be used only in the stage model.
E
esterzhou 已提交
11 12 13 14


## Modules to Import

E
ester.zhou 已提交
15
```ts
E
esterzhou 已提交
16 17 18
import WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'
```

19 20 21 22 23 24
## Attributes

**System capability**: SystemCapability.ResourceSchedule.WorkScheduler

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
25
| context<sup>10+</sup> | [WorkSchedulerExtensionContext](js-apis-inner-application-WorkSchedulerExtensionContext.md)  | Yes| No| Context of the **WorkSchedulerExtension**. This context is inherited from **ExtensionContext**.|
26

E
esterzhou 已提交
27 28
## WorkSchedulerExtensionAbility.onWorkStart

E
ester.zhou 已提交
29
onWorkStart(work: workScheduler.WorkInfo): void
E
esterzhou 已提交
30

G
Gloria 已提交
31
Called when the system starts scheduling the deferred task.
E
esterzhou 已提交
32

E
ester.zhou 已提交
33 34 35 36
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler

**Parameters**

E
ester.zhou 已提交
37 38
| Name | Type                                      | Mandatory  | Description            |
| ---- | ---------------------------------------- | ---- | -------------- |
39
| work | [workScheduler.WorkInfo](js-apis-resourceschedule-workScheduler.md#workinfo) | Yes   | Target task.|
E
ester.zhou 已提交
40 41

**Example**
E
esterzhou 已提交
42

E
ester.zhou 已提交
43
  ```ts
E
esterzhou 已提交
44 45 46 47 48 49 50 51 52
    export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
        onWorkStart(workInfo) {
            console.log('MyWorkSchedulerExtensionAbility onWorkStart' + JSON.stringify(workInfo));
        }
    }
  ```

## WorkSchedulerExtensionAbility.onWorkStop

E
ester.zhou 已提交
53
onWorkStop(work: workScheduler.WorkInfo): void
E
esterzhou 已提交
54

G
Gloria 已提交
55
Called when the system stops scheduling the deferred task.
E
esterzhou 已提交
56

E
ester.zhou 已提交
57 58 59 60
**System capability**: SystemCapability.ResourceSchedule.WorkScheduler

**Parameters**

E
ester.zhou 已提交
61 62
| Name | Type                                      | Mandatory  | Description            |
| ---- | ---------------------------------------- | ---- | -------------- |
63
| work | [workScheduler.WorkInfo](js-apis-resourceschedule-workScheduler.md#workinfo) | Yes   | Target task.|
E
ester.zhou 已提交
64 65 66


**Example**
E
esterzhou 已提交
67

E
ester.zhou 已提交
68
  ```ts
E
esterzhou 已提交
69 70 71 72 73 74
    export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
        onWorkStop(workInfo) {
            console.log('MyWorkSchedulerExtensionAbility onWorkStop' + JSON.stringify(workInfo));
        }
    }
  ```