Hongmeng Next application continuous development: a guide to seamless relay for multi-device
The application continues to enable seamless task migration when users switch applications between mobile phones/tablets/cars.This article analyzes the core API and practical processes to help you create a cross-device collaborative experience~
1. Continuing capability: "Task Relay Race" for multiple equipment
Core Concept
Distributed State Synchronization: Synchronize application data and interface status through soft bus
Migrate three elements: Save data from source equipment → Transfer → Recovery of target equipment
Typical Scenario
Scene
Continuation effect
Mobile phone → Tablet reading
Browser page scrolling position and bookmark synchronization
PC→Car navigation
Seamless migration of map routes and destination parameters
Tablet → Watch To-Do
Real-time synchronization of task progress and reminder settings
2. Practical analysis of core APIs
1. Source data preparation: onContinue()
exportdefaultclassSourceAbilityextendsUIAbility{onContinue(wantParam:Record<string,any>){// 1. Version compatibility verificationconsttargetVersion=wantParam.version||0;if (targetVersion<2.0){returnAbilityConstant.OnContinueResult.MISMATCH;// Version is incompatible}// 2. Package migration data (text, progress, etc.)wantParam['editorContent']=this.currentText;wantParam['scrollPos']=this.scrollY;returnAbilityConstant.OnContinueResult.AGREE;}}
onNewWant(want:Want,launchParam:any){if (launchParam.launchReason==='CONTINUATION'){// Process the connection request of the started applicationconstdata=want.parameters?.migrationData;this.updateData(data);}}
3. Continuing process optimization strategy
1. Data transmission optimization
Incremental Synchronization: Only transfer of change data to reduce traffic consumption
// Calculate data differencesconstdiff=calculateDiff(oldData,newData);wantParam['deltaData']=diff;
Compression transmission: Real-time compression of large file data
Local cache data, automatically transfer after network recovery
Device offline
Prompt the user's device status and provide a list of alternative devices
Data format incompatible
Version negotiation mechanism, automatic conversion of data format
4. Practical cases: Document editing continuation
1. Source (mobile phone) save logic
onContinue(wantParam){// Save document content, cursor position, format settingswantParam['docContent']=this.editor.getContent();wantParam['cursorPos']=this.editor.getCursor();wantParam['formatStyle']=this.editor.getStyle();}
2. Target side (tablet) recovery logic
onCreate(want){if (isContinuation(want)){constcontent=want.parameters.docContent;constpos=want.parameters.cursorPos;// Restore document content and cursor positionthistableteditor.setContent(content);thistableteditor.setCursor(pos);}}
Summary: Three Principles of Continuing Development
Data minimization: Only transfer necessary states to avoid redundant data
Compatibility priority: Version negotiation mechanism ensures seamless connection between new and old devices
Fault-tolerant design: A guarantee solution for abnormal scenarios such as network fluctuations and equipment offline
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)