本模块提供分享数据创建及分享面板拉起的功能,提供多种系统标准分享服务,例如分享数据给其他应用、复制、打印等。
分享接入应用需要配置、呈现和关闭分享面板。
分享面板的配置包括数据对象、呈现视图的方式、预览模式等。
模型约束: 此模块的接口仅可在Stage模型下使用。
起始版本: 4.1.0(11)
可以设置的相对锚点的窗口偏移值。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| x | number | 否 | 否 | x点的坐标。单位:像素。 |
| y | number | 否 | 否 | y点的坐标。单位:像素。 |
锚点矩形的尺寸,缺省时,锚点是一个点,即宽高都为0。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| width | number | 否 | 否 | 组件宽度,单位:像素。负数按0处理。 |
| height | number | 否 | 否 | 组件高度,单位:像素。负数按0处理。 |
意图框架推荐联系人的信息。当分享到推荐联系人时,携带此参数用于区分。
数据捐献参考:共享联系人信息到分享推荐区。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| contactType | string | 否 | 否 | 联系人类型:取值来源应用向意图捐赠数据中的entityName字段,例如"Contact"、"ChatGroup"。 |
| contactId | string | 否 | 否 | 联系人ID:取值来源应用向意图捐赠数据中的entityId字段。 |
分享面板选择模式。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
| 名称 | 值 | 说明 |
|---|---|---|
| SINGLE | 0 | 单选模式,当传入多条分享数据记录(SharedRecord)时,需要用户多选一进行分享。 |
| BATCH | 1 | 批量模式,分享全部数据记录。 |
constructor(record: SharedRecord)
用于构造分享数据对象。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| record | SharedRecord | 是 | 分享数据记录对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
示例:
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: 'Hello HarmonyOS'
}); addRecord(record: SharedRecord): void
在当前分享数据中添加一条分享数据记录。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| record | SharedRecord | 是 | 要添加到分享数据对象中的数据记录,该记录为SharedRecord对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
| 1003700001 | The number of records exceeds the maximum. |
示例:
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.IMAGE,
// ...
title: 'Picture Title',
description: 'Picture Description',
label: 'Poster'
});
try {
shareData.addRecord({
utd: utd.UniformDataType.HYPERLINK,
content: 'https://www.vmall.com/index.html?cid=128688',
title: 'Huawei Vmall',
description: 'Phone',
label: 'Huawei Vmall',
// ...
});
} catch (error) {
hilog.error(DOMAIN, 'testTag', `addRecord error. Code: ${error?.code}, message: ${error?.message}`);
} getRecords(): Array<SharedRecord>
获取当前分享数据中的所有数据记录。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
返回值:
| 类型 | 说明 |
|---|---|
| Array<SharedRecord> | 当前分享数据对象内所包含的记录。 |
示例:
aboutToAppear(): void {
systemShare.getSharedData(this.want).then((data: systemShare.SharedData) => {
let records: systemShare.SharedRecord[] = data.getRecords();
records.forEach(async (record: systemShare.SharedRecord) => {
// ...
});
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, 'testTag', `getSharedData error. Code: ${error?.code}, message: ${error?.message}`);
});
} constructor(data: SharedData)
用于创建分享面板的控制器。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | SharedData | 是 | 分享数据。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
示例:
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.HYPERLINK,
content: 'https://www.vmall.com/index.html?cid=128688',
title: 'Huawei Vmall',
description: 'Phone',
});
let controller: systemShare.ShareController = new systemShare.ShareController(shareData); show(context: common.UIAbilityContext, options: ShareControllerOptions): Promise<void>
显示分享面板,使用Promise异步回调。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | 是 | 拉起分享面板的上下文对象。 | |
| options | ShareControllerOptions | 是 | 分享控制器配置项。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
| 1003702001 | Record types are not support. (The batch and multiple selection modes support { @link UDMF.File } type records only.) |
| 1003702002 | IPC data is oversized. |
示例:
@Component
export default struct LinkScenario {
private async share() {
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.HYPERLINK,
content: 'https://www.vmall.com/index.html?cid=128688',
title: 'Huawei Vmall',
description: 'Phone',
});
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
const uiContext: UIContext = this.getUIContext();
const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
controller.show(context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DEFAULT,
}).then(() => {
hilog.info(DOMAIN, 'testTag', 'ShareController show success.');
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, 'testTag', `ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
}
// ...
} on(event: 'dismiss', callback: () => void): void
注册分享面板关闭事件监听。使用callback异步回调。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| event | string | 是 | 事件回调类型,支持的事件为'dismiss',当分享面板关闭时,触发该事件。 |
| callback | () => void | 是 | 事件回调。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
示例:
private async handelShareDismiss(): Promise<void> {
const uiContext: UIContext = this.getUIContext();
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: 'Hello HarmonyOS',
});
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
const callback = () => {
try {
hilog.info(DOMAIN, 'testTag', 'HuaweiShare_ dismiss invoked.');
uiContext.getPromptAction().showToast({ message: 'dismiss' });
} catch (error) {
hilog.error(DOMAIN, 'testTag', `showToast error. Code: ${error?.code}, message: ${error?.message}`);
}
};
controller.on('dismiss', callback);
controller.show(context, {
previewMode: systemShare.SharePreviewMode.DEFAULT,
selectionMode: systemShare.SelectionMode.SINGLE,
}).then(() => {
hilog.info(DOMAIN, 'testTag', 'HuaweiShare_ show');
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, 'testTag', `HuaweiShare_ show error. Code: ${error?.code}, message: ${error?.message}`);
});
} off(event: 'dismiss', callback: () => void): void
取消分享面板关闭事件监听。使用callback异步回调。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| event | string | 是 | 事件回调类型,支持的事件为'dismiss',取消触发该事件。 |
| callback | () => void | 是 | 回调函数。传入on中的callback取消对应的监听。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
示例:
private async handelShareDisableDismiss(): Promise<void> {
const uiContext: UIContext = this.getUIContext();
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: 'Hello HarmonyOS',
});
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
const callback = () => {
try {
hilog.info(DOMAIN, 'testTag', 'HuaweiShare_ dismiss invoked.');
uiContext.getPromptAction().showToast({ message: 'dismiss' });
} catch (error) {
hilog.error(DOMAIN, 'testTag', `showToast error. Code: ${error?.code}, message: ${error?.message}`);
}
};
controller.on('dismiss', callback);
controller.off('dismiss', callback);
} getContactInfo(want: Want): Promise<ContactInfo>
通过目标应用获取的want信息,从中解析联系人信息,使用Promise异步回调。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| want | Want | 是 | 目标应用获取的want信息。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<ContactInfo> | Promise对象,返回联系人信息。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
| 1003703001 | Parse data failed. |
示例:
aboutToAppear(): void {
systemShare.getContactInfo(this.want).then(async (contact: systemShare.ContactInfo) => {
let data = await systemShare.getSharedData(this.want);
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, 'testTag', `getContactInfo error. Code: ${error?.code}, message: ${error?.message}`);
});
} getWant(data: SharedData, options?: ShareControllerOptions): Promise<Want>
基于SharedData和预览模式构造want数据,使用Promise异步回调。
请勿随意修改返回值want数据中的参数,可能会导致未知的错误。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.SystemShare
起始版本: 5.0.0(12)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | SharedData | 是 | 分享目标应用的分享数据信息。 |
| options | ShareControllerOptions | 否 | 分享控制器配置项。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Want> | Promise对象,返回分享目标应用的want信息。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. |
| 1003703001 | Parse data failed. |
示例:
不配置预览模式
onSessionCreate(): void {
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: 'Hello HarmonyOS'
});
data.addRecord({
utd: utd.UniformDataType.PNG,
uri: 'file://.../test.png'
});
systemShare.getWant(data)
.then((want) => {
})
.catch((error: BusinessError) => {
hilog.error(DOMAIN, 'testTag', `Failed to getWant. Code: ${error?.code}, message: ${error?.message}`);
});
} 配置预览模式
onSessionCreatePreview(): void {
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: 'Hello HarmonyOS'
});
data.addRecord({
utd: utd.UniformDataType.PNG,
uri: 'file://.../test.png'
});
let options : systemShare.ShareControllerOptions = {
previewMode: systemShare.SharePreviewMode.DETAIL,
selectionMode: systemShare.SelectionMode.SINGLE
};
systemShare.getWant(data, options)
.then((want) => {
})
.catch((error: BusinessError) => {
hilog.error(DOMAIN, 'testTag', `Failed to getWant. Code: ${error?.code}, message: ${error?.message}`);
});
}