If a service needs to be continued when the application or service module is running in the background (not visible to users), the application or service module can request a transient task or continuous task for delayed suspension based on the service type.
If a service needs to be continued when the application or service module is running in the background (not visible to users), the application or service module can request a transient task or continuous task for delayed suspension based on the service type.
## Transient Tasks
### Available APIs
**Table 1** Main APIs for transient tasks
| API| Description|
| -------- | -------- |
| function requestSuspendDelay(reason: string, callback: Callback<void>): **DelaySuspendInfo**; | Requests delayed suspension after the application switches to the background.<br>The default duration of delayed suspension is 180000 when the battery level is higher than or equal to the broadcast low battery level and 60000 when the battery level is lower than the broadcast low battery level.|
| function getRemainingDelayTime(requestId: number, callback: AsyncCallback<number>): void;<br>function getRemainingDelayTime(requestId: number): Promise<number>; | Obtains the remaining duration before the application is suspended. (The value of **requestId** is obtained from the return value of **requestSuspendDelay**.)<br>Two asynchronous methods are provided: callback and promise.|
| function cancelSuspendDelay(requestId: number): void; | Cancels the suspension delay. (The value of **requestId** is obtained from the return value of **requestSuspendDelay**.)|
**Table 2** Parameters in DelaySuspendInfo
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| requestId | number | Yes| ID of the suspension delay request.|
| actualDelayTime | number | Yes| Actual suspension delay duration of the application, in milliseconds.|
| requestSuspendDelay(reason: string, callback: Callback<void>): [DelaySuspendInfo](../reference/apis/js-apis-backgroundTaskManager.md#delaysuspendinfo) | Requests delayed suspension after the application switches to the background.<br>The default duration value of delayed suspension is 180000 when the battery level is normal and 60000 when the battery level is low.|
| getRemainingDelayTime(requestId: number): Promise<number> | Obtains the remaining duration before the application is suspended.<br>This API uses a promise to return the task execution result.|
| cancelSuspendDelay(requestId: number): void | Cancels the suspension delay.|
## How to Develop
### How to Develop
1. Request a suspension delay.
...
...
@@ -42,7 +30,8 @@ import backgroundTaskManager from '@ohos.backgroundTaskManager';
console.info("Request suspension delay will time out.");
});
var id = delayInfo.requestId;console.info("requestId is: " + id);
var id = delayInfo.requestId;
console.info("requestId is: " + id);
```
...
...
@@ -50,9 +39,9 @@ import backgroundTaskManager from '@ohos.backgroundTaskManager';
```js
backgroundTaskManager.getRemainingDelayTime(id).then( res => {
| function startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: WantAgent, callback: AsyncCallback<void>): void;<br>function startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: WantAgent): Promise<void>; | Requests a continuous task from the system so that the application keeps running in the background.|
| function stopBackgroundRunning(context: Context, callback: AsyncCallback<void>): void;<br>function stopBackgroundRunning(context: Context): Promise<void>; | Cancels the continuous task.|
| startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: WantAgent): Promise<void> | Requests a continuous task from the system so that the application keeps running in the background.|
| stopBackgroundRunning(context: Context): Promise<void> | Cancels the continuous task.|
For details about **wantAgent**, see [WantAgent](../reference/apis/js-apis-wantAgent.md).
**Table 4** Background modes
**Table 3** Background modes
| Name| ID Value| Description| Item|
| -------- | -------- | -------- | -------- |
...
...
@@ -123,39 +114,30 @@ For details about **wantAgent**, see [WantAgent](../reference/apis/js-apis-wantA
After a service is started, call the API for requesting a continuous task in the **onStart** callback of the Service ability to declare that the service needs to run in the background for a long time. In the **onStop** callback, call the API for canceling the continuous task.
In the **service.js** file:
...
...
@@ -220,25 +202,25 @@ function startBackgroundRunning() {
| reason | string | Yes | Reason for delayed transition to the suspended state. |
| callback | Callback<void> | Yes | Invoked when a delay is about to time out. Generally, this callback is used to notify the application 6 seconds before the delay times out.|
| callback | Callback<void> | Yes | Invoked when a delay is about to time out. Generally, this callback is used to notify the application 6 seconds before the delay times out.|
**Return value**
| Type | Description |
...
...
@@ -33,11 +33,17 @@ The default duration of delayed suspension is 180000 when the battery level is h
| [DelaySuspendInfo](#delaysuspendinfo) | Information about the suspension delay.|