目录

Transient Task (ArkTS)

Overview

An application is suspended after it runs in the background for a short period of time. If the application needs to execute a transient task in the background, for example, saving the status or sending messages, it can request a transient task to extend the running time in the background.

Constraints

Available APIs

Table 1 Main APIs for transient tasks

The table below lists the main APIs used for transient task development. For details about more APIs and their usage, see Background Task Management.

API Description
requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspendInfo Applies for a transient task. The DelaySuspendInfo returns the values of requestId and actualDelayTime.
getRemainingDelayTime(requestId: number): Promise<number> Obtains the remaining time of a transient task.
cancelSuspendDelay(requestId: number): void Cancels a transient task.

How to Develop

  1. Import the module. No permission configuration is required.

    import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Request a transient task and implement the callback. The callback is triggered when the transient task is about to end and is independent of the service of the application. After the request for the transient task is successful, the application normally executes its own service logic.

    let id: number = -1;         // ID of the transient task.
    let delayTime: number;  // Remaining time of the transient task.
    
    // Request a transient task.
    function requestSuspendDelay() {
      let myReason = 'test requestSuspendDelay';   // Reason for the request.
      try {
        let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => {
          // Callback function, which is triggered when the transient task is about to time out. The application can carry out data clear and annotation, and cancel the task in the callback.
          console.info('suspend delay task will timeout');
          try {
            backgroundTaskManager.cancelSuspendDelay(id);
          } catch (error) {
            console.error(`Operation requestSuspendDelay failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
          }
        })
        id = delayInfo.requestId;
        delayTime = delayInfo.actualDelayTime;
        console.info(`Operation requestSuspendDelay success. id is ${id} delayTime is ${delayTime}`);
      } catch (error) {
        console.error(`Operation requestSuspendDelay failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
      }
    }
  3. Obtain the remaining time of the transient task. Based on the remaining time, the application determines whether to continue to run other services. For example, the application has two small tasks. After the first task is executed, it queries the remaining time of the current transient task to determine whether to execute the second task.

    async function getRemainingDelayTime() {
      backgroundTaskManager.getRemainingDelayTime(id).then((res: number) => {
        console.info(`Succeeded in getting remaining delay time. time is ${res}`);
      }).catch((err: BusinessError) => {
        console.error(`Failed to get remaining delay time. Code: ${err.code}, message: ${err.message}`);
      })
    }
  4. Cancels a transient task.

    function cancelSuspendDelay() {
      try {
        backgroundTaskManager.cancelSuspendDelay(id);
        console.info('Operation cancelSuspendDelay Succeeded.');
      } catch (error) {
        console.error(`Operation cancelSuspendDelay failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
      }
    }