• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 [Universal Error Codes](../errorcode-universal.md) and [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.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 [Universal Error Codes](../errorcode-universal.md) and [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.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
117## EfficiencyResourcesRequest
118
119Describes the parameters for requesting efficiency resources.
120
121**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply
122
123**System API**: This is a system API.
124
125| Name            | Type    | Mandatory  | Description                                      |
126| --------------- | ------ | ---- | ---------------------------------------- |
127| resourceTypes   | number  | Yes   | Type of the resource to request.                              |
128| isApply         | boolean | Yes   | Whether the request is used to apply for resources.<br>- **true**: The request is used to apply for resources.<br>- **false**: The request is used to release resources.|
129| timeOut         | number  | Yes   | Duration for which the resource will be used, in milliseconds.               |
130| isPersist       | boolean | No   | Whether the resource is permanently held. The default value is **false**.<br>- **true**: The resource is permanently held.<br>- **false**: The resource is held for a limited period of time.|
131| isProcess       | boolean | No   | Whether the request is initiated by a process. The default value is **false**.<br>- **true**: The request is initiated by a process.<br>- **false**: The request is initiated by an application.        |
132| reason          | string  | Yes   | Reason for requesting the resource.               |
133
134## ResourceType
135
136Enumerates the efficiency resource types.
137
138**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply
139
140**System API**: This is a system API.
141
142| Name                    | Value | Description                   |
143| ----------------------- | ---- | --------------------- |
144| CPU                     | 1    | CPU resource. Such type of resource prevents an application from being suspended.            |
145| COMMON_EVENT            | 2    | Common event resource. Such type of resource ensures that an application in the suspended state can receive common events.|
146| TIMER                   | 4    | Timer resource. Such type of resource ensures that an application in the suspended state can be woken up by system timers.|
147| WORK_SCHEDULER          | 8    | Deferred task resource. Such type of resource provides a loose control policy for an application.|
148| BLUETOOTH               | 16   | Bluetooth resource. Such type of resource ensures that an application in the suspended state can be woken up by Bluetooth-related events.|
149| GPS                     | 32   | GPS resource. Such type of resource ensures that an application in the suspended state can be woken up by GPS-related events.|
150| AUDIO                   | 64   | Audio resource. Such type of resource prevents an application from being suspended when the application has an audio being played.|
151| RUNNING_LOCK<sup>10+</sup> | 128 | RUNNING_LOCK resources are not proxied when the application is suspended.|
152| SENSOR<sup>10+</sup> | 256 | Sensor callbacks are not intercepted.|
153