The **WorkSchedulerExtensionAbility** module provides callbacks for Work Scheduler tasks.
> The initial APIs of this module are supported since API version 9. API version 9 is a canary version for trial use. The APIs of this version may be unstable.
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.
The **workScheduler** module provides the APIs for registering, canceling, and querying Work Scheduler tasks, which do not have real-time constraints.
> The initial APIs of this module are supported since API version 9. API version 9 is a canary version for trial use. The APIs of this version may be unstable.
The system executes Work Scheduler tasks at an appropriate time, subject to the storage space, power consumption, temperature, and more.
> **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.
## Modules to Import
## Modules to Import
```
```js
importworkSchedulerfrom'@ohos.workScheduler'
importworkSchedulerfrom'@ohos.workScheduler'
```
```
...
@@ -31,15 +37,20 @@ Instructs the **WorkSchedulerService** to add the specified task to the executio
...
@@ -31,15 +37,20 @@ Instructs the **WorkSchedulerService** to add the specified task to the executio
@@ -261,7 +277,7 @@ Checks whether the last execution of the specified task timed out. This API uses
...
@@ -261,7 +277,7 @@ Checks whether the last execution of the specified task timed out. This API uses
**Example**
**Example**
```
```js
workScheduler.isLastWorkTimeOut(500)
workScheduler.isLastWorkTimeOut(500)
.then(res=>{
.then(res=>{
console.info('workschedulerLog isLastWorkTimeOut success, data is:'+res);
console.info('workschedulerLog isLastWorkTimeOut success, data is:'+res);
...
@@ -272,27 +288,28 @@ Checks whether the last execution of the specified task timed out. This API uses
...
@@ -272,27 +288,28 @@ Checks whether the last execution of the specified task timed out. This API uses
```
```
## WorkInfo
## WorkInfo
Provides detailed information about the task.
Provides detailed information about the task. For details about the constraints on configuring **WorkInfo**, see [Work Scheduler Overview](../../task-management/work-scheduler-overview.md).
function startWork(work: WorkInfo): boolean; | Starts a Work Scheduler task.
startWork(work: WorkInfo): boolean; | Starts a Work Scheduler task.
function stopWork(work: WorkInfo, needCancel?: boolean): boolean; | Stops a Work Scheduler task.
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.
getWorkStatus(workId: number, callback: AsyncCallback\<WorkInfo>): void; | Obtains the status of a Work Scheduler task. This API 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.
getWorkStatus(workId: number): Promise\<WorkInfo>; | Obtains the status of a Work Scheduler task. This API 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.
obtainAllWorks(callback: AsyncCallback\<void>): Array\<WorkInfo>; | Obtains Work Scheduler tasks. This API 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.
obtainAllWorks(): Promise<Array\<WorkInfo>>; | Obtains Work Scheduler tasks. This API uses a promise to return the result.
function stopAndClearWorks(): boolean;| Stops and clears Work Scheduler tasks.
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.
isLastWorkTimeOut(workId: number, callback: AsyncCallback\<void>): boolean; | Checks whether the last execution of the specified task has timed out. This API 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.
isLastWorkTimeOut(workId: number): Promise\<boolean>; | Checks whether the last execution of the specified task has timed out. This API uses a promise to return the result. It is applicable to repeated tasks.
**Table 2** WorkInfo parameters
**Table 2** WorkInfo parameters
API|Description|Type
> **NOTE**
>
> For details about the constraints on configuring WorkInfo, see [Work Scheduler Overview](./work-scheduler-overview.md).
@@ -11,10 +11,23 @@ If your application needs to execute a non-real-time task, for example, data lea
...
@@ -11,10 +11,23 @@ If your application needs to execute a non-real-time task, for example, data lea
The use of the Work Scheduler must comply with the following restrictions and rules:
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.
-**Timeout**: The Work Scheduler callback can run only within the specified period of time. After the timeout, the callback automatically stops.
-**Execution frequency**: The system controls the execution frequency of Work Scheduler tasks based on the activity level of their respective applications.
Application Group | Work Scheduler Task Execution Frequency
--------------------|-------------------------
Active| At a minimum interval of 2 hours
Used every day| At a minimum interval of 4 hours
Frequently used| At a minimum interval of 24 hours
Infrequently used| At a minimum interval of 48 hours
Restricted| Prohibited
Unused| Prohibited
-**WorkInfo setting**
-**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.
-**workId**, **bundleName**, and **abilityName** are mandatory. **bundleName** must be set to the name of the current application. Otherwise, the verification will fail.
- At least one condition must be set.
(2) At least one condition must be set.
- The repeat interval must be at least 20 minutes and must work with the Always repeat pattern or repeat times.
(3) The repeat interval must be at least 20 minutes and must work with the Always repeat pattern or repeat times.
- The carried parameters can be of the number, string, or bool type.