本模块为统一数据管理框架(Unified Data Management Framework,UDMF)的组成部分,针对多对多跨应用数据共享的不同业务场景提供了标准化的数据通路,提供了标准化的数据接入与读取接口。同时对文本、图片等数据类型提供了标准化定义,方便不同应用间进行数据交互,减少数据类型适配的工作量。
说明:
本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
import { unifiedDataChannel } from '@kit.ArkData';
UDMF支持的设备内使用范围类型枚举。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
名称 | 值 | 说明 |
---|---|---|
IN_APP | 0 | 表示允许在本设备同应用内使用。 |
CROSS_APP | 1 | 表示允许在本设备内跨应用使用。 |
type GetDelayData = (type: string) => UnifiedData
对UnifiedData的延迟封装,支持延迟获取数据。当前只支持同设备剪贴板场景,后续场景待开发。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
type | string | 是 | 作为延迟封装的标识。 |
返回值:
类型 | 说明 |
---|---|
UnifiedData | 当延迟封装触发时,返回一个UnifiedData对象。 |
示例:
import { uniformTypeDescriptor } from '@kit.ArkData';
let getDelayData: unifiedDataChannel.GetDelayData = ((type: string) => {
if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let text = new unifiedDataChannel.Text();
text.details = {
Key: 'textKey',
Value: 'textValue',
};
let textData = new unifiedDataChannel.UnifiedData(text);
return textData;
}
return new unifiedDataChannel.UnifiedData();
});
type ValueType = number | string | boolean | image.PixelMap | Want | ArrayBuffer | object | null | undefined
用于表示统一数据记录允许的数据字段类型。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
类型 | 说明 |
---|---|
number | 表示number的类型。 |
string | 表示string的类型。 |
boolean | 表示boolean的类型。 |
image.PixelMap | 表示image.PixelMap的类型。 |
Want | 表示Want的类型。 |
ArrayBuffer | 表示ArrayBuffer的类型。 |
object | 表示object的类型。 |
null | 表示null。 |
undefined | 表示undefined。 |
定义统一数据对象中所有数据记录的属性,包含时间戳、标签、粘贴范围以及一些附加数据等。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
extras12+ | Record<string, object> | 否 | 是 | 是一个字典类型对象,用于设置其他附加属性数据。非必填字段,默认值为空字典对象。 |
tag12+ | string | 否 | 是 | 用户自定义标签。非必填字段,默认值为空字符串。 |
timestamp12+ | Date | 是 | 是 | UnifiedData的生成时间戳。默认值为1970年1月1日(UTC)。 |
shareOptions12+ | ShareOptions | 否 | 是 | 指示UnifiedData支持的设备内使用范围,非必填字段,默认值为CROSS_APP。 |
getDelayData12+ | GetDelayData | 否 | 是 | 延迟获取数据回调。当前只支持同设备剪贴板场景,后续场景待开发。非必填字段,默认值为undefined。 |
示例:
import { uniformTypeDescriptor } from '@kit.ArkData';
let properties = new unifiedDataChannel.UnifiedDataProperties();
properties.extras = {
key: {
title: 'MyTitle',
content: 'MyContent'
}
};
properties.tag = "this is tag of properties";
properties.shareOptions = unifiedDataChannel.ShareOptions.CROSS_APP;
properties.getDelayData = ((type: string) => {
if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let text = new unifiedDataChannel.Text();
text.details = {
Key: 'textKey',
Value: 'textValue',
};
let textData = new unifiedDataChannel.UnifiedData(text);
return textData;
}
return new unifiedDataChannel.UnifiedData();
});
表示UDMF统一数据对象,提供封装一组数据记录的方法。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
properties12+ | UnifiedDataProperties | 否 | 否 | 当前统一数据对象中所有数据记录的属性,包含时间戳、标签、粘贴范围以及一些附加数据等。 原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。 |
constructor()
用于创建统一数据对象。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
示例:
let unifiedData = new unifiedDataChannel.UnifiedData();
constructor(record: UnifiedRecord)
用于创建带有一条数据记录的统一数据对象。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
record | UnifiedRecord | 是 | 要添加到统一数据对象中的数据记录,该记录为UnifiedRecord或其子类对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
let text = new unifiedDataChannel.PlainText();
text.textContent = 'this is textContent of text';
let unifiedData = new unifiedDataChannel.UnifiedData(text);
addRecord(record: UnifiedRecord): void
在当前统一数据对象中添加一条数据记录。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
record | UnifiedRecord | 是 | 要添加到统一数据对象中的数据记录,该记录为UnifiedRecord子类对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
let text1 = new unifiedDataChannel.PlainText();
text1.textContent = 'this is textContent of text1';
let unifiedData = new unifiedDataChannel.UnifiedData(text1);
let text2 = new unifiedDataChannel.PlainText();
text2.textContent = 'this is textContent of text2';
unifiedData.addRecord(text2);
getRecords(): Array<UnifiedRecord>
将当前统一数据对象中的所有数据记录取出。通过本接口取出的数据为UnifiedRecord类型,需通过getType获取数据类型后转为子类再使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力 :SystemCapability.DistributedDataManager.UDMF.Core
返回值:
类型 | 说明 |
---|---|
Array<UnifiedRecord> | 当前统一数据对象内所添加的记录。 |
示例:
import { uniformTypeDescriptor } from '@kit.ArkData';
let text = new unifiedDataChannel.PlainText();
text.textContent = 'this is textContent of text';
let unifiedData = new unifiedDataChannel.UnifiedData(text);
let link = new unifiedDataChannel.Hyperlink();
link.url = 'www.XXX.com';
unifiedData.addRecord(link);
let records = unifiedData.getRecords();
for (let i = 0; i < records.length; i++) {
let record = records[i];
if (record.getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let plainText = record as unifiedDataChannel.PlainText;
console.info(`textContent: ${plainText.textContent}`);
} else if (record.getType() == uniformTypeDescriptor.UniformDataType.HYPERLINK) {
let hyperlink = record as unifiedDataChannel.Hyperlink;
console.info(`linkUrl: ${hyperlink.url}`);
}
}
hasType(type: string): boolean
检查当前统一数据对象中是否有指定的数据类型。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力 :SystemCapability.DistributedDataManager.UDMF.Core
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
type | string | 是 | 要查询的数据类型,见UniformDataType。 |
返回值:
类型 | 说明 |
---|---|
boolean | 有指定的数据类型返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { uniformTypeDescriptor } from '@kit.ArkData';
let text = new unifiedDataChannel.PlainText();
text.textContent = 'this is textContent of text';
let unifiedData = new unifiedDataChannel.UnifiedData(text);
let link = new unifiedDataChannel.Hyperlink();
link.url = 'www.XXX.com';
unifiedData.addRecord(link);
let hasPlainText = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT);
let hasLink = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.HYPERLINK);
getTypes(): Array<string>
获取当前统一数据对象所有数据记录的类型。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力 :SystemCapability.DistributedDataManager.UDMF.Core
返回值:
类型 | 说明 |
---|---|
Array<string> | UniformDataType类型的数组,表示当前统一数据对象所有数据记录对应的数据类型。 |
示例:
let text = new unifiedDataChannel.PlainText();
text.textContent = 'this is textContent of text';
let unifiedData = new unifiedDataChannel.UnifiedData(text);
let link = new unifiedDataChannel.Hyperlink();
link.url = 'www.XXX.com';
unifiedData.addRecord(link);
let types = unifiedData.getTypes();
描述某一统一数据对象的数据摘要,包括所含数据类型及大小。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
summary | Record<string, number> | 否 | 否 | 是一个字典类型对象,key表示数据类型(见UniformDataType),value为统一数据对象中该类型记录大小总和(单位:Byte)。 |
totalSize | number | 否 | 否 | 统一数据对象内记录总大小(单位:Byte)。 |
对UDMF支持的数据内容的抽象定义,称为数据记录。一个统一数据对象内包含一条或多条数据记录,例如一条文本记录、一条图片记录、一条HTML记录等。
constructor()
用于创建数据记录。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
示例:
let unifiedRecord = new unifiedDataChannel.UnifiedRecord();
constructor(type: string, value: ValueType)
用于创建指定类型和值的数据记录。
当参数value为image.PixelMap类型时,参数type必须对应为UniformDataType中OPENHARMONY_PIXEL_MAP的值;
当参数value为Want类型时,参数type必须对应为UniformDataType中OPENHARMONY_WANT的值。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
type | string | 是 | 要创建的数据记录的类型。 |
value | ValueType | 是 | 要创建的数据记录的值。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. |
示例:
import { image } from '@kit.ImageKit';
import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData';
import { Want } from '@kit.AbilityKit';
let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text');
let link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, 'www.XXX.com');
let object: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'entryAbility',
};
let wantRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_WANT, object);
const color = new ArrayBuffer(96);
let opts: image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
let pixelMap = image.createPixelMapSync(color, opts);
let pixelMapRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP, pixelMap);
let hyperlinkDetails : Record<string, string> = {
'attr1': 'value1',
'attr2': 'value2',
}
let hyperlink : uniformDataStruct.Hyperlink = {
uniformDataType:'general.hyperlink',
url : 'www.XXX.com',
description : 'This is the description of this hyperlink',
details : hyperlinkDetails,
}
let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
getType(): string
获取当前数据记录的类型。由于从统一数据对象中调用getRecords所取出的数据是UnifiedRecord对象,因此需要通过本接口查询此记录的具体类型,再将该UnifiedRecord对象转换为其子类,调用子类接口。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力 :SystemCapability.DistributedDataManager.UDMF.Core
返回值:
类型 | 说明 |
---|---|
string | 当前数据记录对应的具体数据类型,见UniformDataType。 |
示例:
import { uniformTypeDescriptor } from '@kit.ArkData';
let text = new unifiedDataChannel.PlainText();
text.textContent = 'this is textContent of text';
let unifiedData = new unifiedDataChannel.UnifiedData(text);
let records = unifiedData.getRecords();
if (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let plainText = records[0] as unifiedDataChannel.PlainText;
console.info(`textContent: ${plainText.textContent}`);
}
getValue(): ValueType
获取当前数据记录的值。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力 :SystemCapability.DistributedDataManager.UDMF.Core
返回值:
类型 | 说明 |
---|---|
ValueType | 当前数据记录对应的值。 |
示例:
import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData';
let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text');
let value = text.getValue();
let hyperlinkDetails : Record<string, string> = {
'attr1': 'value1',
'attr2': 'value2',
}
let hyperlink : uniformDataStruct.Hyperlink = {
uniformDataType:'general.hyperlink',
url : 'www.XXX.com',
description : 'This is the description of this hyperlink',
details : hyperlinkDetails,
}
let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
let hyperlinkValue = hyperlinkRecord.getValue();
文本类型数据,是UnifiedRecord的子类,也是文本类型数据的基类,用于描述文本类数据,推荐开发者优先使用Text的子类描述数据,如PlainText、Hyperlink、HTML等具体子类。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
details | Record<string, string> | 否 | 是 | 是一个字典类型对象,key和value都是string类型,用于描述文本内容。例如,可生成一个details内容为 { "title":"标题", "content":"内容" } 的数据对象,用于描述一篇文章。非必填字段,默认值为空字典对象。 |
示例:
let text = new unifiedDataChannel.Text();
text.details = {
title: 'MyTitle',
content: 'this is content',
};
let unifiedData = new unifiedDataChannel.UnifiedData(text);
纯文本类型数据,是Text的子类,用于描述纯文本类数据。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
textContent | string | 否 | 否 | 纯文本内容。 |
abstract | string | 否 | 是 | 纯文本摘要,非必填字段,默认值为空字符串。 |
示例:
let text = new unifiedDataChannel.PlainText();
text.textContent = 'this is textContent';
text.abstract = 'this is abstract';
超链接类型数据,是Text的子类,用于描述超链接类型数据。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
url | string | 否 | 否 | 链接url。 |
description | string | 否 | 是 | 链接内容描述,非必填字段,默认值为空字符串。 |
示例:
let link = new unifiedDataChannel.Hyperlink();
link.url = 'www.XXX.com';
link.description = 'this is description';
HTML类型数据,是Text的子类,用于描述超文本标记语言数据。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
htmlContent | string | 否 | 否 | html格式内容。 |
plainContent | string | 否 | 是 | 去除html标签后的纯文本内容,非必填字段,默认值为空字符串。 |
示例:
let html = new unifiedDataChannel.HTML();
html.htmlContent = '<div><p>标题</p></div>';
html.plainContent = 'this is plainContent';
File类型数据,是UnifiedRecord的子类,也是文件类型数据的基类,用于描述文件类型数据,推荐开发者优先使用File的子类描述数据,如Image、Video、Folder等具体子类。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
details | Record<string, string> | 否 | 是 | 是一个字典类型对象,key和value都是string类型,用于描述文件相关信息。例如,可生成一个details内容为 { "name":"文件名", "type":"文件类型" } 的数据对象,用于描述一个文件。非必填字段,默认值为空字典对象。 |
uri | string | 否 | 否 | 文件数据uri。 |
示例:
let file = new unifiedDataChannel.File();
file.details = {
name: 'test',
type: 'txt',
};
file.uri = 'schema://com.samples.test/files/test.txt';
图片类型数据,是File的子类,用于描述图片文件。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
imageUri | string | 否 | 否 | 图片数据uri。 |
示例:
let image = new unifiedDataChannel.Image();
image.imageUri = 'schema://com.samples.test/files/test.jpg';
视频类型数据,是File的子类,用于描述视频文件。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
videoUri | string | 否 | 否 | 视频数据uri。 |
示例:
let video = new unifiedDataChannel.Video();
video.videoUri = 'schema://com.samples.test/files/test.mp4';
音频类型数据,是File的子类,用于描述音频文件。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
audioUri | string | 否 | 否 | 音频数据uri。 |
示例:
let audio = new unifiedDataChannel.Audio();
audio.audioUri = 'schema://com.samples.test/files/test.mp3';
文件夹类型数据,是File的子类,用于描述文件夹。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
folderUri | string | 否 | 否 | 文件夹uri。 |
示例:
let folder = new unifiedDataChannel.Folder();
folder.folderUri = 'schema://com.samples.test/files/folder/';
SystemDefinedRecord是UnifiedRecord的子类,也是OpenHarmony系统特有数据类型的基类,用于描述仅在OpenHarmony系统范围内流通的特有数据类型,推荐开发者优先使用SystemDefinedRecord的子类描述数据,如SystemDefinedForm、SystemDefinedAppItem、SystemDefinedPixelMap等具体子类。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
details | Record<string, number | string | Uint8Array> | 否 | 是 | 是一个字典类型对象,key是string类型,value可以写入number(数值类型)、string(字符串类型)、Uint8Array(二进制字节数组)类型数据。非必填字段,默认值为空字典对象。 |
示例:
let sdr = new unifiedDataChannel.SystemDefinedRecord();
let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
sdr.details = {
title: 'recordTitle',
version: 1,
content: u8Array,
};
let unifiedData = new unifiedDataChannel.UnifiedData(sdr);
系统定义的桌面卡片类型数据,是SystemDefinedRecord的子类。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
formId | number | 否 | 否 | 卡片id。 |
formName | string | 否 | 否 | 卡片名称。 |
bundleName | string | 否 | 否 | 卡片所属的bundle名。 |
abilityName | string | 否 | 否 | 卡片对应的ability名。 |
module | string | 否 | 否 | 卡片所属的module名。 |
示例:
let form = new unifiedDataChannel.SystemDefinedForm();
form.formId = 123456;
form.formName = 'MyFormName';
form.bundleName = 'MyBundleName';
form.abilityName = 'MyAbilityName';
form.module = 'MyModule';
let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
form.details = {
formKey1: 123,
formKey2: 'formValue',
formKey3: u8Array,
};
let unifiedData = new unifiedDataChannel.UnifiedData(form);
系统定义的桌面图标类型数据,是SystemDefinedRecord的子类。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
appId | string | 否 | 否 | 图标对应的应用id。 |
appName | string | 否 | 否 | 图标对应的应用名。 |
appIconId | string | 否 | 否 | 图标的图片id。 |
appLabelId | string | 否 | 否 | 图标名称对应的标签id。 |
bundleName | string | 否 | 否 | 图标对应的应用bundle名。 |
abilityName | string | 否 | 否 | 图标对应的应用ability名。 |
示例:
let appItem = new unifiedDataChannel.SystemDefinedAppItem();
appItem.appId = 'MyAppId';
appItem.appName = 'MyAppName';
appItem.appIconId = 'MyAppIconId';
appItem.appLabelId = 'MyAppLabelId';
appItem.bundleName = 'MyBundleName';
appItem.abilityName = 'MyAbilityName';
let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
appItem.details = {
appItemKey1: 123,
appItemKey2: 'appItemValue',
appItemKey3: u8Array,
};
let unifiedData = new unifiedDataChannel.UnifiedData(appItem);
与系统侧定义的PixelMap数据类型对应的图片数据类型,是SystemDefinedRecord的子类,仅保存PixelMap的二进制数据。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
rawData | Uint8Array | 否 | 否 | PixelMap对象的二进制数据。 |
示例:
import { image } from '@kit.ImageKit'; // PixelMap类定义所在模块
import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
const color = new ArrayBuffer(96); // 创建pixelmap对象
let opts: image.InitializationOptions = {
editable: true, pixelFormat: 3, size: {
height: 4, width: 6
}
}
image.createPixelMap(color, opts, (error, pixelmap) => {
if (error) {
console.error('Failed to create pixelmap.');
} else {
console.info('Succeeded in creating pixelmap.');
let arrayBuf = new ArrayBuffer(pixelmap.getPixelBytesNumber());
pixelmap.readPixelsToBuffer(arrayBuf);
let u8Array = new Uint8Array(arrayBuf);
let sdpixel = new unifiedDataChannel.SystemDefinedPixelMap();
sdpixel.rawData = u8Array;
let unifiedData = new unifiedDataChannel.UnifiedData(sdpixel);
// 从unifiedData中读取pixelMap类型的record
let records = unifiedData.getRecords();
for (let i = 0; i < records.length; i++) {
if (records[i].getType() === uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP) {
let pixelmapRecord = records[i] as unifiedDataChannel.SystemDefinedPixelMap;
let newArraybuf = pixelmapRecord.rawData.buffer;
pixelmap.writeBufferToPixels(newArraybuf).then(() => {
console.info('Succeeded in writing data from buffer to a pixelMap');
}).catch((error: BusinessError) => {
console.error(`Failed to write data from a buffer to a PixelMap. code is ${error.code}, message is ${error.message}`);
})
}
}
}
})
ApplicationDefinedRecord是UnifiedRecord的子类,也是应用自定义数据类型的基类,用于描述仅在应用生态内部流通的自定义数据类型,应用可基于此类进行自定义数据类型的扩展。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力:SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
applicationDefinedType | string | 否 | 否 | 应用自定义类型标识符,必须以'ApplicationDefined'开头。 |
rawData | Uint8Array | 否 | 否 | 应用自定义数据类型的二进制数据。 |
示例:
let record = new unifiedDataChannel.ApplicationDefinedRecord();
let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
record.applicationDefinedType = 'ApplicationDefinedType';
record.rawData = u8Array;
let unifiedData = new unifiedDataChannel.UnifiedData(record);
UDMF已经支持的数据通路枚举类型。其主要用途是标识各种UDMF数据通路所面向的不同业务场景。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
名称 | 值 | 说明 |
---|---|---|
DATA_HUB | 'DataHub' | 公共数据通路。 原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。 |
DRAG14+ | 'Drag' | 拖拽类型数据通道。 模型约束: 此接口仅可在Stage模型下使用。 |
type Options = { intention?: Intention; key?: string; }
UDMF提供的数据操作接口可选项,包含intention和key两个可选参数。无默认值,当对应接口不需要此参数时可不填,具体要求参照方法接口的参数说明。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
intention | Intention | 否 | 表示数据操作相关的数据通路类型。 |
key | string | 否 | UDMF中数据对象的唯一标识符,可通过insertData接口的返回值获取。 由udmf:/、intention、bundleName和groupId四部分组成,以'/'连接,比如:udmf://DataHub/com.ohos.test/0123456789。 其中udmf:/固定,DataHub为对应枚举的取值,com.ohos.test为包名,0123456789为随机生成的groupId。 |
insertData(options: Options, data: UnifiedData, callback: AsyncCallback<string>): void
将数据写入UDMF的公共数据通路中,并生成数据的唯一标识符,使用callback异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,仅需要intention的值。 |
data | UnifiedData | 是 | 目标数据。 |
callback | AsyncCallback<string> | 是 | 回调函数,返回写入UDMF的数据的唯一标识符key的值。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let plainText = new unifiedDataChannel.PlainText();
plainText.textContent = 'hello world!';
let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
let options: unifiedDataChannel.Options = {
intention: unifiedDataChannel.Intention.DATA_HUB
}
try {
unifiedDataChannel.insertData(options, unifiedData, (err, data) => {
if (err === undefined) {
console.info(`Succeeded in inserting data. key = ${data}`);
} else {
console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `);
}
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
}
insertData(options: Options, data: UnifiedData): Promise<string>
将数据写入UDMF的公共数据通路中,并生成数据的唯一标识符,使用Promise异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,仅需要intention的值。 |
data | UnifiedData | 是 | 目标数据。 |
返回值:
类型 | 说明 |
---|---|
Promise<string> | Promise对象,返回写入UDMF的数据的唯一标识符key的值。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let plainText = new unifiedDataChannel.PlainText();
plainText.textContent = 'hello world!';
let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
let options: unifiedDataChannel.Options = {
intention: unifiedDataChannel.Intention.DATA_HUB
}
try {
unifiedDataChannel.insertData(options, unifiedData).then((data) => {
console.info(`Succeeded in inserting data. key = ${data}`);
}).catch((err: BusinessError) => {
console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `);
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
}
updateData(options: Options, data: UnifiedData, callback: AsyncCallback<void>): void
更新已写入UDMF的公共数据通路的数据,使用callback异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,仅需要key的值。 |
data | UnifiedData | 是 | 目标数据。 |
callback | AsyncCallback<void> | 是 | 回调函数。当更新数据成功,err为undefined,否则为错误对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let plainText = new unifiedDataChannel.PlainText();
plainText.textContent = 'hello world!';
let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
let options: unifiedDataChannel.Options = {
key: 'udmf://DataHub/com.ohos.test/0123456789'
};
try {
unifiedDataChannel.updateData(options, unifiedData, (err) => {
if (err === undefined) {
console.info('Succeeded in updating data.');
} else {
console.error(`Failed to update data. code is ${err.code},message is ${err.message} `);
}
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `);
}
updateData(options: Options, data: UnifiedData): Promise<void>
更新已写入UDMF的公共数据通路的数据,使用Promise异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,仅需要key的值。 |
data | UnifiedData | 是 | 目标数据。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let plainText = new unifiedDataChannel.PlainText();
plainText.textContent = 'hello world!';
let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
let options: unifiedDataChannel.Options = {
key: 'udmf://DataHub/com.ohos.test/0123456789'
};
try {
unifiedDataChannel.updateData(options, unifiedData).then(() => {
console.info('Succeeded in updating data.');
}).catch((err: BusinessError) => {
console.error(`Failed to update data. code is ${err.code},message is ${err.message} `);
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `);
}
queryData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void
查询UDMF公共数据通路的数据,使用callback异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 |
callback | AsyncCallback<Array<UnifiedData>> | 是 | 回调函数,返回查询到的所有数据。 如果options中填入的是key,则返回key对应的数据。 如果options中填入的是intention,则返回intention下所有数据。 如intention和key均填写了,取两者查询数据的交集,与options只填入key的获取结果一致;如没有交集报错。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let options: unifiedDataChannel.Options = {
intention: unifiedDataChannel.Intention.DATA_HUB
};
try {
unifiedDataChannel.queryData(options, (err, data) => {
if (err === undefined) {
console.info(`Succeeded in querying data. size = ${data.length}`);
for (let i = 0; i < data.length; i++) {
let records = data[i].getRecords();
for (let j = 0; j < records.length; j++) {
if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let text = records[j] as unifiedDataChannel.PlainText;
console.info(`${i + 1}.${text.textContent}`);
}
}
}
} else {
console.error(`Failed to query data. code is ${err.code},message is ${err.message} `);
}
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `);
}
queryData(options: Options): Promise<Array<UnifiedData>>
查询UDMF公共数据通路的数据,使用Promise异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 |
返回值:
类型 | 说明 |
---|---|
Promise<Array<UnifiedData>> | Promise对象,返回查询到的所有数据。 如果options中填入的是key,则返回key对应的数据。 如果options中填入的是intention,则返回intention下所有数据。 如intention和key均填写了,取两者查询数据的交集,与options只填入key的获取结果一致;如没有交集报错。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let options: unifiedDataChannel.Options = {
key: 'udmf://DataHub/com.ohos.test/0123456789'
};
try {
unifiedDataChannel.queryData(options).then((data) => {
console.info(`Succeeded in querying data. size = ${data.length}`);
for (let i = 0; i < data.length; i++) {
let records = data[i].getRecords();
for (let j = 0; j < records.length; j++) {
if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let text = records[j] as unifiedDataChannel.PlainText;
console.info(`${i + 1}.${text.textContent}`);
}
}
}
}).catch((err: BusinessError) => {
console.error(`Failed to query data. code is ${err.code},message is ${err.message} `);
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `);
}
deleteData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void
删除UDMF公共数据通路的数据,返回删除的数据集,使用callback异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 |
callback | AsyncCallback<Array<UnifiedData>> | 是 | 回调函数,返回删除的所有数据。 如果options中填入的是key,则删除key对应的数据并返回该数据。 如果options中填入的是intention,则删除intention下所有数据并返回删除的数据。 如intention和key均填写了,取两者数据的交集进行删除,并返回删除的数据,与options只填入key的结果一致;如没有交集报错。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let options: unifiedDataChannel.Options = {
intention: unifiedDataChannel.Intention.DATA_HUB
};
try {
unifiedDataChannel.deleteData(options, (err, data) => {
if (err === undefined) {
console.info(`Succeeded in deleting data. size = ${data.length}`);
for (let i = 0; i < data.length; i++) {
let records = data[i].getRecords();
for (let j = 0; j < records.length; j++) {
if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let text = records[j] as unifiedDataChannel.PlainText;
console.info(`${i + 1}.${text.textContent}`);
}
}
}
} else {
console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `);
}
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `);
}
deleteData(options: Options): Promise<Array<UnifiedData>>
删除UDMF公共数据通路的数据,返回删除的数据集,使用Promise异步回调。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | Options | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 |
返回值:
类型 | 说明 |
---|---|
Promise<Array<UnifiedData>> | Promise对象,返回删除的所有数据。 如果options中填入的是key,则删除key对应的数据并返回该数据。 如果options中填入的是intention,则删除intention下所有数据并返回删除的数据。 如intention和key均填写了,取两者数据的交集进行删除,并返回删除的数据,与options只填入key的结果一致;如没有交集报错。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. |
示例:
import { unifiedDataChannel } from '@kit.ArkData';
import { uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
let options: unifiedDataChannel.Options = {
key: 'udmf://DataHub/com.ohos.test/0123456789'
};
try {
unifiedDataChannel.deleteData(options).then((data) => {
console.info(`Succeeded in deleting data. size = ${data.length}`);
for (let i = 0; i < data.length; i++) {
let records = data[i].getRecords();
for (let j = 0; j < records.length; j++) {
if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
let text = records[j] as unifiedDataChannel.PlainText;
console.info(`${i + 1}.${text.textContent}`);
}
}
}
}).catch((err: BusinessError) => {
console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `);
});
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `);
}
setAppShareOptions(intention: Intention, shareOptions: ShareOptions): void
设置应用内拖拽通道数据可使用的范围ShareOptions,目前仅支持DRAG类型数据通道的管控设置。
需要权限: ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
intention | Intention | 是 | 表示数据操作相关的数据通路类型,目前仅支持DRAG类型数据通道。 |
shareOptions | ShareOptions | 是 | 指示UnifiedData支持的设备内使用范围。 |
错误码:
以下错误码的详细介绍请参见通用错误码和统一数据管理框架错误码。
错误码ID | 错误信息 |
---|---|
201 | Permission denied. Interface caller does not have permission "ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION". |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
20400001 | Settings already exist. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
try {
unifiedDataChannel.setAppShareOptions(unifiedDataChannel.Intention.DRAG, unifiedDataChannel.ShareOptions.IN_APP);
console.info(`[UDMF]setAppShareOptions success. `);
}catch (e){
let error: BusinessError = e as BusinessError;
console.error(`[UDMF]setAppShareOptions throws an exception. code is ${error.code},message is ${error.message} `);
}
removeAppShareOptions(intention: Intention): void
清除setAppShareOptions设置的管控信息。
需要权限: ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.DistributedDataManager.UDMF.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
intention | Intention | 是 | 表示数据操作相关的数据通路类型,目前仅支持DRAG类型数据通道。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
201 | Permission denied. Interface caller does not have permission "ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION". |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
try {
unifiedDataChannel.removeAppShareOptions(unifiedDataChannel.Intention.DRAG);
console.info(`[UDMF]removeAppShareOptions success. `);
}catch (e){
let error: BusinessError = e as BusinessError;
console.error(`[UDMF]removeAppShareOptions throws an exception. code is ${error.code},message is ${error.message} `);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。