1e41f4b71Sopenharmony_ci# @ohos.data.unifiedDataChannel (标准化数据通路) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块为统一数据管理框架(Unified Data Management Framework,UDMF)的组成部分,针对多对多跨应用数据共享的不同业务场景提供了标准化的数据通路,提供了标准化的数据接入与读取接口。同时对文本、图片等数据类型提供了标准化定义,方便不同应用间进行数据交互,减少数据类型适配的工作量。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 导入模块 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## ShareOptions<sup>12+</sup> 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciUDMF支持的设备内使用范围类型枚举。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 24e41f4b71Sopenharmony_ci|-------------|---|-------------------| 25e41f4b71Sopenharmony_ci| IN_APP | 0 | 表示允许在本设备同应用内使用。 | 26e41f4b71Sopenharmony_ci| CROSS_APP | 1 | 表示允许在本设备内跨应用使用。 | 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## GetDelayData<sup>12+</sup> 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_citype GetDelayData = (type: string) => UnifiedData 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci对UnifiedData的延迟封装,支持延迟获取数据。当前只支持同设备剪贴板场景,后续场景待开发。 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**参数:** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 41e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 42e41f4b71Sopenharmony_ci| type | string | 是 | 作为延迟封装的标识。 | 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci**返回值:** 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci| 类型 | 说明 | 47e41f4b71Sopenharmony_ci| ---------------------------------------- |-------------------------| 48e41f4b71Sopenharmony_ci| [UnifiedData](#unifieddata) | 当延迟封装触发时,返回一个UnifiedData对象。 | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**示例:** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```ts 53e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_cilet getDelayData: unifiedDataChannel.GetDelayData = ((type: string) => { 56e41f4b71Sopenharmony_ci if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 57e41f4b71Sopenharmony_ci let text = new unifiedDataChannel.Text(); 58e41f4b71Sopenharmony_ci text.details = { 59e41f4b71Sopenharmony_ci Key: 'textKey', 60e41f4b71Sopenharmony_ci Value: 'textValue', 61e41f4b71Sopenharmony_ci }; 62e41f4b71Sopenharmony_ci let textData = new unifiedDataChannel.UnifiedData(text); 63e41f4b71Sopenharmony_ci return textData; 64e41f4b71Sopenharmony_ci } 65e41f4b71Sopenharmony_ci return new unifiedDataChannel.UnifiedData(); 66e41f4b71Sopenharmony_ci}); 67e41f4b71Sopenharmony_ci``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci## ValueType<sup>12+</sup> 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_citype ValueType = number | string | boolean | image.PixelMap | Want | ArrayBuffer | object | null | undefined 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci用于表示统一数据记录允许的数据字段类型。 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci| 类型 | 说明 | 80e41f4b71Sopenharmony_ci| -------- | -------- | 81e41f4b71Sopenharmony_ci| number | 表示number的类型。 | 82e41f4b71Sopenharmony_ci| string | 表示string的类型。 | 83e41f4b71Sopenharmony_ci| boolean | 表示boolean的类型。 | 84e41f4b71Sopenharmony_ci| image.PixelMap | 表示[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)的类型。 | 85e41f4b71Sopenharmony_ci| Want | 表示[Want](../apis-ability-kit/js-apis-app-ability-want.md)的类型。 | 86e41f4b71Sopenharmony_ci| ArrayBuffer | 表示ArrayBuffer的类型。 | 87e41f4b71Sopenharmony_ci| object | 表示object的类型。 | 88e41f4b71Sopenharmony_ci| null | 表示null。 | 89e41f4b71Sopenharmony_ci| undefined | 表示undefined。 | 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci## UnifiedDataProperties<sup>12+</sup> 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci定义统一数据对象中所有数据记录的属性,包含时间戳、标签、粘贴范围以及一些附加数据等。 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 100e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 101e41f4b71Sopenharmony_ci| extras<sup>12+</sup> | Record<string, object> | 否 | 是 | 是一个字典类型对象,用于设置其他附加属性数据。非必填字段,默认值为空字典对象。 | 102e41f4b71Sopenharmony_ci| tag<sup>12+</sup> | string | 否 | 是 | 用户自定义标签。非必填字段,默认值为空字符串。 | 103e41f4b71Sopenharmony_ci| timestamp<sup>12+</sup> | Date | 是 | 是 | [UnifiedData](#unifieddata)的生成时间戳。默认值为1970年1月1日(UTC)。 | 104e41f4b71Sopenharmony_ci| shareOptions<sup>12+</sup> | [ShareOptions](#shareoptions12) | 否 | 是 | 指示[UnifiedData](#unifieddata)支持的设备内使用范围,非必填字段,默认值为CROSS_APP。 | 105e41f4b71Sopenharmony_ci| getDelayData<sup>12+</sup> | [GetDelayData](#getdelaydata12) | 否 | 是 | 延迟获取数据回调。当前只支持同设备剪贴板场景,后续场景待开发。非必填字段,默认值为undefined。 | 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**示例:** 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci```ts 110e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_cilet properties = new unifiedDataChannel.UnifiedDataProperties(); 113e41f4b71Sopenharmony_ciproperties.extras = { 114e41f4b71Sopenharmony_ci key: { 115e41f4b71Sopenharmony_ci title: 'MyTitle', 116e41f4b71Sopenharmony_ci content: 'MyContent' 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci}; 119e41f4b71Sopenharmony_ciproperties.tag = "this is tag of properties"; 120e41f4b71Sopenharmony_ciproperties.shareOptions = unifiedDataChannel.ShareOptions.CROSS_APP; 121e41f4b71Sopenharmony_ciproperties.getDelayData = ((type: string) => { 122e41f4b71Sopenharmony_ci if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 123e41f4b71Sopenharmony_ci let text = new unifiedDataChannel.Text(); 124e41f4b71Sopenharmony_ci text.details = { 125e41f4b71Sopenharmony_ci Key: 'textKey', 126e41f4b71Sopenharmony_ci Value: 'textValue', 127e41f4b71Sopenharmony_ci }; 128e41f4b71Sopenharmony_ci let textData = new unifiedDataChannel.UnifiedData(text); 129e41f4b71Sopenharmony_ci return textData; 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci return new unifiedDataChannel.UnifiedData(); 132e41f4b71Sopenharmony_ci}); 133e41f4b71Sopenharmony_ci``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci## UnifiedData 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci表示UDMF统一数据对象,提供封装一组数据记录的方法。 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci### 属性 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 144e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |-------------------------------------------------------------------------------------------------| 145e41f4b71Sopenharmony_ci| properties<sup>12+</sup> | [UnifiedDataProperties](#unifieddataproperties12) | 否 | 否 | 当前统一数据对象中所有数据记录的属性,包含时间戳、标签、粘贴范围以及一些附加数据等。<br />**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci### constructor<sup>12+</sup> 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciconstructor() 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci用于创建统一数据对象。 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**示例:** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci```ts 160e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(); 161e41f4b71Sopenharmony_ci``` 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci### constructor 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ciconstructor(record: UnifiedRecord) 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci用于创建带有一条数据记录的统一数据对象。 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**参数:** 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 176e41f4b71Sopenharmony_ci| ------ | ------------------------------- | ---- |-----------------------------------------| 177e41f4b71Sopenharmony_ci| record | [UnifiedRecord](#unifiedrecord) | 是 | 要添加到统一数据对象中的数据记录,该记录为UnifiedRecord或其子类对象。 | 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**错误码:** 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 184e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 185e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**示例:** 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci```ts 190e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.PlainText(); 191e41f4b71Sopenharmony_citext.textContent = 'this is textContent of text'; 192e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text); 193e41f4b71Sopenharmony_ci``` 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci### addRecord 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciaddRecord(record: UnifiedRecord): void 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci在当前统一数据对象中添加一条数据记录。 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**参数:** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 208e41f4b71Sopenharmony_ci| ------ | ------------------------------- | ---- |---------------------------------------------| 209e41f4b71Sopenharmony_ci| record | [UnifiedRecord](#unifiedrecord) | 是 | 要添加到统一数据对象中的数据记录,该记录为UnifiedRecord子类对象。| 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**错误码:** 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 216e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 217e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci**示例:** 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci```ts 222e41f4b71Sopenharmony_cilet text1 = new unifiedDataChannel.PlainText(); 223e41f4b71Sopenharmony_citext1.textContent = 'this is textContent of text1'; 224e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text1); 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_cilet text2 = new unifiedDataChannel.PlainText(); 227e41f4b71Sopenharmony_citext2.textContent = 'this is textContent of text2'; 228e41f4b71Sopenharmony_ciunifiedData.addRecord(text2); 229e41f4b71Sopenharmony_ci``` 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci### getRecords 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_cigetRecords(): Array\<UnifiedRecord\> 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci将当前统一数据对象中的所有数据记录取出。通过本接口取出的数据为UnifiedRecord类型,需通过[getType](#gettype)获取数据类型后转为子类再使用。 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**返回值:** 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci| 类型 | 说明 | 244e41f4b71Sopenharmony_ci| ---------------------------------------- |-------------------------| 245e41f4b71Sopenharmony_ci| Array\<[UnifiedRecord](#unifiedrecord)\> | 当前统一数据对象内所添加的记录。 | 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci**示例:** 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci```ts 250e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.PlainText(); 253e41f4b71Sopenharmony_citext.textContent = 'this is textContent of text'; 254e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text); 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_cilet link = new unifiedDataChannel.Hyperlink(); 257e41f4b71Sopenharmony_cilink.url = 'www.XXX.com'; 258e41f4b71Sopenharmony_ciunifiedData.addRecord(link); 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_cilet records = unifiedData.getRecords(); 261e41f4b71Sopenharmony_cifor (let i = 0; i < records.length; i++) { 262e41f4b71Sopenharmony_ci let record = records[i]; 263e41f4b71Sopenharmony_ci if (record.getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 264e41f4b71Sopenharmony_ci let plainText = record as unifiedDataChannel.PlainText; 265e41f4b71Sopenharmony_ci console.info(`textContent: ${plainText.textContent}`); 266e41f4b71Sopenharmony_ci } else if (record.getType() == uniformTypeDescriptor.UniformDataType.HYPERLINK) { 267e41f4b71Sopenharmony_ci let hyperlink = record as unifiedDataChannel.Hyperlink; 268e41f4b71Sopenharmony_ci console.info(`linkUrl: ${hyperlink.url}`); 269e41f4b71Sopenharmony_ci } 270e41f4b71Sopenharmony_ci} 271e41f4b71Sopenharmony_ci``` 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci### hasType<sup>12+</sup> 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_cihasType(type: string): boolean 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci检查当前统一数据对象中是否有指定的数据类型。 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 284e41f4b71Sopenharmony_ci| ------ | ------------------------------- | ---- |---------------------------------------------| 285e41f4b71Sopenharmony_ci| type | string | 是 | 要查询的数据类型,见[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)。| 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci**返回值:** 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci| 类型 | 说明 | 290e41f4b71Sopenharmony_ci| ---------------------------------------- |-------------------------| 291e41f4b71Sopenharmony_ci| boolean | 有指定的数据类型返回true,否则返回false。 | 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci**错误码:** 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 298e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 299e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci**示例:** 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci```ts 304e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.PlainText(); 307e41f4b71Sopenharmony_citext.textContent = 'this is textContent of text'; 308e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text); 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_cilet link = new unifiedDataChannel.Hyperlink(); 311e41f4b71Sopenharmony_cilink.url = 'www.XXX.com'; 312e41f4b71Sopenharmony_ciunifiedData.addRecord(link); 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_cilet hasPlainText = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT); 315e41f4b71Sopenharmony_cilet hasLink = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.HYPERLINK); 316e41f4b71Sopenharmony_ci``` 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci### getTypes<sup>12+</sup> 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_cigetTypes(): Array\<string\> 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci获取当前统一数据对象所有数据记录的类型。 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci**返回值:** 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci| 类型 | 说明 | 331e41f4b71Sopenharmony_ci| ---------------------------------------- |-------------------------| 332e41f4b71Sopenharmony_ci| Array\<string\> | [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)类型的数组,表示当前统一数据对象所有数据记录对应的数据类型。 | 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci**示例:** 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci```ts 337e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.PlainText(); 338e41f4b71Sopenharmony_citext.textContent = 'this is textContent of text'; 339e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text); 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_cilet link = new unifiedDataChannel.Hyperlink(); 342e41f4b71Sopenharmony_cilink.url = 'www.XXX.com'; 343e41f4b71Sopenharmony_ciunifiedData.addRecord(link); 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_cilet types = unifiedData.getTypes(); 346e41f4b71Sopenharmony_ci``` 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci## Summary 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci描述某一统一数据对象的数据摘要,包括所含数据类型及大小。 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 357e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 358e41f4b71Sopenharmony_ci| summary | Record<string, number> | 否 | 否 | 是一个字典类型对象,key表示数据类型(见[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)),value为统一数据对象中该类型记录大小总和(单位:Byte)。 | 359e41f4b71Sopenharmony_ci| totalSize | number | 否 | 否 | 统一数据对象内记录总大小(单位:Byte)。 | 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci## UnifiedRecord 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci对UDMF支持的数据内容的抽象定义,称为数据记录。一个统一数据对象内包含一条或多条数据记录,例如一条文本记录、一条图片记录、一条HTML记录等。 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci### constructor<sup>12+</sup> 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ciconstructor() 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci用于创建数据记录。 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci**示例:** 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci```ts 378e41f4b71Sopenharmony_cilet unifiedRecord = new unifiedDataChannel.UnifiedRecord(); 379e41f4b71Sopenharmony_ci``` 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci### constructor<sup>12+</sup> 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ciconstructor(type: string, value: ValueType) 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci用于创建指定类型和值的数据记录。<br />当参数value为[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)类型时,参数type必须对应为[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)中OPENHARMONY_PIXEL_MAP的值;<br />当参数value为[Want](../apis-ability-kit/js-apis-app-ability-want.md)类型时,参数type必须对应为[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)中OPENHARMONY_WANT的值。 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci**参数:** 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 394e41f4b71Sopenharmony_ci| ------ | ------------------------------- | ---- |-----------------------------------------| 395e41f4b71Sopenharmony_ci| type | string | 是 | 要创建的数据记录的类型。 | 396e41f4b71Sopenharmony_ci| value | [ValueType](#valuetype12) | 是 | 要创建的数据记录的值。 | 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci**错误码:** 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 403e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 404e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci**示例:** 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci```ts 409e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit'; 410e41f4b71Sopenharmony_ciimport { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 411e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text'); 414e41f4b71Sopenharmony_cilet link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, 'www.XXX.com'); 415e41f4b71Sopenharmony_cilet object: Want = { 416e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 417e41f4b71Sopenharmony_ci abilityName: 'entryAbility', 418e41f4b71Sopenharmony_ci}; 419e41f4b71Sopenharmony_cilet wantRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_WANT, object); 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ciconst color = new ArrayBuffer(96); 422e41f4b71Sopenharmony_cilet opts: image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }; 423e41f4b71Sopenharmony_cilet pixelMap = image.createPixelMapSync(color, opts); 424e41f4b71Sopenharmony_cilet pixelMapRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP, pixelMap); 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_cilet hyperlinkDetails : Record<string, string> = { 427e41f4b71Sopenharmony_ci 'attr1': 'value1', 428e41f4b71Sopenharmony_ci 'attr2': 'value2', 429e41f4b71Sopenharmony_ci} 430e41f4b71Sopenharmony_cilet hyperlink : uniformDataStruct.Hyperlink = { 431e41f4b71Sopenharmony_ci uniformDataType:'general.hyperlink', 432e41f4b71Sopenharmony_ci url : 'www.XXX.com', 433e41f4b71Sopenharmony_ci description : 'This is the description of this hyperlink', 434e41f4b71Sopenharmony_ci details : hyperlinkDetails, 435e41f4b71Sopenharmony_ci} 436e41f4b71Sopenharmony_cilet hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 437e41f4b71Sopenharmony_ci``` 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ci### getType 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_cigetType(): string 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci获取当前数据记录的类型。由于从统一数据对象中调用[getRecords](#getrecords)所取出的数据是UnifiedRecord对象,因此需要通过本接口查询此记录的具体类型,再将该UnifiedRecord对象转换为其子类,调用子类接口。 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci**返回值:** 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci| 类型 | 说明 | 452e41f4b71Sopenharmony_ci| ------ |------------------------------------------------------| 453e41f4b71Sopenharmony_ci| string | 当前数据记录对应的具体数据类型,见[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)。| 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci**示例:** 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci```ts 458e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.PlainText(); 461e41f4b71Sopenharmony_citext.textContent = 'this is textContent of text'; 462e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text); 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_cilet records = unifiedData.getRecords(); 465e41f4b71Sopenharmony_ciif (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 466e41f4b71Sopenharmony_ci let plainText = records[0] as unifiedDataChannel.PlainText; 467e41f4b71Sopenharmony_ci console.info(`textContent: ${plainText.textContent}`); 468e41f4b71Sopenharmony_ci} 469e41f4b71Sopenharmony_ci``` 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci### getValue<sup>12+</sup> 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_cigetValue(): ValueType 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci获取当前数据记录的值。 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci**返回值:** 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ci| 类型 | 说明 | 484e41f4b71Sopenharmony_ci| ------ |------------------------------------------------------| 485e41f4b71Sopenharmony_ci| [ValueType](#valuetype12) | 当前数据记录对应的值。 | 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci**示例:** 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci```ts 490e41f4b71Sopenharmony_ciimport { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text'); 493e41f4b71Sopenharmony_cilet value = text.getValue(); 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_cilet hyperlinkDetails : Record<string, string> = { 496e41f4b71Sopenharmony_ci 'attr1': 'value1', 497e41f4b71Sopenharmony_ci 'attr2': 'value2', 498e41f4b71Sopenharmony_ci} 499e41f4b71Sopenharmony_cilet hyperlink : uniformDataStruct.Hyperlink = { 500e41f4b71Sopenharmony_ci uniformDataType:'general.hyperlink', 501e41f4b71Sopenharmony_ci url : 'www.XXX.com', 502e41f4b71Sopenharmony_ci description : 'This is the description of this hyperlink', 503e41f4b71Sopenharmony_ci details : hyperlinkDetails, 504e41f4b71Sopenharmony_ci} 505e41f4b71Sopenharmony_cilet hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 506e41f4b71Sopenharmony_cilet hyperlinkValue = hyperlinkRecord.getValue(); 507e41f4b71Sopenharmony_ci``` 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci## Text 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci文本类型数据,是[UnifiedRecord](#unifiedrecord)的子类,也是文本类型数据的基类,用于描述文本类数据,推荐开发者优先使用Text的子类描述数据,如[PlainText](#plaintext)、[Hyperlink](#hyperlink)、[HTML](#html)等具体子类。 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 518e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 519e41f4b71Sopenharmony_ci| details | Record<string, string> | 否 | 是 | 是一个字典类型对象,key和value都是string类型,用于描述文本内容。例如,可生成一个details内容为<br />{<br />"title":"标题",<br />"content":"内容"<br />}<br />的数据对象,用于描述一篇文章。非必填字段,默认值为空字典对象。 | 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci**示例:** 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ci```ts 524e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.Text(); 525e41f4b71Sopenharmony_citext.details = { 526e41f4b71Sopenharmony_ci title: 'MyTitle', 527e41f4b71Sopenharmony_ci content: 'this is content', 528e41f4b71Sopenharmony_ci}; 529e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(text); 530e41f4b71Sopenharmony_ci``` 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ci## PlainText 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci纯文本类型数据,是[Text](#text)的子类,用于描述纯文本类数据。 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 541e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 542e41f4b71Sopenharmony_ci| textContent | string | 否 | 否 | 纯文本内容。 | 543e41f4b71Sopenharmony_ci| abstract | string | 否 | 是 | 纯文本摘要,非必填字段,默认值为空字符串。 | 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci**示例:** 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_ci```ts 548e41f4b71Sopenharmony_cilet text = new unifiedDataChannel.PlainText(); 549e41f4b71Sopenharmony_citext.textContent = 'this is textContent'; 550e41f4b71Sopenharmony_citext.abstract = 'this is abstract'; 551e41f4b71Sopenharmony_ci``` 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci## Hyperlink 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci超链接类型数据,是[Text](#text)的子类,用于描述超链接类型数据。 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 562e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 563e41f4b71Sopenharmony_ci| url | string | 否 | 否 | 链接url。 | 564e41f4b71Sopenharmony_ci| description | string | 否 | 是 | 链接内容描述,非必填字段,默认值为空字符串。 | 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ci**示例:** 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci```ts 569e41f4b71Sopenharmony_cilet link = new unifiedDataChannel.Hyperlink(); 570e41f4b71Sopenharmony_cilink.url = 'www.XXX.com'; 571e41f4b71Sopenharmony_cilink.description = 'this is description'; 572e41f4b71Sopenharmony_ci``` 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci## HTML 575e41f4b71Sopenharmony_ci 576e41f4b71Sopenharmony_ciHTML类型数据,是[Text](#text)的子类,用于描述超文本标记语言数据。 577e41f4b71Sopenharmony_ci 578e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 581e41f4b71Sopenharmony_ci 582e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 583e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 584e41f4b71Sopenharmony_ci| htmlContent | string | 否 | 否 | html格式内容。 | 585e41f4b71Sopenharmony_ci| plainContent | string | 否 | 是 | 去除html标签后的纯文本内容,非必填字段,默认值为空字符串。 | 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci**示例:** 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci```ts 590e41f4b71Sopenharmony_cilet html = new unifiedDataChannel.HTML(); 591e41f4b71Sopenharmony_cihtml.htmlContent = '<div><p>标题</p></div>'; 592e41f4b71Sopenharmony_cihtml.plainContent = 'this is plainContent'; 593e41f4b71Sopenharmony_ci``` 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci## File 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ciFile类型数据,是[UnifiedRecord](#unifiedrecord)的子类,也是文件类型数据的基类,用于描述文件类型数据,推荐开发者优先使用File的子类描述数据,如[Image](#image)、[Video](#video)、[Folder](#folder)等具体子类。 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 604e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 605e41f4b71Sopenharmony_ci| details | Record<string, string> | 否 | 是 | 是一个字典类型对象,key和value都是string类型,用于描述文件相关信息。例如,可生成一个details内容为<br />{<br />"name":"文件名",<br />"type":"文件类型"<br />}<br />的数据对象,用于描述一个文件。非必填字段,默认值为空字典对象。 | 606e41f4b71Sopenharmony_ci| uri | string | 否 | 否 | 文件数据uri。 | 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ci**示例:** 609e41f4b71Sopenharmony_ci 610e41f4b71Sopenharmony_ci```ts 611e41f4b71Sopenharmony_cilet file = new unifiedDataChannel.File(); 612e41f4b71Sopenharmony_cifile.details = { 613e41f4b71Sopenharmony_ci name: 'test', 614e41f4b71Sopenharmony_ci type: 'txt', 615e41f4b71Sopenharmony_ci}; 616e41f4b71Sopenharmony_cifile.uri = 'schema://com.samples.test/files/test.txt'; 617e41f4b71Sopenharmony_ci``` 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci## Image 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci图片类型数据,是[File](#file)的子类,用于描述图片文件。 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 628e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 629e41f4b71Sopenharmony_ci| imageUri | string | 否 | 否 | 图片数据uri。 | 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci**示例:** 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci```ts 634e41f4b71Sopenharmony_cilet image = new unifiedDataChannel.Image(); 635e41f4b71Sopenharmony_ciimage.imageUri = 'schema://com.samples.test/files/test.jpg'; 636e41f4b71Sopenharmony_ci``` 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ci## Video 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ci视频类型数据,是[File](#file)的子类,用于描述视频文件。 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 647e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 648e41f4b71Sopenharmony_ci| videoUri | string | 否 | 否 | 视频数据uri。 | 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci**示例:** 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci```ts 653e41f4b71Sopenharmony_cilet video = new unifiedDataChannel.Video(); 654e41f4b71Sopenharmony_civideo.videoUri = 'schema://com.samples.test/files/test.mp4'; 655e41f4b71Sopenharmony_ci``` 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci## Audio 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci音频类型数据,是[File](#file)的子类,用于描述音频文件。 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 666e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 667e41f4b71Sopenharmony_ci| audioUri | string | 否 | 否 | 音频数据uri。 | 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci**示例:** 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci```ts 672e41f4b71Sopenharmony_cilet audio = new unifiedDataChannel.Audio(); 673e41f4b71Sopenharmony_ciaudio.audioUri = 'schema://com.samples.test/files/test.mp3'; 674e41f4b71Sopenharmony_ci``` 675e41f4b71Sopenharmony_ci 676e41f4b71Sopenharmony_ci## Folder 677e41f4b71Sopenharmony_ci 678e41f4b71Sopenharmony_ci文件夹类型数据,是[File](#file)的子类,用于描述文件夹。 679e41f4b71Sopenharmony_ci 680e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 681e41f4b71Sopenharmony_ci 682e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 683e41f4b71Sopenharmony_ci 684e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 685e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 686e41f4b71Sopenharmony_ci| folderUri | string | 否 | 否 | 文件夹uri。 | 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci**示例:** 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_ci```ts 691e41f4b71Sopenharmony_cilet folder = new unifiedDataChannel.Folder(); 692e41f4b71Sopenharmony_cifolder.folderUri = 'schema://com.samples.test/files/folder/'; 693e41f4b71Sopenharmony_ci``` 694e41f4b71Sopenharmony_ci 695e41f4b71Sopenharmony_ci## SystemDefinedRecord 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ciSystemDefinedRecord是[UnifiedRecord](#unifiedrecord)的子类,也是OpenHarmony系统特有数据类型的基类,用于描述仅在OpenHarmony系统范围内流通的特有数据类型,推荐开发者优先使用SystemDefinedRecord的子类描述数据,如[SystemDefinedForm](#systemdefinedform)、[SystemDefinedAppItem](#systemdefinedappitem)、[SystemDefinedPixelMap](#systemdefinedpixelmap)等具体子类。 698e41f4b71Sopenharmony_ci 699e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 702e41f4b71Sopenharmony_ci 703e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 704e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 705e41f4b71Sopenharmony_ci| details | Record<string, number \| string \| Uint8Array> | 否 | 是 | 是一个字典类型对象,key是string类型,value可以写入number(数值类型)、string(字符串类型)、Uint8Array(二进制字节数组)类型数据。非必填字段,默认值为空字典对象。| 706e41f4b71Sopenharmony_ci 707e41f4b71Sopenharmony_ci**示例:** 708e41f4b71Sopenharmony_ci 709e41f4b71Sopenharmony_ci```ts 710e41f4b71Sopenharmony_cilet sdr = new unifiedDataChannel.SystemDefinedRecord(); 711e41f4b71Sopenharmony_cilet u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 712e41f4b71Sopenharmony_cisdr.details = { 713e41f4b71Sopenharmony_ci title: 'recordTitle', 714e41f4b71Sopenharmony_ci version: 1, 715e41f4b71Sopenharmony_ci content: u8Array, 716e41f4b71Sopenharmony_ci}; 717e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(sdr); 718e41f4b71Sopenharmony_ci``` 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ci## SystemDefinedForm 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci系统定义的桌面卡片类型数据,是[SystemDefinedRecord](#systemdefinedrecord)的子类。 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 729e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 730e41f4b71Sopenharmony_ci| formId | number | 否 | 否 | 卡片id。 | 731e41f4b71Sopenharmony_ci| formName | string | 否 | 否 | 卡片名称。 | 732e41f4b71Sopenharmony_ci| bundleName | string | 否 | 否 | 卡片所属的bundle名。 | 733e41f4b71Sopenharmony_ci| abilityName | string | 否 | 否 | 卡片对应的ability名。 | 734e41f4b71Sopenharmony_ci| module | string | 否 | 否 | 卡片所属的module名。 | 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci**示例:** 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_ci```ts 739e41f4b71Sopenharmony_cilet form = new unifiedDataChannel.SystemDefinedForm(); 740e41f4b71Sopenharmony_ciform.formId = 123456; 741e41f4b71Sopenharmony_ciform.formName = 'MyFormName'; 742e41f4b71Sopenharmony_ciform.bundleName = 'MyBundleName'; 743e41f4b71Sopenharmony_ciform.abilityName = 'MyAbilityName'; 744e41f4b71Sopenharmony_ciform.module = 'MyModule'; 745e41f4b71Sopenharmony_cilet u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 746e41f4b71Sopenharmony_ciform.details = { 747e41f4b71Sopenharmony_ci formKey1: 123, 748e41f4b71Sopenharmony_ci formKey2: 'formValue', 749e41f4b71Sopenharmony_ci formKey3: u8Array, 750e41f4b71Sopenharmony_ci}; 751e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(form); 752e41f4b71Sopenharmony_ci``` 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ci## SystemDefinedAppItem 755e41f4b71Sopenharmony_ci 756e41f4b71Sopenharmony_ci系统定义的桌面图标类型数据,是[SystemDefinedRecord](#systemdefinedrecord)的子类。 757e41f4b71Sopenharmony_ci 758e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 759e41f4b71Sopenharmony_ci 760e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 763e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 764e41f4b71Sopenharmony_ci| appId | string | 否 | 否 | 图标对应的应用id。 | 765e41f4b71Sopenharmony_ci| appName | string | 否 | 否 | 图标对应的应用名。 | 766e41f4b71Sopenharmony_ci| appIconId | string | 否 | 否 | 图标的图片id。 | 767e41f4b71Sopenharmony_ci| appLabelId | string | 否 | 否 | 图标名称对应的标签id。 | 768e41f4b71Sopenharmony_ci| bundleName | string | 否 | 否 | 图标对应的应用bundle名。 | 769e41f4b71Sopenharmony_ci| abilityName | string | 否 | 否 | 图标对应的应用ability名。 | 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci**示例:** 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci```ts 774e41f4b71Sopenharmony_cilet appItem = new unifiedDataChannel.SystemDefinedAppItem(); 775e41f4b71Sopenharmony_ciappItem.appId = 'MyAppId'; 776e41f4b71Sopenharmony_ciappItem.appName = 'MyAppName'; 777e41f4b71Sopenharmony_ciappItem.appIconId = 'MyAppIconId'; 778e41f4b71Sopenharmony_ciappItem.appLabelId = 'MyAppLabelId'; 779e41f4b71Sopenharmony_ciappItem.bundleName = 'MyBundleName'; 780e41f4b71Sopenharmony_ciappItem.abilityName = 'MyAbilityName'; 781e41f4b71Sopenharmony_cilet u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 782e41f4b71Sopenharmony_ciappItem.details = { 783e41f4b71Sopenharmony_ci appItemKey1: 123, 784e41f4b71Sopenharmony_ci appItemKey2: 'appItemValue', 785e41f4b71Sopenharmony_ci appItemKey3: u8Array, 786e41f4b71Sopenharmony_ci}; 787e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(appItem); 788e41f4b71Sopenharmony_ci``` 789e41f4b71Sopenharmony_ci 790e41f4b71Sopenharmony_ci## SystemDefinedPixelMap 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci与系统侧定义的[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)数据类型对应的图片数据类型,是[SystemDefinedRecord](#systemdefinedrecord)的子类,仅保存PixelMap的二进制数据。 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 795e41f4b71Sopenharmony_ci 796e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 797e41f4b71Sopenharmony_ci 798e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 799e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 800e41f4b71Sopenharmony_ci| rawData | Uint8Array | 否 | 否 | PixelMap对象的二进制数据。 | 801e41f4b71Sopenharmony_ci 802e41f4b71Sopenharmony_ci**示例:** 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ci```ts 805e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit'; // PixelMap类定义所在模块 806e41f4b71Sopenharmony_ciimport { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData'; 807e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 808e41f4b71Sopenharmony_ci 809e41f4b71Sopenharmony_ciconst color = new ArrayBuffer(96); // 创建pixelmap对象 810e41f4b71Sopenharmony_cilet opts: image.InitializationOptions = { 811e41f4b71Sopenharmony_ci editable: true, pixelFormat: 3, size: { 812e41f4b71Sopenharmony_ci height: 4, width: 6 813e41f4b71Sopenharmony_ci } 814e41f4b71Sopenharmony_ci} 815e41f4b71Sopenharmony_ciimage.createPixelMap(color, opts, (error, pixelmap) => { 816e41f4b71Sopenharmony_ci if (error) { 817e41f4b71Sopenharmony_ci console.error('Failed to create pixelmap.'); 818e41f4b71Sopenharmony_ci } else { 819e41f4b71Sopenharmony_ci console.info('Succeeded in creating pixelmap.'); 820e41f4b71Sopenharmony_ci let arrayBuf = new ArrayBuffer(pixelmap.getPixelBytesNumber()); 821e41f4b71Sopenharmony_ci pixelmap.readPixelsToBuffer(arrayBuf); 822e41f4b71Sopenharmony_ci let u8Array = new Uint8Array(arrayBuf); 823e41f4b71Sopenharmony_ci let sdpixel = new unifiedDataChannel.SystemDefinedPixelMap(); 824e41f4b71Sopenharmony_ci sdpixel.rawData = u8Array; 825e41f4b71Sopenharmony_ci let unifiedData = new unifiedDataChannel.UnifiedData(sdpixel); 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci // 从unifiedData中读取pixelMap类型的record 828e41f4b71Sopenharmony_ci let records = unifiedData.getRecords(); 829e41f4b71Sopenharmony_ci for (let i = 0; i < records.length; i++) { 830e41f4b71Sopenharmony_ci if (records[i].getType() === uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP) { 831e41f4b71Sopenharmony_ci let pixelmapRecord = records[i] as unifiedDataChannel.SystemDefinedPixelMap; 832e41f4b71Sopenharmony_ci let newArraybuf = pixelmapRecord.rawData.buffer; 833e41f4b71Sopenharmony_ci pixelmap.writeBufferToPixels(newArraybuf).then(() => { 834e41f4b71Sopenharmony_ci console.info('Succeeded in writing data from buffer to a pixelMap'); 835e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 836e41f4b71Sopenharmony_ci console.error(`Failed to write data from a buffer to a PixelMap. code is ${error.code}, message is ${error.message}`); 837e41f4b71Sopenharmony_ci }) 838e41f4b71Sopenharmony_ci } 839e41f4b71Sopenharmony_ci } 840e41f4b71Sopenharmony_ci } 841e41f4b71Sopenharmony_ci}) 842e41f4b71Sopenharmony_ci``` 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ci## ApplicationDefinedRecord 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ciApplicationDefinedRecord是[UnifiedRecord](#unifiedrecord)的子类,也是应用自定义数据类型的基类,用于描述仅在应用生态内部流通的自定义数据类型,应用可基于此类进行自定义数据类型的扩展。 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 853e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 854e41f4b71Sopenharmony_ci| applicationDefinedType | string | 否 | 否 | 应用自定义类型标识符,必须以'ApplicationDefined'开头。 | 855e41f4b71Sopenharmony_ci| rawData | Uint8Array | 否 | 否 | 应用自定义数据类型的二进制数据。 | 856e41f4b71Sopenharmony_ci 857e41f4b71Sopenharmony_ci**示例:** 858e41f4b71Sopenharmony_ci 859e41f4b71Sopenharmony_ci```ts 860e41f4b71Sopenharmony_cilet record = new unifiedDataChannel.ApplicationDefinedRecord(); 861e41f4b71Sopenharmony_cilet u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 862e41f4b71Sopenharmony_cirecord.applicationDefinedType = 'ApplicationDefinedType'; 863e41f4b71Sopenharmony_cirecord.rawData = u8Array; 864e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(record); 865e41f4b71Sopenharmony_ci``` 866e41f4b71Sopenharmony_ci 867e41f4b71Sopenharmony_ci## Intention 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ciUDMF已经支持的数据通路枚举类型。其主要用途是标识各种UDMF数据通路所面向的不同业务场景。 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 874e41f4b71Sopenharmony_ci|----------|-----------|---------| 875e41f4b71Sopenharmony_ci| DATA_HUB | 'DataHub' | 公共数据通路。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 876e41f4b71Sopenharmony_ci 877e41f4b71Sopenharmony_ci## Options 878e41f4b71Sopenharmony_ci 879e41f4b71Sopenharmony_ciUDMF提供的数据操作接口可选项,包含intention和key两个可选参数。无默认值,当对应接口不需要此参数时可不填,具体要求参照方法接口的参数说明。 880e41f4b71Sopenharmony_ci 881e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 882e41f4b71Sopenharmony_ci 883e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 884e41f4b71Sopenharmony_ci 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 887e41f4b71Sopenharmony_ci|-----------|-------------------------|----|----|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 888e41f4b71Sopenharmony_ci| intention | [Intention](#intention) | 否 | 是 | 表示数据操作相关的数据通路类型。 | 889e41f4b71Sopenharmony_ci| key | string | 否 | 是 | UDMF中数据对象的唯一标识符,可通过[insertData](#unifieddatachannelinsertdata)接口的返回值获取。<br>由udmf:/、intention、bundleName和groupId四部分组成,以'/'连接,比如:udmf://DataHub/com.ohos.test/0123456789。<br>其中udmf:/固定,DataHub为对应枚举的取值,com.ohos.test为包名,0123456789为随机生成的groupId。 | 890e41f4b71Sopenharmony_ci 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci 893e41f4b71Sopenharmony_ci## unifiedDataChannel.insertData 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ciinsertData(options: Options, data: UnifiedData, callback: AsyncCallback<string>): void 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci将数据写入UDMF的公共数据通路中,并生成数据的唯一标识符,使用callback异步回调。 898e41f4b71Sopenharmony_ci 899e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 900e41f4b71Sopenharmony_ci 901e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci**参数:** 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 906e41f4b71Sopenharmony_ci|----------|----------------------------|----|------------------------------| 907e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,仅需要intention的值。 | 908e41f4b71Sopenharmony_ci| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 909e41f4b71Sopenharmony_ci| callback | AsyncCallback<string> | 是 | 回调函数,返回写入UDMF的数据的唯一标识符key的值。 | 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci**错误码:** 912e41f4b71Sopenharmony_ci 913e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 914e41f4b71Sopenharmony_ci 915e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 916e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 917e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 918e41f4b71Sopenharmony_ci 919e41f4b71Sopenharmony_ci**示例:** 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci```ts 922e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 923e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_cilet plainText = new unifiedDataChannel.PlainText(); 926e41f4b71Sopenharmony_ciplainText.textContent = 'hello world!'; 927e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(plainText); 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 930e41f4b71Sopenharmony_ci intention: unifiedDataChannel.Intention.DATA_HUB 931e41f4b71Sopenharmony_ci} 932e41f4b71Sopenharmony_citry { 933e41f4b71Sopenharmony_ci unifiedDataChannel.insertData(options, unifiedData, (err, data) => { 934e41f4b71Sopenharmony_ci if (err === undefined) { 935e41f4b71Sopenharmony_ci console.info(`Succeeded in inserting data. key = ${data}`); 936e41f4b71Sopenharmony_ci } else { 937e41f4b71Sopenharmony_ci console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 938e41f4b71Sopenharmony_ci } 939e41f4b71Sopenharmony_ci }); 940e41f4b71Sopenharmony_ci } catch (e) { 941e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 942e41f4b71Sopenharmony_ci console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 943e41f4b71Sopenharmony_ci} 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ci``` 946e41f4b71Sopenharmony_ci 947e41f4b71Sopenharmony_ci## unifiedDataChannel.insertData 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ciinsertData(options: Options, data: UnifiedData): Promise<string> 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_ci将数据写入UDMF的公共数据通路中,并生成数据的唯一标识符,使用Promise异步回调。 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci**参数:** 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 960e41f4b71Sopenharmony_ci|---------|-----------------------------|----|-----------------------| 961e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,仅需要intention的值。 | 962e41f4b71Sopenharmony_ci| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 963e41f4b71Sopenharmony_ci 964e41f4b71Sopenharmony_ci**返回值:** 965e41f4b71Sopenharmony_ci 966e41f4b71Sopenharmony_ci| 类型 | 说明 | 967e41f4b71Sopenharmony_ci|-----------------------|-----------------------------------| 968e41f4b71Sopenharmony_ci| Promise<string> | Promise对象,返回写入UDMF的数据的唯一标识符key的值。 | 969e41f4b71Sopenharmony_ci 970e41f4b71Sopenharmony_ci**错误码:** 971e41f4b71Sopenharmony_ci 972e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 973e41f4b71Sopenharmony_ci 974e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 975e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 976e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 977e41f4b71Sopenharmony_ci 978e41f4b71Sopenharmony_ci**示例:** 979e41f4b71Sopenharmony_ci 980e41f4b71Sopenharmony_ci```ts 981e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 982e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_cilet plainText = new unifiedDataChannel.PlainText(); 985e41f4b71Sopenharmony_ciplainText.textContent = 'hello world!'; 986e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(plainText); 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 989e41f4b71Sopenharmony_ci intention: unifiedDataChannel.Intention.DATA_HUB 990e41f4b71Sopenharmony_ci} 991e41f4b71Sopenharmony_citry { 992e41f4b71Sopenharmony_ci unifiedDataChannel.insertData(options, unifiedData).then((data) => { 993e41f4b71Sopenharmony_ci console.info(`Succeeded in inserting data. key = ${data}`); 994e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 995e41f4b71Sopenharmony_ci console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 996e41f4b71Sopenharmony_ci }); 997e41f4b71Sopenharmony_ci} catch (e) { 998e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 999e41f4b71Sopenharmony_ci console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 1000e41f4b71Sopenharmony_ci} 1001e41f4b71Sopenharmony_ci``` 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ci## unifiedDataChannel.updateData 1004e41f4b71Sopenharmony_ci 1005e41f4b71Sopenharmony_ciupdateData(options: Options, data: UnifiedData, callback: AsyncCallback<void>): void 1006e41f4b71Sopenharmony_ci 1007e41f4b71Sopenharmony_ci更新已写入UDMF的公共数据通路的数据,使用callback异步回调。 1008e41f4b71Sopenharmony_ci 1009e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1010e41f4b71Sopenharmony_ci 1011e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 1012e41f4b71Sopenharmony_ci 1013e41f4b71Sopenharmony_ci**参数:** 1014e41f4b71Sopenharmony_ci 1015e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1016e41f4b71Sopenharmony_ci|----------|-----------------------------|----|-------------------------------------| 1017e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,仅需要key的值。 | 1018e41f4b71Sopenharmony_ci| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 1019e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当更新数据成功,err为undefined,否则为错误对象。 | 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ci**错误码:** 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 1026e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 1027e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci**示例:** 1030e41f4b71Sopenharmony_ci 1031e41f4b71Sopenharmony_ci```ts 1032e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 1033e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_cilet plainText = new unifiedDataChannel.PlainText(); 1036e41f4b71Sopenharmony_ciplainText.textContent = 'hello world!'; 1037e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(plainText); 1038e41f4b71Sopenharmony_ci 1039e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 1040e41f4b71Sopenharmony_ci key: 'udmf://DataHub/com.ohos.test/0123456789' 1041e41f4b71Sopenharmony_ci}; 1042e41f4b71Sopenharmony_ci 1043e41f4b71Sopenharmony_citry { 1044e41f4b71Sopenharmony_ci unifiedDataChannel.updateData(options, unifiedData, (err) => { 1045e41f4b71Sopenharmony_ci if (err === undefined) { 1046e41f4b71Sopenharmony_ci console.info('Succeeded in updating data.'); 1047e41f4b71Sopenharmony_ci } else { 1048e41f4b71Sopenharmony_ci console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 1049e41f4b71Sopenharmony_ci } 1050e41f4b71Sopenharmony_ci }); 1051e41f4b71Sopenharmony_ci} catch (e) { 1052e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 1053e41f4b71Sopenharmony_ci console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 1054e41f4b71Sopenharmony_ci} 1055e41f4b71Sopenharmony_ci``` 1056e41f4b71Sopenharmony_ci 1057e41f4b71Sopenharmony_ci## unifiedDataChannel.updateData 1058e41f4b71Sopenharmony_ci 1059e41f4b71Sopenharmony_ciupdateData(options: Options, data: UnifiedData): Promise<void> 1060e41f4b71Sopenharmony_ci 1061e41f4b71Sopenharmony_ci更新已写入UDMF的公共数据通路的数据,使用Promise异步回调。 1062e41f4b71Sopenharmony_ci 1063e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 1066e41f4b71Sopenharmony_ci 1067e41f4b71Sopenharmony_ci**参数:** 1068e41f4b71Sopenharmony_ci 1069e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1070e41f4b71Sopenharmony_ci|---------|-----------------------------|----|-----------------| 1071e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,仅需要key的值。 | 1072e41f4b71Sopenharmony_ci| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 1073e41f4b71Sopenharmony_ci 1074e41f4b71Sopenharmony_ci**返回值:** 1075e41f4b71Sopenharmony_ci 1076e41f4b71Sopenharmony_ci| 类型 | 说明 | 1077e41f4b71Sopenharmony_ci|---------------------|----------------------------| 1078e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1079e41f4b71Sopenharmony_ci 1080e41f4b71Sopenharmony_ci**错误码:** 1081e41f4b71Sopenharmony_ci 1082e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1083e41f4b71Sopenharmony_ci 1084e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 1085e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 1086e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1087e41f4b71Sopenharmony_ci 1088e41f4b71Sopenharmony_ci**示例:** 1089e41f4b71Sopenharmony_ci 1090e41f4b71Sopenharmony_ci```ts 1091e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 1092e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_cilet plainText = new unifiedDataChannel.PlainText(); 1095e41f4b71Sopenharmony_ciplainText.textContent = 'hello world!'; 1096e41f4b71Sopenharmony_cilet unifiedData = new unifiedDataChannel.UnifiedData(plainText); 1097e41f4b71Sopenharmony_ci 1098e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 1099e41f4b71Sopenharmony_ci key: 'udmf://DataHub/com.ohos.test/0123456789' 1100e41f4b71Sopenharmony_ci}; 1101e41f4b71Sopenharmony_ci 1102e41f4b71Sopenharmony_citry { 1103e41f4b71Sopenharmony_ci unifiedDataChannel.updateData(options, unifiedData).then(() => { 1104e41f4b71Sopenharmony_ci console.info('Succeeded in updating data.'); 1105e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1106e41f4b71Sopenharmony_ci console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 1107e41f4b71Sopenharmony_ci }); 1108e41f4b71Sopenharmony_ci} catch (e) { 1109e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 1110e41f4b71Sopenharmony_ci console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 1111e41f4b71Sopenharmony_ci} 1112e41f4b71Sopenharmony_ci``` 1113e41f4b71Sopenharmony_ci 1114e41f4b71Sopenharmony_ci## unifiedDataChannel.queryData 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ciqueryData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ci查询UDMF公共数据通路的数据,使用callback异步回调。 1119e41f4b71Sopenharmony_ci 1120e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1121e41f4b71Sopenharmony_ci 1122e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 1123e41f4b71Sopenharmony_ci 1124e41f4b71Sopenharmony_ci**参数:** 1125e41f4b71Sopenharmony_ci 1126e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1127e41f4b71Sopenharmony_ci|----------|---------------------------------------------------------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1128e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 1129e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | 是 | 回调函数,返回查询到的所有数据。<br>如果options中填入的是key,则返回key对应的数据。<br>如果options中填入的是intention,则返回intention下所有数据。<br>如intention和key均填写了,取两者查询数据的交集,与options只填入key的获取结果一致;如没有交集报错。 | 1130e41f4b71Sopenharmony_ci 1131e41f4b71Sopenharmony_ci**错误码:** 1132e41f4b71Sopenharmony_ci 1133e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1134e41f4b71Sopenharmony_ci 1135e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 1136e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 1137e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1138e41f4b71Sopenharmony_ci 1139e41f4b71Sopenharmony_ci**示例:** 1140e41f4b71Sopenharmony_ci 1141e41f4b71Sopenharmony_ci```ts 1142e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 1143e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 1144e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 1147e41f4b71Sopenharmony_ci intention: unifiedDataChannel.Intention.DATA_HUB 1148e41f4b71Sopenharmony_ci}; 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_citry { 1151e41f4b71Sopenharmony_ci unifiedDataChannel.queryData(options, (err, data) => { 1152e41f4b71Sopenharmony_ci if (err === undefined) { 1153e41f4b71Sopenharmony_ci console.info(`Succeeded in querying data. size = ${data.length}`); 1154e41f4b71Sopenharmony_ci for (let i = 0; i < data.length; i++) { 1155e41f4b71Sopenharmony_ci let records = data[i].getRecords(); 1156e41f4b71Sopenharmony_ci for (let j = 0; j < records.length; j++) { 1157e41f4b71Sopenharmony_ci if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1158e41f4b71Sopenharmony_ci let text = records[j] as unifiedDataChannel.PlainText; 1159e41f4b71Sopenharmony_ci console.info(`${i + 1}.${text.textContent}`); 1160e41f4b71Sopenharmony_ci } 1161e41f4b71Sopenharmony_ci } 1162e41f4b71Sopenharmony_ci } 1163e41f4b71Sopenharmony_ci } else { 1164e41f4b71Sopenharmony_ci console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 1165e41f4b71Sopenharmony_ci } 1166e41f4b71Sopenharmony_ci }); 1167e41f4b71Sopenharmony_ci} catch (e) { 1168e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 1169e41f4b71Sopenharmony_ci console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 1170e41f4b71Sopenharmony_ci} 1171e41f4b71Sopenharmony_ci``` 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ci## unifiedDataChannel.queryData 1174e41f4b71Sopenharmony_ci 1175e41f4b71Sopenharmony_ciqueryData(options: Options): Promise<Array<UnifiedData>> 1176e41f4b71Sopenharmony_ci 1177e41f4b71Sopenharmony_ci查询UDMF公共数据通路的数据,使用Promise异步回调。 1178e41f4b71Sopenharmony_ci 1179e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_ci**参数:** 1184e41f4b71Sopenharmony_ci 1185e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1186e41f4b71Sopenharmony_ci|---------|---------------------|----|-----------------------------------------------| 1187e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 1188e41f4b71Sopenharmony_ci 1189e41f4b71Sopenharmony_ci**返回值:** 1190e41f4b71Sopenharmony_ci 1191e41f4b71Sopenharmony_ci| 类型 | 说明 | 1192e41f4b71Sopenharmony_ci|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| 1193e41f4b71Sopenharmony_ci| Promise<Array<[UnifiedData](#unifieddata)>> | Promise对象,返回查询到的所有数据。<br>如果options中填入的是key,则返回key对应的数据。<br>如果options中填入的是intention,则返回intention下所有数据。<br>如intention和key均填写了,取两者查询数据的交集,与options只填入key的获取结果一致;如没有交集报错。 | 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_ci**错误码:** 1196e41f4b71Sopenharmony_ci 1197e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1198e41f4b71Sopenharmony_ci 1199e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 1200e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 1201e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1202e41f4b71Sopenharmony_ci 1203e41f4b71Sopenharmony_ci**示例:** 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci```ts 1206e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 1207e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 1208e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1209e41f4b71Sopenharmony_ci 1210e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 1211e41f4b71Sopenharmony_ci key: 'udmf://DataHub/com.ohos.test/0123456789' 1212e41f4b71Sopenharmony_ci}; 1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_citry { 1215e41f4b71Sopenharmony_ci unifiedDataChannel.queryData(options).then((data) => { 1216e41f4b71Sopenharmony_ci console.info(`Succeeded in querying data. size = ${data.length}`); 1217e41f4b71Sopenharmony_ci for (let i = 0; i < data.length; i++) { 1218e41f4b71Sopenharmony_ci let records = data[i].getRecords(); 1219e41f4b71Sopenharmony_ci for (let j = 0; j < records.length; j++) { 1220e41f4b71Sopenharmony_ci if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1221e41f4b71Sopenharmony_ci let text = records[j] as unifiedDataChannel.PlainText; 1222e41f4b71Sopenharmony_ci console.info(`${i + 1}.${text.textContent}`); 1223e41f4b71Sopenharmony_ci } 1224e41f4b71Sopenharmony_ci } 1225e41f4b71Sopenharmony_ci } 1226e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1227e41f4b71Sopenharmony_ci console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 1228e41f4b71Sopenharmony_ci }); 1229e41f4b71Sopenharmony_ci} catch (e) { 1230e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 1231e41f4b71Sopenharmony_ci console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 1232e41f4b71Sopenharmony_ci} 1233e41f4b71Sopenharmony_ci``` 1234e41f4b71Sopenharmony_ci 1235e41f4b71Sopenharmony_ci## unifiedDataChannel.deleteData 1236e41f4b71Sopenharmony_ci 1237e41f4b71Sopenharmony_cideleteData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 1238e41f4b71Sopenharmony_ci 1239e41f4b71Sopenharmony_ci删除UDMF公共数据通路的数据,返回删除的数据集,使用callback异步回调。 1240e41f4b71Sopenharmony_ci 1241e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1242e41f4b71Sopenharmony_ci 1243e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 1244e41f4b71Sopenharmony_ci 1245e41f4b71Sopenharmony_ci**参数:** 1246e41f4b71Sopenharmony_ci 1247e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1248e41f4b71Sopenharmony_ci|----------|---------------------------------------------------------------|----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1249e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 1250e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | 是 | 回调函数,返回删除的所有数据。<br>如果options中填入的是key,则删除key对应的数据并返回该数据。<br>如果options中填入的是intention,则删除intention下所有数据并返回删除的数据。<br>如intention和key均填写了,取两者数据的交集进行删除,并返回删除的数据,与options只填入key的结果一致;如没有交集报错。 | 1251e41f4b71Sopenharmony_ci 1252e41f4b71Sopenharmony_ci**错误码:** 1253e41f4b71Sopenharmony_ci 1254e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1255e41f4b71Sopenharmony_ci 1256e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 1257e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 1258e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1259e41f4b71Sopenharmony_ci 1260e41f4b71Sopenharmony_ci**示例:** 1261e41f4b71Sopenharmony_ci 1262e41f4b71Sopenharmony_ci```ts 1263e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 1264e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 1265e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1266e41f4b71Sopenharmony_ci 1267e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 1268e41f4b71Sopenharmony_ci intention: unifiedDataChannel.Intention.DATA_HUB 1269e41f4b71Sopenharmony_ci}; 1270e41f4b71Sopenharmony_ci 1271e41f4b71Sopenharmony_citry { 1272e41f4b71Sopenharmony_ci unifiedDataChannel.deleteData(options, (err, data) => { 1273e41f4b71Sopenharmony_ci if (err === undefined) { 1274e41f4b71Sopenharmony_ci console.info(`Succeeded in deleting data. size = ${data.length}`); 1275e41f4b71Sopenharmony_ci for (let i = 0; i < data.length; i++) { 1276e41f4b71Sopenharmony_ci let records = data[i].getRecords(); 1277e41f4b71Sopenharmony_ci for (let j = 0; j < records.length; j++) { 1278e41f4b71Sopenharmony_ci if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1279e41f4b71Sopenharmony_ci let text = records[j] as unifiedDataChannel.PlainText; 1280e41f4b71Sopenharmony_ci console.info(`${i + 1}.${text.textContent}`); 1281e41f4b71Sopenharmony_ci } 1282e41f4b71Sopenharmony_ci } 1283e41f4b71Sopenharmony_ci } 1284e41f4b71Sopenharmony_ci } else { 1285e41f4b71Sopenharmony_ci console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `); 1286e41f4b71Sopenharmony_ci } 1287e41f4b71Sopenharmony_ci }); 1288e41f4b71Sopenharmony_ci} catch (e) { 1289e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 1290e41f4b71Sopenharmony_ci console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `); 1291e41f4b71Sopenharmony_ci} 1292e41f4b71Sopenharmony_ci``` 1293e41f4b71Sopenharmony_ci 1294e41f4b71Sopenharmony_ci## unifiedDataChannel.deleteData 1295e41f4b71Sopenharmony_ci 1296e41f4b71Sopenharmony_cideleteData(options: Options): Promise<Array<UnifiedData>> 1297e41f4b71Sopenharmony_ci 1298e41f4b71Sopenharmony_ci删除UDMF公共数据通路的数据,返回删除的数据集,使用Promise异步回调。 1299e41f4b71Sopenharmony_ci 1300e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1301e41f4b71Sopenharmony_ci 1302e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 1303e41f4b71Sopenharmony_ci 1304e41f4b71Sopenharmony_ci**参数:** 1305e41f4b71Sopenharmony_ci 1306e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1307e41f4b71Sopenharmony_ci|---------|---------------------|----|--------| 1308e41f4b71Sopenharmony_ci| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 1309e41f4b71Sopenharmony_ci 1310e41f4b71Sopenharmony_ci**返回值:** 1311e41f4b71Sopenharmony_ci 1312e41f4b71Sopenharmony_ci| 类型 | 说明 | 1313e41f4b71Sopenharmony_ci|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| 1314e41f4b71Sopenharmony_ci| Promise<Array<[UnifiedData](#unifieddata)>> | Promise对象,返回删除的所有数据。<br>如果options中填入的是key,则删除key对应的数据并返回该数据。<br>如果options中填入的是intention,则删除intention下所有数据并返回删除的数据。<br>如intention和key均填写了,取两者数据的交集进行删除,并返回删除的数据,与options只填入key的结果一致;如没有交集报错。 | 1315e41f4b71Sopenharmony_ci 1316e41f4b71Sopenharmony_ci**错误码:** 1317e41f4b71Sopenharmony_ci 1318e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1319e41f4b71Sopenharmony_ci 1320e41f4b71Sopenharmony_ci| **错误码ID** | **错误信息** | 1321e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 1322e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1323e41f4b71Sopenharmony_ci 1324e41f4b71Sopenharmony_ci**示例:** 1325e41f4b71Sopenharmony_ci 1326e41f4b71Sopenharmony_ci```ts 1327e41f4b71Sopenharmony_ciimport { unifiedDataChannel } from '@kit.ArkData'; 1328e41f4b71Sopenharmony_ciimport { uniformTypeDescriptor } from '@kit.ArkData'; 1329e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1330e41f4b71Sopenharmony_ci 1331e41f4b71Sopenharmony_cilet options: unifiedDataChannel.Options = { 1332e41f4b71Sopenharmony_ci key: 'udmf://DataHub/com.ohos.test/0123456789' 1333e41f4b71Sopenharmony_ci}; 1334e41f4b71Sopenharmony_ci 1335e41f4b71Sopenharmony_citry { 1336e41f4b71Sopenharmony_ci unifiedDataChannel.deleteData(options).then((data) => { 1337e41f4b71Sopenharmony_ci console.info(`Succeeded in deleting data. size = ${data.length}`); 1338e41f4b71Sopenharmony_ci for (let i = 0; i < data.length; i++) { 1339e41f4b71Sopenharmony_ci let records = data[i].getRecords(); 1340e41f4b71Sopenharmony_ci for (let j = 0; j < records.length; j++) { 1341e41f4b71Sopenharmony_ci if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1342e41f4b71Sopenharmony_ci let text = records[j] as unifiedDataChannel.PlainText; 1343e41f4b71Sopenharmony_ci console.info(`${i + 1}.${text.textContent}`); 1344e41f4b71Sopenharmony_ci } 1345e41f4b71Sopenharmony_ci } 1346e41f4b71Sopenharmony_ci } 1347e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1348e41f4b71Sopenharmony_ci console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `); 1349e41f4b71Sopenharmony_ci }); 1350e41f4b71Sopenharmony_ci} catch (e) { 1351e41f4b71Sopenharmony_ci let error: BusinessError = e as BusinessError; 1352e41f4b71Sopenharmony_ci console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 1353e41f4b71Sopenharmony_ci} 1354e41f4b71Sopenharmony_ci``` 1355