With the rapid development of social media platforms and the increasing user demand for content sharing, social sharing buttons have also become an important tool in digital marketing and search engine optimization (SEO) strategies to boost website traffic and content exposure. In addition, the transfer of images, videos, and files is becoming more frequently used in collaborative work and friend sharing.
HarmonyOS SDKShared Services(Share Kit) provides apps with cross-application sharing capabilities for text, images, videos, and other content used to send content to other apps.
The application configures the content to be shared and the preview style to the sharing service, which will build a sharing panel based on the type and quantity of data to be shared, providing users with content previews, recommended sharing contacts, associated applications, and operation interfaces, making it easy for users to quickly select a sharing application or operation to distribute the content to the target application.
Scene Introduction
Smart Office
Users can quickly and seamlessly transfer files and documents from one device to another. In addition, the system provides a variety of capabilities such as printing, staging and other capabilities that provide great convenience in handling documents, optimizing the document management process and making the daily office more efficient.
video entertainment
Users can easily share pictures, videos and other content to others or devices such as Smart Screen. In addition, the recommendation function is able to recommend contacts based on users' behavioral habits, enhancing the personalized sharing experience.
Cell phone sharing panel effect
Popup format effect on 2in1 device
Functional Advantages
1. Seamless cross-device connectivity
The HarmonyOS SDK sharing service enables seamless connectivity between different devices, including smartphones, tablets, laptops and other compatible smart devices. Users can quickly transfer files between multiple devices with a simple operation, regardless of device type and brand.
2. Fast transmission speed
Compared to traditional Bluetooth transmission, the HarmonyOS SDK sharing service utilizes a more efficient transmission protocol that significantly improves file transfer speeds. Transfer times for large files are dramatically reduced, allowing users to be more efficient when handling heavy tasks.
3. Intelligent recognition and recommendation
The HarmonyOS SDK sharing service uses intelligent recognition technology to automatically identify the receiving device and recommend the appropriate sharing method based on the type of content. For example, when you try to share a photo, the system will automatically select the best receiving device and provide optimized display options.
4. Privacy protection and security
HarmonyOS SDK sharing service focuses on user privacy and data security, and all transmission processes are encrypted to ensure data security during transmission. In addition, users can set sharing privileges according to their needs to further protect their privacy.
5. Convenient operation experience
Ease of operation is a key feature of HarmonyOS SDK sharing service. Users can share files by simply dragging, dropping, clicking or selecting, without complicated settings or tedious steps. This intuitive operation greatly enhances the user experience.
6. Cross-platform compatibility
It not only supports sharing between HarmonyOS devices, but also cross-platform file sharing with other major operating systems such as Android and iOS. No matter which operating system you use, you can enjoy the convenience of HarmonyOS SDK sharing service.
7. Support for a variety of file formats
HarmonyOS SDK sharing service supports the transfer of a variety of file formats, including images, videos, documents and so on. Users don't need to worry about file format incompatibility and can freely share various types of files to meet the needs of different scenarios.
development step
I. Host application initiated sharing
1) Mobile app initiates system sharing
1. Import the relevant modules.
import { common } from '@';
import { systemShare } from '@';
import { uniformTypeDescriptor as utd } from '@';
2. Construct sharing data to add multiple sharing records.
// Construct ShareData with a valid data message to configure
let data: = new ({
utd: .PLAIN_TEXT, content: 'Hello HarmonyOS'
content: 'Hello HarmonyOS'
});
// Add multiple records
({
utd: , uri: 'file://...
uri: 'file://... /'
});
3. Launch the Share panel.
// Build the ShareController
let controller: = new (data);
// Get the UIAbility context object
let context: = getContext(this) as ;
// Register the share panel close listener
('dismiss', () => {
('Share panel closed');
// Sharing is closed and other business can be taken care of.
});
// Perform the share panel display
(context, {
previewMode: ,
selectionMode.
}).
2) 2in1 Application Launching System Sharing
1. Import the relevant modules.
import { common } from '@';
import { systemShare } from '@';
import { uniformTypeDescriptor as utd } from '@';
2. Construct sharing data to add multiple sharing records.
// Construct ShareData with a valid data message to configure
let data: = new ({
utd: .PLAIN_TEXT, content: 'Hello HarmonyOS'
content: 'Hello HarmonyOS'
});
// Add an additional record
({
utd: , uri: 'file://...
uri: 'file://... /'
}).
3. To start the sharing panel, you must configure the location information or the associated component ID that the sharing panel displays, and the panel will be displayed as a Popup.
// Build the ShareController
let controller: = new (data);
// Get the UIAbility context object
let context: = getContext(this) as ;
// Register the share panel close listener
('dismiss', () => {
('Share panel closed');
// Sharing is closed and other business can be taken care of.
});
// Perform the share panel display
// Method 1: Configure the control ID associated with the share panel
(context, {
anchor: 'shareButtonId'
}); // Method 1: Configure the ID of the control associated with the share panel.
// Method 2: Configure the coordinates of the share panel display
(context, {
anchor: {
// Mandatory, anchor coordinates
windowOffset: {x: 100, y: 100}, // Optional, the component's width and height.
// Optional, the width and height of the component, which will be combined to calculate the size of the component when configured
size: {width: 0, height: 0}
}
});
II. Targeted applications to address shared content
1) In-app processing of shared content
1. Import the relevant modules.
import { AbilityConstant, UIAbility, Want } from '@';
import { window } from '@';
import { systemShare } from '@';
import { BusinessError } from '@';
2. The target application can implement UIAbility and get sharing data from want.
export default class TestUIAbility extends UIAbility {
onCreate(want: Want, launchParam: ): void {
(want)
.then((data: ) => {
().forEach((record: ) => {
// Processing of shared data
});
})
.catch((error: BusinessError) => {
(`Failed to getSharedData. Code: ${}, message: ${}`);
();
});
}
onWindowStageCreate(windowStage: ): void {
// Main window is created, set main page for this ability
('pages/Index', (err) => {
if () {
('Failed to load the content. Cause: ' + (err) ?? '');
return;
}
('Succeeded in loading the content.');
});
}
}
3. After building UIAbility, you need to register in the skills configuration of the application configuration file (src/main/module.json5). Configuration actions for; uris need to exhaust all supported data types.
"abilities": [
{
"name": "TestUIAbility",
"srcEntry": ". /ets/entryability/",
"description":"$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label":"$string:EntryAbility_label",
"startWindowIcon":"$media:startIcon",
"startWindowBackground":"$color:start_window_background",
"exported": true, "skills": [ $color:start_window_background
"skills": [
{
"actions": [
""
], [ "actions": [ "" ], [ "" ], [ "" ]
// The target application needs to exhaust the supported UTDs when configuring the data types it supports receiving, e.g., support for all image types can be declared:
// maxFileSupported Maximum number of supported UTDs Not filled in defaults to 0
"uris": [
{
"scheme": "file",
"utd": "",
"maxFileSupported": 1
},
{
"scheme": "file",
"utd": "", "maxFileSupported": 1 }, { "scheme": "file", "utd": "",
"maxFileSupported": 1
},
{
"scheme": "file",
"utd": "", "maxFileSupported": 1 }, { "scheme": "file", "utd": "",
"maxFileSupported": 1
}
]
}
]
}
]
2) Secondary panel to handle shared content
1. Import the relevant modules.
import { Want, ShareExtensionAbility, UIExtensionContentSession } from '@';
import { systemShare } from '@';
import { BusinessError } from '@';
2. The target application can use ShareExtensionAbility as the base class to build the sharing ability Ability. when Ability is started by the system, Ability will receive want data and get the sharing data from want.
export default class TestShareAbility extends ShareExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
(want)
.then((data: ) => {
().forEach((record: ) => {
// Processing of shared data
});
('pages/Index');
})
.catch((error: BusinessError) => {
(`Failed to getSharedData. Code: ${}, message: ${}`);
();
});
}
}
3. After building the sharing ability Ability, you need to register in the skills configuration of the application configuration file (src/main/module.json5). Configuration actions for, and uris need to exhaust all supported data types.
"extensionAbilities": [
{
"name": "TestShareAbility",
"srcEntry": ". /ets/abilities/", "type": "share", //supportShareAbility", ".
"type": "share", // support for sharing data handling
"description": "xxx", // support for sharing data processing
"exported": true, "label":"$string": "xxx", // support for sharing data processing
"label": "$string:xx_label", // Support for sharing data processing.
"icon": "$media:icon",
"skills": [
{
"actions": [
""
], // The target application needs to exhaust the UTD when configuring the types of data it will support.
// The target application needs to exhaust the supported UTDs when configuring the data types it supports receiving, e.g., support for all image types can be declared:
// maxFileSupported Maximum number of supported UTDs. defaults to 0 if not filled.
"uris": [
{
"scheme": "file",
"utd": "",
"maxFileSupported": 1
},
{
"scheme": "file",
"utd": "", "maxFileSupported": 1 }, { "scheme": "file", "utd": "",
"maxFileSupported": 1
},
{
"scheme": "file",
"utd": "", "maxFileSupported": 1 }, { "scheme": "file", "utd": "",
"maxFileSupported": 1
}
]
}
]
}
]
3) Secondary panel to close the sharing panel
1. Import the relevant modules.
import { ShareExtensionAbility, UIExtensionContentSession, Want } from '@';
import { systemShare } from '@';
2. The target application can set the resultCode value to close the sharing panel via the terminateSelfWithResult interface.
export default class TestShareAbility extends ShareExtensionAbility {
async onSessionCreate(want: Want, session: UIExtensionContentSession) {
({
resultCode:
});
}
}
Find out more>>
interviewsSharing Services Alliance Official Website
gainShare Service Development Guidance Document