1# @ohos.data.unifiedDataChannel (标准化数据通路) 2 3本模块为统一数据管理框架(Unified Data Management Framework,UDMF)的组成部分,针对多对多跨应用数据共享的不同业务场景提供了标准化的数据通路,提供了标准化的数据接入与读取接口。同时对文本、图片等数据类型提供了标准化定义,方便不同应用间进行数据交互,减少数据类型适配的工作量。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```js 12import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 13``` 14 15## UnifiedData 16 17表示UDMF统一数据对象,提供封装一组数据记录的方法。 18 19**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 20 21### constructor 22 23constructor(record: UnifiedRecord) 24 25用于创建带有一条数据记录的统一数据对象。 26 27**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| ------ | ------------------------------- | ---- |-----------------------------------------| 33| record | [UnifiedRecord](#unifiedrecord) | 是 | 要添加到统一数据对象中的数据记录,该记录为UnifiedRecord子类对象。 | 34 35**示例:** 36 37```js 38let text = new unifiedDataChannel.PlainText(); 39text.textContent = 'this is textContent of text'; 40let unifiedData = new unifiedDataChannel.UnifiedData(text); 41``` 42 43### addRecord 44 45addRecord(record: UnifiedRecord): void 46 47在当前统一数据对象中添加一条数据记录。 48 49**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 50 51**参数:** 52 53| 参数名 | 类型 | 必填 | 说明 | 54| ------ | ------------------------------- | ---- |---------------------------------------------| 55| record | [UnifiedRecord](#unifiedrecord) | 是 | 要添加到统一数据对象中的数据记录,该记录为UnifiedRecord子类对象。| 56 57**示例:** 58 59```js 60let text1 = new unifiedDataChannel.PlainText(); 61text1.textContent = 'this is textContent of text1'; 62let unifiedData = new unifiedDataChannel.UnifiedData(text1); 63 64let text2 = new unifiedDataChannel.PlainText(); 65text2.textContent = 'this is textContent of text2'; 66unifiedData.addRecord(text2); 67``` 68 69### getRecords 70 71getRecords(): Array\<UnifiedRecord\> 72 73将当前统一数据对象中的所有数据记录取出。通过本接口取出的数据为UnifiedRecord类型,需通过[getType](#gettype)获取数据类型后转为子类再使用。 74 75**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 76 77**返回值:** 78 79| 类型 | 说明 | 80| ---------------------------------------- |-------------------------| 81| Array\<[UnifiedRecord](#unifiedrecord)\> | 当前统一数据对象内所添加的记录。 | 82 83**示例:** 84 85```js 86import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 87 88let text = new unifiedDataChannel.PlainText(); 89text.textContent = 'this is textContent of text'; 90let unifiedData = new unifiedDataChannel.UnifiedData(text); 91 92let link = new unifiedDataChannel.Hyperlink(); 93link.url = 'www.XXX.com'; 94unifiedData.addRecord(link); 95 96let records = unifiedData.getRecords(); 97for (let i = 0; i < records.length; i++) { 98 let record = records[i]; 99 if (record.getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 100 let plainText = record as unifiedDataChannel.PlainText; 101 console.info(`textContent: ${plainText.textContent}`); 102 } else if (record.getType() == uniformTypeDescriptor.UniformDataType.HYPERLINK) { 103 let hyperlink = record as unifiedDataChannel.Hyperlink; 104 console.info(`linkUrl: ${hyperlink.url}`); 105 } 106} 107``` 108 109## Summary 110 111描述某一统一数据对象的数据摘要,包括所含数据类型及大小,当前暂不支持。 112 113**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 114 115| 名称 | 类型 | 可读 | 可写 | 说明 | 116| --------- | ------------------------- | ---- | ---- |-----------------------------------------------------------------------------------| 117| summary | Record<string, number> | 是 | 否 | 是一个字典类型对象,key表示数据类型(见[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)),value为统一数据对象中该类型记录大小总和(单位:Byte)。 | 118| totalSize | number | 是 | 否 | 统一数据对象内记录总大小(单位:Byte)。 | 119 120## UnifiedRecord 121 122对UDMF支持的数据内容的抽象定义,称为数据记录。一个统一数据对象内包含一条或多条数据记录,例如一条文本记录、一条图片记录、一条HTML记录等。 123 124UnifiedRecord是一个抽象父类,无法保存具体数据内容,应用在使用时,不能将其添加到统一数据对象中,而应该创建带有数据内容的具体子类,如Text、Image等。 125 126### getType 127 128getType(): string 129 130获取当前数据记录的类型。由于从统一数据对象中调用[getRecords](#getrecords)所取出的数据是UnifiedRecord对象,因此需要通过本接口查询此记录的具体类型,再将该UnifiedRecord对象转换为其子类,调用子类接口。 131 132**系统能力** :SystemCapability.DistributedDataManager.UDMF.Core 133 134**返回值:** 135 136| 类型 | 说明 | 137| ------ |------------------------------------------------------| 138| string | 当前数据记录对应的具体数据类型,见[UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)。| 139 140**示例:** 141 142```js 143import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 144 145let text = new unifiedDataChannel.PlainText(); 146text.textContent = 'this is textContent of text'; 147let unifiedData = new unifiedDataChannel.UnifiedData(text); 148 149let records = unifiedData.getRecords(); 150if (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 151 let plainText = records[0] as unifiedDataChannel.PlainText; 152 console.info(`textContent: ${plainText.textContent}`); 153} 154``` 155 156## Text 157 158文本类型数据,是[UnifiedRecord](#unifiedrecord)的子类,也是文本类型数据的基类,用于描述文本类数据,推荐开发者优先使用Text的子类描述数据,如[PlainText](#plaintext)、[Hyperlink](#hyperlink)、[HTML](#html)等具体子类。 159 160**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 161 162| 名称 | 类型 | 可读 | 可写 | 说明 | 163| ------- | ------------------------- | ---- | ---- |-----------------------------------------------------------------------------------------------------------------------------------------------------| 164| details | Record<string, string> | 是 | 是 | 是一个字典类型对象,key和value都是string类型,用于描述文本内容。例如,可生成一个details内容为<br />{<br />"title":"标题",<br />"content":"内容"<br />}<br />的数据对象,用于描述一篇文章。非必填字段,默认值为空字典对象。 | 165 166**示例:** 167 168```js 169let text = new unifiedDataChannel.Text(); 170text.details = { 171 title: 'MyTitle', 172 content: 'this is content', 173}; 174let unifiedData = new unifiedDataChannel.UnifiedData(text); 175``` 176 177## PlainText 178 179纯文本类型数据,是[Text](#text)的子类,用于描述纯文本类数据。 180 181**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 182 183| 名称 | 类型 | 可读 | 可写 | 说明 | 184| ----------- | ------ | ---- | ---- |-----------------------| 185| textContent | string | 是 | 是 | 纯文本内容。 | 186| abstract | string | 是 | 是 | 纯文本摘要,非必填字段,默认值为空字符串。 | 187 188**示例:** 189 190```js 191let text = new unifiedDataChannel.PlainText(); 192text.textContent = 'this is textContent'; 193text.abstract = 'this is abstract'; 194``` 195 196## Hyperlink 197 198超链接类型数据,是[Text](#text)的子类,用于描述超链接类型数据。 199 200**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 201 202| 名称 | 类型 | 可读 | 可写 | 说明 | 203| ----------- | ------ | ---- | ---- |--------------| 204| url | string | 是 | 是 | 链接url。 | 205| description | string | 是 | 是 | 链接内容描述,非必填字段,默认值为空字符串。 | 206 207**示例:** 208 209```js 210let link = new unifiedDataChannel.Hyperlink(); 211link.url = 'www.XXX.com'; 212link.description = 'this is description'; 213``` 214 215## HTML 216 217HTML类型数据,是[Text](#text)的子类,用于描述超文本标记语言数据。 218 219**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 220 221| 名称 | 类型 | 可读 | 可写 | 说明 | 222| ------------ | ------ | ---- | ---- |-----------------------| 223| htmlContent | string | 是 | 是 | html格式内容。 | 224| plainContent | string | 是 | 是 | 去除html标签后的纯文本内容,非必填字段,默认值为空字符串。 | 225 226**示例:** 227 228```js 229let html = new unifiedDataChannel.HTML(); 230html.htmlContent = '<div><p>标题</p></div>'; 231html.plainContent = 'this is plainContent'; 232``` 233 234## File 235 236File类型数据,是[UnifiedRecord](#unifiedrecord)的子类,也是文件类型数据的基类,用于描述文件类型数据,推荐开发者优先使用File的子类描述数据,如[Image](#image)、[Video](#video)、[Folder](#folder)等具体子类。 237 238**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 239 240| 名称 | 类型 | 可读 | 可写 | 说明 | 241|---------|---------------------------| ---- | ---- |------------------------------------------------------------------------------------------------------------------------------------------------------| 242| details | Record<string, string> | 是 | 是 | 是一个字典类型对象,key和value都是string类型,用于描述文件相关信息。例如,可生成一个details内容为<br />{<br />"name":"文件名",<br />"type":"文件类型"<br />}<br />的数据对象,用于描述一个文件。非必填字段,默认值为空字典对象。 | 243| uri | string | 是 | 是 | 文件数据uri。 | 244 245**示例:** 246 247```js 248let file = new unifiedDataChannel.File(); 249file.details = { 250 name: 'test', 251 type: 'txt', 252}; 253file.uri = 'schema://com.samples.test/files/test.txt'; 254``` 255 256## Image 257 258图片类型数据,是[File](#file)的子类,用于描述图片文件。 259 260**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 261 262| 名称 | 类型 | 可读 | 可写 | 说明 | 263| -------- | ------ | ---- | ---- |----------| 264| imageUri | string | 是 | 是 | 图片数据uri。 | 265 266**示例:** 267 268```js 269let image = new unifiedDataChannel.Image(); 270image.imageUri = 'schema://com.samples.test/files/test.jpg'; 271``` 272 273## Video 274 275视频类型数据,是[File](#file)的子类,用于描述视频文件。 276 277**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 278 279| 名称 | 类型 | 可读 | 可写 | 说明 | 280| -------- | ------ | ---- | ---- |----------| 281| videoUri | string | 是 | 是 | 视频数据uri。 | 282 283**示例:** 284 285```js 286let video = new unifiedDataChannel.Video(); 287video.videoUri = 'schema://com.samples.test/files/test.mp4'; 288``` 289 290## Audio 291 292音频类型数据,是[File](#file)的子类,用于描述音频文件。 293 294**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 295 296| 名称 | 类型 | 可读 | 可写 | 说明 | 297|----------|--------|----|----|----------| 298| audioUri | string | 是 | 是 | 音频数据uri。 | 299 300**示例:** 301 302```js 303let audio = new unifiedDataChannel.Audio(); 304audio.audioUri = 'schema://com.samples.test/files/test.mp3'; 305``` 306 307## Folder 308 309文件夹类型数据,是[File](#file)的子类,用于描述文件夹。 310 311**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 312 313| 名称 | 类型 | 可读 | 可写 | 说明 | 314| -------- | ------ | ---- | ---- |---------| 315| folderUri | string | 是 | 是 | 文件夹uri。 | 316 317**示例:** 318 319```js 320let folder = new unifiedDataChannel.Folder(); 321folder.folderUri = 'schema://com.samples.test/files/folder/'; 322``` 323 324## SystemDefinedRecord 325 326SystemDefinedRecord是[UnifiedRecord](#unifiedrecord)的子类,也是OpenHarmony系统特有数据类型的基类,用于描述仅在OpenHarmony系统范围内流通的特有数据类型,推荐开发者优先使用SystemDefinedRecord的子类描述数据,如[SystemDefinedForm](#systemdefinedform)、[SystemDefinedAppItem](#systemdefinedappitem)、[SystemDefinedPixelMap](#systemdefinedpixelmap)等具体子类。 327 328**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 329 330| 名称 | 类型 | 可读 | 可写 | 说明 | 331| ------- |------------------------|----------| ---- | ------------------------------------------------------------ | 332| details | Record<string, number \| string \| Uint8Array> | 是 | 是 | 是一个字典类型对象,key是string类型,value可以写入number(数值类型)、string(字符串类型)、Uint8Array(二进制字节数组)类型数据。非必填字段,默认值为空字典对象。| 333 334**示例:** 335 336```js 337let sdr = new unifiedDataChannel.SystemDefinedRecord(); 338let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 339sdr.details = { 340 title: 'recordTitle', 341 version: 1, 342 content: u8Array, 343}; 344let unifiedData = new unifiedDataChannel.UnifiedData(sdr); 345``` 346 347## SystemDefinedForm 348 349系统定义的桌面卡片类型数据,是[SystemDefinedRecord](#systemdefinedrecord)的子类。 350 351**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 352 353| 名称 | 类型 | 可读 | 可写 | 说明 | 354| ----------- | ------ | ---- | ---- |----------------| 355| formId | number | 是 | 是 | 卡片id。 | 356| formName | string | 是 | 是 | 卡片名称。 | 357| bundleName | string | 是 | 是 | 卡片所属的bundle名。 | 358| abilityName | string | 是 | 是 | 卡片对应的ability名。 | 359| module | string | 是 | 是 | 卡片所属的module名。 | 360 361**示例:** 362 363```js 364let form = new unifiedDataChannel.SystemDefinedForm(); 365form.formId = 123456; 366form.formName = 'MyFormName'; 367form.bundleName = 'MyBundleName'; 368form.abilityName = 'MyAbilityName'; 369form.module = 'MyModule'; 370let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 371form.details = { 372 formKey1: 123, 373 formKey2: 'formValue', 374 formKey3: u8Array, 375}; 376let unifiedData = new unifiedDataChannel.UnifiedData(form); 377``` 378 379## SystemDefinedAppItem 380 381系统定义的桌面图标类型数据,是[SystemDefinedRecord](#systemdefinedrecord)的子类。 382 383**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 384 385| 名称 | 类型 | 可读 | 可写 | 说明 | 386| ----------- | ------ | ---- | ---- |-----------------| 387| appId | string | 是 | 是 | 图标对应的应用id。 | 388| appName | string | 是 | 是 | 图标对应的应用名。 | 389| appIconId | string | 是 | 是 | 图标的图片id。 | 390| appLabelId | string | 是 | 是 | 图标名称对应的标签id。 | 391| bundleName | string | 是 | 是 | 图标对应的应用bundle名。 | 392| abilityName | string | 是 | 是 | 图标对应的应用ability名。 | 393 394**示例:** 395 396```js 397let appItem = new unifiedDataChannel.SystemDefinedAppItem(); 398appItem.appId = 'MyAppId'; 399appItem.appName = 'MyAppName'; 400appItem.appIconId = 'MyAppIconId'; 401appItem.appLabelId = 'MyAppLabelId'; 402appItem.bundleName = 'MyBundleName'; 403appItem.abilityName = 'MyAbilityName'; 404let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 405appItem.details = { 406 appItemKey1: 123, 407 appItemKey2: 'appItemValue', 408 appItemKey3: u8Array, 409}; 410let unifiedData = new unifiedDataChannel.UnifiedData(appItem); 411``` 412 413## SystemDefinedPixelMap 414 415与系统侧定义的[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)数据类型对应的图片数据类型,是[SystemDefinedRecord](#systemdefinedrecord)的子类,仅保存PixelMap的二进制数据。 416 417**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 418 419| 名称 | 类型 | 可读 | 可写 | 说明 | 420| ------- | ---------- | ---- | ---- |-------------------| 421| rawData | Uint8Array | 是 | 是 | PixelMap对象的二进制数据。 | 422 423**示例:** 424 425```js 426import image from '@ohos.multimedia.image'; // PixelMap类定义所在模块 427 428const color = new ArrayBuffer(96); // 创建pixelmap对象 429let opts: image.InitializationOptions = { 430 editable: true, pixelFormat: 3, size: { 431 height: 4, width: 6 432 } 433} 434image.createPixelMap(color, opts, (error, pixelmap) => { 435 if (error) { 436 console.error('Failed to create pixelmap.'); 437 } else { 438 console.info('Succeeded in creating pixelmap.'); 439 let arrayBuf = new ArrayBuffer(pixelmap.getPixelBytesNumber()); 440 pixelmap.readPixelsToBuffer(arrayBuf); 441 let u8Array = new Uint8Array(arrayBuf); 442 let sdpixel = new unifiedDataChannel.SystemDefinedPixelMap(); 443 sdpixel.rawData = u8Array; 444 let unifiedData = new unifiedDataChannel.UnifiedData(sdpixel); 445 } 446}) 447``` 448 449## ApplicationDefinedRecord 450 451ApplicationDefinedRecord是[UnifiedRecord](#unifiedrecord)的子类,也是应用自定义数据类型的基类,用于描述仅在应用生态内部流通的自定义数据类型,应用可基于此类进行自定义数据类型的扩展。 452 453**系统能力**:SystemCapability.DistributedDataManager.UDMF.Core 454 455| 名称 | 类型 | 可读 | 可写 | 说明 | 456|------------------------|------------| ---- | ---- |---------------------------------------| 457| applicationDefinedType | string | 是 | 是 | 应用自定义类型标识符,必须以'ApplicationDefined'开头。 | 458| rawData | Uint8Array | 是 | 是 | 应用自定义数据类型的二进制数据。 | 459 460**示例:** 461 462```js 463let record = new unifiedDataChannel.ApplicationDefinedRecord(); 464let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 465record.applicationDefinedType = 'ApplicationDefinedType'; 466record.rawData = u8Array; 467let unifiedData = new unifiedDataChannel.UnifiedData(record); 468``` 469 470## Intention 471 472UDMF已经支持的数据通路枚举类型。其主要用途是标识各种UDMF数据通路所面向的不同业务场景。 473 474**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 475 476| 名称 | 值 | 说明 | 477|----------|-----------|---------| 478| DATA_HUB | 'DataHub' | 公共数据通路。 | 479 480## Options 481 482UDMF提供的数据操作接口可选项,包含intention和key两个可选参数。无默认值,当对应接口不需要此参数时可不填,具体要求参照方法接口的参数说明。 483 484**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 485 486 487| 名称 | 类型 | 可读 | 可写 | 必填 | 说明 | 488|-----------|-------------------------|----|----|----|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 489| intention | [Intention](#intention) | 是 | 是 | 否 | 表示数据操作相关的数据通路类型。 | 490| key | string | 是 | 是 | 否 | UDMF中数据对象的唯一标识符,可通过[insertData](#unifieddatachannelinsertdata)接口的返回值获取。<br>由udmf:/、intention、bundleName和groupId四部分组成,以'/'连接,比如:udmf://DataHub/com.ohos.test/0123456789。<br>其中udmf:/固定,DataHub为对应枚举的取值,com.ohos.test为包名,0123456789为随机生成的groupId。 | 491 492 493 494## unifiedDataChannel.insertData 495 496insertData(options: Options, data: UnifiedData, callback: AsyncCallback<string>): void 497 498将数据写入UDMF的公共数据通路中,并生成数据的唯一标识符,使用callback异步回调。 499 500**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 501 502**参数:** 503 504| 参数名 | 类型 | 必填 | 说明 | 505|----------|----------------------------|----|------------------------------| 506| options | [Options](#options) | 是 | 配置项参数,仅需要intention的值。 | 507| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 508| callback | AsyncCallback<string> | 是 | 回调函数,返回写入UDMF的数据的唯一标识符key的值。 | 509 510**示例:** 511 512```ts 513import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 514import { BusinessError } from '@ohos.base'; 515 516let plainText = new unifiedDataChannel.PlainText(); 517plainText.textContent = 'hello world!'; 518let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 519 520let options: unifiedDataChannel.Options = { 521 intention: unifiedDataChannel.Intention.DATA_HUB 522} 523try { 524 unifiedDataChannel.insertData(options, unifiedData, (err, data) => { 525 if (err === undefined) { 526 console.info(`Succeeded in inserting data. key = ${data}`); 527 } else { 528 console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 529 } 530 }); 531 } catch (e) { 532 let error: BusinessError = e as BusinessError; 533 console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 534} 535 536``` 537 538## unifiedDataChannel.insertData 539 540insertData(options: Options, data: UnifiedData): Promise<string> 541 542将数据写入UDMF的公共数据通路中,并生成数据的唯一标识符,使用Promise异步回调。 543 544**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 545 546**参数:** 547 548| 参数名 | 类型 | 必填 | 说明 | 549|---------|-----------------------------|----|-----------------------| 550| options | [Options](#options) | 是 | 配置项参数,仅需要intention的值。 | 551| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 552 553**返回值:** 554 555| 类型 | 说明 | 556|-----------------------|-----------------------------------| 557| Promise<string> | Promise对象,返回写入UDMF的数据的唯一标识符key的值。 | 558 559**示例:** 560 561```ts 562import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 563import { BusinessError } from '@ohos.base'; 564 565let plainText = new unifiedDataChannel.PlainText(); 566plainText.textContent = 'hello world!'; 567let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 568 569let options: unifiedDataChannel.Options = { 570 intention: unifiedDataChannel.Intention.DATA_HUB 571} 572try { 573 unifiedDataChannel.insertData(options, unifiedData).then((data) => { 574 console.info(`Succeeded in inserting data. key = ${data}`); 575 }).catch((err: BusinessError) => { 576 console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 577 }); 578} catch (e) { 579 let error: BusinessError = e as BusinessError; 580 console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 581} 582``` 583 584## unifiedDataChannel.updateData 585 586updateData(options: Options, data: UnifiedData, callback: AsyncCallback<void>): void 587 588更新已写入UDMF的公共数据通路的数据,使用callback异步回调。 589 590**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 591 592**参数:** 593 594| 参数名 | 类型 | 必填 | 说明 | 595|----------|-----------------------------|----|-------------------------------------| 596| options | [Options](#options) | 是 | 配置项参数,仅需要key的值。 | 597| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 598| callback | AsyncCallback<void> | 是 | 回调函数。当更新数据成功,err为undefined,否则为错误对象。 | 599 600**示例:** 601 602```ts 603import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 604import { BusinessError } from '@ohos.base'; 605 606let plainText = new unifiedDataChannel.PlainText(); 607plainText.textContent = 'hello world!'; 608let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 609 610let options: unifiedDataChannel.Options = { 611 key: 'udmf://DataHub/com.ohos.test/0123456789' 612}; 613 614try { 615 unifiedDataChannel.updateData(options, unifiedData, (err) => { 616 if (err === undefined) { 617 console.info('Succeeded in updating data.'); 618 } else { 619 console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 620 } 621 }); 622} catch (e) { 623 let error: BusinessError = e as BusinessError; 624 console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 625} 626``` 627 628## unifiedDataChannel.updateData 629 630updateData(options: Options, data: UnifiedData): Promise<void> 631 632更新已写入UDMF的公共数据通路的数据,使用Promise异步回调。 633 634**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 635 636**参数:** 637 638| 参数名 | 类型 | 必填 | 说明 | 639|---------|-----------------------------|----|-----------------| 640| options | [Options](#options) | 是 | 配置项参数,仅需要key的值。 | 641| data | [UnifiedData](#unifieddata) | 是 | 目标数据。 | 642 643**返回值:** 644 645| 类型 | 说明 | 646|---------------------|----------------------------| 647| Promise<void> | Promise对象。无返回结果的Promise对象。 | 648 649**示例:** 650 651```ts 652import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 653import { BusinessError } from '@ohos.base'; 654 655let plainText = new unifiedDataChannel.PlainText(); 656plainText.textContent = 'hello world!'; 657let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 658 659let options: unifiedDataChannel.Options = { 660 key: 'udmf://DataHub/com.ohos.test/0123456789' 661}; 662 663try { 664 unifiedDataChannel.updateData(options, unifiedData).then(() => { 665 console.info('Succeeded in updating data.'); 666 }).catch((err: BusinessError) => { 667 console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 668 }); 669} catch (e) { 670 let error: BusinessError = e as BusinessError; 671 console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 672} 673``` 674 675## unifiedDataChannel.queryData 676 677queryData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 678 679查询UDMF公共数据通路的数据,使用callback异步回调。 680 681**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 682 683**参数:** 684 685| 参数名 | 类型 | 必填 | 说明 | 686|----------|---------------------------------------------------------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------| 687| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 688| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | 是 | 回调函数,返回查询到的所有数据。<br>如果options中填入的是key,则返回key对应的数据。<br>如果options中填入的是intention,则返回intention下所有数据。<br>如intention和key均填写了,取两者查询数据的交集,与options只填入key的获取结果一致;如没有交集报错。 | 689 690**示例:** 691 692```ts 693import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 694import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 695import { BusinessError } from '@ohos.base'; 696 697let options: unifiedDataChannel.Options = { 698 intention: unifiedDataChannel.Intention.DATA_HUB 699}; 700 701try { 702 unifiedDataChannel.queryData(options, (err, data) => { 703 if (err === undefined) { 704 console.info(`Succeeded in querying data. size = ${data.length}`); 705 for (let i = 0; i < data.length; i++) { 706 let records = data[i].getRecords(); 707 for (let j = 0; j < records.length; j++) { 708 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 709 let text = records[j] as unifiedDataChannel.PlainText; 710 console.info(`${i + 1}.${text.textContent}`); 711 } 712 } 713 } 714 } else { 715 console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 716 } 717 }); 718} catch (e) { 719 let error: BusinessError = e as BusinessError; 720 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 721} 722``` 723 724## unifiedDataChannel.queryData 725 726queryData(options: Options): Promise<Array<UnifiedData>> 727 728查询UDMF公共数据通路的数据,使用Promise异步回调。 729 730**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 731 732**参数:** 733 734| 参数名 | 类型 | 必填 | 说明 | 735|---------|---------------------|----|-----------------------------------------------| 736| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 737 738**返回值:** 739 740| 类型 | 说明 | 741|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| 742| Promise<Array<[UnifiedData](#unifieddata)>> | Promise对象,返回查询到的所有数据。<br>如果options中填入的是key,则返回key对应的数据。<br>如果options中填入的是intention,则返回intention下所有数据。<br>如intention和key均填写了,取两者查询数据的交集,与options只填入key的获取结果一致;如没有交集报错。 | 743 744**示例:** 745 746```ts 747import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 748import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 749import { BusinessError } from '@ohos.base'; 750 751let options: unifiedDataChannel.Options = { 752 key: 'udmf://DataHub/com.ohos.test/0123456789' 753}; 754 755try { 756 unifiedDataChannel.queryData(options).then((data) => { 757 console.info(`Succeeded in querying data. size = ${data.length}`); 758 for (let i = 0; i < data.length; i++) { 759 let records = data[i].getRecords(); 760 for (let j = 0; j < records.length; j++) { 761 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 762 let text = records[j] as unifiedDataChannel.PlainText; 763 console.info(`${i + 1}.${text.textContent}`); 764 } 765 } 766 } 767 }).catch((err: BusinessError) => { 768 console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 769 }); 770} catch (e) { 771 let error: BusinessError = e as BusinessError; 772 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 773} 774``` 775 776## unifiedDataChannel.deleteData 777 778deleteData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 779 780删除UDMF公共数据通路的数据,返回删除的数据集,使用callback异步回调。 781 782**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 783 784**参数:** 785 786| 参数名 | 类型 | 必填 | 说明 | 787|----------|---------------------------------------------------------------|----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 788| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 789| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | 是 | 回调函数,返回删除的所有数据。<br>如果options中填入的是key,则删除key对应的数据并返回该数据。<br>如果options中填入的是intention,则删除intention下所有数据并返回删除的数据。<br>如intention和key均填写了,取两者数据的交集进行删除,并返回删除的数据,与options只填入key的结果一致;如没有交集报错。 | 790 791**示例:** 792 793```ts 794import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 795import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 796import { BusinessError } from '@ohos.base'; 797 798let options: unifiedDataChannel.Options = { 799 intention: unifiedDataChannel.Intention.DATA_HUB 800}; 801 802try { 803 unifiedDataChannel.deleteData(options, (err, data) => { 804 if (err === undefined) { 805 console.info(`Succeeded in deleting data. size = ${data.length}`); 806 for (let i = 0; i < data.length; i++) { 807 let records = data[i].getRecords(); 808 for (let j = 0; j < records.length; j++) { 809 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 810 let text = records[j] as unifiedDataChannel.PlainText; 811 console.info(`${i + 1}.${text.textContent}`); 812 } 813 } 814 } 815 } else { 816 console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `); 817 } 818 }); 819} catch (e) { 820 let error: BusinessError = e as BusinessError; 821 console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `); 822} 823``` 824 825## unifiedDataChannel.deleteData 826 827deleteData(options: Options): Promise<Array<UnifiedData>> 828 829删除UDMF公共数据通路的数据,返回删除的数据集,使用Promise异步回调。 830 831**系统能力:** SystemCapability.DistributedDataManager.UDMF.Core 832 833**参数:** 834 835| 参数名 | 类型 | 必填 | 说明 | 836|---------|---------------------|----|--------| 837| options | [Options](#options) | 是 | 配置项参数,key和intention均为可选,根据传入的参数做相应的校验以返回不同的值。 | 838 839**返回值:** 840 841| 类型 | 说明 | 842|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| 843| Promise<Array<[UnifiedData](#unifieddata)>> | Promise对象,返回删除的所有数据。<br>如果options中填入的是key,则删除key对应的数据并返回该数据。<br>如果options中填入的是intention,则删除intention下所有数据并返回删除的数据。<br>如intention和key均填写了,取两者数据的交集进行删除,并返回删除的数据,与options只填入key的结果一致;如没有交集报错。 | 844 845**示例:** 846 847```ts 848import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 849import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 850import { BusinessError } from '@ohos.base'; 851 852let options: unifiedDataChannel.Options = { 853 key: 'udmf://DataHub/com.ohos.test/0123456789' 854}; 855 856try { 857 unifiedDataChannel.deleteData(options).then((data) => { 858 console.info(`Succeeded in deleting data. size = ${data.length}`); 859 for (let i = 0; i < data.length; i++) { 860 let records = data[i].getRecords(); 861 for (let j = 0; j < records.length; j++) { 862 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 863 let text = records[j] as unifiedDataChannel.PlainText; 864 console.info(`${i + 1}.${text.textContent}`); 865 } 866 } 867 } 868 }).catch((err: BusinessError) => { 869 console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `); 870 }); 871} catch (e) { 872 let error: BusinessError = e as BusinessError; 873 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 874} 875```