DEV Community

SameX
SameX

Posted on

Deferred Task Management: Flexible Background Scheduling in HarmonyOS Next

This article aims to deeply explore the technical details of Huawei's HarmonyOS Next system (as of API 12 currently) and is summarized based on actual development practices. It is mainly used as a carrier for technical sharing and exchange. Inevitable errors and omissions may exist. Colleagues are welcome to put forward valuable opinions and questions for common progress. This article is original content. Any form of reprint must indicate the source and original author.

Deferred tasks are a flexible way of background task scheduling provided by HarmonyOS Next. It allows applications to automatically execute tasks when specific conditions are met, such as network connection status, battery power, charging status, etc. This on-demand triggering mechanism can effectively optimize the execution of background tasks, avoid unnecessary resource consumption, and improve user experience.

Deferred tasks are suitable for the following scenarios:

  • Regular data synchronization: For example, an application can be set to automatically synchronize data when connected to a Wi-Fi network, avoiding data synchronization over mobile networks and saving traffic.
  • Low battery optimization: An application can be set to pause some background tasks when the battery power is below a certain percentage to avoid excessive power consumption.
  • Execution when charging: An application can be set to execute some time-consuming background tasks when charging, such as downloading large files and updating applications.

Trigger conditions and parameter configuration of deferred tasks

Developers can set the trigger conditions and parameters of deferred tasks through the WorkInfo object, such as:

  • Network type: Specify that the task is executed when connected to a specific type of network, such as Wi-Fi, mobile data, etc.
  • Battery status: Specify that the task is executed when the battery power is above or below a specific percentage.
  • Charging type: Specify that the task is executed when connected to a specific type of charger, such as fast charging, wireless charging, etc.
  • Storage status: Specify that the task is executed when there is sufficient or insufficient storage space.
  • Timing status: Specify that the task is executed at a specific time, such as 2 am every day.

Code example:

import { workScheduler } from '@kit.BackgroundTasksKit';
const workInfo: workScheduler.WorkInfo = {
  workId: 1,
  networkType: workScheduler.NetworkType.NETWORK_TYPE_WIFI,
  bundleName: 'com.example.application',
  abilityName: 'MyWorkSchedulerExtensionAbility',
  batteryLevel: 20, // Execute when battery power is below 20%.
  chargerType: workScheduler.ChargingType.CHARGER_TYPE_WIRELESS // Execute when connected to a wireless charger.
};
Enter fullscreen mode Exit fullscreen mode

Code example: Applying for a deferred task based on network and battery status

The following code example shows how to create a deferred task based on network and battery status:

import { workScheduler } from '@kit.BackgroundTasksKit';
const workInfo: workScheduler.WorkInfo = {
  workId: 1,
  networkType: workScheduler.NetworkType.NETWORK_TYPE_WIFI,
  bundleName: 'com.example.application',
  abilityName: 'MyWorkSchedulerExtensionAbility',
  batteryLevel: 20, // Execute when battery power is below 20%.
  chargerType: workScheduler.ChargingType.CHARGER_TYPE_WIRELESS // Execute when connected to a wireless charger.
};
try {
  workScheduler.startWork(workInfo);
  console.info(`startWork success`);
} catch (error) {
  console.error(`startWork failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
}
Enter fullscreen mode Exit fullscreen mode

System resource optimization and task scheduling

The system will intelligently schedule the execution of deferred tasks according to the current state and resource situation of the device. For example, when the system memory is insufficient or the temperature is too high, the system will pause the execution of some deferred tasks to ensure the normal operation of the device.

System scheduling principle:

  • The system will group applications according to their activity levels and set different execution frequencies for deferred tasks for different groups.
  • The system will dynamically adjust the execution timing of deferred tasks according to states such as memory, temperature, and power consumption.
  • The system will give priority to executing deferred tasks that have a greater impact on users, such as emergency notifications and system updates.

Table: Execution frequency of deferred tasks for different activity groups

Activity group Minimum interval
Active group 2 hours
Frequently used group 4 hours
Commonly used group 24 hours
Rarely used group 48 hours
Restricted use group Prohibited
Never used group Prohibited

Summary

Deferred tasks provide a flexible way of background task scheduling for HarmonyOS Next. It can effectively optimize the execution of background tasks, avoid unnecessary resource consumption, and improve user experience. We can set the trigger conditions and parameters of deferred tasks according to actual needs and combine with the system resource optimization mechanism to achieve efficient background task management.

Top comments (0)