1# @ohos.resourceschedule.backgroundTaskManager (后台任务管理)(系统接口) 2 3本模块提供申请后台任务的接口。当应用退至后台时,开发者可以通过本模块接口为应用申请短时、长时任务,避免应用进程被终止或挂起。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> - 当前页面仅包含本模块的系统接口,其他公开接口请参见[@ohos.resourceschedule.backgroundTaskManager (后台任务管理)](js-apis-resourceschedule-backgroundTaskManager.md)。 10 11## 导入模块 12 13```ts 14import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; 15``` 16 17## backgroundTaskManager.applyEfficiencyResources 18 19applyEfficiencyResources(request: EfficiencyResourcesRequest): void 20 21申请能效资源。 22 23**系统能力**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 24 25**系统API**: 此接口为系统接口。 26 27**参数**: 28 29| 参数名 | 类型 | 必填 | 说明 | 30| ------- | ------- | ---- | ---------------------------------------- | 31| request | [EfficiencyResourcesRequest](#efficiencyresourcesrequest) | 是 | 请求的必要信息,包括资源类型、超时时间等。 | 32 33 34**错误码**: 35 36以下错误码的详细介绍请参见[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)。 37 38| 错误码ID | 错误信息 | 39| ---- | --------------------- | 40| 9800001 | Memory operation failed. | 41| 9800002 | Parcel operation failed. | 42| 9800003 | Inner transact failed. | | 43| 9800004 | System service operation failed. | 44| 18700001 | Caller information verification failed. | 45 46**示例**: 47 48```js 49import { BusinessError } from '@ohos.base'; 50 51let request: backgroundTaskManager.EfficiencyResourcesRequest = { 52 resourceTypes: backgroundTaskManager.ResourceType.CPU, 53 isApply: true, 54 timeOut: 0, 55 reason: "apply", 56 isPersist: true, 57 isProcess: false, 58}; 59try { 60 backgroundTaskManager.applyEfficiencyResources(request); 61 console.info("applyEfficiencyResources success. "); 62} catch (error) { 63 console.error(`applyEfficiencyResources failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); 64} 65``` 66 67## backgroundTaskManager.resetAllEfficiencyResources 68 69resetAllEfficiencyResources(): void 70 71释放已申请的全部能效资源。 72 73**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 74 75**系统API**: 此接口为系统接口。 76 77**错误码**: 78 79以下错误码的详细介绍请参见[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)。 80 81| 错误码ID | 错误信息 | 82| ---- | --------------------- | 83| 9800001 | Memory operation failed. | 84| 9800002 | Parcel operation failed. | 85| 9800003 | Inner transact failed. | | 86| 9800004 | System service operation failed. | 87| 18700001 | Caller information verification failed. | 88 89**示例**: 90 91```js 92import { BusinessError } from '@ohos.base'; 93 94try { 95 backgroundTaskManager.resetAllEfficiencyResources(); 96} catch (error) { 97 console.error(`resetAllEfficiencyResources failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); 98} 99``` 100 101## BackgroundMode 102 103长时任务模式。 104 105**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask 106 107| 名称 | 值 | 说明 | 108| ----------------------- | ---- | --------------------- | 109| WIFI_INTERACTION | 7 | WLAN相关。<br>**系统API**: 此接口为系统接口。 | 110| VOIP | 8 | 音视频通话。<br>**系统API**: 此接口为系统接口。 | 111 112## EfficiencyResourcesRequest 113 114能效资源申请参数。 115 116**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 117 118**系统API**: 此接口为系统接口。 119 120| 名称 | 类型 | 必填 | 说明 | 121| --------------- | ------ | ---- | ---------------------------------------- | 122| resourceTypes | number | 是 | 申请的资源类型。 | 123| isApply | boolean | 是 | 申请或释放资源。<br>- true表示申请资源,false表示释放部分资源。 | 124| timeOut | number | 是 | 资源使用时间,单位为毫秒。 | 125| isPersist | boolean | 否 | 是否永久持有资源,默认为false。<br>- true表示永久持有,false表示有限时间内持有。| 126| isProcess | boolean | 否 | 进程或应用申请,默认为false。<br>- true表示进程申请,false表示应用申请。 | 127| reason | string | 是 | 申请资源原因。 | 128 129## ResourceType 130 131能效资源类型。 132 133**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 134 135**系统API**: 此接口为系统接口。 136 137| 名称 | 值 | 说明 | 138| ----------------------- | ---- | --------------------- | 139| CPU | 1 | CPU资源,申请后应用进程不被挂起。 | 140| COMMON_EVENT | 2 | 公共事件资源,申请后应用进程被挂起后,可以收到公共事件。 | 141| TIMER | 4 | 计时器,申请后应用进程被挂起后,Timer仍然可以唤醒应用。 | 142| WORK_SCHEDULER | 8 | 延迟任务资源,申请后延迟任务管控变宽松。 | 143| BLUETOOTH | 16 | 蓝牙资源,申请后应用进程被挂起后,蓝牙相关事件仍然可以唤醒应用。 | 144| GPS | 32 | GPS资源,申请后应用进程被挂起后,GPS相关事件可以唤醒应用。 | 145| AUDIO | 64 | 音频资源,有音频播放时对应的应用进程不被挂起。 | 146| RUNNING_LOCK<sup>10+</sup> | 128 | RUNNING_LOCK资源,申请后挂起状态不会代理RUNNING_BACKGROUND锁。 | 147| SENSOR<sup>10+</sup> | 256 | 申请后不拦截Sensor回调。 | 148