This module provides Huawei Share event registration.
Model restriction: APIs of this module can be used only in the stage model.
Since: 5.0.0(12)
Reason for rejecting a sharing callback. The user will be informed of the reason through a system notification.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 5.0.3(15)
| Name | Value | Description |
|---|---|---|
| NO_CONTENT_ERROR | 1 | Used when no content is shared. |
| NO_INTERNET_ERROR | 2 | Used when there is no network connection available. |
| DOWNLOAD_ERROR | 3 | Used when download fails. |
Reason for rejecting a callback for receiving data in sandbox mode. The user will be informed of the reason through a system notification.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Value | Description |
|---|---|---|
| NO_RECEIVABLE_ERROR | 1 | Used when data cannot be received. |
Callback parameter after a Huawei Share event is triggered. This parameter can be used for cross-device sharing.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 5.0.0(12)
reject(error: SharableErrorCode): Promise<void>
Rejects a sharing that cannot proceed for any reason upon receiving a callback. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 5.0.3(15)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| error | SharableErrorCode | Yes | Reason for rejecting the sharing. The user will be informed of the reason through a system message. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes:
For details about the error codes, please refer to Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. |
Example:
import { harmonyShare } from '@kit.ShareKit';
// Register a knockShare event listener.
harmonyShare.on('knockShare', (sharableTarget: harmonyShare.SharableTarget) => {
// Reject the sharing.
sharableTarget.reject(harmonyShare.SharableErrorCode.NO_CONTENT_ERROR);
}); getInfo(): SharableTargetInfo
Obtains information about the Tap to Transfer event triggered by a PC/2-in-1 device or a tablet as the sender.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 26.0.0
Returns:
| Type | Description |
|---|---|
| SharableTargetInfo | Information about the Tap to Transfer event triggered by a PC/2-in-1 device or a tablet as the sender, such as the coordinates of the position where the screen is tapped. |
Example:
import { harmonyShare } from '@kit.ShareKit';
// Register a knockShare event listener.
harmonyShare.on('knockShare', (sharableTarget: harmonyShare.SharableTarget) => {
// Obtain the coordinates when the screen is tapped.
let sharableTargetInfo = sharableTarget.getInfo();
console.info('screenX is:', sharableTargetInfo.coordinate?.screenX);
console.info('screenY is:', sharableTargetInfo.coordinate?.screenY);
}); Parameter in the callback for receiving data in sandbox mode.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
receive(receiveUri: string, callback: ReceiveCallback): Promise<void>
Provides the URI of the sandbox directory for receiving data. The received files are saved to this directory. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| receiveUri | string | Yes | Directory for storing data received by the sandbox. The directory must exist. Note: Share Kit will obtain authorization for this directory before the transfer starts and automatically revoke the authorization after the transfer is complete. You are advised to use an existing empty directory to ensure information security. |
| callback | ReceiveCallback | Yes | Callback for receiving data in sandbox mode. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example:
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { systemShare, harmonyShare } from '@kit.ShareKit';
import { common } from '@kit.AbilityKit';
let capabilityRegistry: harmonyShare.RecvCapabilityRegistry = {
windowId: 999, // This is only an example. Replace it with the correct window ID.
capabilities: [{
utd: utd.UniformDataType.IMAGE,
maxSupportedCount: 1
}]
}
// Register a dataReceive event listener.
harmonyShare.on('dataReceive', capabilityRegistry, (receivableTarget: harmonyShare.ReceivableTarget) => {
let uiContext: UIContext = this.getUIContext();
let context = uiContext.getHostContext() as common.UIAbilityContext;
receivableTarget.receive(context.filesDir, { // This is only an example. Replace it with the actual path.
onDataReceived: (sharedData: systemShare.SharedData) => {
let sharedRecords = sharedData.getRecords();
sharedRecords.forEach((record: systemShare.SharedRecord) => {
// Process the shared data.
});
},
onResult(resultCode: harmonyShare.ShareResultCode) {
if (resultCode === harmonyShare.ShareResultCode.SHARE_SUCCESS) {
// To do things.
}
}
});
}); reject(error: ReceivableErrorCode): Promise<void>
Rejects a sharing if data reception cannot proceed for any reason upon receiving a callback. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| error | ReceivableErrorCode | Yes | Reason for rejecting data reception in sandbox mode. The user will be informed of the reason through a system message. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example:
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { harmonyShare } from '@kit.ShareKit';
let capabilityRegistry: harmonyShare.RecvCapabilityRegistry = {
windowId: 999, // This is only an example. Replace it with the correct window ID.
capabilities: [{
utd: utd.UniformDataType.IMAGE,
maxSupportedCount: 1
}]
}
// Register a dataReceive event listener.
harmonyShare.on('dataReceive', capabilityRegistry, (receivableTarget: harmonyShare.ReceivableTarget) => {
receivableTarget.reject(harmonyShare.ReceivableErrorCode.NO_RECEIVABLE_ERROR);
}); getInfo(): ReceivableTargetInfo
Obtains information about the Tap to Transfer event triggered by a PC/2-in-1 device or a tablet as the receiver.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 26.0.0
Returns:
| Type | Description |
|---|---|
| ReceivableTargetInfo | Information about the Tap to Transfer event triggered by a PC/2-in-1 device or a tablet as the receiver, such as the coordinates of the position where the screen is tapped. |
Example:
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { systemShare, harmonyShare } from '@kit.ShareKit';
import { common } from '@kit.AbilityKit';
import { fileUri } from '@kit.CoreFileKit';
let capabilityRegistry: harmonyShare.RecvCapabilityRegistry = {
windowId: 999, // This is only an example. Replace it with the correct window ID.
capabilities: [{
utd: utd.UniformDataType.IMAGE,
maxSupportedCount: 1
}]
}
// Register a dataReceive event listener.
harmonyShare.on('dataReceive', capabilityRegistry, (receivableTarget: harmonyShare.ReceivableTarget) => {
let uiContext: UIContext = this.getUIContext();
let context = uiContext.getHostContext() as common.UIAbilityContext;
let sandboxUri = fileUri.getUriFromPath(context.filesDir);
let receivableTargetInfo = receivableTarget.getInfo();
console.info('screenX is:', receivableTargetInfo.coordinate?.screenX);
console.info('screenY is:', receivableTargetInfo.coordinate?.screenY);
receivableTarget.receive(sandboxUri, {
onDataReceived: (sharedData: systemShare.SharedData) => {
let sharedRecords = sharedData.getRecords();
sharedRecords.forEach((record: systemShare.SharedRecord) => {
// Process the shared data.
});
},
onResult(resultCode: harmonyShare.ShareResultCode) {
if (resultCode === harmonyShare.ShareResultCode.SHARE_SUCCESS) {
// To do things.
}
}
});
}); Data sent by the Huawei Share event.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| thumbnailUri | string | No | Yes | URI of a preview image. |
Basic configuration for Huawei Share event registration.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| windowId | number | No | No | Window ID. |
Window registration configuration item for the Huawei Share event. This API is inherited from BaseCapabilityRegistry.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| sendOnly | boolean | No | Yes | Indicates whether only data sending is supported. The default value is false, indicating that data sending and reception are both supported. If this parameter is set to true for both devices, two-way sharing is not allowed. Note: This API is only used for the Tap to Transfer feature. |
Sets the data reception capability for the sandbox, including the file types and the maximum number of files that can be received at a time.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| utd | string | No | No | Uniform data type. For details, please refer to @ohos.data.uniformTypeDescriptor (Uniform Data Definition and Description). |
| maxSupportedCount | number | No | No | Maximum number of files that can be received at a time. The value must be a positive integer. |
Window registration configuration item for the event of receiving data in sandbox mode. This API is inherited from BaseCapabilityRegistry.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| capabilities | RecvCapability[] | No | No | Data reception capability of the sandbox, including the supported data types and quantity that can be received at a time. |
Callback function for file transfer completion when the sandbox receives files.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| onResult | Callback<ShareResultCode> | No | Yes | Callback function for file transfer completion. It is used to notify the transfer result. |
Callback function triggered when the sandbox completes file receiving. It can be used to listen for the completion event and process the received data. This API is inherited from TransferBaseResults.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| onDataReceived | Callback<systemShare.SharedData> | No | No | Callback function triggered when the sandbox completes file receiving. It is used to provide the list of files saved to the sandbox path. |
Message used to notify the user of the reason why the sharing is unavailable.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 6.0.2(22)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| message | string | No | Yes | Message content. |
Coordinates of the position where the screen is tapped when the Tap to Transfer event is triggered between a phone and a PC/2-in-1 device or a tablet.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 26.0.0
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| screenX | number | No | No | X coordinate with the upper left corner of the screen as the origin when a Tap to Transfer event is triggered. The value can only be set to an integer. The unit is px. |
| screenY | number | No | No | Y coordinate with the upper left corner of the screen as the origin when a Tap to Transfer event is triggered. The value can only be set to an integer. The unit is px. |
Information about the Tap to Transfer event triggered by a PC/2-in-1 device or a tablet as the sender.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 26.0.0
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| coordinate | CoordinateInfo | No | Yes | Coordinates of the position where the screen is tapped when the Tap to Transfer event is triggered. |
Information about the Tap to Transfer event triggered by a PC/2-in-1 device or a tablet as the receiver.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Since: 26.0.0
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| coordinate | CoordinateInfo | No | Yes | Coordinates of the position where the screen is tapped when the Tap to Transfer event is triggered. |
on(event: 'dataReceive', capability: RecvCapabilityRegistry, callback: Callback<ReceivableTarget>): void
Registers an event listener for receiving data in sandbox mode. Only file data is supported. The original receiving logic is retained for text data (including links). This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Device behavior differences: For API version 6.0.2(22) and earlier, this API is supported on PCs and 2-in-1 devices. On other device types, it returns error code 801. Since API version 6.1.0(23), this API is supported on PCs, 2-in-1 devices, and tablets, but does not respond on other device types.
Since: 6.0.0(20)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| event | string | Yes | Event callback type. The value is dataReceive, which is triggered when a phone lightly touches a PC, 2-in-1, or tablet device screen. |
| capability | RecvCapabilityRegistry | Yes | Event attribute. |
| callback | Callback<ReceivableTarget> | Yes | Callback used to return the sharing receiving object, which is used to receive the shared content. |
Example:
private dataReceiveCallback = (receivableTarget: harmonyShare.ReceivableTarget) => {
let uiContext: UIContext = this.getUIContext();
let context = uiContext.getHostContext() as common.UIAbilityContext;
let sandboxUri = fileUri.getUriFromPath(context.filesDir);
receivableTarget.receive(sandboxUri, {
onDataReceived: (sharedData: systemShare.SharedData) => {
let sharedRecords = sharedData.getRecords();
sharedRecords.forEach((record: systemShare.SharedRecord) => {
this.dataReceiveUri = record.uri;
});
},
onResult: (resultCode: harmonyShare.ShareResultCode) => {
if (resultCode === harmonyShare.ShareResultCode.SHARE_SUCCESS) {
try {
const uiContext: UIContext = this.getUIContext();
uiContext.getPromptAction().showToast({ message: $r('app.string.success_supported') });
} catch (error) {
hilog.error(DOMAIN, 'testTag', `showToast error. Code: ${error?.code}, message: ${error?.message}`);
}
}
}
});
}
private isNoListening() {
return !this.dataReceiveStatus;
}
private dataReceiveListening() {
if (this.isNoListening()) {
if (this.windowId) {
let capabilityRegistry: harmonyShare.RecvCapabilityRegistry = {
windowId: this.windowId,
capabilities: [{
utd: utd.UniformDataType.IMAGE,
maxSupportedCount: 1,
}]
}
harmonyShare.on('dataReceive', capabilityRegistry, this.dataReceiveCallback);
this.dataReceiveStatus = true;
}
} else {
try {
const uiContext: UIContext = this.getUIContext();
uiContext.getPromptAction().showToast({ message: $r('app.string.knock_close_other') });
} catch (error) {
hilog.error(DOMAIN, 'testTag', `showToast error. Code: ${error?.code}, message: ${error?.message}`);
}
}
} off(event: 'dataReceive', capability: RecvCapabilityRegistry, callback?: Callback<ReceivableTarget>): void
Unregisters an event listener for receiving data in sandbox mode. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Collaboration.HarmonyShare
Device behavior differences: For API version 6.0.2(22) and earlier, this API is supported on PCs and 2-in-1 devices. On other device types, it returns error code 801. Since API version 6.1.0(23), this API is supported on PCs, 2-in-1 devices, and tablets, but does not respond on other device types.
Since: 6.0.0(20)
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| event | string | Yes | Event callback type. The value is dataReceive, indicating that the event listener for receiving data in sandbox mode is unregistered. |
| capability | RecvCapabilityRegistry | Yes | Event attribute. |
| callback | Callback<ReceivableTarget> | No | Callback function. You can pass the callback used in the on API to unregister the corresponding listener. If you do not pass the callback, you will unregister all listeners. |
Example:
private dataReceiveDisablingListening() {
try {
if (this.windowId) {
let capabilityRegistry: harmonyShare.RecvCapabilityRegistry = {
windowId: this.windowId,
capabilities: [{
utd: utd.UniformDataType.IMAGE,
maxSupportedCount: 1,
}]
}
harmonyShare.off('dataReceive', capabilityRegistry, this.dataReceiveCallback);
this.dataReceiveStatus = false;
}
} catch (error) {
hilog.error(DOMAIN, 'testTag', 'error message: %s', error?.message ?? 'unknown error');
}
}