> 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 WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'
```
## WorkSchedulerExtensionAbility.onWorkStart
-**System capability**
SystemCapability.ResourceSchedule.WorkScheduler
-**API**
onWorkStart(workInfo: WorkInfo);
-**Description**
Triggered when the Work Scheduler task starts.
-**Example**
```
export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
> 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
-**System capability**
SystemCapability.ResourceSchedule.WorkScheduler
-**API**
workScheduler.startWork(work: WorkInfo): boolean
-**Description**
Instructs the **WorkSchedulerService** to add the specified task to the execution queue.
-**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| work | WorkInfo | Yes| Task to be added to the execution queue.|
-**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the task is added to the execution queue; returns **false** otherwise.|
Obtains the latest task status. This method uses an asynchronous callback to return the result.
-**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| workId | number | Yes| Task ID.|
|callback|AsyncCallback<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.|
Obtains the latest task status. This method uses a promise to return the result.
-**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| workId | number | Yes| Task ID.|
-**Return value**
| Type| Description|
| -------- | -------- |
| Promise<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.|
Checks whether the last execution of the specified task timed out. This method uses a promise to return the result.
-**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| workId | number | Yes| Task ID.|
-**Return value**
| 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.|
-**Example**
```
workScheduler.isLastWorkTimeOut(500)
.then(res => {
console.info('workschedulerLog isLastWorkTimeOut success, data is:' + res);
If your application needs to execute a non-real-time task, for example, data learning, you can harness the Work Scheduler mechanism, which will schedule the task based on the storage space, power consumption, temperature, and more when the preset conditions are met.
## Available APIs
Import the **workScheduler** package to implement registration:
```js
importworkSchedulerfrom'@ohos.workScheduler';
```
Import the **WorkSchedulerExtensionAbility** package to implement callback:
function startWork(work: WorkInfo): boolean; | Starts a Work Scheduler task.
function stopWork(work: WorkInfo, needCancel?: boolean): boolean; | Stops a Work Scheduler task.
function getWorkStatus(workId: number, callback: AsyncCallback<WorkInfo>): void; | Obtains the status of a Work Scheduler task. This method uses an asynchronous callback to return the result.
function getWorkStatus(workId: number): Promise<WorkInfo>; | Obtains the status of a Work Scheduler task. This method uses a promise to return the result.
function obtainAllWorks(callback: AsyncCallback<void>): Array<WorkInfo>; | Obtains Work Scheduler tasks. This method uses an asynchronous callback to return the result.
function obtainAllWorks(): Promise<Array<WorkInfo>>; | Obtains Work Scheduler tasks. This method uses a promise to return the result.
function stopAndClearWorks(): boolean; | Stops and clears Work Scheduler tasks.
function isLastWorkTimeOut(workId: number, callback: AsyncCallback<void>): boolean; | Checks whether the last execution of the specified task has timed out. This method uses an asynchronous callback to return the result. It is applicable to repeated tasks.
function isLastWorkTimeOut(workId: number): Promise<boolean>; | Checks whether the last execution of the specified task has timed out. This method uses a promise to return the result. It is applicable to repeated tasks.
The Work Scheduler provides a mechanism for applications to execute non-real-time tasks when the system is idle. If the preset conditions are met, the tasks will be placed in the execution queue and scheduled when the system is idle.
## Usage
If your application needs to execute a non-real-time task, for example, data learning, you can harness the Work Scheduler mechanism, which will schedule the task based on the storage space, power consumption, temperature, and more when the preset conditions are met.
## Constraints
The use of the Work Scheduler must comply with the following restrictions and rules:
-**Timeout**: The Work Scheduler callback can run only within the specified period of time. After the timeout, the callback automatically stops.
-**WorkInfo setting**
(1) **workId**, **bundleName**, and **abilityName** are mandatory. **bundleName** must be set to the name of the current application. Otherwise, the verification will fail.
(2) At least one condition must be set.
(3) The repeat interval must be at least 20 minutes and must work with the Always repeat pattern or repeat times.