DEV Community

lyc233333
lyc233333

Posted on

HarmonyOS Next Energy Saving Synchronizer: Let the background data "breathe smartly"

hello!I am Xiao L, the female programmer who "fights with power consumption" in Hongmeng background~ Do you know?Through delayed tasks + dynamic scheduling, we can make application data synchronization as smart as "breathing" - "big mouth update" when charging WiFi, and "save power with breath" under the mobile network!Today, let’s disassemble how to use HarmonyOS Next to create a “zero perception, high battery life” backend synchronization system, so that your applications are both “diligent” and “power-saving”~

1. The "Survival Wisdom" of the Synchronizer: Conditions trigger "Precise Sniper"

(I) Four-quadrant rule of “environmental perception”

Network Status Charge Status Synchronization Strategy Typical Scenarios
WIFI Charging Full update + large file download Automatic backup of albums at night
WIFI Not charged Incremental update + lightweight data synchronization Daily news/weather update
Mobile Network Charging Key data synchronization only Emergency notification/order status update
Mobile Network Not Charged Pause Sync (unless triggered manually) Avoid traffic waste and power consumption

(II) Example of "conditional combination": "Social and livestock exclusive" synchronization strategy

const workInfo: WorkInfo = {
    workId: 8888,
networkType: NetworkType.WIFI, // must be connected to WiFi
chargerType: ChargingType.WIRED, // And wired charging
startTime: '00:00-06:00', // Automatic execution in the early morning
requiredBattery: 50, // The power needs >50%
dataVolume: 1024 * 1024 * 50 // Allows 50MB of data synchronization
};
Enter fullscreen mode Exit fullscreen mode

Scene Interpretation: When setting up "Late Night Charging + WiFi Environment" for "Office Collaboration" applications, it automatically synchronizes large files, which does not disturb users, and makes full use of idle resources. It can be called a "time management master"!

2. System scheduling "unwritten rules": group management "differential treatment"

(I) Application grouping "Power Consumption Ranking"

graph LR
A[Active Grouping] --> B(2 hours/time)
C[Frequently used] --> D (4 hours/time)
E[Common Grouping] --> F(24 hours/time)
G[Rust use] --> H (48 hours/time)
I[Restricted Grouping] --> J (Execution is prohibited)
Enter fullscreen mode Exit fullscreen mode

Scheduling logic: The more commonly used applications, the more frequent synchronization is allowed, but it is still controlled by the system's "power red line"~

(II) "Dynamic Downgrade" Strategy: "Survival Way" when the battery is in urgent need of

public class SmartSyncManager {
    public void adjustSyncStrategy(int batteryLevel) {
        if (batteryLevel < 20) {
// Switch to "Survival Mode"
workInfo.networkType = NetworkType.NONE; // The network is not synchronized even if it is disconnected
workInfo.chargerType = ChargingType.ANY; // Execute as long as you charge
workInfo.dataVolume = 1024; // Only 1KB of key data synchronizes
        } else if (batteryLevel < 50) {
// Switch to "Power Saving Mode"
workInfo.delayTime = 3600; // Delay execution by 1 hour
workInfo.requiredNetwork = true; // must be connected to WiFi
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Effect: When the power is <20%, the synchronous energy consumption will be reduced by 90%!

3. Practical cases: "Breathing Synchronization" of the "News" APP

(I) Requirements disassembly

  • Core goal: Update information in real time, and at the same time, battery life is "sustained"
  • User Pain Points: ✘ Automatic updates on mobile network waste traffic ✘ Frequent synchronization causes the phone to "heat and power consumption" ✘ Secretly running on the backstage behind the lock screen

(II) Synchronous strategy "combination punch"

1. WiFi environment "Breathing with big mouth"

const fullSyncWork: WorkInfo = {
    workId: 101,
    networkType: NetworkType.WIFI,
chargerType: ChargingType.ANY, // Can charge or not
dataVolume: 10 * 1024 * 1024, // Allow 10MB of graphics and text updates
repeatInterval: 3600, // Synchronize once per hour (active grouping)
    abilityName: 'FullSyncAbility'
};
WorkScheduler.startWork(fullSyncWork);
Enter fullscreen mode Exit fullscreen mode

2. Mobile Network "Shallow Breathing"

const lightSyncWork: WorkInfo = {
    workId: 102,
    networkType: NetworkType.CELLULAR,
dataVolume: 512, // Only synchronize 512KB title updates
repeatInterval: 1800, // Synchronize every 3 hours
    abilityName: 'LightSyncAbility'
};
WorkScheduler.startWork(lightSyncWork);
Enter fullscreen mode Exit fullscreen mode

3. Lock screen state "Hold your breath"

public class ScreenStateObserver implements Observer {
    @Override
    public void onScreenOff() {
WorkScheduler.pauseAll(); // Pause all synchronization tasks after locking the screen
    }

    @Override
    public void onScreenOn() {
WorkScheduler.resumeAll(); // Restore tasks after unlocking
    }
}
Enter fullscreen mode Exit fullscreen mode

(III) Effect comparison

Indicators Traditional synchronization method Respiratory synchronization Improvement range
Traffic consumption 20MB per day 3MB per day 85%
Battery consumption 8% per day 2.5% per day 69%
User Retention Rate 72% 89% 24%

4. Avoiding pits: Make the synchronizer "behaved and sensible"

(I) "Permission Minesce" detour

  1. Dynamic application for network permissions: If the task needs to be distinguished from WiFi/mobile networks, you must apply for permission dynamically, otherwise it may be intercepted by the system.
   if (!checkPermission("ohos.permission.GET_NETWORK_INFO")) {
       requestPermission("ohos.permission.GET_NETWORK_INFO");
       return;
   }
Enter fullscreen mode Exit fullscreen mode
  1. Battery Optimization Discount: Remind users to add the application to the "Battery Optimization Whitelist", otherwise the background tasks may be forced to be killed by the system.
   if (isWorkAborted(workId)) {
showBatterySettingsGuide(); // Pop-up window guides user settings
   }
Enter fullscreen mode Exit fullscreen mode

(II) "Parameter magic" formula

  1. Delayed time "Food after the day":
  2. Daytime (9:00-22:00): 1-2 hours delay (user may be using the device)
  3. Night time (22:00-9:00): 3-6 hours delay (avoid disturbing users)

  4. Data quantity "tailored":

  5. Active users: 5-10MB/time allowed (app is opened frequently, and rich content is required)

  6. Silent user: Limit 1MB/time (avoid "invalid update" power consumption)

   int getDataVolume(UserActivityLevel level) {
       return level == ACTIVE ? 10*1024*1024 : 1*1024*1024;
   }
Enter fullscreen mode Exit fullscreen mode

5. Future Evolution: The "Super Power Awakening" of the Synchronizer

(I) "Cross-device relay" synchronization

In the future, support "multi-terminal collaboration" synchronization!for example:

  • When the mobile phone connects to WiFi, it will automatically trigger the tablet/PC to synchronize the same data
  • The watch detects user movement, triggers the mobile phone to synchronize the motion data to the cloud
graph LR
A[Mobile WiFi Connection] --> B[Tablet Synchronization Document]
C[Watch movement starts] --> D[Mobile uploading exercise data on mobile phone]
Enter fullscreen mode Exit fullscreen mode

(II) Synchronization of "AI Prediction"

Combining user behavior data, the best synchronization time is automatically calculated!for example:

  • Synchronize the news of the day 15 minutes in advance based on user commuting time
  • Predict the meeting schedule and synchronize relevant information before the meeting begins

(III) "Low Carbon Mode" Synchronization

Responding to the call for environmental protection, give priority to performing large file synchronization during the "grid trough period" (such as night time) to contribute to carbon neutrality~

Last reminder: The "Iron Law of Survival" of Synchronizer

User experience = (data freshness × system friendly) ÷ resource consumption

  • Freshness: Don't let users open the app to see "old data" (such as the weather is still yesterday)
  • Friendliness: Don't "make trouble" when the user calls/navigates (reduce the priority of background tasks)
  • Resource consumption: Don't let synchronization become a "battery killer" (always keep in mind the "energy-saving mission" of delayed tasks)

Want to know how to use Hongmeng to implement the "real-time monitoring dashboard for synchronous tasks"?Follow me, the next article will help you unlock "Data Synchronization Visualization"!If you think the article is useful, share it with the team's product manager. Let's make the backend synchronous "quietly become efficient"~ 😉

Top comments (0)