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 '@kit.BackgroundTasksKit';
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)和[通用错误码](../errorcode-universal.md)。
37
38| 错误码ID  | 错误信息             |
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**示例**:
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
74释放已申请的全部能效资源。
75
76**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply
77
78**系统API**: 此接口为系统接口。
79
80**错误码**:
81
82以下错误码的详细介绍请参见[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)和[通用错误码](../errorcode-universal.md)。
83
84| 错误码ID  | 错误信息             |
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**示例**:
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
109长时任务模式。
110
111**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask
112
113| 名称                     | 值  | 说明                    |
114| ----------------------- | ---- | --------------------- |
115| WIFI_INTERACTION        | 7    | WLAN相关。<br>**系统API**: 此接口为系统接口。 |
116| VOIP                    | 8    | 音视频通话。<br>**系统API**: 此接口为系统接口。 |
117
118## EfficiencyResourcesRequest
119
120能效资源申请参数。
121
122**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply
123
124**系统API**: 此接口为系统接口。
125
126| 名称             | 类型     | 必填   | 说明                                       |
127| --------------- | ------ | ---- | ---------------------------------------- |
128| resourceTypes   | number  | 是    | 申请的资源类型。                               |
129| isApply         | boolean | 是    | 申请或释放资源。<br>- true表示申请资源,false表示释放部分资源。 |
130| timeOut         | number  | 是    | 资源使用时间,单位为毫秒。                |
131| isPersist       | boolean | 否    | 是否永久持有资源,默认为false。<br>- true表示永久持有,false表示有限时间内持有。|
132| isProcess       | boolean | 否    | 进程或应用申请,默认为false。<br>- true表示进程申请,false表示应用申请。         |
133| reason          | string  | 是    | 申请资源原因。                |
134
135## ResourceType
136
137能效资源类型。
138
139**系统能力:** SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply
140
141**系统API**: 此接口为系统接口。
142
143| 名称                     | 值  | 说明                    |
144| ----------------------- | ---- | --------------------- |
145| CPU                     | 1    | CPU资源,申请后应用进程不被挂起。             |
146| COMMON_EVENT            | 2    | 公共事件资源,申请后应用进程被挂起后,可以收到公共事件。 |
147| TIMER                   | 4    | 计时器,申请后应用进程被挂起后,Timer仍然可以唤醒应用。 |
148| WORK_SCHEDULER          | 8    | 延迟任务资源,申请后延迟任务管控变宽松。 |
149| BLUETOOTH               | 16   | 蓝牙资源,申请后应用进程被挂起后,蓝牙相关事件仍然可以唤醒应用。 |
150| GPS                     | 32   | GPS资源,申请后应用进程被挂起后,GPS相关事件可以唤醒应用。 |
151| AUDIO                   | 64   | 音频资源,有音频播放时对应的应用进程不被挂起。 |
152| RUNNING_LOCK<sup>10+</sup> | 128 | RUNNING_LOCK资源,申请后挂起状态不会代理RUNNING_BACKGROUND锁。 |
153| SENSOR<sup>10+</sup> | 256 | 申请后不拦截Sensor回调。 |
154