【鸿蒙next开发】后台任务开发:@ohos.resourceschedule.workScheduler (延迟任务调度)_鸿蒙延迟任务
往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)
-
鸿蒙开发核心知识点,看这篇文章就够了
-
最新版!鸿蒙HarmonyOS Next应用开发实战学习路线
-
鸿蒙HarmonyOS NEXT开发技术最全学习路线指南
-
鸿蒙应用开发实战项目,看这一篇文章就够了(部分项目附源码)
@ohos.resourceschedule.workScheduler (延迟任务调度)
本模块提供延迟任务注册、取消、查询的能力。在开发过程中,对于实时性要求不高的任务,可以调用本模块接口注册延迟任务,在系统空闲时根据性能、功耗、热等情况进行调度执行。
说明
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
导入模块
import { workScheduler } from \'@kit.BackgroundTasksKit\';
workScheduler.startWork
startWork(work: WorkInfo): void
申请延迟任务。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; let workInfo: workScheduler.WorkInfo = { workId: 1, batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW, isRepeat: false, isPersisted: true, bundleName: \"com.example.myapplication\", abilityName: \"MyExtension\", parameters: { mykey0: 1, mykey1: \"string value\", mykey2: true, mykey3: 1.5 } } try{ workScheduler.startWork(workInfo); console.info(\'workschedulerLog startWork success\'); } catch (error) { console.error(`workschedulerLog startwork failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); }
workScheduler.stopWork
stopWork(work: WorkInfo, needCancel?: boolean): void
取消延迟任务。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
是否需要移除的任务。
- true表示停止并移除,false表示只停止不移除。默认为false。
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; let workInfo: workScheduler.WorkInfo = { workId: 1, batteryStatus:workScheduler.BatteryStatus.BATTERY_STATUS_LOW, isRepeat: false, isPersisted: true, bundleName: \"com.example.myapplication\", abilityName: \"MyExtension\", parameters: { mykey0: 1, mykey1: \"string value\", mykey2: true, mykey3: 1.5 } } try{ workScheduler.stopWork(workInfo, false); console.info(\'workschedulerLog stopWork success\'); } catch (error) { console.error(`workschedulerLog stopWork failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); }
workScheduler.getWorkStatus
getWorkStatus(workId: number, callback : AsyncCallback): void
通过workId获取延迟任务,使用Callback异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; workScheduler.getWorkStatus(50, (error: BusinessError, res: workScheduler.WorkInfo) => { if (error) { console.error(`workschedulerLog getWorkStatus failed. code is ${error.code} message is ${error.message}`); } else { console.info(`workschedulerLog getWorkStatus success, ${JSON.stringify(res)}`); } });
workScheduler.getWorkStatus
getWorkStatus(workId: number): Promise
通过workId获取延迟任务,使用Promise异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; workScheduler.getWorkStatus(50).then((res: workScheduler.WorkInfo) => { console.info(`workschedulerLog getWorkStatus success, ${JSON.stringify(res)}`); }).catch((error: BusinessError) => { console.error(`workschedulerLog getWorkStatus failed. code is ${error.code} message is ${error.message}`); })
workScheduler.obtainAllWorksdeprecated
obtainAllWorks(callback : AsyncCallback) : Array
从API version 10开始不再维护,建议使用workScheduler.obtainAllWorks10+替代
获取当前应用所有的延迟任务,使用Callback异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
错误码:
以下错误码的详细介绍。
workScheduler.obtainAllWorks10+
obtainAllWorks(callback : AsyncCallback<Array>): void
获取当前应用所有的延迟任务,使用Callback异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; workScheduler.obtainAllWorks((error: BusinessError, res: Array) =>{ if (error) { console.error(`workschedulerLog obtainAllWorks failed. code is ${error.code} message is ${error.message}`); } else { console.info(`workschedulerLog obtainAllWorks success, data is: ${JSON.stringify(res)}`); } });
workScheduler.obtainAllWorks
obtainAllWorks(): Promise<Array>
获取当前应用所有的延迟任务,使用Promise异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; workScheduler.obtainAllWorks().then((res: Array) => { console.info(`workschedulerLog obtainAllWorks success, data is: ${JSON.stringify(res)}`); }).catch((error: BusinessError) => { console.error(`workschedulerLog obtainAllWorks failed. code is ${error.code} message is ${error.message}`); })
workScheduler.stopAndClearWorks
stopAndClearWorks(): void
停止和取消当前应用所有的延迟任务。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; try{ workScheduler.stopAndClearWorks(); console.info(`workschedulerLog stopAndClearWorks success`); } catch (error) { console.error(`workschedulerLog stopAndClearWorks failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); }
workScheduler.isLastWorkTimeOutdeprecated
isLastWorkTimeOut(workId: number, callback : AsyncCallback): boolean
从API version 10开始不再维护,建议使用workScheduler.isLastWorkTimeOut10+替代
检查延迟任务的最后一次执行是否超时,使用Callback异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
错误码:
以下错误码的详细介绍。
workScheduler.isLastWorkTimeOut10+
isLastWorkTimeOut(workId: number, callback : AsyncCallback): void
检查延迟任务的最后一次执行是否超时,使用Callback异步回调。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; workScheduler.isLastWorkTimeOut(500, (error: BusinessError, res: boolean) =>{ if (error) { console.error(`workschedulerLog isLastWorkTimeOut failed. code is ${error.code} message is ${error.message}`); } else { console.info(`workschedulerLog isLastWorkTimeOut success, data is: ${res}`); } });
workScheduler.isLastWorkTimeOut
isLastWorkTimeOut(workId: number): Promise
检查延迟任务的最后一次执行是否超时,使用Promise形式返回。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\'; workScheduler.isLastWorkTimeOut(500) .then((res: boolean) => { console.info(`workschedulerLog isLastWorkTimeOut success, data is: ${res}`); }) .catch((error: BusinessError) => { console.error(`workschedulerLog isLastWorkTimeOut failed. code is ${error.code} message is ${error.message}`); });
WorkInfo
延迟任务的具体信息。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
是否充电。
- true表示充电触发延迟回调,false表示不充电触发延迟回调。
是否循环任务。
- true表示循环任务,false表示非循环任务。
注册的延迟任务是否可保存在系统中。
- true表示可保存,即系统重启后,任务可恢复。false表示不可保存。
是否要求设备进入空闲状态。
- true表示需要,false表示不需要。
NetworkType
触发延迟回调的网络类型。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
ChargingType
触发延迟回调的充电类型。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
BatteryStatus
触发延迟回调的电池状态。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler
StorageRequest
触发延迟回调的存储状态。
系统能力:SystemCapability.ResourceSchedule.WorkScheduler