1# Work Scheduler Development 2 3## When to Use 4 5If 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. For details about the constraints on the Work Scheduler usage, see [Work Scheduler Overview](./work-scheduler-overview.md). 6 7 8## Available APIs 9Import the **workScheduler** package to implement registration: 10```js 11import workScheduler from '@ohos.workScheduler'; 12``` 13 14Import the **WorkSchedulerExtensionAbility** package to implement callback: 15```js 16import WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'; 17``` 18 19### Work Scheduler 20 21**Table 1** Major workScheduler APIs 22 23 API | Description 24 ------------------------------------------------------------ | ------------------------------------------------------------ 25 startWork(work: WorkInfo): boolean | Starts a Work Scheduler task. 26 stopWork(work: WorkInfo, needCancel?: boolean): boolean | Stops a Work Scheduler task. 27 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. 28 getWorkStatus(workId: number): Promise\<WorkInfo> | Obtains the status of a Work Scheduler task. This API uses a promise to return the result. 29 obtainAllWorks(callback: AsyncCallback\<void>): Array\<WorkInfo> | Obtains Work Scheduler tasks. This API uses an asynchronous callback to return the result. 30 obtainAllWorks(): Promise<Array\<WorkInfo>> | Obtains Work Scheduler tasks. This API uses a promise to return the result. 31 stopAndClearWorks(): boolean | Stops and clears Work Scheduler tasks. 32 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. 33 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. 34 35**Table 2** WorkInfo parameters 36 37> **NOTE** 38> 39> For details about the constraints on configuring **WorkInfo**, see [Work Scheduler Overview](./work-scheduler-overview.md). 40 41Name| Type|Description 42---------------------------------------------------------|-----------------------------------------|--------------------------------------------------------- 43workId| number | Work ID. Mandatory. 44bundleName| string | Name of the Work Scheduler task bundle. Mandatory. 45abilityName| string | Name of the component to be notified by a Work Scheduler callback. Mandatory. 46networkType | [NetworkType](../reference/apis/js-apis-workScheduler.md#networktype) | Network type. 47isCharging| boolean | Whether the device is charging. 48chargerType| [ChargingType](../reference/apis/js-apis-workScheduler.md#chargingtype) | Charging type. 49batteryLevel| number | Battery level. 50batteryStatus| [BatteryStatus](../reference/apis/js-apis-workScheduler.md#batterystatus) | Battery status. 51storageRequest| [StorageRequest](../reference/apis/js-apis-workScheduler.md#storagerequest) |Storage status. 52isRepeat| boolean |Whether the task is repeated. 53repeatCycleTime| number |Repeat interval. 54repeatCount | number|Number of repeat times. 55 56**Table 3** Work Scheduler callbacks 57 58Name | Description 59---------------------------------------------------------|----------------------------------------- 60onWorkStart(work: WorkInfo): void | Triggered when the Work Scheduler task starts. 61onWorkStop(work: WorkInfo): void | Triggered when the Work Scheduler task stops. 62 63### How to Develop 64 65**Implementing WorkSchedulerExtensionAbility** 66 67 import WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'; 68 69 export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility { 70 onWorkStart(workInfo) { 71 console.log('MyWorkSchedulerExtensionAbility onWorkStart' + JSON.stringify(workInfo)); 72 } 73 onWorkStop(workInfo) { 74 console.log('MyWorkSchedulerExtensionAbility onWorkStop' + JSON.stringify(workInfo)); 75 } 76 } 77 78 79**Registering a Work Scheduler Task** 80 81 82 83 import workScheduler from '@ohos.workScheduler'; 84 85 let workInfo = { 86 workId: 1, 87 batteryLevel:50, 88 batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW, 89 isRepeat: false, 90 isPersisted: true, 91 bundleName: "com.example.myapplication", 92 abilityName: "MyExtension" 93 } 94 var res = workScheduler.startWork(workInfo); 95 console.info("workschedulerLog res:" + res); 96 97 98**Canceling the Work Scheduler Task** 99 100 101 import workScheduler from '@ohos.workScheduler'; 102 103 let workInfo = { 104 workId: 1, 105 batteryLevel:50, 106 batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW, 107 isRepeat: false, 108 isPersisted: true, 109 bundleName: "com.example.myapplication", 110 abilityName: "MyExtension" 111 } 112 var res = workScheduler.stopWork(workInfo, false); 113 console.info("workschedulerLog res:" + res); 114 115 116**Obtaining a Specified Work Scheduler Task** 117 1181. Callback syntax 119 120 workScheduler.getWorkStatus(50, (err, res) => { 121 if (err) { 122 console.info('workschedulerLog getWorkStatus failed, because:' + err.data); 123 } else { 124 for (let item in res) { 125 console.info('workschedulerLog getWorkStatuscallback success,' + item + ' is:' + res[item]); 126 } 127 } 128 }); 129 130 1312. Promise syntax 132 133 workScheduler.getWorkStatus(50).then((res) => { 134 for (let item in res) { 135 console.info('workschedulerLog getWorkStatus success,' + item + ' is:' + res[item]); 136 } 137 }).catch((err) => { 138 console.info('workschedulerLog getWorkStatus failed, because:' + err.data); 139 }) 140 141 142**Obtaining All Work Scheduler Tasks** 143 1441. Callback syntax 145 146 workScheduler.obtainAllWorks((err, res) =>{ 147 if (err) { 148 console.info('workschedulerLog obtainAllWorks failed, because:' + err.data); 149 } else { 150 console.info('workschedulerLog obtainAllWorks success, data is:' + JSON.stringify(res)); 151 } 152 }); 153 1542. Promise syntax 155 156 workScheduler.obtainAllWorks().then((res) => { 157 console.info('workschedulerLog obtainAllWorks success, data is:' + JSON.stringify(res)); 158 }).catch((err) => { 159 console.info('workschedulerLog obtainAllWorks failed, because:' + err.data); 160 }) 161 162**Stopping and Clearing Work Scheduler Tasks** 163 164 let res = workScheduler.stopAndClearWorks(); 165 console.info("workschedulerLog res:" + res); 166 167**Checking Whether the Last Execution Has Timed Out** 168 1691. Callback syntax 170 171 workScheduler.isLastWorkTimeOut(500, (err, res) =>{ 172 if (err) { 173 console.info('workschedulerLog isLastWorkTimeOut failed, because:' + err.data); 174 } else { 175 console.info('workschedulerLog isLastWorkTimeOut success, data is:' + res); 176 } 177 }); 178 1792. Promise syntax 180 181 workScheduler.isLastWorkTimeOut(500) 182 .then(res => { 183 console.info('workschedulerLog isLastWorkTimeOut success, data is:' + res); 184 }) 185 .catch(err => { 186 console.info('workschedulerLog isLastWorkTimeOut failed, because:' + err.data); 187 }); 188 }) 189