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```js 12import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; 13``` 14 15## UnifiedData 16 17Provides APIs for encapsulating a set of data records. 18 19**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 20 21### constructor 22 23constructor(record: UnifiedRecord) 24 25A constructor used to create a **UnifiedData** object with a data record. 26 27**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 28 29**Parameters** 30 31| Name| Type | Mandatory| Description | 32| ------ | ------------------------------- | ---- |-----------------------------------------| 33| record | [UnifiedRecord](#unifiedrecord) | Yes | Data record in the **UnifiedData** object. It is a **UnifiedRecord** child class object.| 34 35**Example** 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 47Adds a data record to this **UnifiedRecord** object. 48 49**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 50 51**Parameters** 52 53| Name| Type | Mandatory| Description | 54| ------ | ------------------------------- | ---- |---------------------------------------------| 55| record | [UnifiedRecord](#unifiedrecord) | Yes | Data record to add. It is a **UnifiedRecord** child class object.| 56 57**Example** 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 73Obtains 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. 74 75**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 76 77**Return value** 78 79| Type | Description | 80| ---------------------------------------- |-------------------------| 81| Array\<[UnifiedRecord](#unifiedrecord)\> | Records in the **UnifiedData** object obtained.| 82 83**Example** 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 111Defines the summary of a **UnifiedData object**, including the data types and sizes. Currently, it is not supported. 112 113**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 114 115| Name | Type | Readable| Writable| Description | 116| --------- | ------------------------- | ---- | ---- |-----------------------------------------------------------------------------------| 117| summary | Record<string, number> | Yes | 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 **UnifiedData** object.| 118| totalSize | number | Yes | No | Total size of all the records in the **UnifiedData** object, in bytes. | 119 120## UnifiedRecord 121 122An 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. 123 124**UnifiedRecord** is an abstract parent class that does not hold specific data content. It cannot be added to a **UnifiedData** object directly. Instead, a child class with specific content, such as text and image, must be created. 125 126### getType 127 128getType(): string 129 130Obtains 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. 131 132**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 133 134**Return value** 135 136| Type | Description | 137| ------ |------------------------------------------------------| 138| string | Data type obtained. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 139 140**Example** 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 158Represents 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. 159 160**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 161 162| Name | Type | Readable| Writable| Description | 163| ------- | ------------------------- | ---- | ---- |-----------------------------------------------------------------------------------------------------------------------------------------------------| 164| details | Record<string, string> | Yes | 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>This parameter is optional. The default value is an empty dictionary object. | 165 166**Example** 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 179Represents the plaintext data. It is a child class of [Text](#text) and is used to describe plaintext data. 180 181**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 182 183| Name | Type | Readable| Writable| Description | 184| ----------- | ------ | ---- | ---- |-----------------------| 185| textContent | string | Yes | Yes | Plaintext content. | 186| abstract | string | Yes | Yes | Text abstract. This parameter is optional. The default value is an empty string.| 187 188**Example** 189 190```js 191let text = new unifiedDataChannel.PlainText(); 192text.textContent = 'this is textContent'; 193text.abstract = 'this is abstract'; 194``` 195 196## Hyperlink 197 198Represents hyperlink data. It is a child class of [Text](#text) and is used to describe data of the hyperlink type. 199 200**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 201 202| Name | Type | Readable| Writable| Description | 203| ----------- | ------ | ---- | ---- |--------------| 204| url | string | Yes | Yes | URL. | 205| description | string | Yes | Yes | Description of the linked content. This parameter is optional. The default value is an empty string.| 206 207**Example** 208 209```js 210let link = new unifiedDataChannel.Hyperlink(); 211link.url = 'www.XXX.com'; 212link.description = 'this is description'; 213``` 214 215## HTML 216 217Represents the HTML data. It is a child class of [Text](#text) and is used to describe HTML data. 218 219**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 220 221| Name | Type | Readable| Writable| Description | 222| ------------ | ------ | ---- | ---- |-----------------------| 223| htmlContent | string | Yes | Yes | Content in HTML format. | 224| plainContent | string | Yes | Yes | Plaintext without HTML tags. This parameter is optional. The default value is an empty string.| 225 226**Example** 227 228```js 229let html = new unifiedDataChannel.HTML(); 230html.htmlContent = '<div><p>Title</p></div>'; 231html.plainContent = 'this is plainContent'; 232``` 233 234## File 235 236Represents 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. 237 238**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 239 240| Name | Type | Readable| Writable| Description | 241|---------|---------------------------| ---- | ---- |------------------------------------------------------------------------------------------------------------------------------------------------------| 242| details | Record<string, string> | Yes | 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>This parameter is optional. The default value is an empty dictionary object. | 243| uri | string | Yes | Yes | URI of the file data. | 244 245**Example** 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 258Represents the image data. It is a child class of [File](#file) and is used to describe images. 259 260**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 261 262| Name | Type | Readable| Writable| Description | 263| -------- | ------ | ---- | ---- |----------| 264| imageUri | string | Yes | Yes | URI of the image.| 265 266**Example** 267 268```js 269let image = new unifiedDataChannel.Image(); 270image.imageUri = 'schema://com.samples.test/files/test.jpg'; 271``` 272 273## Video 274 275Represents video data. It is a child class of [File](#file) and is used to describe a video file. 276 277**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 278 279| Name | Type | Readable| Writable| Description | 280| -------- | ------ | ---- | ---- |----------| 281| videoUri | string | Yes | Yes | URI of the video file.| 282 283**Example** 284 285```js 286let video = new unifiedDataChannel.Video(); 287video.videoUri = 'schema://com.samples.test/files/test.mp4'; 288``` 289 290## Audio 291 292Represents video data. It is a child class of [File](#file) and is used to describe a video file. 293 294**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 295 296| Name | Type | Readable| Writable| Description | 297|----------|--------|----|----|----------| 298| audioUri | string | Yes | Yes | Audio data URI.| 299 300**Example** 301 302```js 303let audio = new unifiedDataChannel.Audio(); 304audio.audioUri = 'schema://com.samples.test/files/test.mp3'; 305``` 306 307## Folder 308 309Represents the folder data. It is a child class of [File](#file) and is used to describe a folder. 310 311**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 312 313| Name | Type | Readable| Writable| Description | 314| -------- | ------ | ---- | ---- |---------| 315| folderUri | string | Yes | Yes | URI of the folder.| 316 317**Example** 318 319```js 320let folder = new unifiedDataChannel.Folder(); 321folder.folderUri = 'schema://com.samples.test/files/folder/'; 322``` 323 324## SystemDefinedRecord 325 326Represents 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. 327 328**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 329 330| Name | Type | Readable | Writable| Description | 331| ------- |------------------------|----------| ---- | ------------------------------------------------------------ | 332| details | Record<string, number \| string \| Uint8Array> | Yes | 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.<br/>This parameter is optional. The default value is an empty dictionary object.| 333 334**Example** 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 349Represents the service widget data defined by the system, which is a child class of [SystemDefinedRecord](#systemdefinedrecord). 350 351**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 352 353| Name | Type | Readable| Writable| Description | 354| ----------- | ------ | ---- | ---- |----------------| 355| formId | number | Yes | Yes | Service widget ID. | 356| formName | string | Yes | Yes | Widget name. | 357| bundleName | string | Yes | Yes | Name of the bundle to which the widget belongs. | 358| abilityName | string | Yes | Yes | Ability name corresponding to the widget.| 359| module | string | Yes | Yes | Name of the module to which the widget belongs. | 360 361**Example** 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 381Represents the data of the home screen icon defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord). 382 383**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 384 385| Name | Type | Readable| Writable| Description | 386| ----------- | ------ | ---- | ---- |-----------------| 387| appId | string | Yes | Yes | ID of the application, for which the icon is used. | 388| appName | string | Yes | Yes | Name of the application, for which the icon is used. | 389| appIconId | string | Yes | Yes | Image ID of the icon. | 390| appLabelId | string | Yes | Yes | Label ID corresponding to the icon name. | 391| bundleName | string | Yes | Yes | Bundle name corresponding to the icon.| 392| abilityName | string | Yes | Yes | Application ability name corresponding to the icon.| 393 394**Example** 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 415Represents the image data corresponding to the [PixelMap](js-apis-image.md#pixelmap7) defined by OpenHarmony. It is a child class of [SystemDefinedRecord](#systemdefinedrecord) and used to store only the binary data of **PixelMap**. 416 417**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 418 419| Name | Type | Readable| Writable| Description | 420| ------- | ---------- | ---- | ---- |-------------------| 421| rawData | Uint8Array | Yes | Yes | Binary data of the **PixelMap** object.| 422 423**Example** 424 425```js 426import image from '@ohos.multimedia.image'; // Module where the PixelMap class is defined. 427 428const color = new ArrayBuffer(96); // Create a PixelMap object. 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 451Represents 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. 452 453**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 454 455| Name | Type | Readable| Writable| Description | 456|------------------------|------------| ---- | ---- |---------------------------------------| 457| applicationDefinedType | string | Yes | Yes | Application's custom data type identifier, which must start with **ApplicationDefined**.| 458| rawData | Uint8Array | Yes | Yes | Binary data of the custom data type. | 459 460**Example** 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 472Enumerates the data channel types supported by the UDMF. It is used to identify different service scenarios, to which the UDMF data channels apply. 473 474**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 475 476| Name | Value | Description | 477|----------|-----------|---------| 478| DATA_HUB | 'DataHub' | Public data channel.| 479 480## Options 481 482Defines 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. 483 484**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 485 486 487| Name | Type | Readable| Writable| Mandatory| Description | 488|-----------|-------------------------|----|----|----|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 489| intention | [Intention](#intention) | Yes | Yes | No | Type of the data channel related to the data operation. | 490| key | string | Yes | Yes | No | 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.| 491 492 493 494## unifiedDataChannel.insertData 495 496insertData(options: Options, data: UnifiedData, callback: AsyncCallback<string>): void 497 498Inserts data to the UDMF public data channel. This API uses an asynchronous callback to return the unique identifier of the data inserted. 499 500**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 501 502**Parameters** 503 504| Name | Type | Mandatory| Description | 505|----------|----------------------------|----|------------------------------| 506| options | [Options](#options) | Yes | Configuration parameters. Only the **intention** is required. | 507| data | [UnifiedData](#unifieddata) | Yes | Data to insert. | 508| callback | AsyncCallback<string> | Yes | Callback invoked to return the key (unique identifier) of the data inserted.| 509 510**Example** 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 542Inserts data to the UDMF public data channel. This API uses a promise to return the unique identifier of the data inserted. 543 544**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 545 546**Parameters** 547 548| Name | Type | Mandatory| Description | 549|---------|-----------------------------|----|-----------------------| 550| options | [Options](#options) | Yes | Configuration parameters. Only the **intention** is required.| 551| data | [UnifiedData](#unifieddata) | Yes | Data to insert. | 552 553**Return value** 554 555| Type | Description | 556|-----------------------|-----------------------------------| 557| Promise<string> | Promise used to return the key of the data inserted.| 558 559**Example** 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 588Updates the data in the UDMF public data channel. This API uses an asynchronous callback to return the result. 589 590**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 591 592**Parameters** 593 594| Name | Type | Mandatory| Description | 595|----------|-----------------------------|----|-------------------------------------| 596| options | [Options](#options) | Yes | Configuration parameters. Only the value of **key** is required. | 597| data | [UnifiedData](#unifieddata) | Yes | New data. | 598| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the data is updated successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 599 600**Example** 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 632Updates the data in the UDMF public data channel. This API uses a promise to return the result. 633 634**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 635 636**Parameters** 637 638| Name | Type | Mandatory| Description | 639|---------|-----------------------------|----|-----------------| 640| options | [Options](#options) | Yes | Configuration parameters. Only the value of **key** is required.| 641| data | [UnifiedData](#unifieddata) | Yes | New data. | 642 643**Return value** 644 645| Type | Description | 646|---------------------|----------------------------| 647| Promise<void> | Promise that returns no value.| 648 649**Example** 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 679Queries data in the UDMF public data channel. This API uses an asynchronous callback to return the result. 680 681**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 682 683**Parameters** 684 685| Name | Type | Mandatory| Description | 686|----------|---------------------------------------------------------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------| 687| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in. | 688| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | Yes | Callback invoked 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.| 689 690**Example** 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 728Queries data in the UDMF public data channel. This API uses a promise to return the result. 729 730**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 731 732**Parameters** 733 734| Name | Type | Mandatory| Description | 735|---------|---------------------|----|-----------------------------------------------| 736| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in.| 737 738**Return value** 739 740| Type | Description | 741|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| 742| 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.| 743 744**Example** 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 780Deletes data from the UDMF public data channel. This API uses an asynchronous callback to return the result. 781 782**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 783 784**Parameters** 785 786| Name | Type | Mandatory| Description | 787|----------|---------------------------------------------------------------|----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 788| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in. | 789| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | Yes | Callback invoked 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.| 790 791**Example** 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 829Deletes data from the UDMF public data channel. This API uses a promise to return the result. 830 831**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 832 833**Parameters** 834 835| Name | Type | Mandatory| Description | 836|---------|---------------------|----|--------| 837| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in.| 838 839**Return value** 840 841| Type | Description | 842|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| 843| 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.| 844 845**Example** 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``` 876