1# @ohos.resourceschedule.backgroundTaskManager (Background Task Management) (System API) 2 3The **backgroundTaskManager** module provides APIs to request background tasks. You can use the APIs to request transient tasks, continuous tasks, or efficiency resources to prevent the application process from being terminated or suspended when your application is switched to the background. 4 5> **NOTE** 6> 7> - 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. 8> 9> - This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.resourceschedule.backgroundTaskManager (Background Task Management)](js-apis-resourceschedule-backgroundTaskManager.md). 10 11## Modules to Import 12 13```ts 14import { backgroundTaskManager } from '@kit.BackgroundTasksKit'; 15``` 16 17## backgroundTaskManager.applyEfficiencyResources 18 19applyEfficiencyResources(request: EfficiencyResourcesRequest): void 20 21Requests efficiency resources. 22 23**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 24 25**System API**: This is a system API. 26 27**Parameters** 28 29| Name | Type | Mandatory | Description | 30| ------- | ------- | ---- | ---------------------------------------- | 31| request | [EfficiencyResourcesRequest](#efficiencyresourcesrequest) | Yes | Necessary information carried in the request, including the resource type and timeout interval. | 32 33 34**Error codes** 35 36For details about the error codes, see [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.md) and [Universal Error Codes](../errorcode-universal.md). 37 38| ID | Error Message | 39| ---- | --------------------- | 40| 201 | Permission denied. | 41| 202 | Not System App. | 42| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 43| 9800001 | Memory operation failed. | 44| 9800002 | Parcel operation failed. | 45| 9800003 | Internal transaction failed. | 46| 9800004 | System service operation failed. | 47| 18700001 | Caller information verification failed for an energy resource request. | 48 49**Example** 50 51```js 52import { BusinessError } from '@kit.BasicServicesKit'; 53 54let request: backgroundTaskManager.EfficiencyResourcesRequest = { 55 resourceTypes: backgroundTaskManager.ResourceType.CPU, 56 isApply: true, 57 timeOut: 0, 58 reason: "apply", 59 isPersist: true, 60 isProcess: false, 61}; 62try { 63 backgroundTaskManager.applyEfficiencyResources(request); 64 console.info("applyEfficiencyResources success. "); 65} catch (error) { 66 console.error(`applyEfficiencyResources failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); 67} 68``` 69 70## backgroundTaskManager.resetAllEfficiencyResources 71 72resetAllEfficiencyResources(): void 73 74Releases all efficiency resources. 75 76**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 77 78**System API**: This is a system API. 79 80**Error codes** 81 82For details about the error codes, see [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.md) and [Universal Error Codes](../errorcode-universal.md). 83 84| ID | Error Message | 85| ---- | --------------------- | 86| 201 | Permission denied. | 87| 202 | Not System App. | 88| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. | 89| 9800001 | Memory operation failed. | 90| 9800002 | Parcel operation failed. | 91| 9800003 | Internal transaction failed. | 92| 9800004 | System service operation failed. | 93| 18700001 | Caller information verification failed for an energy resource request. | 94 95**Example** 96 97```js 98import { BusinessError } from '@kit.BasicServicesKit'; 99 100try { 101 backgroundTaskManager.resetAllEfficiencyResources(); 102} catch (error) { 103 console.error(`resetAllEfficiencyResources failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); 104} 105``` 106 107## BackgroundMode 108 109Enumerates the continuous task modes. 110 111**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask 112 113| Name | Value | Description | 114| ----------------------- | ---- | --------------------- | 115| WIFI_INTERACTION | 7 | WLAN-related.<br>**System API**: This is a system API. | 116| VOIP | 8 | Audio and video calls.<br>**System API**: This is a system API. | 117 118## EfficiencyResourcesRequest 119 120Describes the parameters for requesting efficiency resources. 121 122**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 123 124**System API**: This is a system API. 125 126| Name | Type | Mandatory | Description | 127| --------------- | ------ | ---- | ---------------------------------------- | 128| resourceTypes | number | Yes | Type of the resource to request. | 129| isApply | boolean | Yes | Whether the request is used to apply for resources.<br>The value **true** means that the request is used to apply for resources, and **false** means that the request is used to release resources. | 130| timeOut | number | Yes | Duration for which the resource will be used, in milliseconds. | 131| isPersist | boolean | No | Whether the resource is permanently held. The default value is **false**.<br>The value **true** means that the resource is permanently held, and **false** means the resource is held within a given period of time.| 132| isProcess | boolean | No | Whether the request is initiated by a process. The default value is **false**.<br>The value **true** means that the request is initiated by a process, and **false** means that the request is initiated by an application. | 133| reason | string | Yes | Reason for requesting the resource. | 134 135## ResourceType 136 137Enumerates the efficiency resource types. 138 139**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply 140 141**System API**: This is a system API. 142 143| Name | Value | Description | 144| ----------------------- | ---- | --------------------- | 145| CPU | 1 | CPU resource. Such type of resource prevents an application from being suspended. | 146| COMMON_EVENT | 2 | Common event resource. Such type of resource ensures that an application in the suspended state can receive common events. | 147| TIMER | 4 | Timer resource. Such type of resource ensures that an application in the suspended state can be woken up by system timers. | 148| WORK_SCHEDULER | 8 | Deferred task resource. Such type of resource provides a loose control policy for an application. | 149| BLUETOOTH | 16 | Bluetooth resource. Such type of resource ensures that an application in the suspended state can be woken up by Bluetooth-related events. | 150| GPS | 32 | GPS resource. Such type of resource ensures that an application in the suspended state can be woken up by GPS-related events. | 151| AUDIO | 64 | Audio resource. Such type of resource prevents an application from being suspended when the application has an audio being played. | 152| RUNNING_LOCK<sup>10+</sup> | 128 | RUNNING_LOCK resources are not proxied when the application is suspended. | 153| SENSOR<sup>10+</sup> | 256 | Sensor callbacks are not intercepted. | 154