harmony 鸿蒙Transient Task

  • 2023-10-30
  • 浏览 (363)

Transient Task

Overview

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

Constraints

  • When to request: An application can request a transient task when it is running in the foreground or within 5 seconds after it switches to the background.

  • Quantity limit: An application can request a maximum of three transient tasks during a time segment. As shown in the figure below, the application requests two transient tasks in the time segments ①, ②, and ③, and one transient task in the time segment ④.

  • Quota mechanism: An application has a certain quota for transient tasks (adjusted based on the system status and user habits). The default quota for a single day (within 24 hours) is 10 minutes, and the maximum quota for each request is 3 minutes. In case of low battery, the default quota for each request is 1 minute. After the quota is used up, the application cannot request transient tasks anymore. The system also provides an API for an application to query the remaining duration of a transient task so as to determine whether to continue running other services.

  • Quota calculation: Transient tasks are timed only when the application is running in the background. If the application has multiple transient tasks during the same time segment, no repeated timing is performed. As in the figure below, the application has two transient tasks, A and B. Task A is requested when the application is running in the foreground, and the timing starts when the application switches to the background (marked as ①). When the application switches to the foreground, the timing stops (marked as ②). When the application switches to the background again, the timing starts again (marked as ③). When task A is finished, task B still exists, and therefore the timing continues (marked as ④). In this process, the total time consumed by the transient tasks is ①+③+④.

Figure 1 Quota calculation for transient tasks

transient-task

NOTE

The application shall proactively cancel a transient task when it is finished. Otherwise, the time frame allowed for the application to run in the background will be affected.

  • Timeout: If a transient task is about to time out, the system notifies the application of the timeout by using a callback. The application needs to cancel the task. Otherwise, the system forcibly terminates the application process.

Available APIs

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

Table 1 Main APIs for transient tasks

API Description
requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspendInfo Requests a transient task.
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.
   import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
   import { BusinessError } from '@ohos.base';
  1. Request a transient task and implement the callback.
   let id: number;         // 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.
     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('Succeeded in requesting suspend delay.');
       backgroundTaskManager.cancelSuspendDelay(id);
     })
     id = delayInfo.requestId;
     delayTime = delayInfo.actualDelayTime;
   }
  1. 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.
   let id: number; // ID of the transient task.

   async function getRemainingDelayTime() {
     backgroundTaskManager.getRemainingDelayTime(id).then((res: number) => {
       console.info('Succeeded in getting remaining delay time.');
     }).catch((err: BusinessError) => {
       console.error(`Failed to get remaining delay time. Code: ${err.code}, message: ${err.message}`);
     })
   }
  1. Cancel the transient task.
   let id: number; // ID of the transient task.
    
   function cancelSuspendDelay() {
     backgroundTaskManager.cancelSuspendDelay(id);
   }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Background Task Management

harmony 鸿蒙Agent-powered Reminder

harmony 鸿蒙Background Task Overview

harmony 鸿蒙Continuous Task

harmony 鸿蒙Requesting Efficiency Resources (for Privileged System Applications Only)

harmony 鸿蒙Deferred Task

0  赞