In the ecological system of HarmonyOS Next, the free flow capability is the key to achieving multi-device collaboration. It breaks down the barriers between devices, enabling users to enjoy a seamless operation experience across different devices. Today, let's conduct an in-depth analysis of this powerful capability.
Core Concepts of Free Flow (Cross-terminal Migration vs. Multi-terminal Collaboration)
Free flow is a general term for distributed operations across multiple devices in HarmonyOS Next. Among them, cross-terminal migration and multi-terminal collaboration are two important implementation forms.
Cross-terminal migration is like an "equipment journey" of an application. During the process of users using devices, once the usage context changes, for example, when returning from outdoors to indoors and finding that the mobile phone originally used is not very convenient for handling certain tasks, while the nearby tablet is more suitable, then cross-terminal migration can be used to "move" the application running on the mobile phone to the tablet for continued use. From a development perspective, it means that the UIAbility running on device A is migrated to device B, and the UIAbility on device A can choose whether to exit according to actual needs. For example, when watching a video on a mobile phone, after getting home, the video can be migrated to a smart screen for continuous playback. The playback progress, playback settings, etc., of the video can be continued intact, bringing users a seamless viewing experience.
Multi-terminal collaboration is more like "teamwork" between devices. Multiple devices, as a whole, jointly provide users with more efficient and immersive services than a single device. Different UIAbility or ServiceExtensionAbility on multiple devices can run simultaneously or alternately to complete the entire business. For example, when opening the same note in the memo on two devices for collaborative editing at the same time, device A can insert pictures from the local gallery, and device B is responsible for editing the text content. Different devices perform their respective functions, greatly improving the user's editing efficiency and experience.
Analysis of Typical Application Scenarios
-
Cross-device Email Editing: This is a typical application of cross-terminal migration. When a user composes an email on a mobile phone and then switches to a computer to continue editing. The free flow capability of HarmonyOS Next will save the editing status of the email, including the entered content, inserted attachments, and set formats, etc. When the email application is opened on the computer, this information will be completely restored, as if the user has been editing on the computer all along. During development, through the application continuation technology, the email data is saved in the
onContinue()interface of the source-side UIAbility, and the data is restored in theonCreate()oronNewWant()interface of the target side to achieve seamless switching of email editing. - Multi-device Collaborative Fitness: In this scenario, multi-terminal collaboration is fully demonstrated. The user wears a smart watch for exercise, and the watch collects data such as heart rate and step count in real time. At the same time, the fitness application on the mobile phone or tablet receives this data and provides personalized exercise suggestions and displays exercise course videos based on the data. The mobile phone can also serve as a control center, making it convenient for users to adjust their fitness plans. During the development process, it is necessary to achieve real-time data transmission and interaction between different devices to ensure that the applications on each device can work collaboratively to provide users with a comprehensive fitness experience.
- Multi-screen Games: Multi-screen games fully showcase the charm of free flow. Take an adventure game as an example. The player conducts the early exploration and operation of the game on a mobile phone. After connecting to a large-screen TV, the game screen automatically switches to the TV. With the large screen and high-definition quality of the TV, the player can obtain a more shocking visual experience. At this time, the mobile phone can be used as a game controller, and accurate control can be achieved by using its sensors. In this process, cross-terminal migration realizes the seamless switching of the game screen and operation, and multi-terminal collaboration enables the mobile phone and the TV to jointly create an immersive gaming experience for the player. Developers need to solve problems such as graphic synchronization between devices, operation instruction transmission, and performance optimization to ensure that the game can run smoothly on different devices.
Practical Case: How to Freely Switch an Application among Multiple Devices
The following is a simple example of a text editor application to show how to achieve the free switching of an application among multiple devices.
Configure the Application Continuation Capability
In the abilities of the module.json5 file, configure the continuable tag as true, indicating that this UIAbility can be migrated:
{
"module": {
"abilities": [
{
"continuable": true
}
]
}
}
Save Data on the Source Side
Implement the onContinue() interface in the source-side UIAbility to save the content in the text editor:
import { AbilityConstant, UIAbility } from '@kit.AbilityKit';
export default class TextEditorAbility extends UIAbility {
onContinue(wantParam: Record<string, Object>) {
// Assume the function to get the content of the text editor is getEditorContent
let editorContent = getEditorContent();
wantParam["editorContent"] = editorContent;
return AbilityConstant.OnContinueResult.AGREE;
}
}
Restore Data on the Target Side
Implement the onCreate() or onNewWant() interface in the target-side UIAbility to restore the content of the text editor:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
export default class TextEditorAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
if (launchParam.launchReason === AbilityConstant.LaunchReason.CONTINUATION) {
let editorContent = want.parameters?.editorContent;
if (editorContent) {
// Assume the function to set the content of the text editor is setEditorContent
setEditorContent(editorContent);
}
}
}
}
Through the above steps, a simple text editor application can be freely switched among multiple devices. When the user opens the application on different devices, they can continue their previous editing work. Of course, in actual application development, more details need to be considered, such as data consistency, device compatibility, etc., but this example provides a basic implementation idea for everyone.
The free flow capability of HarmonyOS Next provides developers with broad space for innovation. By reasonably using cross-terminal migration and multi-terminal collaboration technologies, we can create more intelligent and efficient multi-device applications, bringing users an unprecedented convenient experience. I hope that everyone can make full use of this capability in actual development and create more excellent applications.
Top comments (0)