DEV Community

ι™ˆζ¨
ι™ˆζ¨

Posted on

Treasure Case Sharing of HarmonyOS 5 Development β€” Enhancing Content Publishing Experience with Seamless Application Continuity

🌟[Development Experience Sharing] HarmonyOS Continuation Feature in Practice: Hidden Cases to Help You Achieve Seamless Cross-Device Flow!

Hello, fellow developers! Today, while working on a project, I accidentally unlocked a "treasure skill" in HarmonyOSβ€”the Continuation feature! The official documentation actually hides many practical cases, but many people may have overlooked them. As someone who has stepped on countless pitfalls, let me break down the most essential cross-device continuation development tips for you!

πŸ“± 1. First, let's talk about how awesome this feature is

Imagine this scenario: you're editing a Xiaohongshu post on your phone and suddenly want to continue formatting on your tablet. Just tap the Dock icon on the tablet, and your draft, along with images and text, is instantly and seamlessly transferred! This sci-fi-like experience can be easily achieved with HarmonyOS's distributed capabilities!

(Psst: In actual tests, you can even take a photo on your phone and insert it directly into your tablet!)

πŸ”§ 2. Three Core Development Essentials

Remember these three must-have conditionsβ€”missing any one of them will cause issues:

1️⃣ Both devices must be logged into the same Huawei account

2️⃣ Wi-Fi and Bluetooth must both be enabled (preferably on the same LAN)

3️⃣ Go to Settings β†’ Multi-Device Collaboration β†’ Enable the Continuation feature

πŸ“ 3. Detailed Practical Cases (with Pitfall Avoidance Tips)

🌟 Case 1: Cross-Device Continuation of Image and Text Drafts

// Core code snippet explanation
async onContinue(wantParam: Record<string, Object>) {
  // Generate a distributed session ID (as important as a tracking number!)
  let sessionId = distributedDataObject.genSessionId(); 

  // Convert the image to ArrayBuffer and store it in the distributed directory
  const buffer = await imageToArrayBuffer(pickedImage);
  writeDistributedFile(buffer, 'travel_photo.jpg');

  // Create a data object to wrap all content
  this.distributedObject = distributedDataObject.create(this.context, {
    title: 'Tibet Travel Diary',
    content: 'The Potala Palace is truly breathtaking...',
    images: [distributedAsset] // Key! This is a reference to the distributed file
  });

  // Activate data synchronization (like starting a data delivery service)
  this.distributedObject.setSessionId(sessionId);
  await this.distributedObject.save(targetDevice);
}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Pitfall: Images must be converted to distributed assets; directly passing local paths will fail!

🌟 Case 2: Real-Time Continuation of Collaborative Documents

When users switch from PC to phone, you need to handle rich text:

// When the receiving end restores data
if (status === 'restored') {
  // Get the document version number from the distributed object
  const version = this.distributedObject['docVersion'];

  // Conflict handling: if there are local changes, prompt the user
  if(localVersion > version) {
    showAlert('Version conflict detected, please choose which version to keep');
  }

  // Restore document content (including formatting data)
  quillEditor.setContents(this.distributedObject['delta']);
}
Enter fullscreen mode Exit fullscreen mode

✨ Advanced Tip: Use a JSON diff algorithm for incremental sync to reduce data transfer

🌟 Case 3: Continuation of Social Contacts

Optimizing the transfer of contact avatars:

// Image processing optimization solution
async compressImageForTransfer(pixelMap) {
  // Dynamically compress based on device type
  const targetDevice = getTargetDeviceType();
  const quality = targetDevice === 'phone' ? 0.8 : 1.0;

  // Use HarmonyOS native compression API
  const packOpts: image.PackingOption = { format: 'image/jpeg', quality };
  return await imageSource.createPixelMap(packOpts);
}
Enter fullscreen mode Exit fullscreen mode

Test data: After adopting dynamic compression, transfer speed increased by 40%

πŸ” 4. Debugging Tips You Must Know

  1. Use DevEco Studio's distributed simulator (real device debugging is even better)
  2. Check hilog logs and filter by the keyword "distributed"
  3. If sync fails, first check: Is Bluetooth enabled? Are distributed file permissions granted?

🎯 5. Think that's all? Here are more cool tricks

  • Combine AI capabilities for intelligent continuation content recommendations
  • Use device sensor status to determine the best continuation timing
  • Data sharding transfer in multi-device relay scenarios

A final word: At first, the official documentation was a bit confusing, but when I finally got the first continuation demo running, the excitement of "Wow, it actually works!" was amazing! I hope this sharing helps you avoid detours. If you have other cool tricks, feel free to battle in the comments~ Next time, we'll talk about how to use HarmonyOS for cross-device game state sync. If you're interested, don't forget to like, comment, and follow! πŸ’ͺ

HuaweiHarmony #HarmonyOS #CrossDeviceDevelopment #MobileDevelopment #ProgrammerLife

Top comments (0)