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
...
...
@@ -101,7 +101,7 @@ For details about **wantAgent**, see [WantAgent](../reference/apis/js-apis-wantA
**Table 3** Background modes
| Name| ID Value| Description| Item|
| Name| ID| Description| Item|
| -------- | -------- | -------- | -------- |
| DATA_TRANSFER | 1 | Data transfer.| dataTransfer |
@@ -116,9 +116,9 @@ For details about **wantAgent**, see [WantAgent](../reference/apis/js-apis-wantA
### How to Develop
1. Configure the continuous task permission and background mode type in the **config.json** file, with the ability type set to **service**.
1. Create an API version 8 project. Then right-click the project directory and choose **New > Ability > Service Ability** to create a Service ability. Configure the continuous task permission and background mode type in the **config.json** file, with the ability type set to **service**.
```json
```
"module": {
"package": "com.example.myapplication",
"abilities": [
...
...
@@ -157,7 +157,7 @@ For details about **wantAgent**, see [WantAgent](../reference/apis/js-apis-wantA
@@ -183,29 +183,36 @@ For details about **wantAgent**, see [WantAgent](../reference/apis/js-apis-wantA
```
### Development Examples
For details about how to use the Service ability in the FA model, see [Service Ability Development](../ability/fa-serviceability.md).
If an application does not need to interact with a continuous task in the background, you can use **startAbility()** to start the Service ability. In the **onStart** callback of the Service ability, call **startBackgroundRunning()** to declare that the Service ability needs to run in the background for a long time. After the task execution is complete, call **stopBackgroundRunning()** to release resources.
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:
If an application needs to interact with a continuous task in the background (for example, an application related to music playback), you can use **connectAbility()** to start and connect to the Service ability. After obtaining the proxy of the Service ability, the application can communicate with the Service ability and control the application and cancellation of continuous tasks.