The Art of Data Synchronization in HarmonyOS Next Part Five: Cross - application Data Interaction

xun_wang_6384a403f9817c2

SameX

Posted on November 15, 2024

The Art of Data Synchronization in HarmonyOS Next Part Five: Cross - application Data Interaction

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices. It mainly serves as a carrier for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together. This article is original content, and any form of reprint must indicate the source and the original author.
In many application scenarios, users need to conduct data interaction between different applications. For example:

  • Dragging pictures or files to another application: Users can drag pictures or files from one application to another. For example, drag a picture to a picture - editing application for editing.
  • Sharing data to other applications: Users can share data to other applications. For example, share contacts to a social application. ### Implementation Methods of Cross - application Data Interaction Huawei HarmonyOS Next provides two implementation methods for cross - application data interaction:
  • Data Drag - and - Drop: Data drag - and - drop means that users can transfer data from one application to another through drag - and - drop operations. Data drag - and - drop requires cooperation between two applications. One application serves as the drag - and - drop data source, and the other application serves as the drag - and - drop data receiver.
  • Data Sharing: Data sharing means that users can send data to other applications through sharing operations. Data sharing requires applications to provide data - sharing interfaces and allow other applications to receive data. ### Combination of Data Drag - and - Drop Data and Standardized Data Structures The combination of data drag - and - drop data and standardized data structures can conveniently realize data exchange and sharing between different applications. Example Code:
// Import modules
import { unifiedDataChannel } from '@kit.ArkData';
// Create drag - and - drop data
let dataObject = new unifiedDataChannel.UnifiedData();
dataObject.addRecord(new unifiedDataChannel.UnifiedRecord({
    uniformDataType: 'general.image',
    url: 'https://www.example.com/image.jpg'
}));
// Send drag - and - drop data
context.sendDragEvent(dataObject);
// Receive drag - and - drop data
context.onDragEvent((event) => {
    let data = event.data;
    let records = data.getRecords();
    let imageRecord = records.find((record) => record.getType() === 'general.image');
    if (imageRecord) {
        let imageURL = imageRecord.getValue().url;
        console.log(`Received image URL: ${imageURL}`);
    }
});
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

  1. Import Modules: First, import the unifiedDataChannel module, which provides interfaces for creating and operating unified data objects.
  2. Create Drag - and - Drop Data: Use unifiedDataChannel.UnifiedData to create an instance of a unified data object, and add drag - and - drop data (such as pictures) to the object.
  3. Send Drag - and - Drop Data: Use the context.sendDragEvent method to send drag - and - drop data, triggering the drag - and - drop data receiving event of other applications.
  4. Receive Drag - and - Drop Data: Use the context.onDragEvent method to listen to the drag - and - drop data receiving event, obtain the drag - and - drop data and process it. ### Implementation of Data Sharing Data sharing requires applications to provide data - sharing interfaces and allow other applications to receive data. Huawei HarmonyOS Next provides the AbilityShare interface for implementing the data - sharing function. Example Code:
// Import modules
import { AbilityShare } from '@kit.AbilityKit';
// Create sharing data
let shareData = new AbilityShare.Data();
shareData.title = 'Sample Title';
shareData.text = 'Sample Text';
shareData.uri = 'https://www.example.com';
// Share data
AbilityShare.share(shareData);
// Receive sharing data
context.onShare((event) => {
    let data = event.data;
    console.log(`Received sharing data: ${data.title}, ${data.text}, ${data.uri}`);
});
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

  1. Import Modules: First, import the AbilityShare module, which provides the data - sharing interface.
  2. Create Sharing Data: Use AbilityShare.Data to create a data - sharing object and set the title, text, and URL of the sharing data.
  3. Share Data: Use the AbilityShare.share method to share data, triggering the sharing data receiving event of other applications.
  4. Receive Sharing Data: Use the context.onShare method to listen to the sharing data receiving event, obtain the sharing data and process it. ### Summary Huawei HarmonyOS Next provides rich cross - application data interaction functions, including data drag - and - drop and data sharing. We can use standardized data structures and related interfaces to easily realize data exchange and sharing between different applications and improve the user experience.
💖 💪 🙅 🚩
xun_wang_6384a403f9817c2
SameX

Posted on November 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related