1# Work Scheduler Development 2 3## When to Use 4 5If your application needs to execute a non-real-time task or a persistent 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. Your application must implement the callbacks provided by [WorkSchedulerExtensionAbility](./workscheduler-extensionability.md) for Work Scheduler tasks. 6For details about the restrictions, see [Restrictions on Using Work Scheduler](./background-task-overview.md#restrictions-on-using-work-scheduler). 7 8## Available APIs 9 10**Table 1** Major workScheduler APIs 11 12API | Description 13---------------------------------------------------------|----------------------------------------- 14startWork(work: WorkInfo): void; | Starts a Work Scheduler task. 15stopWork(work: WorkInfo, needCancel?: boolean): void; | Stops a Work Scheduler task. 16getWorkStatus(workId: number, callback: AsyncCallback\<WorkInfo>): void;| Obtains the status of a Work Scheduler task. This API uses an asynchronous callback to return the result. 17getWorkStatus(workId: number): Promise\<WorkInfo>; | Obtains the status of a Work Scheduler task. This API uses a promise to return the result. 18obtainAllWorks(callback: AsyncCallback\<void>): Array\<WorkInfo>;| Obtains all the Work Scheduler tasks. This API uses an asynchronous callback to return the result. 19obtainAllWorks(): Promise<Array\<WorkInfo>>;| Obtains all the Work Scheduler tasks. This API uses a promise to return the result. 20stopAndClearWorks(): void;| Stops and clears all the Work Scheduler tasks. 21isLastWorkTimeOut(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. 22isLastWorkTimeOut(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. 23 24**Table 2** WorkInfo parameters 25 26For details about the restriction on configuring **WorkInfo**, see [Restrictions on Using Work Scheduler](./background-task-overview.md#restrictions-on-using-work-scheduler). 27 28Name| Type|Description 29---------------------------------------------------------|-----------------------------------------|--------------------------------------------------------- 30workId| number | ID of the Work Scheduler task. Mandatory. 31bundleName| string | Bundle name of the Work Scheduler task. Mandatory. 32abilityName| string | Name of the ability to be notified by a Work Scheduler callback. Mandatory. 33networkType | [NetworkType](../reference/apis/js-apis-resourceschedule-workScheduler.md#networktype) | Network type. 34isCharging| boolean | Whether the device is charging. 35chargerType| [ChargingType](../reference/apis/js-apis-resourceschedule-workScheduler.md#chargingtype) | Charging type. 36batteryLevel| number | Battery level. 37batteryStatus| [BatteryStatus](../reference/apis/js-apis-resourceschedule-workScheduler.md#batterystatus) | Battery status. 38storageRequest| [StorageRequest](../reference/apis/js-apis-resourceschedule-workScheduler.md#storagerequest) |Storage status. 39isRepeat| boolean |Whether the task is repeated. 40repeatCycleTime| number |Repeat interval. 41repeatCount | number|Number of repeat times. 42parameters | {[key: string]: number | string | boolean} |Carried parameters. 43 44**Table 3** Work Scheduler callbacks 45 46API | Description 47---------------------------------------------------------|----------------------------------------- 48onWorkStart(work: WorkInfo): void | Called when the Work Scheduler task starts. 49onWorkStop(work: WorkInfo): void | Called when the Work Scheduler task stops. 50 51### How to Develop 52 531. Import the modules. 54 55 Import the **workScheduler** module. 56 57 ```js 58 import workScheduler from '@ohos.resourceschedule.workScheduler'; 59 ``` 60 61 Import the **WorkSchedulerExtensionAbility** module. 62 63 ```js 64 import WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'; 65 ``` 66 672. Develop an ExtensionAbility to execute a Work Scheduler task. For details about the ExtensionAbility, see [ExtensionAbility Component Overview](../application-models/extensionability-overview.md) and [WorkSchedulerExtensionAbility Development](./workscheduler-extensionability.md). 68 69 ```ts 70 import WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'; 71 72 export default class MyExtension extends WorkSchedulerExtensionAbility { 73 onWorkStart(workInfo) { 74 console.log('MyWorkSchedulerExtensionAbility onWorkStart' + JSON.stringify(workInfo)); 75 } 76 onWorkStop(workInfo) { 77 console.log('MyWorkSchedulerExtensionAbility onWorkStop' + JSON.stringify(workInfo)); 78 } 79 } 80 ``` 81 823. Start a Work Scheduler task. 83 84 ```ts 85 import workScheduler from '@ohos.resourceschedule.workScheduler'; 86 87 let workInfo = { 88 workId: 1, 89 batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW, 90 isRepeat: false, 91 isPersisted: true, 92 bundleName: "com.example.myapplication", 93 abilityName: "MyExtension", 94 parameters: { 95 mykey0: 1, 96 mykey1: "string value", 97 mykey2: true, 98 mykey3: 1.5 99 } 100 } 101 try{ 102 workScheduler.startWork(workInfo); 103 console.info('workschedulerLog startWork success'); 104 } catch (error) { 105 console.error(`workschedulerLog startwork failed. code is ${error.code} message is ${error.message}`); 106 } 107 ``` 108 1094. Stop the Work Scheduler task. 110 111 ```ts 112 import workScheduler from '@ohos.resourceschedule.workScheduler'; 113 114 let workInfo = { 115 workId: 1, 116 batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW, 117 isRepeat: false, 118 isPersisted: true, 119 bundleName: "com.example.myapplication", 120 abilityName: "MyExtension", 121 parameters: { 122 mykey0: 1, 123 mykey1: "string value", 124 mykey2: true, 125 mykey3: 1.5 126 } 127 } 128 try{ 129 workScheduler.stopWork(workInfo, false); 130 console.info('workschedulerLog stopWork success'); 131 } catch (error) { 132 console.error(`workschedulerLog stopWork failed. code is ${error.code} message is ${error.message}`); 133 } 134 ``` 135 1365. Obtain a specified Work Scheduler task. 137 138 ```ts 139 try{ 140 workScheduler.getWorkStatus(50, (error, res) => { 141 if (error) { 142 console.error(`workschedulerLog getWorkStatus failed. code is ${error.code} message is ${error.message}`); 143 } else { 144 for (let item in res) { 145 console.info(`workschedulerLog getWorkStatus success, ${item} is: ${res[item]}`); 146 } 147 } 148 }); 149 } catch (error) { 150 console.error(`workschedulerLog getWorkStatus failed. code is ${error.code} message is ${error.message}`); 151 } 152 ``` 153 1546. Obtain all the Work Scheduler tasks. 155 156 ```ts 157 try{ 158 workScheduler.obtainAllWorks((error, res) =>{ 159 if (error) { 160 console.error(`workschedulerLog obtainAllWorks failed. code is ${error.code} message is ${error.message}`); 161 } else { 162 console.info(`workschedulerLog obtainAllWorks success, data is: ${JSON.stringify(res)}`); 163 } 164 }); 165 } catch (error) { 166 console.error(`workschedulerLog obtainAllWorks failed. code is ${error.code} message is ${error.message}`); 167 } 168 ``` 169 1707. Stop and clear all the Work Scheduler tasks. 171 172 ```ts 173 try{ 174 workScheduler.stopAndClearWorks(); 175 console.info(`workschedulerLog stopAndClearWorks success`); 176 } catch (error) { 177 console.error(`workschedulerLog stopAndClearWorks failed. code is ${error.code} message is ${error.message}`); 178 } 179 ``` 180 1818. Check whether the last execution of a specified Work Scheduler task has timed out. 182 183 ```ts 184 try{ 185 workScheduler.isLastWorkTimeOut(500, (error, res) =>{ 186 if (error) { 187 console.error(`workschedulerLog isLastWorkTimeOut failed. code is ${error.code} message is ${error.message}`); 188 } else { 189 console.info(`workschedulerLog isLastWorkTimeOut success, data is: ${res}`); 190 } 191 }); 192 } catch (error) { 193 console.error(`workschedulerLog isLastWorkTimeOut failed. code is ${error.code} message is ${error.message}`); 194 } 195 ```