1# @ohos.data.unifiedDataChannel (Unified Data Channel) 2 3As a part of the Unified Data Management Framework (UDMF), the **unifiedDataChannel** module provides unified data channels and standard data access interfaces for many-to-many data sharing across applications. It also provides definitions for uniform data types, such as text and image, to streamline data interaction between different applications and minimize the workload of data type adaptation. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```ts 12import { unifiedDataChannel } from '@kit.ArkData'; 13``` 14 15## ShareOptions<sup>12+</sup> 16 17Enumerates the options for using **UnifiedData** in a device. 18 19**Atomic service API**: This API can be used in atomic services since API version 12. 20 21**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 22 23| Name | Value| Description | 24|-------------|---|-------------------| 25| IN_APP | 0 | **UnifiedData** can be used only in the same application of a device.| 26| CROSS_APP | 1 | **UnifiedData** can be used across applications of a device.| 27 28## GetDelayData<sup>12+</sup> 29 30type GetDelayData = (type: string) => UnifiedData 31 32A type that defines a function used to obtain a deferred **UnifiedData** object. Currently, it can be used only in the pasteboard application of the same device. 33 34**Atomic service API**: This API can be used in atomic services since API version 12. 35 36**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 37 38**Parameters** 39 40| Name| Type| Mandatory| Description| 41| -------- | -------- | -------- | -------- | 42| type | string | Yes| Identifier of the deferred encapsulation.| 43 44**Return value** 45 46| Type | Description | 47| ---------------------------------------- |-------------------------| 48| [UnifiedData](#unifieddata) | **UnifiedData** object.| 49 50**Example** 51 52```ts 53import { uniformTypeDescriptor } from '@kit.ArkData'; 54 55let getDelayData: unifiedDataChannel.GetDelayData = ((type: string) => { 56 if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 57 let text = new unifiedDataChannel.Text(); 58 text.details = { 59 Key: 'textKey', 60 Value: 'textValue', 61 }; 62 let textData = new unifiedDataChannel.UnifiedData(text); 63 return textData; 64 } 65 return new unifiedDataChannel.UnifiedData(); 66}); 67``` 68 69## ValueType<sup>12+</sup> 70 71type ValueType = number | string | boolean | image.PixelMap | Want | ArrayBuffer | object | null | undefined 72 73Enumerates the data field types allowed in a unified data record. 74 75**Atomic service API**: This API can be used in atomic services since API version 12. 76 77**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 78 79| Type| Description| 80| -------- | -------- | 81| number | Number.| 82| string | String.| 83| boolean | Boolean.| 84| image.PixelMap | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7).| 85| Want | [Want](../apis-ability-kit/js-apis-app-ability-want.md).| 86| ArrayBuffer | ArrayBuffer.| 87| object | Object.| 88| null | Null.| 89| undefined | Undefined.| 90 91## UnifiedDataProperties<sup>12+</sup> 92 93Defines the properties of the data records in the unified data object, including the timestamp, tag, pasting range, and additional data. 94 95**Atomic service API**: This API can be used in atomic services since API version 12. 96 97**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 98 99| Name| Type| Read-Only| Optional| Description| 100| -------- | -------- | -------- | -------- | -------- | 101| extras<sup>12+</sup> | Record<string, object> | No| Yes| Object of the dictionary type used to set other properties. The default value is an empty dictionary object.| 102| tag<sup>12+</sup> | string | No| Yes| Customized tag. The default value is an empty string.| 103| timestamp<sup>12+</sup> | Date | Yes| Yes| Timestamp when [UnifiedData](#unifieddata) is generated. The default value is January 1, 1970 (UTC).| 104| shareOptions<sup>12+</sup> | [ShareOptions](#shareoptions12) | No| Yes| Range, in which [UnifiedData](#unifieddata) can be used. The default value is **CROSS_APP**.| 105| getDelayData<sup>12+</sup> | [GetDelayData](#getdelaydata12) | No| Yes| Callback for obtaining the deferred data. Currently, it can be used only in the pasteboard application of the same device. The default value is **undefined**.| 106 107**Example** 108 109```ts 110import { uniformTypeDescriptor } from '@kit.ArkData'; 111 112let properties = new unifiedDataChannel.UnifiedDataProperties(); 113properties.extras = { 114 key: { 115 title: 'MyTitle', 116 content: 'MyContent' 117 } 118}; 119properties.tag = "this is tag of properties"; 120properties.shareOptions = unifiedDataChannel.ShareOptions.CROSS_APP; 121properties.getDelayData = ((type: string) => { 122 if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 123 let text = new unifiedDataChannel.Text(); 124 text.details = { 125 Key: 'textKey', 126 Value: 'textValue', 127 }; 128 let textData = new unifiedDataChannel.UnifiedData(text); 129 return textData; 130 } 131 return new unifiedDataChannel.UnifiedData(); 132}); 133``` 134 135## UnifiedData 136 137Provides APIs for encapsulating a set of data records. 138 139**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 140 141### Properties 142 143| Name| Type| Read-Only| Optional| Description | 144| -------- | -------- | -------- | -------- |-------------------------------------------------------------------------------------------------| 145| properties<sup>12+</sup> | [UnifiedDataProperties](#unifieddataproperties12) | No| No| Properties of all the data records in a unified data object, including the timestamp, tag, application range, and additional data.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 146 147### constructor<sup>12+</sup> 148 149constructor() 150 151A constructor used to create a **UnifiedData** object. 152 153**Atomic service API**: This API can be used in atomic services since API version 12. 154 155**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 156 157**Example** 158 159```ts 160let unifiedData = new unifiedDataChannel.UnifiedData(); 161``` 162 163### constructor 164 165constructor(record: UnifiedRecord) 166 167A constructor used to create a **UnifiedData** object with a data record. 168 169**Atomic service API**: This API can be used in atomic services since API version 11. 170 171**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 172 173**Parameters** 174 175| Name| Type | Mandatory| Description | 176| ------ | ------------------------------- | ---- |-----------------------------------------| 177| record | [UnifiedRecord](#unifiedrecord) | Yes | Data record in the **UnifiedData** object. It is a **UnifiedRecord** object or its child class object.| 178 179**Error codes** 180 181For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 182 183| **ID**| **Error Message** | 184| ------------ | ------------------------------------------- | 185| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 186 187**Example** 188 189```ts 190let text = new unifiedDataChannel.PlainText(); 191text.textContent = 'this is textContent of text'; 192let unifiedData = new unifiedDataChannel.UnifiedData(text); 193``` 194 195### addRecord 196 197addRecord(record: UnifiedRecord): void 198 199Adds a data record to this **UnifiedRecord** object. 200 201**Atomic service API**: This API can be used in atomic services since API version 11. 202 203**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 204 205**Parameters** 206 207| Name| Type | Mandatory| Description | 208| ------ | ------------------------------- | ---- |---------------------------------------------| 209| record | [UnifiedRecord](#unifiedrecord) | Yes | Data record to add. It is a **UnifiedRecord** child class object.| 210 211**Error codes** 212 213For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 214 215| **ID**| **Error Message** | 216| ------------ | ------------------------------------------- | 217| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 218 219**Example** 220 221```ts 222let text1 = new unifiedDataChannel.PlainText(); 223text1.textContent = 'this is textContent of text1'; 224let unifiedData = new unifiedDataChannel.UnifiedData(text1); 225 226let text2 = new unifiedDataChannel.PlainText(); 227text2.textContent = 'this is textContent of text2'; 228unifiedData.addRecord(text2); 229``` 230 231### getRecords 232 233getRecords(): Array\<UnifiedRecord\> 234 235Obtains all data records from this **UnifiedData** object. The data obtained is of the **UnifiedRecord** type. Before using the data, you need to use [getType](#gettype) to obtain the data type and convert the data type to a child class. 236 237**Atomic service API**: This API can be used in atomic services since API version 11. 238 239**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 240 241**Return value** 242 243| Type | Description | 244| ---------------------------------------- |-------------------------| 245| Array\<[UnifiedRecord](#unifiedrecord)\> | Records in the **UnifiedData** object obtained.| 246 247**Example** 248 249```ts 250import { uniformTypeDescriptor } from '@kit.ArkData'; 251 252let text = new unifiedDataChannel.PlainText(); 253text.textContent = 'this is textContent of text'; 254let unifiedData = new unifiedDataChannel.UnifiedData(text); 255 256let link = new unifiedDataChannel.Hyperlink(); 257link.url = 'www.XXX.com'; 258unifiedData.addRecord(link); 259 260let records = unifiedData.getRecords(); 261for (let i = 0; i < records.length; i++) { 262 let record = records[i]; 263 if (record.getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 264 let plainText = record as unifiedDataChannel.PlainText; 265 console.info(`textContent: ${plainText.textContent}`); 266 } else if (record.getType() == uniformTypeDescriptor.UniformDataType.HYPERLINK) { 267 let hyperlink = record as unifiedDataChannel.Hyperlink; 268 console.info(`linkUrl: ${hyperlink.url}`); 269 } 270} 271``` 272 273### hasType<sup>12+</sup> 274 275hasType(type: string): boolean 276 277Checks whether this **UnifiedData** object has the specified data type. 278 279**Atomic service API**: This API can be used in atomic services since API version 12. 280 281**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 282 283| Name| Type | Mandatory| Description | 284| ------ | ------------------------------- | ---- |---------------------------------------------| 285| type | string | Yes | Data type to check. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 286 287**Return value** 288 289| Type | Description | 290| ---------------------------------------- |-------------------------| 291| boolean | Returns **true** if the specified data type exists; returns **false** otherwise.| 292 293**Error codes** 294 295For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 296 297| **ID**| **Error Message** | 298| ------------ | ------------------------------------------- | 299| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 300 301**Example** 302 303```ts 304import { uniformTypeDescriptor } from '@kit.ArkData'; 305 306let text = new unifiedDataChannel.PlainText(); 307text.textContent = 'this is textContent of text'; 308let unifiedData = new unifiedDataChannel.UnifiedData(text); 309 310let link = new unifiedDataChannel.Hyperlink(); 311link.url = 'www.XXX.com'; 312unifiedData.addRecord(link); 313 314let hasPlainText = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT); 315let hasLink = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.HYPERLINK); 316``` 317 318### getTypes<sup>12+</sup> 319 320getTypes(): Array\<string\> 321 322Obtains the types of all data records in this **UnifiedData** object. 323 324**Atomic service API**: This API can be used in atomic services since API version 12. 325 326**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 327 328**Return value** 329 330| Type | Description | 331| ---------------------------------------- |-------------------------| 332| Array\<string\> | Array of the [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype) types obtained.| 333 334**Example** 335 336```ts 337let text = new unifiedDataChannel.PlainText(); 338text.textContent = 'this is textContent of text'; 339let unifiedData = new unifiedDataChannel.UnifiedData(text); 340 341let link = new unifiedDataChannel.Hyperlink(); 342link.url = 'www.XXX.com'; 343unifiedData.addRecord(link); 344 345let types = unifiedData.getTypes(); 346``` 347 348## Summary 349 350Represents the abstract of a uniform data object, including the data type and size. 351 352**Atomic service API**: This API can be used in atomic services since API version 11. 353 354**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 355 356| Name| Type| Read-Only| Optional| Description| 357| -------- | -------- | -------- | -------- | -------- | 358| summary | Record<string, number> | No| No| Dictionary type object, where the key indicates the data type (see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)), and the value indicates the total size (in bytes) of this type of records in the unified data object.| 359| totalSize | number | No| No| Total size of all the records in the **UnifiedData** object, in bytes.| 360 361## UnifiedRecord 362 363An abstract definition of the data content supported by the UDMF. A **UnifiedRecord** object contains one or more data records, for example, a text record, an image record, or an HTML record. 364 365### constructor<sup>12+</sup> 366 367constructor() 368 369A constructor used to create a **UnfiedRecord** object. 370 371**Atomic service API**: This API can be used in atomic services since API version 12. 372 373**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 374 375**Example** 376 377```ts 378let unifiedRecord = new unifiedDataChannel.UnifiedRecord(); 379``` 380 381### constructor<sup>12+</sup> 382 383constructor(type: string, value: ValueType) 384 385A constructor used to create a data record with the specified type and value.<br>If **value** is of the [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) type, **type** must be the value of **OPENHARMONY_PIXEL_MAP** in [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).<br>If **value** is of the [Want](../apis-ability-kit/js-apis-app-ability-want.md) type, **type** must be the value of **OPENHARMONY_WANT** in [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype). 386 387**Atomic service API**: This API can be used in atomic services since API version 12. 388 389**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 390 391**Parameters** 392 393| Name| Type | Mandatory| Description | 394| ------ | ------------------------------- | ---- |-----------------------------------------| 395| type | string | Yes | Type of the data record to create.| 396| value | [ValueType](#valuetype12) | Yes | Value of the data record to create.| 397 398**Error codes** 399 400For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 401 402| **ID**| **Error Message** | 403| ------------ | ------------------------------------------- | 404| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 405 406**Example** 407 408```ts 409import { image } from '@kit.ImageKit'; 410import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 411import { Want } from '@kit.AbilityKit'; 412 413let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text'); 414let link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, 'www.XXX.com'); 415let object: Want = { 416 bundleName: 'com.example.myapplication', 417 abilityName: 'entryAbility', 418}; 419let wantRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_WANT, object); 420 421const color = new ArrayBuffer(96); 422let opts: image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }; 423let pixelMap = image.createPixelMapSync(color, opts); 424let pixelMapRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP, pixelMap); 425 426let hyperlinkDetails : Record<string, string> = { 427 'attr1': 'value1', 428 'attr2': 'value2', 429} 430let hyperlink : uniformDataStruct.Hyperlink = { 431 uniformDataType:'general.hyperlink', 432 url : 'www.XXX.com', 433 description : 'This is the description of this hyperlink', 434 details : hyperlinkDetails, 435} 436let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 437``` 438 439### getType 440 441getType(): string 442 443Obtains the type of this **UnfiedRecord**. The data obtained by [getRecords](#getrecords) from the **UnifiedData** object is a **UnifiedRecord** object. You need to use this API to obtain the specific type of the record, convert the **UnifiedRecord** object to its child class, and call the child class interfaces. 444 445**Atomic service API**: This API can be used in atomic services since API version 11. 446 447**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 448 449**Return value** 450 451| Type | Description | 452| ------ |------------------------------------------------------| 453| string | Data type obtained. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 454 455**Example** 456 457```ts 458import { uniformTypeDescriptor } from '@kit.ArkData'; 459 460let text = new unifiedDataChannel.PlainText(); 461text.textContent = 'this is textContent of text'; 462let unifiedData = new unifiedDataChannel.UnifiedData(text); 463 464let records = unifiedData.getRecords(); 465if (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 466 let plainText = records[0] as unifiedDataChannel.PlainText; 467 console.info(`textContent: ${plainText.textContent}`); 468} 469``` 470 471### getValue<sup>12+</sup> 472 473getValue(): ValueType 474 475Obtains the value of this data record. 476 477**Atomic service API**: This API can be used in atomic services since API version 12. 478 479**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 480 481**Return value** 482 483| Type | Description | 484| ------ |------------------------------------------------------| 485| [ValueType](#valuetype12) | Value obtained.| 486 487**Example** 488 489```ts 490import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 491 492let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text'); 493let value = text.getValue(); 494 495let hyperlinkDetails : Record<string, string> = { 496 'attr1': 'value1', 497 'attr2': 'value2', 498} 499let hyperlink : uniformDataStruct.Hyperlink = { 500 uniformDataType:'general.hyperlink', 501 url : 'www.XXX.com', 502 description : 'This is the description of this hyperlink', 503 details : hyperlinkDetails, 504} 505let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 506let hyperlinkValue = hyperlinkRecord.getValue(); 507``` 508 509## Text 510 511Represents the text data. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of text data. You are advised to use the child class of **Text**, for example, [PlainText](#plaintext), [Hyperlink](#hyperlink), and [HTML](#html), to describe data. 512 513**Atomic service API**: This API can be used in atomic services since API version 11. 514 515**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 516 517| Name| Type| Read-Only| Optional| Description| 518| -------- | -------- | -------- | -------- | -------- | 519| details | Record<string, string> | No| Yes| A dictionary type object, where both the key and value are of the string type and are used to describe the text content. For example, a data object with the following content can be created to describe a text file:<br>{<br>"title":"Title",<br>"content":"Content"<br>}<br> The default value is an empty dictionary object.| 520 521**Example** 522 523```ts 524let text = new unifiedDataChannel.Text(); 525text.details = { 526 title: 'MyTitle', 527 content: 'this is content', 528}; 529let unifiedData = new unifiedDataChannel.UnifiedData(text); 530``` 531 532## PlainText 533 534Represents the plaintext data. It is a child class of [Text](#text) and is used to describe plaintext data. 535 536**Atomic service API**: This API can be used in atomic services since API version 11. 537 538**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 539 540| Name| Type| Read-Only| Optional| Description| 541| -------- | -------- | -------- | -------- | -------- | 542| textContent | string | No| No| Plaintext content. | 543| abstract | string | No| Yes| Text abstract. This parameter is optional. The default value is an empty string.| 544 545**Example** 546 547```ts 548let text = new unifiedDataChannel.PlainText(); 549text.textContent = 'this is textContent'; 550text.abstract = 'this is abstract'; 551``` 552 553## Hyperlink 554 555Represents hyperlink data. It is a child class of [Text](#text) and is used to describe data of the hyperlink type. 556 557**Atomic service API**: This API can be used in atomic services since API version 11. 558 559**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 560 561| Name| Type| Read-Only| Optional| Description| 562| -------- | -------- | -------- | -------- | -------- | 563| url | string | No| No| URL. | 564| description | string | No| Yes| Description of the linked content. This parameter is optional. The default value is an empty string.| 565 566**Example** 567 568```ts 569let link = new unifiedDataChannel.Hyperlink(); 570link.url = 'www.XXX.com'; 571link.description = 'this is description'; 572``` 573 574## HTML 575 576Represents the HTML data. It is a child class of [Text](#text) and is used to describe HTML data. 577 578**Atomic service API**: This API can be used in atomic services since API version 11. 579 580**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 581 582| Name| Type| Read-Only| Optional| Description| 583| -------- | -------- | -------- | -------- | -------- | 584| htmlContent | string | No| No| Content in HTML format. | 585| plainContent | string | No| Yes| Plaintext without HTML tags. This parameter is optional. The default value is an empty string.| 586 587**Example** 588 589```ts 590let html = new unifiedDataChannel.HTML(); 591html.htmlContent = '<div><p>Title</p></div>'; 592html.plainContent = 'this is plainContent'; 593``` 594 595## File 596 597Represents the file data. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of the data of the file type. You are advised to use the child class of **File**, for example, [Image](#image), [Video](#video), and [Folder](#folder), to describe data. 598 599**Atomic service API**: This API can be used in atomic services since API version 11. 600 601**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 602 603| Name| Type| Read-Only| Optional| Description| 604| -------- | -------- | -------- | -------- | -------- | 605| details | Record<string, string> | No| Yes| A dictionary type object, where both the key and value are of the string type and are used to describe file information. For example, a data object with the following content can be created to describe a file:<br>{<br>"name":"File name",<br>"type":"File type"<br>}<br> The default value is an empty dictionary object.| 606| uri | string | No| No| URI of the file data. | 607 608**Example** 609 610```ts 611let file = new unifiedDataChannel.File(); 612file.details = { 613 name: 'test', 614 type: 'txt', 615}; 616file.uri = 'schema://com.samples.test/files/test.txt'; 617``` 618 619## Image 620 621Represents the image data. It is a child class of [File](#file) and is used to describe images. 622 623**Atomic service API**: This API can be used in atomic services since API version 11. 624 625**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 626 627| Name| Type| Read-Only| Optional| Description| 628| -------- | -------- | -------- | -------- | -------- | 629| imageUri | string | No| No| URI of the image.| 630 631**Example** 632 633```ts 634let image = new unifiedDataChannel.Image(); 635image.imageUri = 'schema://com.samples.test/files/test.jpg'; 636``` 637 638## Video 639 640Represents video data. It is a child class of [File](#file) and is used to describe a video file. 641 642**Atomic service API**: This API can be used in atomic services since API version 11. 643 644**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 645 646| Name| Type| Read-Only| Optional| Description| 647| -------- | -------- | -------- | -------- | -------- | 648| videoUri | string | No| No| URI of the video file.| 649 650**Example** 651 652```ts 653let video = new unifiedDataChannel.Video(); 654video.videoUri = 'schema://com.samples.test/files/test.mp4'; 655``` 656 657## Audio 658 659Represents audio data. It is a child class of [File](#file) and is used to describe an audio file. 660 661**Atomic service API**: This API can be used in atomic services since API version 11. 662 663**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 664 665| Name| Type| Read-Only| Optional| Description| 666| -------- | -------- | -------- | -------- | -------- | 667| audioUri | string | No| No| Audio data URI.| 668 669**Example** 670 671```ts 672let audio = new unifiedDataChannel.Audio(); 673audio.audioUri = 'schema://com.samples.test/files/test.mp3'; 674``` 675 676## Folder 677 678Represents the folder data. It is a child class of [File](#file) and is used to describe a folder. 679 680**Atomic service API**: This API can be used in atomic services since API version 11. 681 682**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 683 684| Name| Type| Read-Only| Optional| Description| 685| -------- | -------- | -------- | -------- | -------- | 686| folderUri | string | No| No| URI of the folder.| 687 688**Example** 689 690```ts 691let folder = new unifiedDataChannel.Folder(); 692folder.folderUri = 'schema://com.samples.test/files/folder/'; 693``` 694 695## SystemDefinedRecord 696 697Represents specific data types defined by OpenHarmony. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of OpenHarmony-specific data types. You are advised to use the child class of **SystemDefinedRecord**, for example, [SystemDefinedForm](#systemdefinedform), [SystemDefinedAppItem](#systemdefinedappitem), and [SystemDefinedPixelMap](#systemdefinedpixelmap), to describe OpenHarmony-specific data. 698 699**Atomic service API**: This API can be used in atomic services since API version 11. 700 701**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 702 703| Name| Type| Read-Only| Optional| Description| 704| -------- | -------- | -------- | -------- | -------- | 705| details | Record<string, number \| string \| Uint8Array> | No| Yes| A dictionary type object, where the key is of the string type, and the value can be a number, a string, or a Uint8Array. The default value is an empty dictionary object.| 706 707**Example** 708 709```ts 710let sdr = new unifiedDataChannel.SystemDefinedRecord(); 711let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 712sdr.details = { 713 title: 'recordTitle', 714 version: 1, 715 content: u8Array, 716}; 717let unifiedData = new unifiedDataChannel.UnifiedData(sdr); 718``` 719 720## SystemDefinedForm 721 722Represents the service widget data defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord). 723 724**Atomic service API**: This API can be used in atomic services since API version 11. 725 726**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 727 728| Name| Type| Read-Only| Optional| Description| 729| -------- | -------- | -------- | -------- | -------- | 730| formId | number | No| No| Service widget ID. | 731| formName | string | No| No| Widget name. | 732| bundleName | string | No| No| Name of the bundle to which the widget belongs. | 733| abilityName | string | No| No| Ability name corresponding to the widget.| 734| module | string | No| No| Name of the module to which the widget belongs. | 735 736**Example** 737 738```ts 739let form = new unifiedDataChannel.SystemDefinedForm(); 740form.formId = 123456; 741form.formName = 'MyFormName'; 742form.bundleName = 'MyBundleName'; 743form.abilityName = 'MyAbilityName'; 744form.module = 'MyModule'; 745let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 746form.details = { 747 formKey1: 123, 748 formKey2: 'formValue', 749 formKey3: u8Array, 750}; 751let unifiedData = new unifiedDataChannel.UnifiedData(form); 752``` 753 754## SystemDefinedAppItem 755 756Represents the data of the home screen icon defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord). 757 758**Atomic service API**: This API can be used in atomic services since API version 11. 759 760**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 761 762| Name| Type| Read-Only| Optional| Description| 763| -------- | -------- | -------- | -------- | -------- | 764| appId | string | No| No| ID of the application, for which the icon is used. | 765| appName | string | No| No| Name of the application, for which the icon is used. | 766| appIconId | string | No| No| Image ID of the icon. | 767| appLabelId | string | No| No| Label ID corresponding to the icon name. | 768| bundleName | string | No| No| Bundle name corresponding to the icon.| 769| abilityName | string | No| No| Application ability name corresponding to the icon.| 770 771**Example** 772 773```ts 774let appItem = new unifiedDataChannel.SystemDefinedAppItem(); 775appItem.appId = 'MyAppId'; 776appItem.appName = 'MyAppName'; 777appItem.appIconId = 'MyAppIconId'; 778appItem.appLabelId = 'MyAppLabelId'; 779appItem.bundleName = 'MyBundleName'; 780appItem.abilityName = 'MyAbilityName'; 781let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 782appItem.details = { 783 appItemKey1: 123, 784 appItemKey2: 'appItemValue', 785 appItemKey3: u8Array, 786}; 787let unifiedData = new unifiedDataChannel.UnifiedData(appItem); 788``` 789 790## SystemDefinedPixelMap 791 792Represents the image data type corresponding to [PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord) and holds only binary data of PixelMap. 793 794**Atomic service API**: This API can be used in atomic services since API version 11. 795 796**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 797 798| Name| Type| Read-Only| Optional| Description| 799| -------- | -------- | -------- | -------- | -------- | 800| rawData | Uint8Array | No| No| Binary data of the **PixelMap** object.| 801 802**Example** 803 804```ts 805import { image } from '@kit.ImageKit'; // Module where the PixelMap class is defined. 806import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData'; 807import { BusinessError } from '@kit.BasicServicesKit'; 808 809const color = new ArrayBuffer(96); // Create a PixelMap object. 810let opts: image.InitializationOptions = { 811 editable: true, pixelFormat: 3, size: { 812 height: 4, width: 6 813 } 814} 815image.createPixelMap(color, opts, (error, pixelmap) => { 816 if (error) { 817 console.error('Failed to create pixelmap.'); 818 } else { 819 console.info('Succeeded in creating pixelmap.'); 820 let arrayBuf = new ArrayBuffer(pixelmap.getPixelBytesNumber()); 821 pixelmap.readPixelsToBuffer(arrayBuf); 822 let u8Array = new Uint8Array(arrayBuf); 823 let sdpixel = new unifiedDataChannel.SystemDefinedPixelMap(); 824 sdpixel.rawData = u8Array; 825 let unifiedData = new unifiedDataChannel.UnifiedData(sdpixel); 826 827 // Read the record of the pixelMap type from unifiedData. 828 let records = unifiedData.getRecords(); 829 for (let i = 0; i < records.length; i++) { 830 if (records[i].getType() === uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP) { 831 let pixelmapRecord = records[i] as unifiedDataChannel.SystemDefinedPixelMap; 832 let newArraybuf = pixelmapRecord.rawData.buffer; 833 pixelmap.writeBufferToPixels(newArraybuf).then(() => { 834 console.info('Succeeded in writing data from buffer to a pixelMap'); 835 }).catch((error: BusinessError) => { 836 console.error(`Failed to write data from a buffer to a PixelMap. code is ${error.code}, message is ${error.message}`); 837 }) 838 } 839 } 840 } 841}) 842``` 843 844## ApplicationDefinedRecord 845 846Represents the custom data type for applications only. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of custom data types of applications. Applications can extend custom data types based on this class. 847 848**Atomic service API**: This API can be used in atomic services since API version 11. 849 850**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 851 852| Name| Type| Read-Only| Optional| Description| 853| -------- | -------- | -------- | -------- | -------- | 854| applicationDefinedType | string | No| No| Application's custom data type identifier, which must start with **ApplicationDefined**.| 855| rawData | Uint8Array | No| No| Binary data of the custom data type. | 856 857**Example** 858 859```ts 860let record = new unifiedDataChannel.ApplicationDefinedRecord(); 861let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 862record.applicationDefinedType = 'ApplicationDefinedType'; 863record.rawData = u8Array; 864let unifiedData = new unifiedDataChannel.UnifiedData(record); 865``` 866 867## Intention 868 869Enumerates the data channel types supported by the UDMF. It is used to identify different service scenarios, to which the UDMF data channels apply. 870 871**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 872 873| Name | Value | Description | 874|----------|-----------|---------| 875| DATA_HUB | 'DataHub' | Public data channel.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 876 877## Options 878 879Defines the data operation performed by the UDMF. It includes two optional parameters: **intention** and **key**. The two parameters have no default value, and can be left unspecified. For details, see the parameter description of the specific API. 880 881**Atomic service API**: This API can be used in atomic services since API version 11. 882 883**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 884 885 886| Name | Type | Read-Only| Optional| Description | 887|-----------|-------------------------|----|----|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 888| intention | [Intention](#intention) | No | Yes | Type of the data channel related to the data operation. | 889| key | string | No | Yes | Unique identifier of the data object in the UDMF, which can be obtained from the value returned by [insertData](#unifieddatachannelinsertdata).<br>The key consists of **udmf:/**, **intention**, **bundleName**, and **groupId** with a (/) in between, for example, **udmf://DataHub/com.ohos.test/0123456789**.<br>**udmf:/** is fixed, **DataHub** is an enum of **intention**, **com.ohos.test** is the bundle name, and **0123456789** is a group ID randomly generated.| 890 891 892 893## unifiedDataChannel.insertData 894 895insertData(options: Options, data: UnifiedData, callback: AsyncCallback<string>): void 896 897Inserts data to the UDMF public data channel. This API uses an asynchronous callback to return the unique identifier of the data inserted. 898 899**Atomic service API**: This API can be used in atomic services since API version 11. 900 901**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 902 903**Parameters** 904 905| Name | Type | Mandatory| Description | 906|----------|----------------------------|----|------------------------------| 907| options | [Options](#options) | Yes | Configuration parameters. Only the **intention** is required. | 908| data | [UnifiedData](#unifieddata) | Yes | Data to insert. | 909| callback | AsyncCallback<string> | Yes | Callback used to return the key (unique identifier) of the data inserted.| 910 911**Error codes** 912 913For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 914 915| **ID**| **Error Message** | 916| ------------ | ------------------------------------------- | 917| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 918 919**Example** 920 921```ts 922import { unifiedDataChannel } from '@kit.ArkData'; 923import { BusinessError } from '@kit.BasicServicesKit'; 924 925let plainText = new unifiedDataChannel.PlainText(); 926plainText.textContent = 'hello world!'; 927let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 928 929let options: unifiedDataChannel.Options = { 930 intention: unifiedDataChannel.Intention.DATA_HUB 931} 932try { 933 unifiedDataChannel.insertData(options, unifiedData, (err, data) => { 934 if (err === undefined) { 935 console.info(`Succeeded in inserting data. key = ${data}`); 936 } else { 937 console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 938 } 939 }); 940 } catch (e) { 941 let error: BusinessError = e as BusinessError; 942 console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 943} 944 945``` 946 947## unifiedDataChannel.insertData 948 949insertData(options: Options, data: UnifiedData): Promise<string> 950 951Inserts data to the UDMF public data channel. This API uses a promise to return the unique identifier of the data inserted. 952 953**Atomic service API**: This API can be used in atomic services since API version 11. 954 955**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 956 957**Parameters** 958 959| Name | Type | Mandatory| Description | 960|---------|-----------------------------|----|-----------------------| 961| options | [Options](#options) | Yes | Configuration parameters. Only the **intention** is required.| 962| data | [UnifiedData](#unifieddata) | Yes | Data to insert. | 963 964**Return value** 965 966| Type | Description | 967|-----------------------|-----------------------------------| 968| Promise<string> | Promise used to return the key of the data inserted.| 969 970**Error codes** 971 972For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 973 974| **ID**| **Error Message** | 975| ------------ | ------------------------------------------- | 976| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 977 978**Example** 979 980```ts 981import { unifiedDataChannel } from '@kit.ArkData'; 982import { BusinessError } from '@kit.BasicServicesKit'; 983 984let plainText = new unifiedDataChannel.PlainText(); 985plainText.textContent = 'hello world!'; 986let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 987 988let options: unifiedDataChannel.Options = { 989 intention: unifiedDataChannel.Intention.DATA_HUB 990} 991try { 992 unifiedDataChannel.insertData(options, unifiedData).then((data) => { 993 console.info(`Succeeded in inserting data. key = ${data}`); 994 }).catch((err: BusinessError) => { 995 console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `); 996 }); 997} catch (e) { 998 let error: BusinessError = e as BusinessError; 999 console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `); 1000} 1001``` 1002 1003## unifiedDataChannel.updateData 1004 1005updateData(options: Options, data: UnifiedData, callback: AsyncCallback<void>): void 1006 1007Updates the data in the UDMF public data channel. This API uses an asynchronous callback to return the result. 1008 1009**Atomic service API**: This API can be used in atomic services since API version 11. 1010 1011**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1012 1013**Parameters** 1014 1015| Name | Type | Mandatory| Description | 1016|----------|-----------------------------|----|-------------------------------------| 1017| options | [Options](#options) | Yes | Configuration parameters. Only the value of **key** is required. | 1018| data | [UnifiedData](#unifieddata) | Yes | New data. | 1019| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the data is updated successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 1020 1021**Error codes** 1022 1023For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1024 1025| **ID**| **Error Message** | 1026| ------------ | ------------------------------------------- | 1027| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1028 1029**Example** 1030 1031```ts 1032import { unifiedDataChannel } from '@kit.ArkData'; 1033import { BusinessError } from '@kit.BasicServicesKit'; 1034 1035let plainText = new unifiedDataChannel.PlainText(); 1036plainText.textContent = 'hello world!'; 1037let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 1038 1039let options: unifiedDataChannel.Options = { 1040 key: 'udmf://DataHub/com.ohos.test/0123456789' 1041}; 1042 1043try { 1044 unifiedDataChannel.updateData(options, unifiedData, (err) => { 1045 if (err === undefined) { 1046 console.info('Succeeded in updating data.'); 1047 } else { 1048 console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 1049 } 1050 }); 1051} catch (e) { 1052 let error: BusinessError = e as BusinessError; 1053 console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 1054} 1055``` 1056 1057## unifiedDataChannel.updateData 1058 1059updateData(options: Options, data: UnifiedData): Promise<void> 1060 1061Updates the data in the UDMF public data channel. This API uses a promise to return the result. 1062 1063**Atomic service API**: This API can be used in atomic services since API version 11. 1064 1065**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1066 1067**Parameters** 1068 1069| Name | Type | Mandatory| Description | 1070|---------|-----------------------------|----|-----------------| 1071| options | [Options](#options) | Yes | Configuration parameters. Only the value of **key** is required.| 1072| data | [UnifiedData](#unifieddata) | Yes | New data. | 1073 1074**Return value** 1075 1076| Type | Description | 1077|---------------------|----------------------------| 1078| Promise<void> | Promise that returns no value.| 1079 1080**Error codes** 1081 1082For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1083 1084| **ID**| **Error Message** | 1085| ------------ | ------------------------------------------- | 1086| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1087 1088**Example** 1089 1090```ts 1091import { unifiedDataChannel } from '@kit.ArkData'; 1092import { BusinessError } from '@kit.BasicServicesKit'; 1093 1094let plainText = new unifiedDataChannel.PlainText(); 1095plainText.textContent = 'hello world!'; 1096let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 1097 1098let options: unifiedDataChannel.Options = { 1099 key: 'udmf://DataHub/com.ohos.test/0123456789' 1100}; 1101 1102try { 1103 unifiedDataChannel.updateData(options, unifiedData).then(() => { 1104 console.info('Succeeded in updating data.'); 1105 }).catch((err: BusinessError) => { 1106 console.error(`Failed to update data. code is ${err.code},message is ${err.message} `); 1107 }); 1108} catch (e) { 1109 let error: BusinessError = e as BusinessError; 1110 console.error(`Update data throws an exception. code is ${error.code},message is ${error.message} `); 1111} 1112``` 1113 1114## unifiedDataChannel.queryData 1115 1116queryData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 1117 1118Queries data in the UDMF public data channel. This API uses an asynchronous callback to return the result. 1119 1120**Atomic service API**: This API can be used in atomic services since API version 11. 1121 1122**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1123 1124**Parameters** 1125 1126| Name | Type | Mandatory| Description | 1127|----------|---------------------------------------------------------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1128| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in. | 1129| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | Yes | Callback used to return the queried data.<br>If only the **key** is specified in **options**, the data corresponding to the key is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** is returned.<br>If both **intention** and **key** are specified, the intersection of the two is returned, which is the result obtained when only **key** is specified. If there is no intersection between the specified **intention** and **key**, an error object is returned.| 1130 1131**Error codes** 1132 1133For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1134 1135| **ID**| **Error Message** | 1136| ------------ | ------------------------------------------- | 1137| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1138 1139**Example** 1140 1141```ts 1142import { unifiedDataChannel } from '@kit.ArkData'; 1143import { uniformTypeDescriptor } from '@kit.ArkData'; 1144import { BusinessError } from '@kit.BasicServicesKit'; 1145 1146let options: unifiedDataChannel.Options = { 1147 intention: unifiedDataChannel.Intention.DATA_HUB 1148}; 1149 1150try { 1151 unifiedDataChannel.queryData(options, (err, data) => { 1152 if (err === undefined) { 1153 console.info(`Succeeded in querying data. size = ${data.length}`); 1154 for (let i = 0; i < data.length; i++) { 1155 let records = data[i].getRecords(); 1156 for (let j = 0; j < records.length; j++) { 1157 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1158 let text = records[j] as unifiedDataChannel.PlainText; 1159 console.info(`${i + 1}.${text.textContent}`); 1160 } 1161 } 1162 } 1163 } else { 1164 console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 1165 } 1166 }); 1167} catch (e) { 1168 let error: BusinessError = e as BusinessError; 1169 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 1170} 1171``` 1172 1173## unifiedDataChannel.queryData 1174 1175queryData(options: Options): Promise<Array<UnifiedData>> 1176 1177Queries data in the UDMF public data channel. This API uses a promise to return the result. 1178 1179**Atomic service API**: This API can be used in atomic services since API version 11. 1180 1181**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1182 1183**Parameters** 1184 1185| Name | Type | Mandatory| Description | 1186|---------|---------------------|----|-----------------------------------------------| 1187| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in.| 1188 1189**Return value** 1190 1191| Type | Description | 1192|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| 1193| Promise<Array<[UnifiedData](#unifieddata)>> | Promise used to return the result.<br>If only the **key** is specified in **options**, the data corresponding to the key is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** is returned.<br>If both **intention** and **key** are specified, the intersection of the two is returned, which is the result obtained when only **key** is specified. If there is no intersection between the specified **intention** and **key**, an error object is returned.| 1194 1195**Error codes** 1196 1197For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1198 1199| **ID**| **Error Message** | 1200| ------------ | ------------------------------------------- | 1201| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1202 1203**Example** 1204 1205```ts 1206import { unifiedDataChannel } from '@kit.ArkData'; 1207import { uniformTypeDescriptor } from '@kit.ArkData'; 1208import { BusinessError } from '@kit.BasicServicesKit'; 1209 1210let options: unifiedDataChannel.Options = { 1211 key: 'udmf://DataHub/com.ohos.test/0123456789' 1212}; 1213 1214try { 1215 unifiedDataChannel.queryData(options).then((data) => { 1216 console.info(`Succeeded in querying data. size = ${data.length}`); 1217 for (let i = 0; i < data.length; i++) { 1218 let records = data[i].getRecords(); 1219 for (let j = 0; j < records.length; j++) { 1220 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1221 let text = records[j] as unifiedDataChannel.PlainText; 1222 console.info(`${i + 1}.${text.textContent}`); 1223 } 1224 } 1225 } 1226 }).catch((err: BusinessError) => { 1227 console.error(`Failed to query data. code is ${err.code},message is ${err.message} `); 1228 }); 1229} catch (e) { 1230 let error: BusinessError = e as BusinessError; 1231 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 1232} 1233``` 1234 1235## unifiedDataChannel.deleteData 1236 1237deleteData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 1238 1239Deletes data from the UDMF public data channel. This API uses an asynchronous callback to return the result. 1240 1241**Atomic service API**: This API can be used in atomic services since API version 11. 1242 1243**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1244 1245**Parameters** 1246 1247| Name | Type | Mandatory| Description | 1248|----------|---------------------------------------------------------------|----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1249| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in. | 1250| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | Yes | Callback used to return the data deleted.<br>If only the **key** is specified in **options**, the data corresponding to the key deleted is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** deleted is returned.<br>If both **intention** and **key** are specified, the intersection of the two deleted is returned. If there is no intersection between the two, an error object is returned.| 1251 1252**Error codes** 1253 1254For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1255 1256| **ID**| **Error Message** | 1257| ------------ | ------------------------------------------- | 1258| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1259 1260**Example** 1261 1262```ts 1263import { unifiedDataChannel } from '@kit.ArkData'; 1264import { uniformTypeDescriptor } from '@kit.ArkData'; 1265import { BusinessError } from '@kit.BasicServicesKit'; 1266 1267let options: unifiedDataChannel.Options = { 1268 intention: unifiedDataChannel.Intention.DATA_HUB 1269}; 1270 1271try { 1272 unifiedDataChannel.deleteData(options, (err, data) => { 1273 if (err === undefined) { 1274 console.info(`Succeeded in deleting data. size = ${data.length}`); 1275 for (let i = 0; i < data.length; i++) { 1276 let records = data[i].getRecords(); 1277 for (let j = 0; j < records.length; j++) { 1278 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1279 let text = records[j] as unifiedDataChannel.PlainText; 1280 console.info(`${i + 1}.${text.textContent}`); 1281 } 1282 } 1283 } 1284 } else { 1285 console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `); 1286 } 1287 }); 1288} catch (e) { 1289 let error: BusinessError = e as BusinessError; 1290 console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `); 1291} 1292``` 1293 1294## unifiedDataChannel.deleteData 1295 1296deleteData(options: Options): Promise<Array<UnifiedData>> 1297 1298Deletes data from the UDMF public data channel. This API uses a promise to return the result. 1299 1300**Atomic service API**: This API can be used in atomic services since API version 11. 1301 1302**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1303 1304**Parameters** 1305 1306| Name | Type | Mandatory| Description | 1307|---------|---------------------|----|--------| 1308| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in.| 1309 1310**Return value** 1311 1312| Type | Description | 1313|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| 1314| Promise<Array<[UnifiedData](#unifieddata)>> | Promise used to return the data deleted.<br>If only the **key** is specified in **options**, the data corresponding to the key deleted is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** deleted is returned.<br>If both **intention** and **key** are specified, the intersection of the two deleted is returned. If there is no intersection between the two, an error object is returned.| 1315 1316**Error codes** 1317 1318For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1319 1320| **ID**| **Error Message** | 1321| ------------ | ------------------------------------------- | 1322| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1323 1324**Example** 1325 1326```ts 1327import { unifiedDataChannel } from '@kit.ArkData'; 1328import { uniformTypeDescriptor } from '@kit.ArkData'; 1329import { BusinessError } from '@kit.BasicServicesKit'; 1330 1331let options: unifiedDataChannel.Options = { 1332 key: 'udmf://DataHub/com.ohos.test/0123456789' 1333}; 1334 1335try { 1336 unifiedDataChannel.deleteData(options).then((data) => { 1337 console.info(`Succeeded in deleting data. size = ${data.length}`); 1338 for (let i = 0; i < data.length; i++) { 1339 let records = data[i].getRecords(); 1340 for (let j = 0; j < records.length; j++) { 1341 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 1342 let text = records[j] as unifiedDataChannel.PlainText; 1343 console.info(`${i + 1}.${text.textContent}`); 1344 } 1345 } 1346 } 1347 }).catch((err: BusinessError) => { 1348 console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `); 1349 }); 1350} catch (e) { 1351 let error: BusinessError = e as BusinessError; 1352 console.error(`Query data throws an exception. code is ${error.code},message is ${error.message} `); 1353} 1354``` 1355