1# @ohos.rpc (RPC) 2 3The **RPC** module implements communication between processes, including inter-process communication (IPC) on a single device and remote procedure call (RPC) between processes on difference devices. IPC is implemented based on the binder driver, and RPC is based on the DSoftBus driver. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> - This module supports return of error codes since API version 9. 10 11## Modules to Import 12 13``` 14import rpc from '@ohos.rpc'; 15``` 16 17## ErrorCode<sup>9+</sup> 18 19The APIs of this module return exceptions since API version 9. The following table lists the error codes. 20 21**System capability**: SystemCapability.Communication.IPC.Core 22 23 | Name | Value | Description | 24 | ------------------------------------- | ------- | --------------------------------------------- | 25 | CHECK_PARAM_ERROR | 401 | Parameter check failed. | 26 | OS_MMAP_ERROR | 1900001 | Failed to call mmap. | 27 | OS_IOCTL_ERROR | 1900002 | Failed to call **ioctl** with the shared memory file descriptor.| 28 | WRITE_TO_ASHMEM_ERROR | 1900003 | Failed to write data to the shared memory. | 29 | READ_FROM_ASHMEM_ERROR | 1900004 | Failed to read data from the shared memory. | 30 | ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | This operation is allowed only on the proxy object. | 31 | ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | This operation is allowed only on the remote object. | 32 | COMMUNICATION_ERROR | 1900007 | Failed to communicate with the remote object over IPC. | 33 | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | Invalid proxy or remote object. | 34 | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | Failed to write data to MessageSequence. | 35 | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | Failed to read data from MessageSequence. | 36 | PARCEL_MEMORY_ALLOC_ERROR | 1900011 | Failed to allocate memory during serialization. | 37 | CALL_JS_METHOD_ERROR | 1900012 | Failed to invoke the JS callback. | 38 | OS_DUP_ERROR | 1900013 | Failed to call dup. | 39 40 41## MessageSequence<sup>9+</sup> 42 43 Provides APIs for reading and writing data in specific format. During RPC or IPC, the sender can use the **write()** method provided by **MessageSequence** to write data in specific format to a **MessageSequence** object. The receiver can use the **read()** method provided by **MessageSequence** to read data in specific format from a **MessageSequence** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 44 45### create 46 47 static create(): MessageSequence 48 49 Creates a **MessageSequence** object. This API is a static method. 50 51**System capability**: SystemCapability.Communication.IPC.Core 52 53**Return value** 54 55| Type | Description | 56| --------------- | ------------------------------- | 57| [MessageSequence](#messagesequence9) | **MessageSequence** object created.| 58 59**Example** 60 61 ```ts 62 import hilog from '@ohos.hilog'; 63 64 let data = rpc.MessageSequence.create(); 65 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 66 ``` 67 68### reclaim 69 70reclaim(): void 71 72Reclaims the **MessageSequence** object that is no longer used. 73 74**System capability**: SystemCapability.Communication.IPC.Core 75 76**Example** 77 78 ```ts 79 let reply = rpc.MessageSequence.create(); 80 reply.reclaim(); 81 ``` 82 83### writeRemoteObject 84 85writeRemoteObject(object: IRemoteObject): void 86 87Serializes a remote object and writes it to this **MessageSequence** object. 88 89**System capability**: SystemCapability.Communication.IPC.Core 90 91**Parameters** 92 93 | Name| Type | Mandatory| Description | 94 | ------ | ------------------------------- | ---- | ----------------------------------------- | 95 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageSequence** object.| 96 97**Error codes** 98 99For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 100 101 | ID| Error Message| 102 | -------- | -------- | 103 | 1900008 | proxy or remote object is invalid | 104 | 1900009 | write data to message sequence failed | 105 106**Example** 107 108 ```ts 109 import hilog from '@ohos.hilog'; 110 import { BusinessError } from '@ohos.base'; 111 112 class TestRemoteObject extends rpc.RemoteObject { 113 constructor(descriptor: string) { 114 super(descriptor); 115 } 116 } 117 let data = rpc.MessageSequence.create(); 118 let testRemoteObject = new TestRemoteObject("testObject"); 119 try { 120 data.writeRemoteObject(testRemoteObject); 121 } catch(error) { 122 let e: BusinessError = error as BusinessError; 123 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 124 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 125 } 126 ``` 127 128### readRemoteObject 129 130readRemoteObject(): IRemoteObject 131 132Reads the remote object from **MessageSequence**. You can use this API to deserialize the **MessageSequence** object to generate an **IRemoteObject**. The remote object is read in the order in which it is written to this **MessageSequence** object. 133 134**System capability**: SystemCapability.Communication.IPC.Core 135 136**Return value** 137 138 | Type | Description | 139 | ------------------------------- | ------------------ | 140 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 141 142**Error codes** 143 144For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 145 146 | ID| Error Message| 147 | -------- | -------- | 148 | 1900008 | proxy or remote object is invalid | 149 | 1900010 | read data from message sequence failed | 150 151**Example** 152 153 ```ts 154 import hilog from '@ohos.hilog'; 155 import { BusinessError } from '@ohos.base'; 156 157 class TestRemoteObject extends rpc.RemoteObject { 158 constructor(descriptor: string) { 159 super(descriptor); 160 } 161 } 162 let data = rpc.MessageSequence.create(); 163 let testRemoteObject = new TestRemoteObject("testObject"); 164 try { 165 data.writeRemoteObject(testRemoteObject); 166 let proxy = data.readRemoteObject(); 167 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 168 } catch(error) { 169 let e: BusinessError = error as BusinessError; 170 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 171 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 172 } 173 ``` 174 175### writeInterfaceToken 176 177writeInterfaceToken(token: string): void 178 179Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication. 180 181**System capability**: SystemCapability.Communication.IPC.Core 182 183**Parameters** 184 185 | Name| Type | Mandatory| Description | 186 | ------ | ------ | ---- | ------------------ | 187 | token | string | Yes | Interface token to write.| 188 189**Error codes** 190 191For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 192 193 | ID| Error Message| 194 | -------- | -------- | 195 | 1900009 | write data to message sequence failed | 196 197**Example** 198 199 ```ts 200 import hilog from '@ohos.hilog'; 201 import { BusinessError } from '@ohos.base'; 202 203 let data = rpc.MessageSequence.create(); 204 try { 205 data.writeInterfaceToken("aaa"); 206 } catch(error) { 207 let e: BusinessError = error as BusinessError; 208 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 209 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 210 } 211 ``` 212 213### readInterfaceToken 214 215readInterfaceToken(): string 216 217Reads the interface token from this **MessageSequence** object. The interface token is read in the sequence in which it is written to the **MessageSequence** object. The local object can use it to verify the communication. 218 219**System capability**: SystemCapability.Communication.IPC.Core 220 221**Return value** 222 223 | Type | Description | 224 | ------ | ------------------------ | 225 | string | Interface token obtained.| 226 227**Error codes** 228 229For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 230 231 | ID| Error Message| 232 | -------- | -------- | 233 | 1900010 | read data from message sequence failed | 234 235**Example** 236 237```ts 238import hilog from '@ohos.hilog'; 239import { BusinessError } from '@ohos.base'; 240 241class Stub extends rpc.RemoteObject { 242 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 243 try { 244 let interfaceToken = data.readInterfaceToken(); 245 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 246 } catch(error) { 247 let e: BusinessError = error as BusinessError; 248 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 249 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 250 } 251 return true; 252 } 253} 254``` 255 256### getSize 257 258getSize(): number 259 260Obtains the data size of this **MessageSequence** object. 261 262**System capability**: SystemCapability.Communication.IPC.Core 263 264**Return value** 265 266 | Type | Description | 267 | ------ | ----------------------------------------------- | 268 | number | Size of the **MessageSequence** instance obtained, in bytes.| 269 270**Example** 271 272 ```ts 273 import hilog from '@ohos.hilog'; 274 275 let data = rpc.MessageSequence.create(); 276 let size = data.getSize(); 277 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 278 ``` 279 280### getCapacity 281 282getCapacity(): number 283 284Obtains the capacity of this **MessageSequence** object. 285 286**System capability**: SystemCapability.Communication.IPC.Core 287 288**Return value** 289 290 | Type | Description | 291 | ------ | ----- | 292 | number | Capacity of the obtained **MessageSequence** object, in bytes.| 293 294**Example** 295 296 ```ts 297 import hilog from '@ohos.hilog'; 298 299 let data = rpc.MessageSequence.create(); 300 let result = data.getCapacity(); 301 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 302 ``` 303 304### setSize 305 306setSize(size: number): void 307 308Sets the size of the data contained in this **MessageSequence** object. 309 310**System capability**: SystemCapability.Communication.IPC.Core 311 312**Parameters** 313 314 | Name| Type | Mandatory| Description | 315 | ------ | ------ | ---- | ------ | 316 | size | number | Yes | Data size to set, in bytes.| 317 318**Example** 319 320 ```ts 321 import hilog from '@ohos.hilog'; 322 import { BusinessError } from '@ohos.base'; 323 324 let data = rpc.MessageSequence.create(); 325 data.writeString('Hello World'); 326 try { 327 data.setSize(16); 328 } catch(error) { 329 let e: BusinessError = error as BusinessError; 330 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 331 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 332 } 333 ``` 334 335### setCapacity 336 337setCapacity(size: number): void 338 339Sets the storage capacity of this **MessageSequence** object. 340 341**System capability**: SystemCapability.Communication.IPC.Core 342 343**Parameters** 344 345 | Name| Type | Mandatory| Description | 346 | ------ | ------ | ---- | --------------------------------------------- | 347 | size | number | Yes | Storage capacity of the **MessageSequence** object to set, in bytes.| 348 349**Error codes** 350 351For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 352 353 | ID| Error Message| 354 | -------- | -------- | 355 | 1900011 | parcel memory alloc failed | 356 357**Example** 358 359 ```ts 360 import hilog from '@ohos.hilog'; 361 import { BusinessError } from '@ohos.base'; 362 363 let data = rpc.MessageSequence.create(); 364 try { 365 data.setCapacity(100); 366 } catch(error) { 367 let e: BusinessError = error as BusinessError; 368 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 369 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 370 } 371 ``` 372 373### getWritableBytes 374 375getWritableBytes(): number 376 377Obtains the writable capacity (in bytes) of this **MessageSequence** object. 378 379**System capability**: SystemCapability.Communication.IPC.Core 380 381**Return value** 382 383 | Type | Description | 384 | ------ | ------ | 385 | number | Writable capacity of the **MessageSequence** instance, in bytes.| 386 387**Example** 388 389```ts 390import hilog from '@ohos.hilog'; 391 392class Stub extends rpc.RemoteObject { 393 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 394 let getWritableBytes = data.getWritableBytes(); 395 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 396 return true; 397 } 398} 399``` 400 401### getReadableBytes 402 403getReadableBytes(): number 404 405Obtains the readable capacity of this **MessageSequence** object. 406 407**System capability**: SystemCapability.Communication.IPC.Core 408 409**Return value** 410 411 | Type | Description | 412 | ------ | ------- | 413 | number | Readable capacity of the **MessageSequence** instance, in bytes.| 414 415**Example** 416 417```ts 418import hilog from '@ohos.hilog'; 419 420class Stub extends rpc.RemoteObject { 421 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 422 let result = data.getReadableBytes(); 423 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 424 return true; 425 } 426} 427``` 428 429### getReadPosition 430 431getReadPosition(): number 432 433Obtains the read position of this **MessageSequence** object. 434 435**System capability**: SystemCapability.Communication.IPC.Core 436 437**Return value** 438 439 | Type | Description | 440 | ------ | ------ | 441 | number | Read position obtained.| 442 443**Example** 444 445 ```ts 446 import hilog from '@ohos.hilog'; 447 448 let data = rpc.MessageSequence.create(); 449 let readPos = data.getReadPosition(); 450 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 451 ``` 452 453### getWritePosition 454 455getWritePosition(): number 456 457Obtains the write position of this **MessageSequence** object. 458 459**System capability**: SystemCapability.Communication.IPC.Core 460 461**Return value** 462 463 | Type | Description | 464 | ------ | ----- | 465 | number | Write position obtained.| 466 467**Example** 468 469 ```ts 470 import hilog from '@ohos.hilog'; 471 472 let data = rpc.MessageSequence.create(); 473 data.writeInt(10); 474 let bwPos = data.getWritePosition(); 475 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 476 ``` 477 478### rewindRead 479 480rewindRead(pos: number): void 481 482Moves the read pointer to the specified position. 483 484**System capability**: SystemCapability.Communication.IPC.Core 485 486**Parameters** 487 488 | Name| Type | Mandatory| Description | 489 | ------ | ------ | ---- | ------- | 490 | pos | number | Yes | Position from which data is to read.| 491 492**Example** 493 494 ```ts 495 import hilog from '@ohos.hilog'; 496 import { BusinessError } from '@ohos.base'; 497 498 let data = rpc.MessageSequence.create(); 499 data.writeInt(12); 500 data.writeString("sequence"); 501 let number = data.readInt(); 502 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 503 try { 504 data.rewindRead(0); 505 } catch(error) { 506 let e: BusinessError = error as BusinessError; 507 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 508 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 509 } 510 let number2 = data.readInt(); 511 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 512 ``` 513 514### rewindWrite 515 516rewindWrite(pos: number): void 517 518Moves the write pointer to the specified position. 519 520**System capability**: SystemCapability.Communication.IPC.Core 521 522**Parameters** 523 524 | Name| Type | Mandatory| Description | 525 | ------ | ------ | ---- | ----- | 526 | pos | number | Yes | Position from which data is to write.| 527 528**Example** 529 530 ```ts 531 import hilog from '@ohos.hilog'; 532 import { BusinessError } from '@ohos.base'; 533 534 let data = rpc.MessageSequence.create(); 535 data.writeInt(4); 536 try { 537 data.rewindWrite(0); 538 } catch(error) { 539 let e: BusinessError = error as BusinessError; 540 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 541 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 542 } 543 data.writeInt(5); 544 let number = data.readInt(); 545 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 546 ``` 547 548### writeByte 549 550writeByte(val: number): void 551 552Writes a byte value to this **MessageSequence** object. 553 554**System capability**: SystemCapability.Communication.IPC.Core 555 556**Parameters** 557 558 | Name| Type | Mandatory| Description | 559 | ------ | ------ | ---- | ----- | 560 | val | number | Yes | Byte value to write.| 561 562**Error codes** 563 564For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 565 566 | ID| Error Message| 567 | -------- | ------- | 568 | 1900009 | write data to message sequence failed | 569 570**Example** 571 572 ```ts 573 import hilog from '@ohos.hilog'; 574 import { BusinessError } from '@ohos.base'; 575 576 let data = rpc.MessageSequence.create(); 577 try { 578 data.writeByte(2); 579 } catch(error) { 580 let e: BusinessError = error as BusinessError; 581 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 582 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 583 } 584 ``` 585 586### readByte 587 588readByte(): number 589 590Reads the byte value from this **MessageSequence** object. 591 592**System capability**: SystemCapability.Communication.IPC.Core 593 594**Return value** 595 596 | Type | Description | 597 | ------ | ----- | 598 | number | Byte value read.| 599 600**Error codes** 601 602For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 603 604 | ID| Error Message| 605 | ------- | -------- | 606 | 1900010 | read data from message sequence failed | 607 608**Example** 609 610 ```ts 611 import hilog from '@ohos.hilog'; 612 import { BusinessError } from '@ohos.base'; 613 614 let data = rpc.MessageSequence.create(); 615 try { 616 data.writeByte(2); 617 } catch(error) { 618 let e: BusinessError = error as BusinessError; 619 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 620 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 621 } 622 try { 623 let ret = data.readByte(); 624 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 625 } catch(error) { 626 let e: BusinessError = error as BusinessError; 627 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 628 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 629 } 630 ``` 631 632### writeShort 633 634writeShort(val: number): void 635 636Writes a short integer to this **MessageSequence** object. 637 638**System capability**: SystemCapability.Communication.IPC.Core 639 640**Parameters** 641 642 | Name| Type | Mandatory| Description| 643 | ------ | ------ | --- | --- | 644 | val | number | Yes | Short integer to write.| 645 646**Error codes** 647 648For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 649 650 | ID| Error Message| 651 | -------- | -------- | 652 | 1900009 | write data to message sequence failed | 653 654**Example** 655 656 ```ts 657 import hilog from '@ohos.hilog'; 658 import { BusinessError } from '@ohos.base'; 659 660 let data = rpc.MessageSequence.create(); 661 try { 662 data.writeShort(8); 663 } catch(error) { 664 let e: BusinessError = error as BusinessError; 665 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 666 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 667 } 668 ``` 669 670### readShort 671 672readShort(): number 673 674Reads the short integer from this **MessageSequence** object. 675 676**System capability**: SystemCapability.Communication.IPC.Core 677 678**Return value** 679 680 | Type | Description | 681 | ------ | -------------- | 682 | number | Short integer read.| 683 684**Error codes** 685 686For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 687 688 | ID| Error Message| 689 | -------- | -------- | 690 | 1900010 | read data from message sequence failed | 691 692**Example** 693 694 ```ts 695 import hilog from '@ohos.hilog'; 696 import { BusinessError } from '@ohos.base'; 697 698 let data = rpc.MessageSequence.create(); 699 try { 700 data.writeShort(8); 701 } catch(error) { 702 let e: BusinessError = error as BusinessError; 703 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 704 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 705 } 706 try { 707 let ret = data.readShort(); 708 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 709 } catch(error) { 710 let e: BusinessError = error as BusinessError; 711 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 712 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 713 } 714 ``` 715 716### writeInt 717 718writeInt(val: number): void 719 720Writes an integer to this **MessageSequence** object. 721 722**System capability**: SystemCapability.Communication.IPC.Core 723 724**Parameters** 725 726 | Name| Type | Mandatory| Description | 727 | ------ | ------ | ---- | ---------------- | 728 | val | number | Yes | Integer to write.| 729 730**Error codes** 731 732For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 733 734 | ID| Error Message| 735 | -------- | -------- | 736 | 1900009 | write data to message sequence failed | 737 738**Example** 739 740 ```ts 741 import hilog from '@ohos.hilog'; 742 import { BusinessError } from '@ohos.base'; 743 744 let data = rpc.MessageSequence.create(); 745 try { 746 data.writeInt(10); 747 } catch(error) { 748 let e: BusinessError = error as BusinessError; 749 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 750 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 751 } 752 ``` 753 754### readInt 755 756readInt(): number 757 758Reads the integer from this **MessageSequence** object. 759 760**System capability**: SystemCapability.Communication.IPC.Core 761 762**Return value** 763 764 | Type | Description | 765 | ------ | ------------ | 766 | number | Integer read.| 767 768**Error codes** 769 770For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 771 772 | ID| Error Message| 773 | -------- | -------- | 774 | 1900010 | read data from message sequence failed | 775 776**Example** 777 778 ```ts 779 import hilog from '@ohos.hilog'; 780 import { BusinessError } from '@ohos.base'; 781 782 let data = rpc.MessageSequence.create(); 783 try { 784 data.writeInt(10); 785 } catch(error) { 786 let e: BusinessError = error as BusinessError; 787 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 788 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 789 } 790 try { 791 let ret = data.readInt(); 792 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 793 } catch(error) { 794 let e: BusinessError = error as BusinessError; 795 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 796 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 797 } 798 ``` 799 800### writeLong 801 802writeLong(val: number): void 803 804Writes a long integer to this **MessageSequence** object. 805 806**System capability**: SystemCapability.Communication.IPC.Core 807 808**Parameters** 809 810 | Name| Type | Mandatory| Description | 811 | ------ | ------ | ---- | ---------------- | 812 | val | number | Yes | Long integer to write.| 813 814**Error codes** 815 816For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 817 818 | ID| Error Message| 819 | -------- | -------- | 820 | 1900009 | write data to message sequence failed | 821 822**Example** 823 824 ```ts 825 import hilog from '@ohos.hilog'; 826 import { BusinessError } from '@ohos.base'; 827 828 let data = rpc.MessageSequence.create(); 829 try { 830 data.writeLong(10000); 831 } catch(error) { 832 let e: BusinessError = error as BusinessError; 833 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 834 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 835 } 836 ``` 837 838### readLong 839 840readLong(): number 841 842Reads the long integer from this **MessageSequence** object. 843 844**System capability**: SystemCapability.Communication.IPC.Core 845 846**Return value** 847 848 | Type | Description | 849 | ------ | -------------- | 850 | number | Long integer read.| 851 852**Error codes** 853 854For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 855 856 | ID| Error Message| 857 | -------- | -------- | 858 | 1900010 | read data from message sequence failed | 859 860**Example** 861 862 ```ts 863 import hilog from '@ohos.hilog'; 864 import { BusinessError } from '@ohos.base'; 865 866 let data = rpc.MessageSequence.create(); 867 try { 868 data.writeLong(10000); 869 } catch(error) { 870 let e: BusinessError = error as BusinessError; 871 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 872 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 873 } 874 try { 875 let ret = data.readLong(); 876 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 877 } catch(error) { 878 let e: BusinessError = error as BusinessError; 879 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 880 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 881 } 882 ``` 883 884### writeFloat 885 886writeFloat(val: number): void 887 888Writes a floating-point number to this **MessageSequence** object. 889 890**System capability**: SystemCapability.Communication.IPC.Core 891 892**Parameters** 893 894 | Name| Type | Mandatory| Description | 895 | ------ | ------ | ---- | ----- | 896 | val | number | Yes | Floating-point number to write.| 897 898**Error codes** 899 900For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 901 902 | ID| Error Message| 903 | -------- | -------- | 904 | 1900009 | write data to message sequence failed | 905 906**Example** 907 908 ```ts 909 import hilog from '@ohos.hilog'; 910 import { BusinessError } from '@ohos.base'; 911 912 let data = rpc.MessageSequence.create(); 913 try { 914 data.writeFloat(1.2); 915 } catch(error) { 916 let e: BusinessError = error as BusinessError; 917 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 918 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 919 } 920 ``` 921 922### readFloat 923 924readFloat(): number 925 926Reads the floating-pointer number from this **MessageSequence** object. 927 928**System capability**: SystemCapability.Communication.IPC.Core 929 930**Return value** 931 932 | Type | Description | 933 | ------ | ------------ | 934 | number | Floating-point number read.| 935 936**Error codes** 937 938For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 939 940 | ID| Error Message| 941 | -------- | -------- | 942 | 1900010 | read data from message sequence failed | 943 944**Example** 945 946 ```ts 947 import hilog from '@ohos.hilog'; 948 import { BusinessError } from '@ohos.base'; 949 950 let data = rpc.MessageSequence.create(); 951 try { 952 data.writeFloat(1.2); 953 } catch(error) { 954 let e: BusinessError = error as BusinessError; 955 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 956 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 957 } 958 try { 959 let ret = data.readFloat(); 960 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 961 } catch(error) { 962 let e: BusinessError = error as BusinessError; 963 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 964 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 965 } 966 ``` 967 968### writeDouble 969 970writeDouble(val: number): void 971 972Writes a double-precision floating-point number to this **MessageSequence** object. 973 974**System capability**: SystemCapability.Communication.IPC.Core 975 976**Parameters** 977 978 | Name| Type | Mandatory| Description | 979 | ------ | ------ | ---- | ---------------------- | 980 | val | number | Yes | Double-precision floating-point number to write.| 981 982**Error codes** 983 984For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 985 986 | ID| Error Message| 987 | -------- | -------- | 988 | 1900009 | write data to message sequence failed | 989 990**Example** 991 992 ```ts 993 import hilog from '@ohos.hilog'; 994 import { BusinessError } from '@ohos.base'; 995 996 let data = rpc.MessageSequence.create(); 997 try { 998 data.writeDouble(10.2); 999 } catch(error) { 1000 let e: BusinessError = error as BusinessError; 1001 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1002 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1003 } 1004 ``` 1005 1006### readDouble 1007 1008readDouble(): number 1009 1010Reads the double-precision floating-point number from this **MessageSequence** object. 1011 1012**System capability**: SystemCapability.Communication.IPC.Core 1013 1014**Return value** 1015 1016 | Type | Description | 1017 | ------ | ------------------ | 1018 | number | Double-precision floating-point number read.| 1019 1020**Error codes** 1021 1022For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1023 1024 | ID| Error Message| 1025 | -------- | -------- | 1026 | 1900010 | read data from message sequence failed | 1027 1028**Example** 1029 1030 ```ts 1031 import hilog from '@ohos.hilog'; 1032 import { BusinessError } from '@ohos.base'; 1033 1034 let data = rpc.MessageSequence.create(); 1035 try { 1036 data.writeDouble(10.2); 1037 } catch(error) { 1038 let e: BusinessError = error as BusinessError; 1039 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1040 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1041 } 1042 try { 1043 let ret = data.readDouble(); 1044 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1045 } catch(error) { 1046 let e: BusinessError = error as BusinessError; 1047 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1048 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1049 } 1050 ``` 1051 1052### writeBoolean 1053 1054writeBoolean(val: boolean): void 1055 1056Writes a Boolean value to this **MessageSequence** object. 1057 1058**System capability**: SystemCapability.Communication.IPC.Core 1059 1060**Parameters** 1061 1062 | Name| Type | Mandatory| Description | 1063 | ------ | ------- | ---- | ---------------- | 1064 | val | boolean | Yes | Boolean value to write.| 1065 1066**Error codes** 1067 1068For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1069 1070 | ID| Error Message| 1071 | -------- | -------- | 1072 | 1900009 | write data to message sequence failed | 1073 1074**Example** 1075 1076 ```ts 1077 import hilog from '@ohos.hilog'; 1078 import { BusinessError } from '@ohos.base'; 1079 1080 let data = rpc.MessageSequence.create(); 1081 try { 1082 data.writeBoolean(false); 1083 } catch(error) { 1084 let e: BusinessError = error as BusinessError; 1085 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1086 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1087 } 1088 ``` 1089 1090### readBoolean 1091 1092readBoolean(): boolean 1093 1094Reads the Boolean value from this **MessageSequence** object. 1095 1096**System capability**: SystemCapability.Communication.IPC.Core 1097 1098**Return value** 1099 1100 | Type | Description | 1101 | ------- | -------------------- | 1102 | boolean | Boolean value read.| 1103 1104**Error codes** 1105 1106For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1107 1108 | ID| Error Message| 1109 | -------- | -------- | 1110 | 1900010 | read data from message sequence failed | 1111 1112**Example** 1113 1114 ```ts 1115 import hilog from '@ohos.hilog'; 1116 import { BusinessError } from '@ohos.base'; 1117 1118 let data = rpc.MessageSequence.create(); 1119 try { 1120 data.writeBoolean(false); 1121 } catch(error) { 1122 let e: BusinessError = error as BusinessError; 1123 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1124 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1125 } 1126 try { 1127 let ret = data.readBoolean(); 1128 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1129 } catch(error) { 1130 let e: BusinessError = error as BusinessError; 1131 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1132 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1133 } 1134 ``` 1135 1136### writeChar 1137 1138writeChar(val: number): void 1139 1140Writes a character to this **MessageSequence** object. 1141 1142**System capability**: SystemCapability.Communication.IPC.Core 1143 1144**Parameters** 1145 1146 | Name| Type | Mandatory| Description | 1147 | ------ | ------ | ---- | -------------------- | 1148 | val | number | Yes | Single character to write.| 1149 1150**Error codes** 1151 1152For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1153 1154 | ID| Error Message| 1155 | -------- | -------- | 1156 | 1900009 | write data to message sequence failed | 1157 1158**Example** 1159 1160 ```ts 1161 import hilog from '@ohos.hilog'; 1162 import { BusinessError } from '@ohos.base'; 1163 1164 let data = rpc.MessageSequence.create(); 1165 try { 1166 data.writeChar(97); 1167 } catch(error) { 1168 let e: BusinessError = error as BusinessError; 1169 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1170 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1171 } 1172 ``` 1173 1174### readChar 1175 1176readChar(): number 1177 1178Reads the character from this **MessageSequence** object. 1179 1180**System capability**: SystemCapability.Communication.IPC.Core 1181 1182**Return value** 1183 1184 | Type | Description| 1185 | ------ | ---- | 1186 | number | Character read.| 1187 1188**Error codes** 1189 1190For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1191 1192 | ID| Error Message| 1193 | -------- | -------- | 1194 | 1900010 | read data from message sequence failed | 1195 1196**Example** 1197 1198 ```ts 1199 import hilog from '@ohos.hilog'; 1200 import { BusinessError } from '@ohos.base'; 1201 1202 let data = rpc.MessageSequence.create(); 1203 try { 1204 data.writeChar(97); 1205 } catch(error) { 1206 let e: BusinessError = error as BusinessError; 1207 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1208 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1209 } 1210 try { 1211 let ret = data.readChar(); 1212 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1213 } catch(error) { 1214 let e: BusinessError = error as BusinessError; 1215 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1216 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1217 } 1218 ``` 1219 1220### writeString 1221 1222writeString(val: string): void 1223 1224Writes a string to this **MessageSequence** object. 1225 1226**System capability**: SystemCapability.Communication.IPC.Core 1227 1228**Parameters** 1229 1230 | Name| Type | Mandatory| Description | 1231 | ------ | ------ | ---- | ----------------------------------------- | 1232 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 1233 1234**Error codes** 1235 1236For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1237 1238 | ID| Error Message| 1239 | -------- | -------- | 1240 | 1900009 | write data to message sequence failed | 1241 1242**Example** 1243 1244 ```ts 1245 import hilog from '@ohos.hilog'; 1246 import { BusinessError } from '@ohos.base'; 1247 1248 let data = rpc.MessageSequence.create(); 1249 try { 1250 data.writeString('abc'); 1251 } catch(error) { 1252 let e: BusinessError = error as BusinessError; 1253 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1254 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1255 } 1256 ``` 1257 1258### readString 1259 1260readString(): string 1261 1262Reads the string from this **MessageSequence** object. 1263 1264**System capability**: SystemCapability.Communication.IPC.Core 1265 1266**Return value** 1267 1268 | Type | Description | 1269 | ------ | -------------- | 1270 | string | String read.| 1271 1272**Error codes** 1273 1274For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1275 1276 | ID| Error Message| 1277 | -------- | -------- | 1278 | 1900010 | read data from message sequence failed | 1279 1280**Example** 1281 1282 ```ts 1283 import hilog from '@ohos.hilog'; 1284 import { BusinessError } from '@ohos.base'; 1285 1286 let data = rpc.MessageSequence.create(); 1287 try { 1288 data.writeString('abc'); 1289 } catch(error) { 1290 let e: BusinessError = error as BusinessError; 1291 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1292 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1293 } 1294 try { 1295 let ret = data.readString(); 1296 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1297 } catch(error) { 1298 let e: BusinessError = error as BusinessError; 1299 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1300 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1301 } 1302 ``` 1303 1304### writeParcelable 1305 1306writeParcelable(val: Parcelable): void 1307 1308Writes a **Parcelable** object to this **MessageSequence** object. 1309 1310**System capability**: SystemCapability.Communication.IPC.Core 1311 1312**Parameters** 1313 1314| Name| Type| Mandatory| Description| 1315| ------ | --------- | ---- | ------ | 1316| val | [Parcelable](#parcelable9) | Yes | **Parcelable** object to write.| 1317 1318**Error codes** 1319 1320For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1321 1322 | ID| Error Message| 1323 | -------- | -------- | 1324 | 1900009 | write data to message sequence failed | 1325 1326**Example** 1327 1328 ```ts 1329 import hilog from '@ohos.hilog'; 1330 import { BusinessError } from '@ohos.base'; 1331 1332 class MyParcelable implements rpc.Parcelable { 1333 num: number = 0; 1334 str: string = ''; 1335 constructor( num: number, str: string) { 1336 this.num = num; 1337 this.str = str; 1338 } 1339 marshalling(messageSequence: rpc.MessageSequence): boolean { 1340 messageSequence.writeInt(this.num); 1341 messageSequence.writeString(this.str); 1342 return true; 1343 } 1344 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1345 this.num = messageSequence.readInt(); 1346 this.str = messageSequence.readString(); 1347 return true; 1348 } 1349 } 1350 let parcelable = new MyParcelable(1, "aaa"); 1351 let data = rpc.MessageSequence.create(); 1352 try { 1353 data.writeParcelable(parcelable); 1354 } catch(error) { 1355 let e: BusinessError = error as BusinessError; 1356 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1357 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1358 } 1359 ``` 1360 1361### readParcelable 1362 1363readParcelable(dataIn: Parcelable): void 1364 1365Reads a **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**). 1366 1367**System capability**: SystemCapability.Communication.IPC.Core 1368 1369**Parameters** 1370 1371| Name| Type | Mandatory| Description | 1372| ------ | -------------------------- | ---- | ----------------------------------------- | 1373| dataIn | [Parcelable](#parcelable9) | Yes | **Parcelable** object to read.| 1374 1375**Error codes** 1376 1377For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1378 1379 | ID| Error Message| 1380 | -------- | -------- | 1381 | 1900010 | read data from message sequence failed | 1382 | 1900012 | call js callback function failed | 1383 1384**Example** 1385 1386 ```ts 1387 import hilog from '@ohos.hilog'; 1388 import { BusinessError } from '@ohos.base'; 1389 1390 class MyParcelable implements rpc.Parcelable { 1391 num: number = 0; 1392 str: string = ''; 1393 constructor(num: number, str: string) { 1394 this.num = num; 1395 this.str = str; 1396 } 1397 marshalling(messageSequence: rpc.MessageSequence): boolean { 1398 messageSequence.writeInt(this.num); 1399 messageSequence.writeString(this.str); 1400 return true; 1401 } 1402 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1403 this.num = messageSequence.readInt(); 1404 this.str = messageSequence.readString(); 1405 return true; 1406 } 1407 } 1408 let parcelable = new MyParcelable(1, "aaa"); 1409 let data = rpc.MessageSequence.create(); 1410 data.writeParcelable(parcelable); 1411 let ret = new MyParcelable(0, ""); 1412 try { 1413 data.readParcelable(ret); 1414 }catch(error) { 1415 let e: BusinessError = error as BusinessError; 1416 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1417 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1418 } 1419 ``` 1420 1421### writeByteArray 1422 1423writeByteArray(byteArray: number[]): void 1424 1425Writes a byte array to this **MessageSequence** object. 1426 1427**System capability**: SystemCapability.Communication.IPC.Core 1428 1429**Parameters** 1430 1431 | Name | Type | Mandatory| Description | 1432 | --------- | -------- | ---- | ------------------ | 1433 | byteArray | number[] | Yes | Byte array to write.| 1434 1435**Error codes** 1436 1437For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1438 1439 | ID| Error Message| 1440 | -------- | -------- | 1441 | 1900009 | write data to message sequence failed | 1442 1443**Example** 1444 1445 ```ts 1446 import hilog from '@ohos.hilog'; 1447 import { BusinessError } from '@ohos.base'; 1448 1449 let data = rpc.MessageSequence.create(); 1450 let ByteArrayVar = [1, 2, 3, 4, 5]; 1451 try { 1452 data.writeByteArray(ByteArrayVar); 1453 } catch(error) { 1454 let e: BusinessError = error as BusinessError; 1455 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1456 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1457 } 1458 ``` 1459 1460### readByteArray 1461 1462readByteArray(dataIn: number[]): void 1463 1464Reads a byte array from this **MessageSequence** object. 1465 1466**System capability**: SystemCapability.Communication.IPC.Core 1467 1468**Parameters** 1469 1470 | Name| Type | Mandatory| Description | 1471 | ------ | -------- | ---- | ------------------ | 1472 | dataIn | number[] | Yes | Byte array to read.| 1473 1474**Error codes** 1475 1476For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1477 1478 | ID| Error Message| 1479 | -------- | -------- | 1480 | 1900010 | read data from message sequence failed | 1481 1482**Example** 1483 1484 ```ts 1485 import hilog from '@ohos.hilog'; 1486 import { BusinessError } from '@ohos.base'; 1487 1488 let data = rpc.MessageSequence.create(); 1489 let ByteArrayVar = [1, 2, 3, 4, 5]; 1490 try { 1491 data.writeByteArray(ByteArrayVar); 1492 } catch(error) { 1493 let e: BusinessError = error as BusinessError; 1494 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1495 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1496 } 1497 try { 1498 let array: Array<number> = new Array(5); 1499 data.readByteArray(array); 1500 } catch(error) { 1501 let e: BusinessError = error as BusinessError; 1502 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1503 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1504 } 1505 ``` 1506 1507### readByteArray 1508 1509readByteArray(): number[] 1510 1511Reads the byte array from this **MessageSequence** object. 1512 1513**System capability**: SystemCapability.Communication.IPC.Core 1514 1515**Return value** 1516 1517 | Type | Description | 1518 | -------- | -------------- | 1519 | number[] | Byte array read.| 1520 1521**Error codes** 1522 1523For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1524 1525 | ID| Error Message| 1526 | -------- | -------- | 1527 | 1900010 | read data from message sequence failed | 1528 1529**Example** 1530 1531 ```ts 1532 import hilog from '@ohos.hilog'; 1533 import { BusinessError } from '@ohos.base'; 1534 1535 let data = rpc.MessageSequence.create(); 1536 let byteArrayVar = [1, 2, 3, 4, 5]; 1537 try { 1538 data.writeByteArray(byteArrayVar); 1539 } catch(error) { 1540 let e: BusinessError = error as BusinessError; 1541 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1542 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1543 } 1544 try { 1545 let array = data.readByteArray(); 1546 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1547 } catch(error) { 1548 let e: BusinessError = error as BusinessError; 1549 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1550 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1551 } 1552 ``` 1553 1554### writeShortArray 1555 1556writeShortArray(shortArray: number[]): void 1557 1558Writes a short array to this **MessageSequence** object. 1559 1560**System capability**: SystemCapability.Communication.IPC.Core 1561 1562**Parameters** 1563 1564 | Name | Type | Mandatory| Description | 1565 | ---------- | -------- | ---- | -------------------- | 1566 | shortArray | number[] | Yes | Short array to write.| 1567 1568**Error codes** 1569 1570For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1571 1572 | ID| Error Message| 1573 | -------- | -------- | 1574 | 1900009 | write data to message sequence failed | 1575 1576**Example** 1577 1578 ```ts 1579 import hilog from '@ohos.hilog'; 1580 import { BusinessError } from '@ohos.base'; 1581 1582 let data = rpc.MessageSequence.create(); 1583 try { 1584 data.writeShortArray([11, 12, 13]); 1585 } catch(error) { 1586 let e: BusinessError = error as BusinessError; 1587 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1588 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1589 } 1590 ``` 1591 1592### readShortArray 1593 1594readShortArray(dataIn: number[]): void 1595 1596Reads a short array from this **MessageSequence** object. 1597 1598**System capability**: SystemCapability.Communication.IPC.Core 1599 1600**Parameters** 1601 1602 | Name| Type | Mandatory| Description | 1603 | ------ | -------- | ---- | -------------------- | 1604 | dataIn | number[] | Yes | Short array to read.| 1605 1606**Error codes** 1607 1608For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1609 1610 | ID| Error Message| 1611 | -------- | -------- | 1612 | 1900010 | read data from message sequence failed | 1613 1614**Example** 1615 1616 ```ts 1617 import hilog from '@ohos.hilog'; 1618 import { BusinessError } from '@ohos.base'; 1619 1620 let data = rpc.MessageSequence.create(); 1621 try { 1622 data.writeShortArray([11, 12, 13]); 1623 } catch(error) { 1624 let e: BusinessError = error as BusinessError; 1625 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1626 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1627 } 1628 try { 1629 let array: Array<number> = new Array(3); 1630 data.readShortArray(array); 1631 } catch(error) { 1632 let e: BusinessError = error as BusinessError; 1633 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1634 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1635 } 1636 ``` 1637 1638### readShortArray 1639 1640readShortArray(): number[] 1641 1642Reads the short array from this **MessageSequence** object. 1643 1644**System capability**: SystemCapability.Communication.IPC.Core 1645 1646**Return value** 1647 1648 | Type | Description | 1649 | -------- | ---------------- | 1650 | number[] | Short array read.| 1651 1652**Error codes** 1653 1654For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1655 1656 | ID| Error Message| 1657 | -------- | -------- | 1658 | 1900010 | read data from message sequence failed | 1659 1660**Example** 1661 1662 ```ts 1663 import hilog from '@ohos.hilog'; 1664 import { BusinessError } from '@ohos.base'; 1665 1666 let data = rpc.MessageSequence.create(); 1667 try { 1668 data.writeShortArray([11, 12, 13]); 1669 } catch(error) { 1670 let e: BusinessError = error as BusinessError; 1671 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1672 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1673 } 1674 try { 1675 let array = data.readShortArray(); 1676 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1677 } catch(error) { 1678 let e: BusinessError = error as BusinessError; 1679 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1680 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1681 } 1682 ``` 1683 1684### writeIntArray 1685 1686writeIntArray(intArray: number[]): void 1687 1688Writes an integer array to this **MessageSequence** object. 1689 1690**System capability**: SystemCapability.Communication.IPC.Core 1691 1692**Parameters** 1693 1694 | Name | Type | Mandatory| Description | 1695 | -------- | -------- | ---- | ------------------ | 1696 | intArray | number[] | Yes | Integer array to write.| 1697 1698**Error codes** 1699 1700For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1701 1702 | ID| Error Message| 1703 | -------- | -------- | 1704 | 1900009 | write data to message sequence failed | 1705 1706**Example** 1707 1708 ```ts 1709 import hilog from '@ohos.hilog'; 1710 import { BusinessError } from '@ohos.base'; 1711 1712 let data = rpc.MessageSequence.create(); 1713 try { 1714 data.writeIntArray([100, 111, 112]); 1715 } catch(error) { 1716 let e: BusinessError = error as BusinessError; 1717 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1718 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1719 } 1720 ``` 1721 1722### readIntArray 1723 1724readIntArray(dataIn: number[]): void 1725 1726Reads an integer array from this **MessageSequence** object. 1727 1728**System capability**: SystemCapability.Communication.IPC.Core 1729 1730**Parameters** 1731 1732 | Name| Type | Mandatory| Description | 1733 | ------ | -------- | ---- | ------------------ | 1734 | dataIn | number[] | Yes | Integer array to read.| 1735 1736**Error codes** 1737 1738For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1739 1740 | ID| Error Message| 1741 | -------- | -------- | 1742 | 1900010 | read data from message sequence failed | 1743 1744**Example** 1745 1746 ```ts 1747 import hilog from '@ohos.hilog'; 1748 import { BusinessError } from '@ohos.base'; 1749 1750 let data = rpc.MessageSequence.create(); 1751 try { 1752 data.writeIntArray([100, 111, 112]); 1753 } catch(error) { 1754 let e: BusinessError = error as BusinessError; 1755 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1756 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1757 } 1758 let array: Array<number> = new Array(3); 1759 try { 1760 data.readIntArray(array); 1761 } catch(error) { 1762 let e: BusinessError = error as BusinessError; 1763 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1764 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1765 } 1766 ``` 1767 1768### readIntArray 1769 1770readIntArray(): number[] 1771 1772Reads the integer array from this **MessageSequence** object. 1773 1774**System capability**: SystemCapability.Communication.IPC.Core 1775 1776**Return value** 1777 1778 | Type | Description | 1779 | -------- | -------------- | 1780 | number[] | Integer array read.| 1781 1782**Error codes** 1783 1784For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1785 1786 | ID| Error Message| 1787 | -------- | -------- | 1788 | 1900010 | read data from message sequence failed | 1789 1790**Example** 1791 1792 ```ts 1793 import hilog from '@ohos.hilog'; 1794 import { BusinessError } from '@ohos.base'; 1795 1796 let data = rpc.MessageSequence.create(); 1797 try { 1798 data.writeIntArray([100, 111, 112]); 1799 } catch(error) { 1800 let e: BusinessError = error as BusinessError; 1801 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1802 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1803 } 1804 try { 1805 let array = data.readIntArray(); 1806 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1807 } catch(error) { 1808 let e: BusinessError = error as BusinessError; 1809 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1810 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1811 } 1812 ``` 1813 1814### writeLongArray 1815 1816writeLongArray(longArray: number[]): void 1817 1818Writes a long array to this **MessageSequence** object. 1819 1820**System capability**: SystemCapability.Communication.IPC.Core 1821 1822**Parameters** 1823 1824 | Name | Type | Mandatory| Description | 1825 | --------- | -------- | ---- | -------------------- | 1826 | longArray | number[] | Yes | Long array to write.| 1827 1828**Error codes** 1829 1830For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1831 1832 | ID| Error Message| 1833 | -------- | -------- | 1834 | 1900009 | write data to message sequence failed | 1835 1836**Example** 1837 1838 ```ts 1839 import hilog from '@ohos.hilog'; 1840 import { BusinessError } from '@ohos.base'; 1841 1842 let data = rpc.MessageSequence.create(); 1843 try { 1844 data.writeLongArray([1111, 1112, 1113]); 1845 }catch(error){ 1846 let e: BusinessError = error as BusinessError; 1847 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1848 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1849 } 1850 ``` 1851 1852### readLongArray 1853 1854readLongArray(dataIn: number[]): void 1855 1856Reads a long array from this **MessageSequence** object. 1857 1858**System capability**: SystemCapability.Communication.IPC.Core 1859 1860**Parameters** 1861 1862 | Name| Type | Mandatory| Description | 1863 | ------ | -------- | ---- | -------------------- | 1864 | dataIn | number[] | Yes | Long array to read.| 1865 1866**Error codes** 1867 1868For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1869 1870 | ID| Error Message| 1871 | -------- | -------- | 1872 | 1900010 | read data from message sequence failed | 1873 1874**Example** 1875 1876 ```ts 1877 import hilog from '@ohos.hilog'; 1878 import { BusinessError } from '@ohos.base'; 1879 1880 let data = rpc.MessageSequence.create(); 1881 try { 1882 data.writeLongArray([1111, 1112, 1113]); 1883 } catch(error) { 1884 let e: BusinessError = error as BusinessError; 1885 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1886 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1887 } 1888 let array: Array<number> = new Array(3); 1889 try { 1890 data.readLongArray(array); 1891 } catch(error) { 1892 let e: BusinessError = error as BusinessError; 1893 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1894 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1895 } 1896 ``` 1897 1898### readLongArray 1899 1900readLongArray(): number[] 1901 1902Reads the long array from this **MessageSequence** object. 1903 1904**System capability**: SystemCapability.Communication.IPC.Core 1905 1906**Return value** 1907 1908 | Type | Description | 1909 | -------- | ---------------- | 1910 | number[] | Long array read.| 1911 1912**Error codes** 1913 1914For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1915 1916 | ID| Error Message| 1917 | -------- | -------- | 1918 | 1900010 | read data from message sequence failed | 1919 1920**Example** 1921 1922 ```ts 1923 import hilog from '@ohos.hilog'; 1924 import { BusinessError } from '@ohos.base'; 1925 1926 let data = rpc.MessageSequence.create(); 1927 try { 1928 data.writeLongArray([1111, 1112, 1113]); 1929 } catch(error) { 1930 let e: BusinessError = error as BusinessError; 1931 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1932 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1933 } 1934 try { 1935 let array = data.readLongArray(); 1936 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 1937 } catch(error) { 1938 let e: BusinessError = error as BusinessError; 1939 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1940 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1941 } 1942 ``` 1943 1944### writeFloatArray 1945 1946writeFloatArray(floatArray: number[]): void 1947 1948Writes a floating-point array to this **MessageSequence** object. 1949 1950**System capability**: SystemCapability.Communication.IPC.Core 1951 1952**Parameters** 1953 1954 | Name | Type | Mandatory| Description | 1955 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1956 | floatArray | number[] | Yes | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 1957 1958**Error codes** 1959 1960For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1961 1962 | ID| Error Message| 1963 | -------- | -------- | 1964 | 1900009 | write data to message sequence failed | 1965 1966**Example** 1967 1968 ```ts 1969 import hilog from '@ohos.hilog'; 1970 import { BusinessError } from '@ohos.base'; 1971 1972 let data = rpc.MessageSequence.create(); 1973 try { 1974 data.writeFloatArray([1.2, 1.3, 1.4]); 1975 } catch(error) { 1976 let e: BusinessError = error as BusinessError; 1977 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 1978 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 1979 } 1980 ``` 1981 1982### readFloatArray 1983 1984readFloatArray(dataIn: number[]): void 1985 1986Reads a floating-point array from this **MessageSequence** object. 1987 1988**System capability**: SystemCapability.Communication.IPC.Core 1989 1990**Parameters** 1991 1992 | Name| Type | Mandatory| Description | 1993 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1994 | dataIn | number[] | Yes | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 1995 1996**Error codes** 1997 1998For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1999 2000 | ID| Error Message| 2001 | -------- | -------- | 2002 | 1900010 | read data from message sequence failed | 2003 2004**Example** 2005 2006 ```ts 2007 import hilog from '@ohos.hilog'; 2008 import { BusinessError } from '@ohos.base'; 2009 2010 let data = rpc.MessageSequence.create(); 2011 try { 2012 data.writeFloatArray([1.2, 1.3, 1.4]); 2013 }catch(error){ 2014 let e: BusinessError = error as BusinessError; 2015 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2016 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2017 } 2018 let array: Array<number> = new Array(3); 2019 try { 2020 data.readFloatArray(array); 2021 } catch(error) { 2022 let e: BusinessError = error as BusinessError; 2023 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2024 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2025 } 2026 ``` 2027 2028### readFloatArray 2029 2030readFloatArray(): number[] 2031 2032Reads the floating-point array from this **MessageSequence** object. 2033 2034**System capability**: SystemCapability.Communication.IPC.Core 2035 2036**Return value** 2037 2038 | Type | Description | 2039 | -------- | -------------- | 2040 | number[] | Floating-point array read.| 2041 2042**Error codes** 2043 2044For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2045 2046 | ID| Error Message| 2047 | -------- | -------- | 2048 | 1900010 | read data from message sequence failed | 2049 2050**Example** 2051 2052 ```ts 2053 import hilog from '@ohos.hilog'; 2054 import { BusinessError } from '@ohos.base'; 2055 2056 let data = rpc.MessageSequence.create(); 2057 try { 2058 data.writeFloatArray([1.2, 1.3, 1.4]); 2059 } catch(error) { 2060 let e: BusinessError = error as BusinessError; 2061 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2062 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2063 } 2064 try { 2065 let array = data.readFloatArray(); 2066 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2067 } catch(error) { 2068 let e: BusinessError = error as BusinessError; 2069 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2070 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2071 } 2072 ``` 2073 2074### writeDoubleArray 2075 2076writeDoubleArray(doubleArray: number[]): void 2077 2078Writes a double-precision floating-point array to this **MessageSequence** object. 2079 2080**System capability**: SystemCapability.Communication.IPC.Core 2081 2082**Parameters** 2083 2084 | Name | Type | Mandatory| Description | 2085 | ----------- | -------- | ---- | ------------------------ | 2086 | doubleArray | number[] | Yes | Double-precision floating-point array to write.| 2087 2088**Error codes** 2089 2090For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2091 2092 | ID| Error Message| 2093 | -------- | -------- | 2094 | 1900009 | write data to message sequence failed | 2095 2096**Example** 2097 2098 ```ts 2099 import hilog from '@ohos.hilog'; 2100 import { BusinessError } from '@ohos.base'; 2101 2102 let data = rpc.MessageSequence.create(); 2103 try { 2104 data.writeDoubleArray([11.1, 12.2, 13.3]); 2105 } catch(error) { 2106 let e: BusinessError = error as BusinessError; 2107 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2108 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2109 } 2110 ``` 2111 2112### readDoubleArray 2113 2114readDoubleArray(dataIn: number[]): void 2115 2116Reads a double-precision floating-point array from this **MessageSequence** object. 2117 2118**System capability**: SystemCapability.Communication.IPC.Core 2119 2120**Parameters** 2121 2122 | Name| Type | Mandatory| Description | 2123 | ------ | -------- | ---- | ------------------------ | 2124 | dataIn | number[] | Yes | Double-precision floating-point array to read.| 2125 2126**Error codes** 2127 2128For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2129 2130 | ID| Error Message| 2131 | -------- | -------- | 2132 | 1900010 | read data from message sequence failed | 2133 2134**Example** 2135 2136 ```ts 2137 import hilog from '@ohos.hilog'; 2138 import { BusinessError } from '@ohos.base'; 2139 2140 let data = rpc.MessageSequence.create(); 2141 try { 2142 data.writeDoubleArray([11.1, 12.2, 13.3]); 2143 } catch(error) { 2144 let e: BusinessError = error as BusinessError; 2145 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2146 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2147 } 2148 let array: Array<number> = new Array(3); 2149 try { 2150 data.readDoubleArray(array); 2151 } catch(error) { 2152 let e: BusinessError = error as BusinessError; 2153 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2154 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2155 } 2156 ``` 2157 2158### readDoubleArray 2159 2160readDoubleArray(): number[] 2161 2162Reads the double-precision floating-point array from this **MessageSequence** object. 2163 2164**System capability**: SystemCapability.Communication.IPC.Core 2165 2166**Return value** 2167 2168 | Type | Description | 2169 | -------- | -------------------- | 2170 | number[] | Double-precision floating-point array read.| 2171 2172**Error codes** 2173 2174For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2175 2176 | ID| Error Message| 2177 | -------- | -------- | 2178 | 1900010 | read data from message sequence failed | 2179 2180**Example** 2181 2182 ```ts 2183 import hilog from '@ohos.hilog'; 2184 import { BusinessError } from '@ohos.base'; 2185 2186 let data = rpc.MessageSequence.create(); 2187 try { 2188 data.writeDoubleArray([11.1, 12.2, 13.3]); 2189 } catch(error) { 2190 let e: BusinessError = error as BusinessError; 2191 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2192 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2193 } 2194 try { 2195 let array = data.readDoubleArray(); 2196 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2197 } catch(error) { 2198 let e: BusinessError = error as BusinessError; 2199 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2200 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2201 } 2202 ``` 2203 2204### writeBooleanArray 2205 2206writeBooleanArray(booleanArray: boolean[]): void 2207 2208Writes a Boolean array to this **MessageSequence** object. 2209 2210**System capability**: SystemCapability.Communication.IPC.Core 2211 2212**Parameters** 2213 2214 | Name | Type | Mandatory| Description | 2215 | ------------ | --------- | ---- | ------------------ | 2216 | booleanArray | boolean[] | Yes | Boolean array to write.| 2217 2218**Error codes** 2219 2220For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2221 2222 | ID| Error Message| 2223 | -------- | -------- | 2224 | 1900009 | write data to message sequence failed | 2225 2226**Example** 2227 2228 ```ts 2229 import hilog from '@ohos.hilog'; 2230 import { BusinessError } from '@ohos.base'; 2231 2232 let data = rpc.MessageSequence.create(); 2233 try { 2234 data.writeBooleanArray([false, true, false]); 2235 } catch(error) { 2236 let e: BusinessError = error as BusinessError; 2237 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2238 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2239 } 2240 ``` 2241 2242### readBooleanArray 2243 2244readBooleanArray(dataIn: boolean[]): void 2245 2246Reads a Boolean array from this **MessageSequence** object. 2247 2248**System capability**: SystemCapability.Communication.IPC.Core 2249 2250**Parameters** 2251 2252 | Name| Type | Mandatory| Description | 2253 | ------ | --------- | ---- | ------------------ | 2254 | dataIn | boolean[] | Yes | Boolean array to read.| 2255 2256**Error codes** 2257 2258For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2259 2260 | ID| Error Message| 2261 | -------- | -------- | 2262 | 1900010 | read data from message sequence failed | 2263 2264**Example** 2265 2266 ```ts 2267 import hilog from '@ohos.hilog'; 2268 import { BusinessError } from '@ohos.base'; 2269 2270 let data = rpc.MessageSequence.create(); 2271 try { 2272 data.writeBooleanArray([false, true, false]); 2273 } catch(error) { 2274 let e: BusinessError = error as BusinessError; 2275 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2276 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2277 } 2278 let array: Array<boolean> = new Array(3); 2279 try { 2280 data.readBooleanArray(array); 2281 } catch(error) { 2282 let e: BusinessError = error as BusinessError; 2283 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2284 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2285 } 2286 ``` 2287 2288### readBooleanArray 2289 2290readBooleanArray(): boolean[] 2291 2292Reads the Boolean array from this **MessageSequence** object. 2293 2294**System capability**: SystemCapability.Communication.IPC.Core 2295 2296**Return value** 2297 2298 | Type | Description | 2299 | --------- | -------------- | 2300 | boolean[] | Boolean array read.| 2301 2302**Error codes** 2303 2304For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2305 2306 | ID| Error Message| 2307 | -------- | -------- | 2308 | 1900010 | read data from message sequence failed | 2309 2310**Example** 2311 2312 ```ts 2313 import hilog from '@ohos.hilog'; 2314 import { BusinessError } from '@ohos.base'; 2315 2316 let data = rpc.MessageSequence.create(); 2317 try { 2318 data.writeBooleanArray([false, true, false]); 2319 } catch(error) { 2320 let e: BusinessError = error as BusinessError; 2321 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2322 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2323 } 2324 try { 2325 let array = data.readBooleanArray(); 2326 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2327 } catch(error) { 2328 let e: BusinessError = error as BusinessError; 2329 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2330 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2331 } 2332 ``` 2333 2334### writeCharArray 2335 2336writeCharArray(charArray: number[]): void 2337 2338Writes a character array to this **MessageSequence** object. 2339 2340**System capability**: SystemCapability.Communication.IPC.Core 2341 2342**Parameters** 2343 2344 | Name | Type | Mandatory| Description | 2345 | --------- | -------- | ---- | ---------------------- | 2346 | charArray | number[] | Yes | Character array to write.| 2347 2348**Error codes** 2349 2350For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2351 2352 | ID| Error Message| 2353 | -------- | -------- | 2354 | 1900009 | write data to message sequence failed | 2355 2356**Example** 2357 2358 ```ts 2359 import hilog from '@ohos.hilog'; 2360 import { BusinessError } from '@ohos.base'; 2361 2362 let data = rpc.MessageSequence.create(); 2363 try { 2364 data.writeCharArray([97, 98, 88]); 2365 } catch(error) { 2366 let e: BusinessError = error as BusinessError; 2367 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2368 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2369 } 2370 ``` 2371 2372### readCharArray 2373 2374readCharArray(dataIn: number[]): void 2375 2376Reads a character array from this **MessageSequence** object. 2377 2378**System capability**: SystemCapability.Communication.IPC.Core 2379 2380**Parameters** 2381 2382 | Name| Type | Mandatory| Description | 2383 | ------ | -------- | ---- | ---------------------- | 2384 | dataIn | number[] | Yes | Character array to read.| 2385 2386**Error codes** 2387 2388For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2389 2390 | ID| Error Message| 2391 | -------- | -------- | 2392 | 1900010 | read data from message sequence failed | 2393 2394**Example** 2395 2396 ```ts 2397 import hilog from '@ohos.hilog'; 2398 import { BusinessError } from '@ohos.base'; 2399 2400 let data = rpc.MessageSequence.create(); 2401 try { 2402 data.writeCharArray([97, 98, 88]); 2403 } catch(error) { 2404 let e: BusinessError = error as BusinessError; 2405 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2406 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2407 } 2408 let array: Array<number> = new Array(3); 2409 try { 2410 data.readCharArray(array); 2411 } catch(error) { 2412 let e: BusinessError = error as BusinessError; 2413 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2414 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2415 } 2416 ``` 2417 2418### readCharArray 2419 2420readCharArray(): number[] 2421 2422Reads the character array from this **MessageSequence** object. 2423 2424**System capability**: SystemCapability.Communication.IPC.Core 2425 2426**Return value** 2427 2428 | Type | Description | 2429 | -------- | ------------------ | 2430 | number[] | Character array read.| 2431 2432**Error codes** 2433 2434For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2435 2436 | ID| Error Message| 2437 | -------- | -------- | 2438 | 1900010 | read data from message sequence failed | 2439 2440**Example** 2441 2442 ```ts 2443 import hilog from '@ohos.hilog'; 2444 import { BusinessError } from '@ohos.base'; 2445 2446 let data = rpc.MessageSequence.create(); 2447 try { 2448 data.writeCharArray([97, 98, 88]); 2449 } catch(error) { 2450 let e: BusinessError = error as BusinessError; 2451 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2452 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2453 } 2454 try { 2455 let array = data.readCharArray(); 2456 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2457 } catch(error) { 2458 let e: BusinessError = error as BusinessError; 2459 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2460 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2461 } 2462 ``` 2463 2464### writeStringArray 2465 2466writeStringArray(stringArray: string[]): void 2467 2468Writes a string array to this **MessageSequence** object. 2469 2470**System capability**: SystemCapability.Communication.IPC.Core 2471 2472**Parameters** 2473 2474 | Name | Type | Mandatory| Description | 2475 | ----------- | -------- | ---- | ------------------------------------------------------- | 2476 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 2477 2478**Error codes** 2479 2480For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2481 2482 | ID| Error Message| 2483 | -------- | -------- | 2484 | 1900009 | write data to message sequence failed | 2485 2486**Example** 2487 2488 ```ts 2489 import hilog from '@ohos.hilog'; 2490 import { BusinessError } from '@ohos.base'; 2491 2492 let data = rpc.MessageSequence.create(); 2493 try { 2494 data.writeStringArray(["abc", "def"]); 2495 } catch(error) { 2496 let e: BusinessError = error as BusinessError; 2497 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2498 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2499 } 2500 ``` 2501 2502### readStringArray 2503 2504readStringArray(dataIn: string[]): void 2505 2506Reads a string array from this **MessageSequence** object. 2507 2508**System capability**: SystemCapability.Communication.IPC.Core 2509 2510**Parameters** 2511 2512 | Name| Type | Mandatory| Description | 2513 | ------ | -------- | ---- | -------------------- | 2514 | dataIn | string[] | Yes | String array to read.| 2515 2516**Error codes** 2517 2518For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2519 2520 | ID| Error Message| 2521 | -------- | -------- | 2522 | 1900010 | read data from message sequence failed | 2523 2524**Example** 2525 2526 ```ts 2527 import hilog from '@ohos.hilog'; 2528 import { BusinessError } from '@ohos.base'; 2529 2530 let data = rpc.MessageSequence.create(); 2531 try { 2532 data.writeStringArray(["abc", "def"]); 2533 } catch(error) { 2534 let e: BusinessError = error as BusinessError; 2535 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2536 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2537 } 2538 let array: Array<string> = new Array(2); 2539 try { 2540 data.readStringArray(array); 2541 } catch(error) { 2542 let e: BusinessError = error as BusinessError; 2543 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2544 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2545 } 2546 ``` 2547 2548### readStringArray 2549 2550readStringArray(): string[] 2551 2552Reads the string array from this **MessageSequence** object. 2553 2554**System capability**: SystemCapability.Communication.IPC.Core 2555 2556**Return value** 2557 2558 | Type | Description | 2559 | -------- | ---------------- | 2560 | string[] | String array read.| 2561 2562**Error codes** 2563 2564For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2565 2566 | ID| Error Message| 2567 | -------- | -------- | 2568 | 1900010 | read data from message sequence failed | 2569 2570**Example** 2571 2572 ```ts 2573 import hilog from '@ohos.hilog'; 2574 import { BusinessError } from '@ohos.base'; 2575 2576 let data = rpc.MessageSequence.create(); 2577 try { 2578 data.writeStringArray(["abc", "def"]); 2579 } catch(error) { 2580 let e: BusinessError = error as BusinessError; 2581 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2582 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2583 } 2584 try { 2585 let array = data.readStringArray(); 2586 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2587 } catch(error) { 2588 let e: BusinessError = error as BusinessError; 2589 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2590 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2591 } 2592 ``` 2593 2594### writeNoException 2595 2596writeNoException(): void 2597 2598Writes information to this **MessageSequence** object indicating that no exception occurred. 2599 2600**System capability**: SystemCapability.Communication.IPC.Core 2601 2602**Error codes** 2603 2604For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2605 2606 | ID| Error Message| 2607 | -------- | -------- | 2608 | 1900009 | write data to message sequence failed | 2609 2610**Example** 2611 2612 ```ts 2613 import hilog from '@ohos.hilog'; 2614 import { BusinessError } from '@ohos.base'; 2615 2616 class TestRemoteObject extends rpc.RemoteObject { 2617 constructor(descriptor: string) { 2618 super(descriptor); 2619 } 2620 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2621 if (code === 1) { 2622 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2623 try { 2624 reply.writeNoException(); 2625 } catch(error) { 2626 let e: BusinessError = error as BusinessError; 2627 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2628 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2629 } 2630 return true; 2631 } else { 2632 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2633 return false; 2634 } 2635 } 2636 } 2637 ``` 2638 2639### readException 2640 2641readException(): void 2642 2643Reads the exception information from this **MessageSequence** object. 2644 2645**System capability**: SystemCapability.Communication.IPC.Core 2646 2647**Error codes** 2648 2649For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2650 2651 | ID| Error Message| 2652 | -------- | -------- | 2653 | 1900010 | read data from message sequence failed | 2654 2655**Example** 2656 2657 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 2658 2659 ```ts 2660 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 2661 // import FA from "@ohos.ability.featureAbility"; 2662 import Want from '@ohos.app.ability.Want'; 2663 import common from '@ohos.app.ability.common'; 2664 import hilog from '@ohos.hilog'; 2665 2666 let proxy: rpc.IRemoteObject | undefined; 2667 let connect: common.ConnectOptions = { 2668 onConnect: (elementName, remoteProxy) => { 2669 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2670 proxy = remoteProxy; 2671 }, 2672 onDisconnect: (elementName) => { 2673 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2674 }, 2675 onFailed: () => { 2676 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2677 } 2678 }; 2679 let want: Want = { 2680 bundleName: "com.ohos.server", 2681 abilityName: "com.ohos.server.EntryAbility", 2682 }; 2683 2684 // Use this method to connect to the ability for the FA model. 2685 // FA.connectAbility(want,connect); 2686 2687 this.context.connectServiceExtensionAbility(want, connect); 2688 ``` 2689 2690 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 2691 2692 ```ts 2693 import { BusinessError } from '@ohos.base'; 2694 import hilog from '@ohos.hilog'; 2695 2696 let option = new rpc.MessageOption(); 2697 let data = rpc.MessageSequence.create(); 2698 let reply = rpc.MessageSequence.create(); 2699 data.writeNoException(); 2700 data.writeInt(6); 2701 if (proxy != undefined) { 2702 proxy.sendMessageRequest(1, data, reply, option) 2703 .then((result: rpc.RequestResult) => { 2704 if (result.errCode === 0) { 2705 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2706 try { 2707 result.reply.readException(); 2708 } catch(error) { 2709 let e: BusinessError = error as BusinessError; 2710 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2711 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2712 } 2713 let num = result.reply.readInt(); 2714 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2715 } else { 2716 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2717 } 2718 }).catch((e: Error) => { 2719 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2720 }).finally (() => { 2721 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2722 data.reclaim(); 2723 reply.reclaim(); 2724 }); 2725 } 2726 ``` 2727 2728### writeParcelableArray 2729 2730writeParcelableArray(parcelableArray: Parcelable[]): void 2731 2732Writes a **Parcelable** array to this **MessageSequence** object. 2733 2734**System capability**: SystemCapability.Communication.IPC.Core 2735 2736**Parameters** 2737 2738| Name | Type | Mandatory| Description | 2739| --------------- | ------------ | ---- | -------------------------- | 2740| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to write.| 2741 2742**Error codes** 2743 2744For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2745 2746 | ID| Error Message| 2747 | -------- | -------- | 2748 | 1900009 | write data to message sequence failed | 2749 2750**Example** 2751 2752 ```ts 2753 import hilog from '@ohos.hilog'; 2754 import { BusinessError } from '@ohos.base'; 2755 2756 class MyParcelable implements rpc.Parcelable { 2757 num: number = 0; 2758 str: string = ''; 2759 constructor(num: number, str: string) { 2760 this.num = num; 2761 this.str = str; 2762 } 2763 marshalling(messageSequence: rpc.MessageSequence): boolean { 2764 messageSequence.writeInt(this.num); 2765 messageSequence.writeString(this.str); 2766 return true; 2767 } 2768 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2769 this.num = messageSequence.readInt(); 2770 this.str = messageSequence.readString(); 2771 return true; 2772 } 2773 } 2774 let parcelable = new MyParcelable(1, "aaa"); 2775 let parcelable2 = new MyParcelable(2, "bbb"); 2776 let parcelable3 = new MyParcelable(3, "ccc"); 2777 let a = [parcelable, parcelable2, parcelable3]; 2778 let data = rpc.MessageSequence.create(); 2779 try { 2780 data.writeParcelableArray(a); 2781 } catch(error) { 2782 let e: BusinessError = error as BusinessError; 2783 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2784 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2785 } 2786 ``` 2787 2788### readParcelableArray 2789 2790readParcelableArray(parcelableArray: Parcelable[]): void 2791 2792Reads a **Parcelable** array from this **MessageSequence** object. 2793 2794**System capability**: SystemCapability.Communication.IPC.Core 2795 2796**Parameters** 2797 2798| Name | Type | Mandatory| Description | 2799| --------------- | ------------ | ---- | -------------------------- | 2800| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to read.| 2801 2802**Error codes** 2803 2804For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2805 2806 | ID| Error Message| 2807 | -------- | -------- | 2808 | 1900010 | read data from message sequence failed | 2809 | 1900012 | call js callback function failed | 2810 2811**Example** 2812 2813 ```ts 2814 import hilog from '@ohos.hilog'; 2815 import { BusinessError } from '@ohos.base'; 2816 2817 class MyParcelable implements rpc.Parcelable { 2818 num: number = 0; 2819 str: string = ''; 2820 constructor(num: number, str: string) { 2821 this.num = num; 2822 this.str = str; 2823 } 2824 marshalling(messageSequence: rpc.MessageSequence): boolean { 2825 messageSequence.writeInt(this.num); 2826 messageSequence.writeString(this.str); 2827 return true; 2828 } 2829 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2830 this.num = messageSequence.readInt(); 2831 this.str = messageSequence.readString(); 2832 return true; 2833 } 2834 } 2835 let parcelable = new MyParcelable(1, "aaa"); 2836 let parcelable2 = new MyParcelable(2, "bbb"); 2837 let parcelable3 = new MyParcelable(3, "ccc"); 2838 let a = [parcelable, parcelable2, parcelable3]; 2839 let data = rpc.MessageSequence.create(); 2840 data.writeParcelableArray(a); 2841 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2842 try { 2843 data.readParcelableArray(b); 2844 } catch(error) { 2845 let e: BusinessError = error as BusinessError; 2846 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2847 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2848 } 2849 ``` 2850 2851### writeRemoteObjectArray 2852 2853writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2854 2855Writes an array of **IRemoteObject** objects to this **MessageSequence** object. 2856 2857**System capability**: SystemCapability.Communication.IPC.Core 2858 2859**Parameters** 2860 2861| Name | Type | Mandatory| Description | 2862| ----------- | --------------- | ---- | ---------------------------------------------- | 2863| objectArray | [IRemoteObject](#iremoteobject)[] | Yes | Array of **IRemoteObject** objects to write.| 2864 2865**Error codes** 2866 2867For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2868 2869 | ID| Error Message| 2870 | -------- | -------- | 2871 | 1900009 | write data to message sequence failed | 2872 2873**Example** 2874 2875 ```ts 2876 import hilog from '@ohos.hilog'; 2877 import { BusinessError } from '@ohos.base'; 2878 2879 class TestRemoteObject extends rpc.RemoteObject { 2880 constructor(descriptor: string) { 2881 super(descriptor); 2882 this.modifyLocalInterface(this, descriptor); 2883 } 2884 2885 asObject(): rpc.IRemoteObject { 2886 return this; 2887 } 2888 } 2889 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2890 let data = rpc.MessageSequence.create(); 2891 try { 2892 data.writeRemoteObjectArray(a); 2893 } catch(error) { 2894 let e: BusinessError = error as BusinessError; 2895 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2896 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2897 } 2898 ``` 2899 2900### readRemoteObjectArray 2901 2902readRemoteObjectArray(objects: IRemoteObject[]): void 2903 2904Reads an array of **IRemoteObject** objects from this **MessageSequence** object. 2905 2906**System capability**: SystemCapability.Communication.IPC.Core 2907 2908**Parameters** 2909 2910| Name | Type | Mandatory| Description | 2911| ------- | --------------- | ---- | ---------------------------------------------- | 2912| objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 2913 2914**Error codes** 2915 2916For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2917 2918 | ID| Error Message| 2919 | -------- | -------- | 2920 | 1900010 | read data from message sequence failed | 2921 2922**Example** 2923 2924 ```ts 2925 import hilog from '@ohos.hilog'; 2926 import { BusinessError } from '@ohos.base'; 2927 2928 class TestRemoteObject extends rpc.RemoteObject { 2929 constructor(descriptor: string) { 2930 super(descriptor); 2931 this.modifyLocalInterface(this, descriptor); 2932 } 2933 2934 asObject(): rpc.IRemoteObject { 2935 return this; 2936 } 2937 } 2938 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2939 let data = rpc.MessageSequence.create(); 2940 data.writeRemoteObjectArray(a); 2941 let b: Array<rpc.IRemoteObject> = new Array(3); 2942 try { 2943 data.readRemoteObjectArray(b); 2944 } catch(error) { 2945 let e: BusinessError = error as BusinessError; 2946 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 2947 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 2948 } 2949 ``` 2950 2951### readRemoteObjectArray 2952 2953readRemoteObjectArray(): IRemoteObject[] 2954 2955Reads the **IRemoteObject** object array from this **MessageSequence** object. 2956 2957**System capability**: SystemCapability.Communication.IPC.Core 2958 2959**Return value** 2960 2961| Type | Description | 2962| --------------- | --------------------------- | 2963| [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array read.| 2964 2965**Error codes** 2966 2967For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2968 2969 | ID| Error Message| 2970 | -------- | -------- | 2971 | 1900010 | read data from message sequence failed | 2972 2973**Example** 2974 2975 ```ts 2976 import hilog from '@ohos.hilog'; 2977 import { BusinessError } from '@ohos.base'; 2978 2979 class TestRemoteObject extends rpc.RemoteObject { 2980 constructor(descriptor: string) { 2981 super(descriptor); 2982 this.modifyLocalInterface(this, descriptor); 2983 } 2984 2985 asObject(): rpc.IRemoteObject { 2986 return this; 2987 } 2988 } 2989 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2990 let data = rpc.MessageSequence.create(); 2991 data.writeRemoteObjectArray(a); 2992 try { 2993 let b = data.readRemoteObjectArray(); 2994 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 2995 } catch(error) { 2996 let e: BusinessError = error as BusinessError; 2997 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 2998 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 2999 } 3000 ``` 3001 3002### closeFileDescriptor 3003 3004static closeFileDescriptor(fd: number): void 3005 3006Closes a file descriptor. This API is a static method. 3007 3008**System capability**: SystemCapability.Communication.IPC.Core 3009 3010**Parameters** 3011 3012 | Name| Type | Mandatory| Description | 3013 | ------ | ------ | ---- | -------------------- | 3014 | fd | number | Yes | File descriptor to close.| 3015 3016**Example** 3017 3018 ```ts 3019 import fs from '@ohos.file.fs'; 3020 import hilog from '@ohos.hilog'; 3021 import { BusinessError } from '@ohos.base'; 3022 3023 let filePath = "path/to/file"; 3024 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3025 try { 3026 rpc.MessageSequence.closeFileDescriptor(file.fd); 3027 } catch(error) { 3028 let e: BusinessError = error as BusinessError; 3029 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3030 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3031 } 3032 ``` 3033 3034### dupFileDescriptor 3035 3036static dupFileDescriptor(fd: number) :number 3037 3038Duplicates a file descriptor. This API is a static method. 3039 3040**System capability**: SystemCapability.Communication.IPC.Core 3041 3042**Parameters** 3043 3044 | Name| Type | Mandatory| Description | 3045 | ------ | ------ | ---- | ------------------------ | 3046 | fd | number | Yes | File descriptor to duplicate.| 3047 3048**Return value** 3049 3050 | Type | Description | 3051 | ------ | -------------------- | 3052 | number | New file descriptor.| 3053 3054**Error codes** 3055 3056For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3057 3058 | ID| Error Message| 3059 | -------- | -------- | 3060 | 1900013 | call os dup function failed | 3061 3062**Example** 3063 3064 ```ts 3065 import fs from '@ohos.file.fs'; 3066 import hilog from '@ohos.hilog'; 3067 import { BusinessError } from '@ohos.base'; 3068 3069 let filePath = "path/to/file"; 3070 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3071 try { 3072 rpc.MessageSequence.dupFileDescriptor(file.fd); 3073 } catch(error) { 3074 let e: BusinessError = error as BusinessError; 3075 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3076 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3077 } 3078 ``` 3079 3080### containFileDescriptors 3081 3082containFileDescriptors(): boolean 3083 3084Checks whether this **MessageSequence** object contains file descriptors. 3085 3086**System capability**: SystemCapability.Communication.IPC.Core 3087 3088**Return value** 3089 3090 | Type | Description | 3091 | ------- | -------------------------------------------------------------------- | 3092 | boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.| 3093 3094**Example** 3095 3096 ```ts 3097 import fs from '@ohos.file.fs'; 3098 import hilog from '@ohos.hilog'; 3099 import { BusinessError } from '@ohos.base'; 3100 3101 let sequence = new rpc.MessageSequence(); 3102 let filePath = "path/to/file"; 3103 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3104 try { 3105 sequence.writeFileDescriptor(file.fd); 3106 } catch(error) { 3107 let e: BusinessError = error as BusinessError; 3108 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3109 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3110 } 3111 try { 3112 let containFD = sequence.containFileDescriptors(); 3113 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3114 } catch(error) { 3115 let e: BusinessError = error as BusinessError; 3116 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3117 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3118 } 3119 ``` 3120 3121### writeFileDescriptor 3122 3123writeFileDescriptor(fd: number): void 3124 3125Writes a file descriptor to this **MessageSequence** object. 3126 3127**System capability**: SystemCapability.Communication.IPC.Core 3128 3129**Parameters** 3130 3131 | Name| Type | Mandatory| Description | 3132 | ------ | ------ | ---- | ------------ | 3133 | fd | number | Yes | File descriptor to write.| 3134 3135**Error codes** 3136 3137For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3138 3139 | ID| Error Message| 3140 | -------- | -------- | 3141 | 1900009 | write data to message sequence failed | 3142 3143**Example** 3144 3145 ```ts 3146 import fs from '@ohos.file.fs'; 3147 import hilog from '@ohos.hilog'; 3148 import { BusinessError } from '@ohos.base'; 3149 3150 let sequence = new rpc.MessageSequence(); 3151 let filePath = "path/to/file"; 3152 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3153 try { 3154 sequence.writeFileDescriptor(file.fd); 3155 } catch(error) { 3156 let e: BusinessError = error as BusinessError; 3157 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3158 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3159 } 3160 ``` 3161 3162### readFileDescriptor 3163 3164readFileDescriptor(): number 3165 3166Reads the file descriptor from this **MessageSequence** object. 3167 3168**System capability**: SystemCapability.Communication.IPC.Core 3169 3170**Return value** 3171 3172 | Type | Description | 3173 | ------ | ---------------- | 3174 | number | File descriptor read.| 3175 3176**Error codes** 3177 3178For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3179 3180 | ID| Error Message| 3181 | -------- | -------- | 3182 | 1900010 | read data from message sequence failed | 3183 3184**Example** 3185 3186 ```ts 3187 import fs from '@ohos.file.fs'; 3188 import hilog from '@ohos.hilog'; 3189 import { BusinessError } from '@ohos.base'; 3190 3191 let sequence = new rpc.MessageSequence(); 3192 let filePath = "path/to/file"; 3193 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3194 try { 3195 sequence.writeFileDescriptor(file.fd); 3196 } catch(error) { 3197 let e: BusinessError = error as BusinessError; 3198 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3199 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3200 } 3201 try { 3202 let readFD = sequence.readFileDescriptor(); 3203 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3204 } catch(error) { 3205 let e: BusinessError = error as BusinessError; 3206 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3207 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3208 } 3209 ``` 3210 3211### writeAshmem 3212 3213writeAshmem(ashmem: Ashmem): void 3214 3215Writes an anonymous shared object to this **MessageSequence** object. 3216 3217**System capability**: SystemCapability.Communication.IPC.Core 3218 3219**Parameters** 3220 3221| Name| Type | Mandatory| Description | 3222| ------ | ------ | ---- | ------------------------------------- | 3223| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 3224 3225**Error codes** 3226 3227For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3228 3229 | ID| Error Message| 3230 | -------- | ------- | 3231 | 1900003 | write to ashmem failed | 3232 3233**Example** 3234 3235 ```ts 3236 import hilog from '@ohos.hilog'; 3237 import { BusinessError } from '@ohos.base'; 3238 3239 let sequence = new rpc.MessageSequence(); 3240 let ashmem: rpc.Ashmem | undefined = undefined; 3241 try { 3242 ashmem = rpc.Ashmem.create("ashmem", 1024); 3243 try { 3244 sequence.writeAshmem(ashmem); 3245 } catch(error) { 3246 let e: BusinessError = error as BusinessError; 3247 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3248 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3249 } 3250 } catch(error) { 3251 let e: BusinessError = error as BusinessError; 3252 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3253 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3254 } 3255 ``` 3256 3257### readAshmem 3258 3259readAshmem(): Ashmem 3260 3261Reads the anonymous shared object from this **MessageSequence** object. 3262 3263**System capability**: SystemCapability.Communication.IPC.Core 3264 3265**Return value** 3266 3267| Type | Description | 3268| ------ | ------------------ | 3269| [Ashmem](#ashmem8) | Anonymous share object obtained.| 3270 3271**Error codes** 3272 3273For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3274 3275 | ID| Error Message| 3276 | -------- | -------- | 3277 | 1900004 | read from ashmem failed | 3278 3279**Example** 3280 3281 ```ts 3282 import hilog from '@ohos.hilog'; 3283 import { BusinessError } from '@ohos.base'; 3284 3285 let sequence = new rpc.MessageSequence(); 3286 let ashmem: rpc.Ashmem | undefined = undefined; 3287 try { 3288 ashmem = rpc.Ashmem.create("ashmem", 1024); 3289 try { 3290 sequence.writeAshmem(ashmem); 3291 } catch(error) { 3292 let e: BusinessError = error as BusinessError; 3293 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3294 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3295 } 3296 } catch(error) { 3297 let e: BusinessError = error as BusinessError; 3298 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3299 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3300 } 3301 try { 3302 sequence.readAshmem(); 3303 } catch(error) { 3304 let e: BusinessError = error as BusinessError; 3305 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3306 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3307 } 3308 ``` 3309 3310### getRawDataCapacity 3311 3312getRawDataCapacity(): number 3313 3314Obtains the maximum amount of raw data that can be held by this **MessageSequence** object. 3315 3316**System capability**: SystemCapability.Communication.IPC.Core 3317 3318**Return value** 3319 3320 | Type | Description | 3321 | ------ | ------------------------------------------------------------ | 3322 | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageSequence** object.| 3323 3324**Example** 3325 3326 ```ts 3327 import hilog from '@ohos.hilog'; 3328 3329 let sequence = new rpc.MessageSequence(); 3330 let result = sequence.getRawDataCapacity(); 3331 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3332 ``` 3333 3334### writeRawData 3335 3336writeRawData(rawData: number[], size: number): void 3337 3338Writes raw data to this **MessageSequence** object. 3339 3340**System capability**: SystemCapability.Communication.IPC.Core 3341 3342**Parameters** 3343 3344 | Name | Type | Mandatory| Description | 3345 | ------- | -------- | ---- | ---------------------------------- | 3346 | rawData | number[] | Yes | Raw data to write. | 3347 | size | number | Yes | Size of the raw data, in bytes.| 3348 3349**Error codes** 3350 3351For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3352 3353 | ID| Error Message| 3354 | -------- | -------- | 3355 | 1900009 | write data to message sequence failed | 3356 3357**Example** 3358 3359 ```ts 3360 import hilog from '@ohos.hilog'; 3361 import { BusinessError } from '@ohos.base'; 3362 3363 let sequence = new rpc.MessageSequence(); 3364 let arr = [1, 2, 3, 4, 5]; 3365 try { 3366 sequence.writeRawData(arr, arr.length); 3367 } catch(error) { 3368 let e: BusinessError = error as BusinessError; 3369 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3370 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3371 } 3372 ``` 3373 3374### readRawData 3375 3376readRawData(size: number): number[] 3377 3378Reads raw data from this **MessageSequence** object. 3379 3380**System capability**: SystemCapability.Communication.IPC.Core 3381 3382**Parameters** 3383 3384 | Name| Type | Mandatory| Description | 3385 | ------ | ------ | ---- | ------------------------ | 3386 | size | number | Yes | Size of the raw data to read.| 3387 3388**Return value** 3389 3390 | Type | Description | 3391 | -------- | ------------------------------ | 3392 | number[] | Raw data obtained, in bytes.| 3393 3394**Error codes** 3395 3396For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3397 3398 | ID| Error Message| 3399 | -------- | -------- | 3400 | 1900010 | read data from message sequence failed | 3401 3402**Example** 3403 3404 ```ts 3405 import hilog from '@ohos.hilog'; 3406 import { BusinessError } from '@ohos.base'; 3407 3408 let sequence = new rpc.MessageSequence(); 3409 let arr = [1, 2, 3, 4, 5]; 3410 try { 3411 sequence.writeRawData(arr, arr.length); 3412 } catch(error) { 3413 let e: BusinessError = error as BusinessError; 3414 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3415 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3416 } 3417 try { 3418 let result = sequence.readRawData(5); 3419 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3420 } catch(error) { 3421 let e: BusinessError = error as BusinessError; 3422 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3423 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3424 } 3425 ``` 3426 3427## MessageParcel<sup>(deprecated)</sup> 3428 3429>This class is no longer maintained since API version 9. You are advised to use [MessageSequence](#messagesequence9). 3430 3431Provides APIs for reading and writing data in specific format. During RPC, the sender can use the **write()** method provided by **MessageParcel** to write data in specific format to a **MessageParcel** object. The receiver can use the **read()** method provided by **MessageParcel** to read data in specific format from a **MessageParcel** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 3432 3433### create 3434 3435static create(): MessageParcel 3436 3437Creates a **MessageParcel** object. This method is a static method. 3438 3439**System capability**: SystemCapability.Communication.IPC.Core 3440 3441**Return value** 3442 3443 | Type | Description | 3444 | ------------- | ----------------------------- | 3445 | [MessageParcel](#messageparceldeprecated) | **MessageParcel** object created.| 3446 3447**Example** 3448 3449 ```ts 3450 import hilog from '@ohos.hilog'; 3451 3452 let data = rpc.MessageParcel.create(); 3453 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3454 ``` 3455 3456### reclaim 3457 3458reclaim(): void 3459 3460Reclaims the **MessageParcel** object that is no longer used. 3461 3462**System capability**: SystemCapability.Communication.IPC.Core 3463 3464**Example** 3465 3466 ```ts 3467 let reply = rpc.MessageParcel.create(); 3468 reply.reclaim(); 3469 ``` 3470 3471### writeRemoteObject 3472 3473writeRemoteObject(object: IRemoteObject): boolean 3474 3475Serializes a remote object and writes it to this **MessageParcel** object. 3476 3477**System capability**: SystemCapability.Communication.IPC.Core 3478 3479**Parameters** 3480 3481 | Name| Type | Mandatory| Description | 3482 | ------ | ------------------------------- | ---- | --------------------------------------- | 3483 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageParcel** object.| 3484 3485**Return value** 3486 3487 | Type | Description | 3488 | ------- | ----------------------------------------- | 3489 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3490 3491**Example** 3492 3493 ```ts 3494 import hilog from '@ohos.hilog'; 3495 3496 class MyDeathRecipient implements rpc.DeathRecipient { 3497 onRemoteDied() { 3498 hilog.info(0x0000, 'testTag', 'server died'); 3499 } 3500 } 3501 class TestRemoteObject extends rpc.RemoteObject { 3502 constructor(descriptor: string) { 3503 super(descriptor); 3504 } 3505 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3506 return true; 3507 } 3508 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3509 return true; 3510 } 3511 isObjectDead(): boolean { 3512 return false; 3513 } 3514 } 3515 let data = rpc.MessageParcel.create(); 3516 let testRemoteObject = new TestRemoteObject("testObject"); 3517 data.writeRemoteObject(testRemoteObject); 3518 ``` 3519 3520### readRemoteObject 3521 3522readRemoteObject(): IRemoteObject 3523 3524Reads the remote object from this **MessageParcel** object. You can use this method to deserialize the **MessageParcel** object to generate an **IRemoteObject**. The remote objects are read in the order in which they are written to this **MessageParcel** object. 3525 3526**System capability**: SystemCapability.Communication.IPC.Core 3527 3528**Return value** 3529 3530 | Type | Description | 3531 | ------------------------------- | ------------------ | 3532 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 3533 3534**Example** 3535 3536 ```ts 3537 import hilog from '@ohos.hilog'; 3538 3539 class MyDeathRecipient implements rpc.DeathRecipient { 3540 onRemoteDied() { 3541 hilog.info(0x0000, 'testTag', 'server died'); 3542 } 3543 } 3544 class TestRemoteObject extends rpc.RemoteObject { 3545 constructor(descriptor: string) { 3546 super(descriptor); 3547 } 3548 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3549 return true; 3550 } 3551 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3552 return true; 3553 } 3554 isObjectDead(): boolean { 3555 return false; 3556 } 3557 } 3558 let data = rpc.MessageParcel.create(); 3559 let testRemoteObject = new TestRemoteObject("testObject"); 3560 data.writeRemoteObject(testRemoteObject); 3561 let proxy = data.readRemoteObject(); 3562 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3563 ``` 3564 3565### writeInterfaceToken 3566 3567writeInterfaceToken(token: string): boolean 3568 3569Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication. 3570 3571**System capability**: SystemCapability.Communication.IPC.Core 3572 3573**Parameters** 3574 3575 | Name| Type | Mandatory| Description | 3576 | ------ | ------ | ---- | ------------------ | 3577 | token | string | Yes | Interface token to write.| 3578 3579**Return value** 3580 3581 | Type | Description | 3582 | ------- | ----------------------------------------- | 3583 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3584 3585**Example** 3586 3587 ```ts 3588 import hilog from '@ohos.hilog'; 3589 3590 let data = rpc.MessageParcel.create(); 3591 let result = data.writeInterfaceToken("aaa"); 3592 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3593 ``` 3594 3595### readInterfaceToken 3596 3597readInterfaceToken(): string 3598 3599Reads the interface token from this **MessageParcel** object. The interface token is read in the sequence in which it is written to the **MessageParcel** object. The local object can use it to verify the communication. 3600 3601**System capability**: SystemCapability.Communication.IPC.Core 3602 3603**Return value** 3604 3605 | Type | Description | 3606 | ------ | ------------------------ | 3607 | string | Interface token obtained.| 3608 3609**Example** 3610 3611 ```ts 3612 import hilog from '@ohos.hilog'; 3613 3614 class Stub extends rpc.RemoteObject { 3615 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3616 let interfaceToken = data.readInterfaceToken(); 3617 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3618 return true; 3619 } 3620 } 3621 ``` 3622 3623### getSize 3624 3625getSize(): number 3626 3627Obtains the data size of this **MessageParcel** object. 3628 3629**System capability**: SystemCapability.Communication.IPC.Core 3630 3631**Return value** 3632 3633 | Type | Description | 3634 | ------ | --------------------------------------------- | 3635 | number | Size of the **MessageParcel** object obtained, in bytes.| 3636 3637**Example** 3638 3639 ```ts 3640 import hilog from '@ohos.hilog'; 3641 3642 let data = rpc.MessageParcel.create(); 3643 let size = data.getSize(); 3644 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3645 ``` 3646 3647### getCapacity 3648 3649getCapacity(): number 3650 3651Obtains the capacity of this **MessageParcel** object. 3652 3653**System capability**: SystemCapability.Communication.IPC.Core 3654 3655**Return value** 3656 3657 | Type | Description | 3658 | ------ | --------------------------------------------- | 3659 | number | **MessageParcel** capacity obtained, in bytes.| 3660 3661**Example** 3662 3663 ```ts 3664 import hilog from '@ohos.hilog'; 3665 3666 let data = rpc.MessageParcel.create(); 3667 let result = data.getCapacity(); 3668 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 3669 ``` 3670 3671### setSize 3672 3673setSize(size: number): boolean 3674 3675Sets the size of data contained in this **MessageParcel** object. 3676 3677**System capability**: SystemCapability.Communication.IPC.Core 3678 3679**Parameters** 3680 3681 | Name| Type | Mandatory| Description | 3682 | ------ | ------ | ---- | ------------------------------------------- | 3683 | size | number | Yes | Data size to set, in bytes.| 3684 3685**Return value** 3686 3687 | Type | Description | 3688 | ------- | --------------------------------- | 3689 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3690 3691**Example** 3692 3693 ```ts 3694 import hilog from '@ohos.hilog'; 3695 3696 let data = rpc.MessageParcel.create(); 3697 let setSize = data.setSize(16); 3698 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 3699 ``` 3700 3701### setCapacity 3702 3703setCapacity(size: number): boolean 3704 3705Sets the storage capacity of this **MessageParcel** object. 3706 3707**System capability**: SystemCapability.Communication.IPC.Core 3708 3709**Parameters** 3710 3711 | Name| Type | Mandatory| Description | 3712 | ------ | ------ | ---- | ------------------------------------------- | 3713 | size | number | Yes | Storage capacity to set, in bytes.| 3714 3715**Return value** 3716 3717 | Type | Description | 3718 | ------- | --------------------------------- | 3719 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3720 3721**Example** 3722 3723 ```ts 3724 import hilog from '@ohos.hilog'; 3725 3726 let data = rpc.MessageParcel.create(); 3727 let result = data.setCapacity(100); 3728 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 3729 ``` 3730 3731### getWritableBytes 3732 3733getWritableBytes(): number 3734 3735Obtains the writable capacity of this **MessageParcel** object. 3736 3737**System capability**: SystemCapability.Communication.IPC.Core 3738 3739**Return value** 3740 3741 | Type | Description | 3742 | ------ | --------------------------------------------------- | 3743 | number | **MessageParcel** writable capacity obtained, in bytes.| 3744 3745**Example** 3746 3747 ```ts 3748 import hilog from '@ohos.hilog'; 3749 3750 class Stub extends rpc.RemoteObject { 3751 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3752 let getWritableBytes = data.getWritableBytes(); 3753 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 3754 return true; 3755 } 3756 } 3757 ``` 3758 3759### getReadableBytes 3760 3761getReadableBytes(): number 3762 3763Obtains the readable capacity of this **MessageParcel** object. 3764 3765**System capability**: SystemCapability.Communication.IPC.Core 3766 3767**Return value** 3768 3769 | Type | Description | 3770 | ------ | --------------------------------------------------- | 3771 | number | **MessageParcel** object readable capacity, in bytes.| 3772 3773**Example** 3774 3775 ```ts 3776 import hilog from '@ohos.hilog'; 3777 3778 class Stub extends rpc.RemoteObject { 3779 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3780 let result = data.getReadableBytes(); 3781 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 3782 return true; 3783 } 3784 } 3785 ``` 3786 3787### getReadPosition 3788 3789getReadPosition(): number 3790 3791Obtains the read position of this **MessageParcel** object. 3792 3793**System capability**: SystemCapability.Communication.IPC.Core 3794 3795**Return value** 3796 3797 | Type | Description | 3798 | ------ | --------------------------------------- | 3799 | number | Current read position of the **MessageParcel** object.| 3800 3801**Example** 3802 3803 ```ts 3804 import hilog from '@ohos.hilog'; 3805 3806 let data = rpc.MessageParcel.create(); 3807 let readPos = data.getReadPosition(); 3808 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 3809 ``` 3810 3811### getWritePosition 3812 3813getWritePosition(): number 3814 3815Obtains the write position of this **MessageParcel** object. 3816 3817**System capability**: SystemCapability.Communication.IPC.Core 3818 3819**Return value** 3820 3821 | Type | Description | 3822 | ------ | --------------------------------------- | 3823 | number | Current write position of the **MessageParcel** object.| 3824 3825**Example** 3826 3827 ```ts 3828 import hilog from '@ohos.hilog'; 3829 3830 let data = rpc.MessageParcel.create(); 3831 data.writeInt(10); 3832 let bwPos = data.getWritePosition(); 3833 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 3834 ``` 3835 3836### rewindRead 3837 3838rewindRead(pos: number): boolean 3839 3840Moves the read pointer to the specified position. 3841 3842**System capability**: SystemCapability.Communication.IPC.Core 3843 3844**Parameters** 3845 3846 | Name| Type | Mandatory| Description | 3847 | ------ | ------ | ---- | ------------------------ | 3848 | pos | number | Yes | Position from which data is to read.| 3849 3850**Return value** 3851 3852 | Type | Description | 3853 | ------- | ------------------------------------------------- | 3854| boolean | Returns **true** if the read position changes; returns **false** otherwise.| 3855 3856**Example** 3857 3858 ```ts 3859 import hilog from '@ohos.hilog'; 3860 3861 let data = rpc.MessageParcel.create(); 3862 data.writeInt(12); 3863 data.writeString("parcel"); 3864 let number = data.readInt(); 3865 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 3866 data.rewindRead(0); 3867 let number2 = data.readInt(); 3868 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 3869 ``` 3870 3871### rewindWrite 3872 3873rewindWrite(pos: number): boolean 3874 3875Moves the write pointer to the specified position. 3876 3877**System capability**: SystemCapability.Communication.IPC.Core 3878 3879**Parameters** 3880 3881 | Name| Type | Mandatory| Description | 3882 | ------ | ------ | ---- | ------------------------ | 3883 | pos | number | Yes | Position from which data is to write.| 3884 3885**Return value** 3886 3887 | Type | Description | 3888 | ------- | --------------------------------------------- | 3889| boolean | Returns **true** if the write position changes; returns **false** otherwise.| 3890 3891**Example** 3892 3893 ```ts 3894 import hilog from '@ohos.hilog'; 3895 3896 let data = rpc.MessageParcel.create(); 3897 data.writeInt(4); 3898 data.rewindWrite(0); 3899 data.writeInt(5); 3900 let number = data.readInt(); 3901 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 3902 ``` 3903 3904### writeByte 3905 3906writeByte(val: number): boolean 3907 3908Writes a Byte value to this **MessageParcel** object. 3909 3910**System capability**: SystemCapability.Communication.IPC.Core 3911 3912**Parameters** 3913 3914 | Name| Type | Mandatory| Description | 3915 | ------ | ------ | ---- | ---------------- | 3916 | val | number | Yes | Byte value to write.| 3917 3918**Return value** 3919 3920 | Type | Description | 3921 | ------- | ----------------------------- | 3922| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3923 3924**Example** 3925 3926 ```ts 3927 import hilog from '@ohos.hilog'; 3928 3929 let data = rpc.MessageParcel.create(); 3930 let result = data.writeByte(2); 3931 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 3932 ``` 3933 3934### readByte 3935 3936readByte(): number 3937 3938Reads the Byte value from this **MessageParcel** object. 3939 3940**System capability**: SystemCapability.Communication.IPC.Core 3941 3942**Return value** 3943 3944 | Type | Description | 3945 | ------ | ------------ | 3946 | number | Byte value read.| 3947 3948**Example** 3949 3950 ```ts 3951 import hilog from '@ohos.hilog'; 3952 3953 let data = rpc.MessageParcel.create(); 3954 let result = data.writeByte(2); 3955 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 3956 let ret = data.readByte(); 3957 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 3958 ``` 3959 3960### writeShort 3961 3962writeShort(val: number): boolean 3963 3964Writes a Short int value to this **MessageParcel** object. 3965 3966**System capability**: SystemCapability.Communication.IPC.Core 3967 3968**Parameters** 3969 3970 | Name| Type | Mandatory| Description | 3971 | ------ | ------ | ---- | ------------------ | 3972 | val | number | Yes | Short int value to write.| 3973 3974**Return value** 3975 3976 | Type | Description | 3977 | ------- | ----------------------------- | 3978 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3979 3980**Example** 3981 3982 ```ts 3983 import hilog from '@ohos.hilog'; 3984 3985 let data = rpc.MessageParcel.create(); 3986 let result = data.writeShort(8); 3987 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 3988 ``` 3989 3990### readShort 3991 3992readShort(): number 3993 3994Reads the Short int value from this **MessageParcel** object. 3995 3996**System capability**: SystemCapability.Communication.IPC.Core 3997 3998**Return value** 3999 4000 | Type | Description | 4001 | ------ | -------------- | 4002 | number | Short int value read.| 4003 4004**Example** 4005 4006 ```ts 4007 import hilog from '@ohos.hilog'; 4008 4009 let data = rpc.MessageParcel.create(); 4010 let result = data.writeShort(8); 4011 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4012 let ret = data.readShort(); 4013 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4014 ``` 4015 4016### writeInt 4017 4018writeInt(val: number): boolean 4019 4020Writes an Int value to this **MessageParcel** object. 4021 4022**System capability**: SystemCapability.Communication.IPC.Core 4023 4024**Parameters** 4025 4026 | Name| Type | Mandatory| Description | 4027 | ------ | ------ | ---- | ---------------- | 4028 | val | number | Yes | Int value to write.| 4029 4030**Return value** 4031 4032 | Type | Description | 4033 | ------- | ----------------------------- | 4034| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4035 4036**Example** 4037 4038 ```ts 4039 import hilog from '@ohos.hilog'; 4040 4041 let data = rpc.MessageParcel.create(); 4042 let result = data.writeInt(10); 4043 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4044 ``` 4045 4046### readInt 4047 4048readInt(): number 4049 4050Reads the Int value from this **MessageParcel** object. 4051 4052**System capability**: SystemCapability.Communication.IPC.Core 4053 4054**Return value** 4055 4056 | Type | Description | 4057 | ------ | ------------ | 4058 | number | Int value read.| 4059 4060**Example** 4061 4062 ```ts 4063 import hilog from '@ohos.hilog'; 4064 4065 let data = rpc.MessageParcel.create(); 4066 let result = data.writeInt(10); 4067 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4068 let ret = data.readInt(); 4069 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4070 ``` 4071 4072### writeLong 4073 4074writeLong(val: number): boolean 4075 4076Writes a Long int value to this **MessageParcel** object. 4077 4078**System capability**: SystemCapability.Communication.IPC.Core 4079 4080**Parameters** 4081 4082 | Name| Type | Mandatory| Description | 4083 | ------ | ------ | ---- | ---------------- | 4084 | val | number | Yes | Long int value to write.| 4085 4086**Return value** 4087 4088 | Type | Description | 4089 | ------- | --------------------------------- | 4090 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4091 4092**Example** 4093 4094 ```ts 4095 import hilog from '@ohos.hilog'; 4096 4097 let data = rpc.MessageParcel.create(); 4098 let result = data.writeLong(10000); 4099 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4100 ``` 4101 4102### readLong 4103 4104readLong(): number 4105 4106Reads the Long int value from this **MessageParcel** object. 4107 4108**System capability**: SystemCapability.Communication.IPC.Core 4109 4110**Return value** 4111 4112 | Type | Description | 4113 | ------ | -------------- | 4114 | number | Long int value read.| 4115 4116**Example** 4117 4118 ```ts 4119 import hilog from '@ohos.hilog'; 4120 4121 let data = rpc.MessageParcel.create(); 4122 let result = data.writeLong(10000); 4123 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4124 let ret = data.readLong(); 4125 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4126 ``` 4127 4128### writeFloat 4129 4130writeFloat(val: number): boolean 4131 4132Writes a Float value to this **MessageParcel** object. 4133 4134**System capability**: SystemCapability.Communication.IPC.Core 4135 4136**Parameters** 4137 4138 | Name| Type | Mandatory| Description | 4139 | ------ | ------ | ---- | ---------------- | 4140 | val | number | Yes | Float value to write.| 4141 4142**Return value** 4143 4144 | Type | Description | 4145 | ------- | --------------------------------- | 4146 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4147 4148**Example** 4149 4150 ```ts 4151 import hilog from '@ohos.hilog'; 4152 4153 let data = rpc.MessageParcel.create(); 4154 let result = data.writeFloat(1.2); 4155 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4156 ``` 4157 4158### readFloat 4159 4160readFloat(): number 4161 4162Reads the Float value from this **MessageParcel** object. 4163 4164**System capability**: SystemCapability.Communication.IPC.Core 4165 4166**Return value** 4167 4168 | Type | Description | 4169 | ------ | ------------ | 4170 | number | Float value read.| 4171 4172**Example** 4173 4174 ```ts 4175 import hilog from '@ohos.hilog'; 4176 4177 let data = rpc.MessageParcel.create(); 4178 let result = data.writeFloat(1.2); 4179 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4180 let ret = data.readFloat(); 4181 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4182 ``` 4183 4184### writeDouble 4185 4186writeDouble(val: number): boolean 4187 4188Writes a Double value to this **MessageParcel** object. 4189 4190**System capability**: SystemCapability.Communication.IPC.Core 4191 4192**Parameters** 4193 4194 | Name| Type | Mandatory| Description | 4195 | ------ | ------ | ---- | ---------------------- | 4196 | val | number | Yes | Double value to write.| 4197 4198**Return value** 4199 4200 | Type | Description | 4201 | ------- | --------------------------------- | 4202 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4203 4204**Example** 4205 4206 ```ts 4207 import hilog from '@ohos.hilog'; 4208 4209 let data = rpc.MessageParcel.create(); 4210 let result = data.writeDouble(10.2); 4211 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4212 ``` 4213 4214### readDouble 4215 4216readDouble(): number 4217 4218Reads the Double value from this **MessageParcel** object. 4219 4220**System capability**: SystemCapability.Communication.IPC.Core 4221 4222**Return value** 4223 4224 | Type | Description | 4225 | ------ | ------------------ | 4226 | number | Double value read.| 4227 4228**Example** 4229 4230 ```ts 4231 import hilog from '@ohos.hilog'; 4232 4233 let data = rpc.MessageParcel.create(); 4234 let result = data.writeDouble(10.2); 4235 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4236 let ret = data.readDouble(); 4237 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4238 ``` 4239 4240### writeBoolean 4241 4242writeBoolean(val: boolean): boolean 4243 4244Writes a Boolean value to this **MessageParcel** object. 4245 4246**System capability**: SystemCapability.Communication.IPC.Core 4247 4248**Parameters** 4249 4250 | Name| Type | Mandatory| Description | 4251 | ------ | ------- | ---- | ---------------- | 4252 | val | boolean | Yes | Boolean value to write.| 4253 4254**Return value** 4255 4256 | Type | Description | 4257 | ------- | --------------------------------- | 4258 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4259 4260**Example** 4261 4262 ```ts 4263 import hilog from '@ohos.hilog'; 4264 4265 let data = rpc.MessageParcel.create(); 4266 let result = data.writeBoolean(false); 4267 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4268 ``` 4269 4270### readBoolean 4271 4272readBoolean(): boolean 4273 4274Reads the Boolean value from this **MessageParcel** object. 4275 4276**System capability**: SystemCapability.Communication.IPC.Core 4277 4278**Return value** 4279 4280 | Type | Description | 4281 | ------- | -------------------- | 4282 | boolean | Boolean value read.| 4283 4284**Example** 4285 4286 ```ts 4287 import hilog from '@ohos.hilog'; 4288 4289 let data = rpc.MessageParcel.create(); 4290 let result = data.writeBoolean(false); 4291 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4292 let ret = data.readBoolean(); 4293 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4294 ``` 4295 4296### writeChar 4297 4298writeChar(val: number): boolean 4299 4300Writes a Char value to this **MessageParcel** object. 4301 4302**System capability**: SystemCapability.Communication.IPC.Core 4303 4304**Parameters** 4305 4306 | Name| Type | Mandatory| Description | 4307 | ------ | ------ | ---- | -------------------- | 4308 | val | number | Yes | Char value to write.| 4309 4310**Return value** 4311 4312 | Type | Description | 4313 | ------- | ----------------------------- | 4314 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4315 4316**Example** 4317 4318 ```ts 4319 import hilog from '@ohos.hilog'; 4320 4321 let data = rpc.MessageParcel.create(); 4322 let result = data.writeChar(97); 4323 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4324 ``` 4325 4326### readChar 4327 4328readChar(): number 4329 4330Reads the Char value from this **MessageParcel** object. 4331 4332**System capability**: SystemCapability.Communication.IPC.Core 4333 4334**Return value** 4335 4336 | Type | Description | 4337 | ------ | ---------------- | 4338 | number | Char value read.| 4339 4340**Example** 4341 4342 ```ts 4343 import hilog from '@ohos.hilog'; 4344 4345 let data = rpc.MessageParcel.create(); 4346 let result = data.writeChar(97); 4347 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4348 let ret = data.readChar(); 4349 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4350 ``` 4351 4352### writeString 4353 4354writeString(val: string): boolean 4355 4356Writes a string to this **MessageParcel** object. 4357 4358**System capability**: SystemCapability.Communication.IPC.Core 4359 4360**Parameters** 4361 4362 | Name| Type | Mandatory| Description | 4363 | ------ | ------ | ---- | ----------------------------------------- | 4364 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 4365 4366**Return value** 4367 4368 | Type | Description | 4369 | ------- | --------------------------------- | 4370 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4371 4372**Example** 4373 4374 ```ts 4375 import hilog from '@ohos.hilog'; 4376 4377 let data = rpc.MessageParcel.create(); 4378 let result = data.writeString('abc'); 4379 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4380 ``` 4381 4382### readString 4383 4384readString(): string 4385 4386Reads the string from this **MessageParcel** object. 4387 4388**System capability**: SystemCapability.Communication.IPC.Core 4389 4390**Return value** 4391 4392 | Type | Description | 4393 | ------ | -------------- | 4394 | string | String read.| 4395 4396**Example** 4397 4398 ```ts 4399 import hilog from '@ohos.hilog'; 4400 4401 let data = rpc.MessageParcel.create(); 4402 let result = data.writeString('abc'); 4403 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4404 let ret = data.readString(); 4405 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4406 ``` 4407 4408### writeSequenceable 4409 4410writeSequenceable(val: Sequenceable): boolean 4411 4412Writes a sequenceable object to this **MessageParcel** object. 4413 4414**System capability**: SystemCapability.Communication.IPC.Core 4415 4416**Parameters** 4417 4418 | Name| Type | Mandatory| Description | 4419 | ------ | ----------------------------- | ---- | -------------------- | 4420 | val | [Sequenceable](#sequenceabledeprecated) | Yes | Sequenceable object to write.| 4421 4422**Return value** 4423 4424 | Type | Description | 4425 | ------- | -------------------------------- | 4426 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4427 4428**Example** 4429 4430 ```ts 4431 import hilog from '@ohos.hilog'; 4432 4433 class MySequenceable implements rpc.Sequenceable { 4434 num: number = 0; 4435 str: string = ''; 4436 constructor(num: number, str: string) { 4437 this.num = num; 4438 this.str = str; 4439 } 4440 marshalling(messageParcel: rpc.MessageParcel): boolean { 4441 messageParcel.writeInt(this.num); 4442 messageParcel.writeString(this.str); 4443 return true; 4444 } 4445 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4446 this.num = messageParcel.readInt(); 4447 this.str = messageParcel.readString(); 4448 return true; 4449 } 4450 } 4451 let sequenceable = new MySequenceable(1, "aaa"); 4452 let data = rpc.MessageParcel.create(); 4453 let result = data.writeSequenceable(sequenceable); 4454 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4455 ``` 4456 4457### readSequenceable 4458 4459readSequenceable(dataIn: Sequenceable): boolean 4460 4461Reads member variables from this **MessageParcel** object. 4462 4463**System capability**: SystemCapability.Communication.IPC.Core 4464 4465**Parameters** 4466 4467 | Name| Type | Mandatory | Description | 4468 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4469 | dataIn | [Sequenceable](#sequenceabledeprecated) | Yes | Object that reads member variables from the **MessageParcel** object.| 4470 4471**Return value** 4472 4473 | Type | Description | 4474 | ------- | ---------------------------------------- | 4475 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4476 4477**Example** 4478 4479 ```ts 4480 import hilog from '@ohos.hilog'; 4481 4482 class MySequenceable implements rpc.Sequenceable { 4483 num: number = 0; 4484 str: string = ''; 4485 constructor(num: number, str: string) { 4486 this.num = num; 4487 this.str = str; 4488 } 4489 marshalling(messageParcel: rpc.MessageParcel): boolean { 4490 messageParcel.writeInt(this.num); 4491 messageParcel.writeString(this.str); 4492 return true; 4493 } 4494 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4495 this.num = messageParcel.readInt(); 4496 this.str = messageParcel.readString(); 4497 return true; 4498 } 4499 } 4500 let sequenceable = new MySequenceable(1, "aaa"); 4501 let data = rpc.MessageParcel.create(); 4502 let result = data.writeSequenceable(sequenceable); 4503 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4504 let ret = new MySequenceable(0, ""); 4505 let result2 = data.readSequenceable(ret); 4506 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4507 ``` 4508 4509### writeByteArray 4510 4511writeByteArray(byteArray: number[]): boolean 4512 4513Writes a byte array to this **MessageParcel** object. 4514 4515**System capability**: SystemCapability.Communication.IPC.Core 4516 4517**Parameters** 4518 4519 | Name | Type | Mandatory| Description | 4520 | --------- | -------- | ---- | ------------------ | 4521 | byteArray | number[] | Yes | Byte array to write.| 4522 4523**Return value** 4524 4525 | Type | Description | 4526 | ------- | -------------------------------- | 4527 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4528 4529**Example** 4530 4531 ```ts 4532 import hilog from '@ohos.hilog'; 4533 4534 let data = rpc.MessageParcel.create(); 4535 let ByteArrayVar = [1, 2, 3, 4, 5]; 4536 let result = data.writeByteArray(ByteArrayVar); 4537 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4538 ``` 4539 4540### readByteArray 4541 4542readByteArray(dataIn: number[]): void 4543 4544Reads a byte array from this **MessageParcel** object. 4545 4546**System capability**: SystemCapability.Communication.IPC.Core 4547 4548**Parameters** 4549 4550 | Name| Type | Mandatory| Description | 4551 | ------ | -------- | ---- | ------------------ | 4552 | dataIn | number[] | Yes | Byte array to read.| 4553 4554**Example** 4555 4556 ```ts 4557 import hilog from '@ohos.hilog'; 4558 4559 let data = rpc.MessageParcel.create(); 4560 let ByteArrayVar = [1, 2, 3, 4, 5]; 4561 let result = data.writeByteArray(ByteArrayVar); 4562 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4563 let array: Array<number> = new Array(5); 4564 data.readByteArray(array); 4565 ``` 4566 4567### readByteArray 4568 4569readByteArray(): number[] 4570 4571Reads the byte array from this **MessageParcel** object. 4572 4573**System capability**: SystemCapability.Communication.IPC.Core 4574 4575**Return value** 4576 4577 | Type | Description | 4578 | -------- | -------------- | 4579 | number[] | Byte array read.| 4580 4581**Example** 4582 4583 ```ts 4584 import hilog from '@ohos.hilog'; 4585 4586 let data = rpc.MessageParcel.create(); 4587 let ByteArrayVar = [1, 2, 3, 4, 5]; 4588 let result = data.writeByteArray(ByteArrayVar); 4589 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4590 let array = data.readByteArray(); 4591 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4592 ``` 4593 4594### writeShortArray 4595 4596writeShortArray(shortArray: number[]): boolean 4597 4598Writes a short array to this **MessageParcel** object. 4599 4600**System capability**: SystemCapability.Communication.IPC.Core 4601 4602**Parameters** 4603 4604 | Name | Type | Mandatory| Description | 4605 | ---------- | -------- | ---- | -------------------- | 4606 | shortArray | number[] | Yes | Short array to write.| 4607 4608**Return value** 4609 4610 | Type | Description | 4611 | ------- | -------------------------------- | 4612 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4613 4614**Example** 4615 4616 ```ts 4617 import hilog from '@ohos.hilog'; 4618 4619 let data = rpc.MessageParcel.create(); 4620 let result = data.writeShortArray([11, 12, 13]); 4621 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4622 ``` 4623 4624### readShortArray 4625 4626readShortArray(dataIn: number[]): void 4627 4628Reads a short array from this **MessageParcel** object. 4629 4630**System capability**: SystemCapability.Communication.IPC.Core 4631 4632**Parameters** 4633 4634 | Name| Type | Mandatory| Description | 4635 | ------ | -------- | ---- | -------------------- | 4636 | dataIn | number[] | Yes | Short array to read.| 4637 4638**Example** 4639 4640 ```ts 4641 import hilog from '@ohos.hilog'; 4642 4643 let data = rpc.MessageParcel.create(); 4644 let result = data.writeShortArray([11, 12, 13]); 4645 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4646 let array: Array<number> = new Array(3); 4647 data.readShortArray(array); 4648 ``` 4649 4650### readShortArray 4651 4652readShortArray(): number[] 4653 4654Reads the short array from this **MessageParcel** object. 4655 4656**System capability**: SystemCapability.Communication.IPC.Core 4657 4658**Return value** 4659 4660 | Type | Description | 4661 | -------- | ---------------- | 4662 | number[] | Short array read.| 4663 4664**Example** 4665 4666 ```ts 4667 import hilog from '@ohos.hilog'; 4668 4669 let data = rpc.MessageParcel.create(); 4670 let result = data.writeShortArray([11, 12, 13]); 4671 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4672 let array = data.readShortArray(); 4673 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 4674 ``` 4675 4676### writeIntArray 4677 4678writeIntArray(intArray: number[]): boolean 4679 4680Writes an integer array to this **MessageParcel** object. 4681 4682**System capability**: SystemCapability.Communication.IPC.Core 4683 4684**Parameters** 4685 4686 | Name | Type | Mandatory| Description | 4687 | -------- | -------- | ---- | ------------------ | 4688 | intArray | number[] | Yes | Integer array to write.| 4689 4690**Return value** 4691 4692 | Type | Description | 4693 | ------- | -------------------------------- | 4694 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4695 4696**Example** 4697 4698 ```ts 4699 import hilog from '@ohos.hilog'; 4700 4701 let data = rpc.MessageParcel.create(); 4702 let result = data.writeIntArray([100, 111, 112]); 4703 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 4704 ``` 4705 4706### readIntArray 4707 4708readIntArray(dataIn: number[]): void 4709 4710Reads an integer array from this **MessageParcel** object. 4711 4712**System capability**: SystemCapability.Communication.IPC.Core 4713 4714**Parameters** 4715 4716 | Name| Type | Mandatory| Description | 4717 | ------ | -------- | ---- | ------------------ | 4718 | dataIn | number[] | Yes | Integer array to read.| 4719 4720**Example** 4721 4722 ```ts 4723 import hilog from '@ohos.hilog'; 4724 4725 let data = rpc.MessageParcel.create(); 4726 let result = data.writeIntArray([100, 111, 112]); 4727 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 4728 let array: Array<number> = new Array(3); 4729 data.readIntArray(array); 4730 ``` 4731 4732### readIntArray 4733 4734readIntArray(): number[] 4735 4736Reads the integer array from this **MessageParcel** object. 4737 4738**System capability**: SystemCapability.Communication.IPC.Core 4739 4740**Return value** 4741 4742 | Type | Description | 4743 | -------- | -------------- | 4744 | number[] | Integer array read.| 4745 4746**Example** 4747 4748 ```ts 4749 import hilog from '@ohos.hilog'; 4750 4751 let data = rpc.MessageParcel.create(); 4752 let result = data.writeIntArray([100, 111, 112]); 4753 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 4754 let array = data.readIntArray(); 4755 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 4756 ``` 4757 4758### writeLongArray 4759 4760writeLongArray(longArray: number[]): boolean 4761 4762Writes a long array to this **MessageParcel** object. 4763 4764**System capability**: SystemCapability.Communication.IPC.Core 4765 4766**Parameters** 4767 4768 | Name | Type | Mandatory| Description | 4769 | --------- | -------- | ---- | -------------------- | 4770 | longArray | number[] | Yes | Long array to write.| 4771 4772**Return value** 4773 4774 | Type | Description | 4775 | ------- | ----------------------------- | 4776 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4777 4778**Example** 4779 4780 ```ts 4781 import hilog from '@ohos.hilog'; 4782 4783 let data = rpc.MessageParcel.create(); 4784 let result = data.writeLongArray([1111, 1112, 1113]); 4785 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 4786 ``` 4787 4788### readLongArray 4789 4790readLongArray(dataIn: number[]): void 4791 4792Reads a long array from this **MessageParcel** object. 4793 4794**System capability**: SystemCapability.Communication.IPC.Core 4795 4796**Parameters** 4797 4798 | Name| Type | Mandatory| Description | 4799 | ------ | -------- | ---- | -------------------- | 4800 | dataIn | number[] | Yes | Long array to read.| 4801 4802**Example** 4803 4804 ```ts 4805 import hilog from '@ohos.hilog'; 4806 4807 let data = rpc.MessageParcel.create(); 4808 let result = data.writeLongArray([1111, 1112, 1113]); 4809 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 4810 let array: Array<number> = new Array(3); 4811 data.readLongArray(array); 4812 ``` 4813 4814### readLongArray 4815 4816readLongArray(): number[] 4817 4818Reads the long array from this **MessageParcel** object. 4819 4820**System capability**: SystemCapability.Communication.IPC.Core 4821 4822**Return value** 4823 4824 | Type | Description | 4825 | -------- | ---------------- | 4826 | number[] | Long array read.| 4827 4828**Example** 4829 4830 ```ts 4831 import hilog from '@ohos.hilog'; 4832 4833 let data = rpc.MessageParcel.create(); 4834 let result = data.writeLongArray([1111, 1112, 1113]); 4835 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 4836 let array = data.readLongArray(); 4837 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 4838 ``` 4839 4840### writeFloatArray 4841 4842writeFloatArray(floatArray: number[]): boolean 4843 4844Writes a FloatArray to this **MessageParcel** object. 4845 4846**System capability**: SystemCapability.Communication.IPC.Core 4847 4848**Parameters** 4849 4850 | Name| Type| Mandatory| Description | 4851 | ---------- | -------- | ---- | --- | 4852 | floatArray | number[] | Yes | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 4853 4854**Return value** 4855 4856 | Type | Description | 4857 | ------- | -------------------------------- | 4858 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4859 4860**Example** 4861 4862 ```ts 4863 import hilog from '@ohos.hilog'; 4864 4865 let data = rpc.MessageParcel.create(); 4866 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4867 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 4868 ``` 4869 4870### readFloatArray 4871 4872readFloatArray(dataIn: number[]): void 4873 4874Reads a FloatArray from this **MessageParcel** object. 4875 4876**System capability**: SystemCapability.Communication.IPC.Core 4877 4878**Parameters** 4879 4880 | Name| Type | Mandatory| Description | 4881 | ------ | -------- | ---- | ------ | 4882 | dataIn | number[] | Yes | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 4883 4884**Example** 4885 4886 ```ts 4887 import hilog from '@ohos.hilog'; 4888 4889 let data = rpc.MessageParcel.create(); 4890 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4891 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 4892 let array: Array<number> = new Array(3); 4893 data.readFloatArray(array); 4894 ``` 4895 4896### readFloatArray 4897 4898readFloatArray(): number[] 4899 4900Reads the FloatArray from this **MessageParcel** object. 4901 4902**System capability**: SystemCapability.Communication.IPC.Core 4903 4904**Return value** 4905 4906 | Type | Description | 4907 | -------- | -------------- | 4908 | number[] | FloatArray read.| 4909 4910**Example** 4911 4912 ```ts 4913 import hilog from '@ohos.hilog'; 4914 4915 let data = rpc.MessageParcel.create(); 4916 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4917 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 4918 let array = data.readFloatArray(); 4919 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 4920 ``` 4921 4922### writeDoubleArray 4923 4924writeDoubleArray(doubleArray: number[]): boolean 4925 4926Writes a DoubleArray to this **MessageParcel** object. 4927 4928**System capability**: SystemCapability.Communication.IPC.Core 4929 4930**Parameters** 4931 4932 | Name | Type | Mandatory| Description | 4933 | ----------- | -------- | ---- | ------------------------ | 4934 | doubleArray | number[] | Yes | DoubleArray to write.| 4935 4936**Return value** 4937 4938 | Type | Description | 4939 | ------- | -------------------------------- | 4940 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4941 4942**Example** 4943 4944 ```ts 4945 import hilog from '@ohos.hilog'; 4946 4947 let data = rpc.MessageParcel.create(); 4948 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4949 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 4950 ``` 4951 4952### readDoubleArray 4953 4954readDoubleArray(dataIn: number[]): void 4955 4956Reads a DoubleArray from this **MessageParcel** object. 4957 4958**System capability**: SystemCapability.Communication.IPC.Core 4959 4960**Parameters** 4961 4962 | Name| Type | Mandatory| Description | 4963 | ------ | -------- | ---- | ------------------------ | 4964 | dataIn | number[] | Yes | DoubleArray to read.| 4965 4966**Example** 4967 4968 ```ts 4969 import hilog from '@ohos.hilog'; 4970 4971 let data = rpc.MessageParcel.create(); 4972 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4973 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 4974 let array: Array<number> = new Array(3); 4975 data.readDoubleArray(array); 4976 ``` 4977 4978### readDoubleArray 4979 4980readDoubleArray(): number[] 4981 4982Reads the DoubleArray from this **MessageParcel** object. 4983 4984**System capability**: SystemCapability.Communication.IPC.Core 4985 4986**Return value** 4987 4988 | Type | Description | 4989 | -------- | -------------------- | 4990 | number[] | DoubleArray read.| 4991 4992**Example** 4993 4994 ```ts 4995 import hilog from '@ohos.hilog'; 4996 4997 let data = rpc.MessageParcel.create(); 4998 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4999 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5000 let array = data.readDoubleArray(); 5001 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5002 ``` 5003 5004### writeBooleanArray 5005 5006writeBooleanArray(booleanArray: boolean[]): boolean 5007 5008Writes a Boolean array to this **MessageParcel** object. 5009 5010**System capability**: SystemCapability.Communication.IPC.Core 5011 5012**Parameters** 5013 5014 | Name | Type | Mandatory| Description | 5015 | ------------ | --------- | ---- | ------------------ | 5016 | booleanArray | boolean[] | Yes | Boolean array to write.| 5017 5018**Return value** 5019 5020 | Type | Description | 5021 | ------- | -------------------------------- | 5022 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5023 5024**Example** 5025 5026 ```ts 5027 import hilog from '@ohos.hilog'; 5028 5029 let data = rpc.MessageParcel.create(); 5030 let result = data.writeBooleanArray([false, true, false]); 5031 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5032 ``` 5033 5034### readBooleanArray 5035 5036readBooleanArray(dataIn: boolean[]): void 5037 5038Reads a Boolean array from this **MessageParcel** object. 5039 5040**System capability**: SystemCapability.Communication.IPC.Core 5041 5042**Parameters** 5043 5044 | Name| Type | Mandatory| Description | 5045 | ------ | --------- | ---- | ------------------ | 5046 | dataIn | boolean[] | Yes | Boolean array to read.| 5047 5048**Example** 5049 5050 ```ts 5051 import hilog from '@ohos.hilog'; 5052 5053 let data = rpc.MessageParcel.create(); 5054 let result = data.writeBooleanArray([false, true, false]); 5055 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5056 let array: Array<boolean> = new Array(3); 5057 data.readBooleanArray(array); 5058 ``` 5059 5060### readBooleanArray 5061 5062readBooleanArray(): boolean[] 5063 5064Reads the Boolean array from this **MessageParcel** object. 5065 5066**System capability**: SystemCapability.Communication.IPC.Core 5067 5068**Return value** 5069 5070 | Type | Description | 5071 | --------- | -------------- | 5072 | boolean[] | Boolean array read.| 5073 5074**Example** 5075 5076 ```ts 5077 import hilog from '@ohos.hilog'; 5078 5079 let data = rpc.MessageParcel.create(); 5080 let result = data.writeBooleanArray([false, true, false]); 5081 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5082 let array = data.readBooleanArray(); 5083 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5084 ``` 5085 5086### writeCharArray 5087 5088writeCharArray(charArray: number[]): boolean 5089 5090Writes a character array to this **MessageParcel** object. 5091 5092**System capability**: SystemCapability.Communication.IPC.Core 5093 5094**Parameters** 5095 5096 | Name | Type | Mandatory| Description | 5097 | --------- | -------- | ---- | ---------------------- | 5098 | charArray | number[] | Yes | Character array to write.| 5099 5100**Return value** 5101 5102 | Type | Description | 5103 | ------- | -------------------------------- | 5104 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5105 5106**Example** 5107 5108 ```ts 5109 import hilog from '@ohos.hilog'; 5110 5111 let data = rpc.MessageParcel.create(); 5112 let result = data.writeCharArray([97, 98, 88]); 5113 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5114 ``` 5115 5116### readCharArray 5117 5118readCharArray(dataIn: number[]): void 5119 5120Reads a character array from this **MessageParcel** object. 5121 5122**System capability**: SystemCapability.Communication.IPC.Core 5123 5124**Parameters** 5125 5126 | Name| Type | Mandatory| Description | 5127 | ------ | -------- | ---- | ---------------------- | 5128 | dataIn | number[] | Yes | Character array to read.| 5129 5130**Example** 5131 5132 ```ts 5133 import hilog from '@ohos.hilog'; 5134 5135 let data = rpc.MessageParcel.create(); 5136 let result = data.writeCharArray([97, 98, 99]); 5137 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5138 let array: Array<number> = new Array(3); 5139 data.readCharArray(array); 5140 ``` 5141 5142### readCharArray 5143 5144readCharArray(): number[] 5145 5146Reads the character array from this **MessageParcel** object. 5147 5148**System capability**: SystemCapability.Communication.IPC.Core 5149 5150**Return value** 5151 5152 | Type | Description | 5153 | -------- | ------------------ | 5154 | number[] | Character array read.| 5155 5156**Example** 5157 5158 ```ts 5159 import hilog from '@ohos.hilog'; 5160 5161 let data = rpc.MessageParcel.create(); 5162 let result = data.writeCharArray([97, 98, 99]); 5163 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5164 let array = data.readCharArray(); 5165 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5166 ``` 5167 5168### writeStringArray 5169 5170writeStringArray(stringArray: string[]): boolean 5171 5172Writes a string array to this **MessageParcel** object. 5173 5174**System capability**: SystemCapability.Communication.IPC.Core 5175 5176**Parameters** 5177 5178 | Name | Type | Mandatory| Description | 5179 | ----------- | -------- | ---- | ---------------- | 5180 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 5181 5182**Return value** 5183 5184 | Type | Description| 5185 | ------- | -------------------------------- | 5186 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5187 5188**Example** 5189 5190 ```ts 5191 import hilog from '@ohos.hilog'; 5192 5193 let data = rpc.MessageParcel.create(); 5194 let result = data.writeStringArray(["abc", "def"]); 5195 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5196 ``` 5197 5198### readStringArray 5199 5200readStringArray(dataIn: string[]): void 5201 5202Reads a string array from this **MessageParcel** object. 5203 5204**System capability**: SystemCapability.Communication.IPC.Core 5205 5206**Parameters** 5207 5208 | Name| Type | Mandatory| Description | 5209 | ------ | -------- | ---- | -------------------- | 5210 | dataIn | string[] | Yes | String array to read.| 5211 5212**Example** 5213 5214 ```ts 5215 import hilog from '@ohos.hilog'; 5216 5217 let data = rpc.MessageParcel.create(); 5218 let result = data.writeStringArray(["abc", "def"]); 5219 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5220 let array: Array<string> = new Array(2); 5221 data.readStringArray(array); 5222 ``` 5223 5224### readStringArray 5225 5226readStringArray(): string[] 5227 5228Reads the string array from this **MessageParcel** object. 5229 5230**System capability**: SystemCapability.Communication.IPC.Core 5231 5232**Return value** 5233 5234 | Type | Description | 5235 | -------- | ---------------- | 5236 | string[] | String array read.| 5237 5238**Example** 5239 5240 ```ts 5241 import hilog from '@ohos.hilog'; 5242 5243 let data = rpc.MessageParcel.create(); 5244 let result = data.writeStringArray(["abc", "def"]); 5245 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5246 let array = data.readStringArray(); 5247 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5248 ``` 5249 5250### writeNoException<sup>8+</sup> 5251 5252writeNoException(): void 5253 5254Writes information to this **MessageParcel** object indicating that no exception occurred. 5255 5256**System capability**: SystemCapability.Communication.IPC.Core 5257 5258**Example** 5259 5260 ```ts 5261 import hilog from '@ohos.hilog'; 5262 5263 class MyDeathRecipient implements rpc.DeathRecipient { 5264 onRemoteDied() { 5265 hilog.info(0x0000, 'testTag', 'server died'); 5266 } 5267 } 5268 class TestRemoteObject extends rpc.RemoteObject { 5269 constructor(descriptor: string) { 5270 super(descriptor); 5271 } 5272 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5273 return true; 5274 } 5275 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5276 return true; 5277 } 5278 isObjectDead(): boolean { 5279 return false; 5280 } 5281 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5282 if (code === 1) { 5283 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5284 reply.writeNoException(); 5285 return true; 5286 } else { 5287 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5288 return false; 5289 } 5290 } 5291 } 5292 ``` 5293 5294### readException<sup>8+</sup> 5295 5296readException(): void 5297 5298Reads the exception information from this **MessageParcel** object. 5299 5300**System capability**: SystemCapability.Communication.IPC.Core 5301 5302**Example** 5303 5304 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 5305 5306 ```ts 5307 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 5308 // import FA from "@ohos.ability.featureAbility"; 5309 import Want from '@ohos.app.ability.Want'; 5310 import common from '@ohos.app.ability.common'; 5311 import hilog from '@ohos.hilog'; 5312 5313 let proxy: rpc.IRemoteObject | undefined; 5314 let connect: common.ConnectOptions = { 5315 onConnect: (elementName, remoteProxy) => { 5316 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5317 proxy = remoteProxy; 5318 }, 5319 onDisconnect: (elementName) => { 5320 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5321 }, 5322 onFailed: () => { 5323 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5324 } 5325 }; 5326 let want: Want = { 5327 bundleName: "com.ohos.server", 5328 abilityName: "com.ohos.server.EntryAbility", 5329 }; 5330 5331 // Use this method to connect to the ability for the FA model. 5332 // FA.connectAbility(want,connect); 5333 5334 this.context.connectServiceExtensionAbility(want, connect); 5335 ``` 5336 5337 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 5338 5339 ```ts 5340 import hilog from '@ohos.hilog'; 5341 5342 let option = new rpc.MessageOption(); 5343 let data = rpc.MessageParcel.create(); 5344 let reply = rpc.MessageParcel.create(); 5345 data.writeNoException(); 5346 data.writeString('hello'); 5347 if (proxy != undefined) { 5348 let a = proxy.sendRequest(1, data, reply, option) as Object; 5349 let b = a as Promise<rpc.SendRequestResult>; 5350 b.then((result: rpc.SendRequestResult) => { 5351 if (result.errCode === 0) { 5352 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5353 result.reply.readException(); 5354 let msg = result.reply.readString(); 5355 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5356 } else { 5357 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5358 } 5359 }).catch((e: Error) => { 5360 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5361 }).finally (() => { 5362 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5363 data.reclaim(); 5364 reply.reclaim(); 5365 }); 5366 } 5367 ``` 5368 5369### writeSequenceableArray 5370 5371writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5372 5373Writes a sequenceable array to this **MessageParcel** object. 5374 5375**System capability**: SystemCapability.Communication.IPC.Core 5376 5377**Parameters** 5378 5379| Name | Type | Mandatory| Description | 5380| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5381| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | Sequenceable array to write.| 5382 5383**Return value** 5384 5385 | Type | Description | 5386 | ------- | -------------------------------- | 5387 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5388 5389**Example** 5390 5391 ```ts 5392 import hilog from '@ohos.hilog'; 5393 5394 class MySequenceable implements rpc.Sequenceable { 5395 num: number = 0; 5396 str: string = ''; 5397 constructor(num: number, str: string) { 5398 this.num = num; 5399 this.str = str; 5400 } 5401 marshalling(messageParcel: rpc.MessageParcel): boolean { 5402 messageParcel.writeInt(this.num); 5403 messageParcel.writeString(this.str); 5404 return true; 5405 } 5406 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5407 this.num = messageParcel.readInt(); 5408 this.str = messageParcel.readString(); 5409 return true; 5410 } 5411 } 5412 let sequenceable = new MySequenceable(1, "aaa"); 5413 let sequenceable2 = new MySequenceable(2, "bbb"); 5414 let sequenceable3 = new MySequenceable(3, "ccc"); 5415 let a = [sequenceable, sequenceable2, sequenceable3]; 5416 let data = rpc.MessageParcel.create(); 5417 let result = data.writeSequenceableArray(a); 5418 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5419 ``` 5420 5421### readSequenceableArray<sup>8+</sup> 5422 5423readSequenceableArray(sequenceableArray: Sequenceable[]): void 5424 5425Reads a sequenceable array from this **MessageParcel** object. 5426 5427**System capability**: SystemCapability.Communication.IPC.Core 5428 5429**Parameters** 5430 5431| Name | Type | Mandatory| Description | 5432| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5433| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | Sequenceable array to read.| 5434 5435**Example** 5436 5437 ```ts 5438 import hilog from '@ohos.hilog'; 5439 5440 class MySequenceable implements rpc.Sequenceable { 5441 num: number = 0; 5442 str: string = ''; 5443 constructor(num: number, str: string) { 5444 this.num = num; 5445 this.str = str; 5446 } 5447 marshalling(messageParcel: rpc.MessageParcel): boolean { 5448 messageParcel.writeInt(this.num); 5449 messageParcel.writeString(this.str); 5450 return true; 5451 } 5452 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5453 this.num = messageParcel.readInt(); 5454 this.str = messageParcel.readString(); 5455 return true; 5456 } 5457 } 5458 let sequenceable = new MySequenceable(1, "aaa"); 5459 let sequenceable2 = new MySequenceable(2, "bbb"); 5460 let sequenceable3 = new MySequenceable(3, "ccc"); 5461 let a = [sequenceable, sequenceable2, sequenceable3]; 5462 let data = rpc.MessageParcel.create(); 5463 let result = data.writeSequenceableArray(a); 5464 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5465 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5466 data.readSequenceableArray(b); 5467 ``` 5468 5469### writeRemoteObjectArray<sup>8+</sup> 5470 5471writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5472 5473Writes an array of **IRemoteObject** objects to this **MessageParcel** object. 5474 5475**System capability**: SystemCapability.Communication.IPC.Core 5476 5477**Parameters** 5478 5479 | Name | Type | Mandatory| Description | 5480 | ----------- | --------------- | ---- | ----- | 5481 | objectArray | [IRemoteObject](#iremoteobject)[] | Yes | Array of **IRemoteObject** objects to write.| 5482 5483**Return value** 5484 5485 | Type | Description | 5486 | ------- | -------------------------------- | 5487 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5488 5489**Example** 5490 5491 ```ts 5492 import hilog from '@ohos.hilog'; 5493 5494 class MyDeathRecipient implements rpc.DeathRecipient { 5495 onRemoteDied() { 5496 hilog.info(0x0000, 'testTag', 'server died'); 5497 } 5498 } 5499 class TestRemoteObject extends rpc.RemoteObject { 5500 constructor(descriptor: string) { 5501 super(descriptor); 5502 this.attachLocalInterface(this, descriptor); 5503 } 5504 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5505 return true; 5506 } 5507 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5508 return true; 5509 } 5510 isObjectDead(): boolean { 5511 return false; 5512 } 5513 asObject(): rpc.IRemoteObject { 5514 return this; 5515 } 5516 } 5517 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5518 let data = rpc.MessageParcel.create(); 5519 let result = data.writeRemoteObjectArray(a); 5520 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5521 ``` 5522 5523### readRemoteObjectArray<sup>8+</sup> 5524 5525readRemoteObjectArray(objects: IRemoteObject[]): void 5526 5527Reads an **IRemoteObject** array from this **MessageParcel** object. 5528 5529**System capability**: SystemCapability.Communication.IPC.Core 5530 5531**Parameters** 5532 5533 | Name | Type | Mandatory| Description | 5534 | ------- | --------------- | ---- | --------- | 5535 | objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 5536 5537**Example** 5538 5539 ```ts 5540 import hilog from '@ohos.hilog'; 5541 5542 class MyDeathRecipient implements rpc.DeathRecipient { 5543 onRemoteDied() { 5544 hilog.info(0x0000, 'testTag', 'server died'); 5545 } 5546 } 5547 class TestRemoteObject extends rpc.RemoteObject { 5548 constructor(descriptor: string) { 5549 super(descriptor); 5550 this.attachLocalInterface(this, descriptor); 5551 } 5552 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5553 return true; 5554 } 5555 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5556 return true; 5557 } 5558 isObjectDead(): boolean { 5559 return false; 5560 } 5561 asObject(): rpc.IRemoteObject { 5562 return this; 5563 } 5564 } 5565 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5566 let data = rpc.MessageParcel.create(); 5567 data.writeRemoteObjectArray(a); 5568 let b: Array<rpc.IRemoteObject> = new Array(3); 5569 data.readRemoteObjectArray(b); 5570 ``` 5571 5572### readRemoteObjectArray<sup>8+</sup> 5573 5574readRemoteObjectArray(): IRemoteObject[] 5575 5576Reads the **IRemoteObject** array from this **MessageParcel** object. 5577 5578**System capability**: SystemCapability.Communication.IPC.Core 5579 5580**Return value** 5581 5582 | Type | Description | 5583 | --------------- | --------------------------- | 5584 | [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array obtained.| 5585 5586**Example** 5587 5588 ```ts 5589 import hilog from '@ohos.hilog'; 5590 5591 class MyDeathRecipient implements rpc.DeathRecipient { 5592 onRemoteDied() { 5593 hilog.info(0x0000, 'testTag', 'server died'); 5594 } 5595 } 5596 class TestRemoteObject extends rpc.RemoteObject { 5597 constructor(descriptor: string) { 5598 super(descriptor); 5599 this.attachLocalInterface(this, descriptor); 5600 } 5601 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5602 return true; 5603 } 5604 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5605 return true; 5606 } 5607 isObjectDead(): boolean { 5608 return false; 5609 } 5610 asObject(): rpc.IRemoteObject { 5611 return this; 5612 } 5613 } 5614 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5615 let data = rpc.MessageParcel.create(); 5616 let result = data.writeRemoteObjectArray(a); 5617 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5618 let b = data.readRemoteObjectArray(); 5619 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5620 ``` 5621 5622### closeFileDescriptor<sup>8+</sup> 5623 5624static closeFileDescriptor(fd: number): void 5625 5626Closes a file descriptor. This API is a static method. 5627 5628**System capability**: SystemCapability.Communication.IPC.Core 5629 5630**Parameters** 5631 5632 | Name| Type | Mandatory| Description | 5633 | ------ | ------ | ---- | -------------------- | 5634 | fd | number | Yes | File descriptor to close.| 5635 5636**Example** 5637 5638 ```ts 5639 import fs from '@ohos.file.fs'; 5640 5641 let filePath = "path/to/file"; 5642 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5643 rpc.MessageParcel.closeFileDescriptor(file.fd); 5644 ``` 5645 5646### dupFileDescriptor<sup>8+</sup> 5647 5648static dupFileDescriptor(fd: number) :number 5649 5650Duplicates a file descriptor. This API is a static method. 5651 5652**System capability**: SystemCapability.Communication.IPC.Core 5653 5654**Parameters** 5655 5656 | Name| Type | Mandatory| Description | 5657 | ------ | ------ | ---- | ------------------------ | 5658 | fd | number | Yes | File descriptor to duplicate.| 5659 5660**Return value** 5661 5662 | Type | Description | 5663 | ------ | -------------------- | 5664 | number | New file descriptor.| 5665 5666**Example** 5667 5668 ```ts 5669 import fs from '@ohos.file.fs'; 5670 5671 let filePath = "path/to/file"; 5672 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5673 rpc.MessageParcel.dupFileDescriptor(file.fd); 5674 ``` 5675 5676### containFileDescriptors<sup>8+</sup> 5677 5678containFileDescriptors(): boolean 5679 5680Checks whether this **MessageParcel** object contains file descriptors. 5681 5682**System capability**: SystemCapability.Communication.IPC.Core 5683 5684**Return value** 5685 5686 | Type | Description | 5687 | ------- | --------------------------------------------- | 5688 | boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.| 5689 5690**Example** 5691 5692 ```ts 5693 import fs from '@ohos.file.fs'; 5694 import hilog from '@ohos.hilog'; 5695 5696 let parcel = new rpc.MessageParcel(); 5697 let filePath = "path/to/file"; 5698 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5699 let writeResult = parcel.writeFileDescriptor(file.fd); 5700 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 5701 let containFD = parcel.containFileDescriptors(); 5702 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 5703 ``` 5704 5705### writeFileDescriptor<sup>8+</sup> 5706 5707writeFileDescriptor(fd: number): boolean 5708 5709Writes a file descriptor to this **MessageParcel** object. 5710 5711**System capability**: SystemCapability.Communication.IPC.Core 5712 5713**Parameters** 5714 5715 | Name| Type | Mandatory| Description | 5716 | ------ | ------ | ---- | ------------ | 5717 | fd | number | Yes | File descriptor to write.| 5718 5719**Return value** 5720 5721 | Type | Description | 5722 | ------- | -------------------------------- | 5723 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5724 5725**Example** 5726 5727 ```ts 5728 import fs from '@ohos.file.fs'; 5729 import hilog from '@ohos.hilog'; 5730 5731 let parcel = new rpc.MessageParcel(); 5732 let filePath = "path/to/file"; 5733 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5734 let writeResult = parcel.writeFileDescriptor(file.fd); 5735 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 5736 ``` 5737 5738### readFileDescriptor<sup>8+</sup> 5739 5740readFileDescriptor(): number 5741 5742Reads the file descriptor from this **MessageParcel** object. 5743 5744**System capability**: SystemCapability.Communication.IPC.Core 5745 5746**Return value** 5747 5748 | Type | Description | 5749 | ------ | ---------------- | 5750 | number | File descriptor read.| 5751 5752**Example** 5753 5754 ```ts 5755 import fs from '@ohos.file.fs'; 5756 import hilog from '@ohos.hilog'; 5757 5758 let parcel = new rpc.MessageParcel(); 5759 let filePath = "path/to/file"; 5760 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5761 parcel.writeFileDescriptor(file.fd); 5762 let readFD = parcel.readFileDescriptor(); 5763 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 5764 ``` 5765 5766### writeAshmem<sup>8+</sup> 5767 5768writeAshmem(ashmem: Ashmem): boolean 5769 5770Writes an anonymous shared object to this **MessageParcel** object. 5771 5772**System capability**: SystemCapability.Communication.IPC.Core 5773 5774**Parameters** 5775 5776| Name| Type | Mandatory| Description | 5777| ------ | ------ | ---- | ----------------------------------- | 5778| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 5779 5780**Return value** 5781 5782 | Type | Description | 5783 | ------- | -------------------------------- | 5784 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5785 5786**Example** 5787 5788 ```ts 5789 import hilog from '@ohos.hilog'; 5790 5791 let parcel = new rpc.MessageParcel(); 5792 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 5793 let isWriteSuccess = parcel.writeAshmem(ashmem); 5794 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 5795 ``` 5796 5797### readAshmem<sup>8+</sup> 5798 5799readAshmem(): Ashmem 5800 5801Reads the anonymous shared object from this **MessageParcel** object. 5802 5803**System capability**: SystemCapability.Communication.IPC.Core 5804 5805**Return value** 5806 5807| Type | Description | 5808| ------ | ------------------ | 5809| [Ashmem](#ashmem8) | Anonymous share object obtained.| 5810 5811**Example** 5812 5813 ```ts 5814 import hilog from '@ohos.hilog'; 5815 5816 let parcel = new rpc.MessageParcel(); 5817 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 5818 let isWriteSuccess = parcel.writeAshmem(ashmem); 5819 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 5820 let readAshmem = parcel.readAshmem(); 5821 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 5822 ``` 5823 5824### getRawDataCapacity<sup>8+</sup> 5825 5826getRawDataCapacity(): number 5827 5828Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 5829 5830**System capability**: SystemCapability.Communication.IPC.Core 5831 5832**Return value** 5833 5834 | Type | Description | 5835 | ------ | ---------------------------------------------------------- | 5836 | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageParcel** object.| 5837 5838**Example** 5839 5840 ```ts 5841 import hilog from '@ohos.hilog'; 5842 5843 let parcel = new rpc.MessageParcel(); 5844 let result = parcel.getRawDataCapacity(); 5845 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 5846 ``` 5847 5848### writeRawData<sup>8+</sup> 5849 5850writeRawData(rawData: number[], size: number): boolean 5851 5852Writes raw data to this **MessageParcel** object. 5853 5854**System capability**: SystemCapability.Communication.IPC.Core 5855 5856**Parameters** 5857 5858 | Name | Type | Mandatory| Description | 5859 | ------- | -------- | ---- | ---------------------------------- | 5860 | rawData | number[] | Yes | Raw data to write. | 5861 | size | number | Yes | Size of the raw data, in bytes.| 5862 5863**Return value** 5864 5865 | Type | Description | 5866 | ------- | -------------------------------- | 5867 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5868 5869**Example** 5870 5871 ```ts 5872 import hilog from '@ohos.hilog'; 5873 5874 let parcel = new rpc.MessageParcel(); 5875 let arr = [1, 2, 3, 4, 5]; 5876 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 5877 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 5878 ``` 5879 5880### readRawData<sup>8+</sup> 5881 5882readRawData(size: number): number[] 5883 5884Reads raw data from this **MessageParcel** object. 5885 5886**System capability**: SystemCapability.Communication.IPC.Core 5887 5888**Parameters** 5889 5890 | Name| Type | Mandatory| Description | 5891 | ------ | ------ | ---- | ------------------------ | 5892 | size | number | Yes | Size of the raw data to read.| 5893 5894**Return value** 5895 5896 | Type | Description | 5897 | -------- | ------------------------------ | 5898 | number[] | Raw data obtained, in bytes.| 5899 5900**Example** 5901 5902 ```ts 5903 import hilog from '@ohos.hilog'; 5904 5905 let parcel = new rpc.MessageParcel(); 5906 let arr = [1, 2, 3, 4, 5]; 5907 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 5908 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 5909 let result = parcel.readRawData(5); 5910 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 5911 ``` 5912 5913## Parcelable<sup>9+</sup> 5914 5915Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC. 5916 5917### marshalling 5918 5919marshalling(dataOut: MessageSequence): boolean 5920 5921Marshals this **Parcelable** object into a **MessageSequence** object. 5922 5923**System capability**: SystemCapability.Communication.IPC.Core 5924 5925**Parameters** 5926 5927| Name | Type | Mandatory| Description | 5928| ------- | --------------- | ---- | ------------------------------------------- | 5929| dataOut |[MessageSequence](#messagesequence9)| Yes | **MessageSequence** object to which the **Parcelable** object is to be marshaled.| 5930 5931**Return value** 5932 5933 | Type | Description | 5934 | ------- | -------------------------------- | 5935 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5936 5937**Example** 5938 5939 ```ts 5940 import hilog from '@ohos.hilog'; 5941 5942 class MyParcelable implements rpc.Parcelable { 5943 num: number = 0; 5944 str: string = ''; 5945 constructor(num: number, str: string) { 5946 this.num = num; 5947 this.str = str; 5948 } 5949 marshalling(messageSequence: rpc.MessageSequence): boolean { 5950 messageSequence.writeInt(this.num); 5951 messageSequence.writeString(this.str); 5952 return true; 5953 } 5954 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 5955 this.num = messageSequence.readInt(); 5956 this.str = messageSequence.readString(); 5957 return true; 5958 } 5959 } 5960 let parcelable = new MyParcelable(1, "aaa"); 5961 let data = rpc.MessageSequence.create(); 5962 let result = data.writeParcelable(parcelable); 5963 hilog.info(0x0000, 'testTag', 'RpcClient: writeParcelable is ' + result); 5964 let ret = new MyParcelable(0, ""); 5965 let result2 = data.readParcelable(ret); 5966 hilog.info(0x0000, 'testTag', 'RpcClient: readParcelable is ' + result2); 5967 ``` 5968 5969### unmarshalling 5970 5971unmarshalling(dataIn: MessageSequence): boolean 5972 5973Unmarshals this **Parcelable** object from a **MessageSequence** object. 5974 5975**System capability**: SystemCapability.Communication.IPC.Core 5976 5977**Parameters** 5978 5979| Name| Type | Mandatory| Description | 5980| ------ | --------------- | ---- | ----------------------------------------------- | 5981| dataIn | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 5982 5983**Return value** 5984 5985 | Type | Description | 5986 | ------- | ---------------------------------------- | 5987 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5988 5989**Example** 5990 5991 ```ts 5992 import hilog from '@ohos.hilog'; 5993 5994 class MyParcelable implements rpc.Parcelable { 5995 num: number = 0; 5996 str: string = ''; 5997 constructor(num: number, str: string) { 5998 this.num = num; 5999 this.str = str; 6000 } 6001 marshalling(messageSequence: rpc.MessageSequence): boolean { 6002 messageSequence.writeInt(this.num); 6003 messageSequence.writeString(this.str); 6004 return true; 6005 } 6006 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6007 this.num = messageSequence.readInt(); 6008 this.str = messageSequence.readString(); 6009 return true; 6010 } 6011 } 6012 let parcelable = new MyParcelable(1, "aaa"); 6013 let data = rpc.MessageSequence.create(); 6014 let result = data.writeParcelable(parcelable); 6015 hilog.info(0x0000, 'testTag', 'RpcClient: writeParcelable is ' + result); 6016 let ret = new MyParcelable(0, ""); 6017 let result2 = data.readParcelable(ret); 6018 hilog.info(0x0000, 'testTag', 'RpcClient: readParcelable is ' + result2); 6019 ``` 6020 6021## Sequenceable<sup>(deprecated)</sup> 6022 6023>This class is no longer maintained since API version 9. You are advised to use the [Parcelable](#parcelable9). 6024 6025Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 6026 6027### marshalling 6028 6029marshalling(dataOut: MessageParcel): boolean 6030 6031Marshals the sequenceable object into a **MessageParcel** object. 6032 6033**System capability**: SystemCapability.Communication.IPC.Core 6034 6035**Parameters** 6036 6037 | Name | Type | Mandatory| Description | 6038 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6039 | dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 6040 6041**Return value** 6042 6043 | Type | Description | 6044 | ------- | -------------------------------- | 6045 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6046 6047**Example** 6048 6049 ```ts 6050 import hilog from '@ohos.hilog'; 6051 6052 class MySequenceable implements rpc.Sequenceable { 6053 num: number = 0; 6054 str: string = ''; 6055 constructor(num: number, str: string) { 6056 this.num = num; 6057 this.str = str; 6058 } 6059 marshalling(messageParcel: rpc.MessageParcel): boolean { 6060 messageParcel.writeInt(this.num); 6061 messageParcel.writeString(this.str); 6062 return true; 6063 } 6064 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6065 this.num = messageParcel.readInt(); 6066 this.str = messageParcel.readString(); 6067 return true; 6068 } 6069 } 6070 let sequenceable = new MySequenceable(1, "aaa"); 6071 let data = rpc.MessageParcel.create(); 6072 let result = data.writeSequenceable(sequenceable); 6073 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6074 let ret = new MySequenceable(0, ""); 6075 let result2 = data.readSequenceable(ret); 6076 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6077 ``` 6078 6079### unmarshalling 6080 6081unmarshalling(dataIn: MessageParcel): boolean 6082 6083Unmarshals this sequenceable object from a **MessageParcel** object. 6084 6085**System capability**: SystemCapability.Communication.IPC.Core 6086 6087**Parameters** 6088 6089 | Name| Type | Mandatory| Description | 6090 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6091 | dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 6092 6093**Return value** 6094 6095 | Type | Description | 6096 | ------- | ---------------------------------------- | 6097 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6098 6099**Example** 6100 6101 ```ts 6102 import hilog from '@ohos.hilog'; 6103 6104 class MySequenceable implements rpc.Sequenceable { 6105 num: number = 0; 6106 str: string = ''; 6107 constructor(num: number, str: string) { 6108 this.num = num; 6109 this.str = str; 6110 } 6111 marshalling(messageParcel: rpc.MessageParcel): boolean { 6112 messageParcel.writeInt(this.num); 6113 messageParcel.writeString(this.str); 6114 return true; 6115 } 6116 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6117 this.num = messageParcel.readInt(); 6118 this.str = messageParcel.readString(); 6119 return true; 6120 } 6121 } 6122 let sequenceable = new MySequenceable(1, "aaa"); 6123 let data = rpc.MessageParcel.create(); 6124 let result = data.writeSequenceable(sequenceable); 6125 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6126 let ret = new MySequenceable(0, ""); 6127 let result2 = data.readSequenceable(ret); 6128 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6129 ``` 6130 6131## IRemoteBroker 6132 6133Provides the holder of a remote proxy object. 6134 6135### asObject 6136 6137asObject(): IRemoteObject 6138 6139Obtains a proxy or remote object. This API must be implemented by its derived classes. 6140 6141**System capability**: SystemCapability.Communication.IPC.Core 6142 6143**Return value** 6144 6145 | Type | Description | 6146 | ----- | ----- | 6147 | [IRemoteObject](#iremoteobject) | Returns the **RemoteObject** if it is the caller; returns the [IRemoteObject](#iremoteobject), the holder of this **RemoteProxy** object, if the caller is a [RemoteProxy](#remoteproxy) object.| 6148 6149**Example** 6150 6151 ```ts 6152 class TestAbility extends rpc.RemoteObject { 6153 asObject() { 6154 return this; 6155 } 6156 } 6157 let remoteObject = new TestAbility("testObject").asObject(); 6158 ``` 6159 6160**Example** 6161 6162 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6163 6164 ```ts 6165 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6166 // import FA from "@ohos.ability.featureAbility"; 6167 6168 import Want from '@ohos.app.ability.Want'; 6169 import common from '@ohos.app.ability.common'; 6170 import hilog from '@ohos.hilog'; 6171 6172 let proxy: rpc.IRemoteObject | undefined; 6173 let connect: common.ConnectOptions = { 6174 onConnect: (elementName, remoteProxy) => { 6175 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6176 proxy = remoteProxy; 6177 }, 6178 onDisconnect: (elementName) => { 6179 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6180 }, 6181 onFailed: () => { 6182 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6183 } 6184 }; 6185 let want: Want = { 6186 bundleName: "com.ohos.server", 6187 abilityName: "com.ohos.server.EntryAbility", 6188 }; 6189 6190 // Use this method to connect to the ability for the FA model. 6191 // FA.connectAbility(want,connect); 6192 6193 this.context.connectServiceExtensionAbility(want, connect); 6194 ``` 6195 6196 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **asObject()** of the proxy object is called to obtain the proxy or remote object. 6197 6198 ```ts 6199 class TestProxy { 6200 remote: rpc.IRemoteObject; 6201 constructor(remote: rpc.IRemoteObject) { 6202 this.remote = remote; 6203 } 6204 asObject() { 6205 return this.remote; 6206 } 6207 } 6208 if (proxy != undefined) { 6209 let iRemoteObject = new TestProxy(proxy).asObject(); 6210 } 6211 ``` 6212 6213## DeathRecipient 6214 6215Subscribes to death notifications of a remote object. When the remote object is dead, the local end will receive a notification and **[onRemoteDied](#onremotedied)** will be called. A remote object is dead when the process holding the object is terminated or the device of the remote object is shut down or restarted. If the local and remote objects belong to different devices, the remote object is dead when the device holding the remote object is detached from the network. 6216 6217### onRemoteDied 6218 6219onRemoteDied(): void 6220 6221Called to perform subsequent operations when a death notification of the remote object is received. 6222 6223**System capability**: SystemCapability.Communication.IPC.Core 6224 6225**Example** 6226 6227 ```ts 6228 import hilog from '@ohos.hilog'; 6229 6230 class MyDeathRecipient implements rpc.DeathRecipient { 6231 onRemoteDied() { 6232 hilog.info(0x0000, 'testTag', 'server died'); 6233 } 6234 } 6235 ``` 6236 6237## RequestResult<sup>9+</sup> 6238 6239Defines the response to the request. 6240 6241**System capability**: SystemCapability.Communication.IPC.Core 6242 6243| Name | Type | Readable| Writable| Description | 6244| ------- | --------------- | ---- | ---- |-------------------------------------- | 6245| errCode | number | Yes | No | Error code. | 6246| code | number | Yes | No | Message code. | 6247| data | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object sent to the remote process.| 6248| reply | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object returned by the remote process. | 6249 6250## SendRequestResult<sup>8+(deprecated)</sup> 6251 6252>This API is no longer maintained since API version 9. You are advised to use [RequestResult](#requestresult9). 6253 6254Defines the response to the request. 6255 6256**System capability**: SystemCapability.Communication.IPC.Core 6257 6258 | Name | Type | Readable| Writable| Description | 6259 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6260 | errCode | number | Yes | No | Error code. | 6261 | code | number | Yes | No | Message code. | 6262 | data | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object sent to the remote process.| 6263 | reply | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object returned by the remote process. | 6264 6265## IRemoteObject 6266 6267Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 6268 6269### getLocalInterface<sup>9+</sup> 6270 6271getLocalInterface(descriptor: string): IRemoteBroker 6272 6273Obtains the string of the interface descriptor. 6274 6275**System capability**: SystemCapability.Communication.IPC.Core 6276 6277**Parameters** 6278 6279 | Name | Type | Mandatory| Description | 6280 | ---------- | ------ | ---- | -------------------- | 6281 | descriptor | string | Yes | Interface descriptor.| 6282 6283**Return value** 6284 6285| Type | Description | 6286| ------------- | --------------------------------------------- | 6287| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6288 6289### queryLocalInterface<sup>(deprecated)</sup> 6290 6291>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 6292 6293queryLocalInterface(descriptor: string): IRemoteBroker 6294 6295Queries the string of the interface descriptor. 6296 6297**System capability**: SystemCapability.Communication.IPC.Core 6298 6299**Parameters** 6300 6301 | Name | Type | Mandatory| Description | 6302 | ---------- | ------ | ---- | -------------------- | 6303 | descriptor | string | Yes | Interface descriptor.| 6304 6305**Return value** 6306 6307| Type | Description | 6308| ------------- | --------------------------------------------- | 6309| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6310 6311### sendRequest<sup>(deprecated)</sup> 6312 6313>This API is no longer maintained since API version 8. You are advised to use [sendRequest](#sendrequest8deprecated). 6314 6315sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6316 6317Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message does not contain any content. If synchronous mode is set in **options** , a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 6318 6319**System capability**: SystemCapability.Communication.IPC.Core 6320 6321**Parameters** 6322 6323 | Name | Type | Mandatory| Description | 6324 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6325 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6326 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6327 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6328 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6329 6330**Return value** 6331 6332 | Type | Description | 6333 | ------- | -------------------------------- | 6334 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6335 6336### sendMessageRequest<sup>9+</sup> 6337 6338sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6339 6340Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 6341 6342**System capability**: SystemCapability.Communication.IPC.Core 6343 6344**Parameters** 6345 6346 | Name | Type | Mandatory| Description | 6347 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6348 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6349 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6350 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6351 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6352 6353**Return value** 6354 6355 | Type | Description | 6356 | ---------------------------- | ----------------------------------------- | 6357 | Promise<[RequestResult](#requestresult9)> | Promise used to return the **requestResult** object.| 6358 6359### sendRequest<sup>8+(deprecated)</sup> 6360 6361>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6362 6363sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6364 6365Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 6366 6367**System capability**: SystemCapability.Communication.IPC.Core 6368 6369**Parameters** 6370 6371 | Name | Type | Mandatory| Description | 6372 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6373 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6374 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6375 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6376 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6377 6378**Return value** 6379 6380| Type | Description | 6381| ------------------------------------------------------------ | --------------------------------------------- | 6382| Promise<[SendRequestResult](#sendrequestresult8deprecated)> | Promise used to return the **sendRequestResult** object.| 6383 6384### sendMessageRequest<sup>9+</sup> 6385 6386sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6387 6388Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 6389 6390**System capability**: SystemCapability.Communication.IPC.Core 6391 6392**Parameters** 6393 6394 | Name | Type | Mandatory| Description | 6395 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6396 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6397 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6398 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6399 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6400 | callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 6401 6402### sendRequest<sup>8+(deprecated)</sup> 6403 6404>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9-1). 6405 6406sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6407 6408Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 6409 6410**System capability**: SystemCapability.Communication.IPC.Core 6411 6412**Parameters** 6413 6414| Name | Type | Mandatory| Description | 6415| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6416| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6417| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6418| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6419| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6420| callback | AsyncCallback<[SendRequestResult](#sendrequestresult8deprecated)> | Yes | Callback for receiving the sending result. | 6421 6422### registerDeathRecipient<sup>9+</sup> 6423 6424registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6425 6426Registers a callback for receiving death notifications of the remote object. The callback will be called if the remote object process matching the **RemoteProxy** object is killed. 6427 6428**System capability**: SystemCapability.Communication.IPC.Core 6429 6430**Parameters** 6431 6432 | Name | Type | Mandatory| Description | 6433 | --------- | --------------------------------- | ---- | -------------- | 6434| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6435 | flags | number | Yes | Flag of the death notification.| 6436 6437**Error codes** 6438 6439For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6440 6441 | ID| Error Message| 6442 | -------- | -------- | 6443 | 1900008 | proxy or remote object is invalid | 6444 6445### addDeathrecipient<sup>(deprecated)</sup> 6446 6447>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 6448 6449addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6450 6451Adds a callback for receiving death notifications of the remote object. This method is called if the remote object process matching the **RemoteProxy** object is killed. 6452 6453**System capability**: SystemCapability.Communication.IPC.Core 6454 6455**Parameters** 6456 6457 | Name | Type | Mandatory| Description | 6458 | --------- | --------------------------------- | ---- | -------------- | 6459 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add.| 6460 | flags | number | Yes | Flag of the death notification.| 6461 6462**Return value** 6463 6464 | Type | Description | 6465 | ------- | ---------------------------------------- | 6466 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6467 6468### unregisterDeathRecipient<sup>9+</sup> 6469 6470unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6471 6472Unregisters the callback used to receive death notifications of the remote object. 6473 6474**System capability**: SystemCapability.Communication.IPC.Core 6475 6476**Parameters** 6477 6478 | Name | Type | Mandatory| Description | 6479 | --------- | --------------------------------- | ---- | -------------- | 6480 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6481 | flags | number | Yes | Flag of the death notification.| 6482 6483**Error codes** 6484 6485For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6486 6487 | ID| Error Message| 6488 | -------- | -------- | 6489 | 1900008 | proxy or remote object is invalid | 6490 6491### removeDeathRecipient<sup>(deprecated)</sup> 6492 6493>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 6494 6495removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6496 6497Removes the callback used to receive death notifications of the remote object. 6498 6499**System capability**: SystemCapability.Communication.IPC.Core 6500 6501**Parameters** 6502 6503 | Name | Type | Mandatory| Description | 6504 | --------- | --------------------------------- | ---- | -------------- | 6505| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove.| 6506 | flags | number | Yes | Flag of the death notification.| 6507 6508**Return value** 6509 6510 | Type | Description | 6511 | ------- | -----------------------------------------| 6512 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6513 6514### getDescriptor<sup>9+</sup> 6515 6516getDescriptor(): string 6517 6518Obtains the interface descriptor (which is a string) of this object. 6519 6520**System capability**: SystemCapability.Communication.IPC.Core 6521 6522**Return value** 6523 6524 | Type | Description | 6525 | ------ | ---------------- | 6526 | string | Interface descriptor obtained.| 6527 6528**Error codes** 6529 6530For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6531 6532 | ID| Error Message| 6533 | -------- | -------- | 6534 | 1900008 | proxy or remote object is invalid | 6535 6536### getInterfaceDescriptor<sup>(deprecated)</sup> 6537 6538>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 6539 6540getInterfaceDescriptor(): string 6541 6542Obtains the interface descriptor (which is a string) of this object. 6543 6544**System capability**: SystemCapability.Communication.IPC.Core 6545 6546**Return value** 6547 6548 | Type | Description | 6549 | ------ | ---------------- | 6550 | string | Interface descriptor obtained.| 6551 6552### isObjectDead 6553 6554isObjectDead(): boolean 6555 6556Checks whether this object is dead. 6557 6558**System capability**: SystemCapability.Communication.IPC.Core 6559 6560**Return value** 6561 6562 | Type | Description | 6563 | ------- | ---------------------------------- | 6564 | boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6565 6566## RemoteProxy 6567 6568Provides APIs to implement **IRemoteObject**. 6569 6570**System capability**: SystemCapability.Communication.IPC.Core 6571 6572| Name | Value | Description | 6573| --------------------- | ----------------------- | --------------------------------- | 6574| PING_TRANSACTION | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.| 6575| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder. | 6576| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface token. | 6577| MIN_TRANSACTION_ID | 1 (0x00000001) | Minimum valid instruction code. | 6578| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | Maximum valid instruction code. | 6579 6580### sendRequest<sup>(deprecated)</sup> 6581 6582>This API is no longer maintained since API version 8. You are advised to use [sendRequest](#sendrequest8deprecated-2). 6583 6584sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6585 6586Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 6587 6588**System capability**: SystemCapability.Communication.IPC.Core 6589 6590**Parameters** 6591 6592 | Name | Type | Mandatory| Description | 6593 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6594 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6595 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6596 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6597 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6598 6599**Return value** 6600 6601 | Type | Description | 6602 | ------- | ---------------------------------| 6603 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6604 6605**Example** 6606 6607 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6608 6609 ```ts 6610 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6611 // import FA from "@ohos.ability.featureAbility"; 6612 6613 import Want from '@ohos.app.ability.Want'; 6614 import common from '@ohos.app.ability.common'; 6615 import hilog from '@ohos.hilog'; 6616 6617 let proxy: rpc.IRemoteObject | undefined; 6618 let connect: common.ConnectOptions = { 6619 onConnect: (elementName, remoteProxy) => { 6620 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6621 proxy = remoteProxy; 6622 }, 6623 onDisconnect: (elementName) => { 6624 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6625 }, 6626 onFailed: () => { 6627 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6628 } 6629 }; 6630 let want: Want = { 6631 bundleName: "com.ohos.server", 6632 abilityName: "com.ohos.server.EntryAbility", 6633 }; 6634 6635 // Use this method to connect to the ability for the FA model. 6636 // FA.connectAbility(want,connect); 6637 6638 this.context.connectServiceExtensionAbility(want, connect); 6639 ``` 6640 6641 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6642 6643 ```ts 6644 import hilog from '@ohos.hilog'; 6645 6646 let option = new rpc.MessageOption(); 6647 let data = rpc.MessageParcel.create(); 6648 let reply = rpc.MessageParcel.create(); 6649 data.writeInt(1); 6650 data.writeString("hello"); 6651 if (proxy != undefined) { 6652 let ret: boolean = proxy.sendRequest(1, data, reply, option); 6653 if (ret) { 6654 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 6655 let msg = reply.readString(); 6656 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6657 } else { 6658 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 6659 } 6660 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 6661 data.reclaim(); 6662 reply.reclaim(); 6663 } 6664 ``` 6665 6666### sendMessageRequest<sup>9+</sup> 6667 6668sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6669 6670Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 6671 6672**System capability**: SystemCapability.Communication.IPC.Core 6673 6674**Parameters** 6675 6676 | Name | Type | Mandatory| Description | 6677 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6678 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6679 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6680 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6681 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6682 6683**Return value** 6684 6685 | Type | Description | 6686 | ---------------------------- | ----------------------------------------- | 6687 | Promise<[RequestResult](#requestresult9)> | Promise used to return the **requestResult** object.| 6688 6689**Example** 6690 6691 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6692 6693 ```ts 6694 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6695 // import FA from "@ohos.ability.featureAbility"; 6696 import Want from '@ohos.app.ability.Want'; 6697 import common from '@ohos.app.ability.common'; 6698 import hilog from '@ohos.hilog'; 6699 6700 let proxy: rpc.IRemoteObject | undefined; 6701 let connect: common.ConnectOptions = { 6702 onConnect: (elementName, remoteProxy) => { 6703 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6704 proxy = remoteProxy; 6705 }, 6706 onDisconnect: (elementName) => { 6707 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6708 }, 6709 onFailed: () => { 6710 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6711 } 6712 }; 6713 let want: Want = { 6714 bundleName: "com.ohos.server", 6715 abilityName: "com.ohos.server.EntryAbility", 6716 }; 6717 6718 // Use this method to connect to the ability for the FA model. 6719 // FA.connectAbility(want,connect); 6720 6721 this.context.connectServiceExtensionAbility(want, connect); 6722 ``` 6723 6724 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6725 6726 ```ts 6727 import hilog from '@ohos.hilog'; 6728 6729 let option = new rpc.MessageOption(); 6730 let data = rpc.MessageSequence.create(); 6731 let reply = rpc.MessageSequence.create(); 6732 data.writeInt(1); 6733 data.writeString("hello"); 6734 if (proxy != undefined) { 6735 proxy.sendMessageRequest(1, data, reply, option) 6736 .then((result: rpc.RequestResult) => { 6737 if (result.errCode === 0) { 6738 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 6739 let num = result.reply.readInt(); 6740 let msg = result.reply.readString(); 6741 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 6742 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6743 } else { 6744 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 6745 } 6746 }).catch((e: Error) => { 6747 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 6748 }).finally (() => { 6749 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 6750 data.reclaim(); 6751 reply.reclaim(); 6752 }); 6753 } 6754 ``` 6755 6756### sendRequest<sup>8+(deprecated)</sup> 6757 6758>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9-2). 6759 6760sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6761 6762Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 6763 6764**System capability**: SystemCapability.Communication.IPC.Core 6765 6766**Parameters** 6767 6768 | Name | Type | Mandatory| Description | 6769 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6770 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6771 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6772 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6773 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6774 6775**Return value** 6776 6777| Type | Description | 6778| ------------------------------------------------------------ | --------------------------------------------- | 6779| Promise<[SendRequestResult](#sendrequestresult8deprecated)> | Promise used to return the **sendRequestResult** object.| 6780 6781**Example** 6782 6783 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6784 6785 ```ts 6786 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6787 // import FA from "@ohos.ability.featureAbility"; 6788 import Want from '@ohos.app.ability.Want'; 6789 import common from '@ohos.app.ability.common'; 6790 import hilog from '@ohos.hilog'; 6791 6792 let proxy: rpc.IRemoteObject | undefined; 6793 let connect: common.ConnectOptions = { 6794 onConnect: (elementName, remoteProxy) => { 6795 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6796 proxy = remoteProxy; 6797 }, 6798 onDisconnect: (elementName) => { 6799 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6800 }, 6801 onFailed: () => { 6802 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6803 } 6804 }; 6805 let want: Want = { 6806 bundleName: "com.ohos.server", 6807 abilityName: "com.ohos.server.EntryAbility", 6808 }; 6809 6810 // Use this method to connect to the ability for the FA model. 6811 // FA.connectAbility(want,connect); 6812 6813 this.context.connectServiceExtensionAbility(want, connect); 6814 ``` 6815 6816 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6817 6818 ```ts 6819 import hilog from '@ohos.hilog'; 6820 6821 let option = new rpc.MessageOption(); 6822 let data = rpc.MessageParcel.create(); 6823 let reply = rpc.MessageParcel.create(); 6824 data.writeInt(1); 6825 data.writeString("hello"); 6826 if (proxy != undefined) { 6827 let a = proxy.sendRequest(1, data, reply, option) as Object; 6828 let b = a as Promise<rpc.SendRequestResult>; 6829 b.then((result: rpc.SendRequestResult) => { 6830 if (result.errCode === 0) { 6831 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 6832 let num = result.reply.readInt(); 6833 let msg = result.reply.readString(); 6834 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 6835 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6836 } else { 6837 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 6838 } 6839 }).catch((e: Error) => { 6840 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 6841 }).finally (() => { 6842 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 6843 data.reclaim(); 6844 reply.reclaim(); 6845 }); 6846 } 6847 ``` 6848 6849### sendMessageRequest<sup>9+</sup> 6850 6851sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6852 6853Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked at certain time after the response to **sendMessageRequest** is returned, and the reply contains the returned information. 6854 6855**System capability**: SystemCapability.Communication.IPC.Core 6856 6857**Parameters** 6858 6859 | Name | Type | Mandatory| Description | 6860 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6861 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6862 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6863 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6864 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6865 | callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 6866 6867**Example** 6868 6869 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6870 6871 ```ts 6872 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6873 // import FA from "@ohos.ability.featureAbility"; 6874 import Want from '@ohos.app.ability.Want'; 6875 import common from '@ohos.app.ability.common'; 6876 import hilog from '@ohos.hilog'; 6877 import { BusinessError } from '@ohos.base'; 6878 6879 let proxy: rpc.IRemoteObject | undefined; 6880 let connect: common.ConnectOptions = { 6881 onConnect: (elementName, remoteProxy) => { 6882 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6883 proxy = remoteProxy; 6884 }, 6885 onDisconnect: (elementName) => { 6886 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6887 }, 6888 onFailed: () => { 6889 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6890 } 6891 }; 6892 let want: Want = { 6893 bundleName: "com.ohos.server", 6894 abilityName: "com.ohos.server.EntryAbility", 6895 }; 6896 function sendMessageRequestCallback(err: BusinessError, result: rpc.RequestResult) { 6897 if (result.errCode === 0) { 6898 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 6899 let num = result.reply.readInt(); 6900 let msg = result.reply.readString(); 6901 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 6902 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6903 } else { 6904 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 6905 } 6906 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 6907 result.data.reclaim(); 6908 result.reply.reclaim(); 6909} 6910 6911 // Use this method to connect to the ability for the FA model. 6912 // FA.connectAbility(want,connect); 6913 6914 this.context.connectServiceExtensionAbility(want, connect); 6915 ``` 6916 6917 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 6918 6919 ```ts 6920 import hilog from '@ohos.hilog'; 6921 import { BusinessError } from '@ohos.base'; 6922 6923 let option = new rpc.MessageOption(); 6924 let data = rpc.MessageSequence.create(); 6925 let reply = rpc.MessageSequence.create(); 6926 data.writeInt(1); 6927 data.writeString("hello"); 6928 if (proxy != undefined) { 6929 try { 6930 proxy.sendMessageRequest(1, data, reply, option, sendMessageRequestCallback); 6931 } catch(error) { 6932 let e: BusinessError = error as BusinessError; 6933 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorCode ' + e.code); 6934 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorMessage ' + e.message); 6935 } 6936 } 6937 ``` 6938 6939### sendRequest<sup>8+(deprecated)</sup> 6940 6941>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9-3). 6942 6943sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6944 6945Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 6946 6947**System capability**: SystemCapability.Communication.IPC.Core 6948 6949**Parameters** 6950 6951| Name | Type | Mandatory| Description | 6952| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6953| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6954| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6955| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6956| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6957| callback | AsyncCallback<[SendRequestResult](#sendrequestresult8deprecated)> | Yes | Callback for receiving the sending result. | 6958 6959**Example** 6960 6961 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6962 6963 ```ts 6964 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6965 // import FA from "@ohos.ability.featureAbility"; 6966 import Want from '@ohos.app.ability.Want'; 6967 import common from '@ohos.app.ability.common'; 6968 import hilog from '@ohos.hilog'; 6969 import { BusinessError } from '@ohos.base'; 6970 6971 let proxy: rpc.IRemoteObject | undefined; 6972 let connect: common.ConnectOptions = { 6973 onConnect: (elementName, remoteProxy) => { 6974 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6975 proxy = remoteProxy; 6976 }, 6977 onDisconnect: (elementName) => { 6978 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6979 }, 6980 onFailed: () => { 6981 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6982 } 6983 }; 6984 let want: Want = { 6985 bundleName: "com.ohos.server", 6986 abilityName: "com.ohos.server.EntryAbility", 6987 }; 6988 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 6989 if (result.errCode === 0) { 6990 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 6991 let num = result.reply.readInt(); 6992 let msg = result.reply.readString(); 6993 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 6994 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6995 } else { 6996 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 6997 } 6998 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 6999 result.data.reclaim(); 7000 result.reply.reclaim(); 7001} 7002 7003 // Use this method to connect to the ability for the FA model. 7004 // FA.connectAbility(want,connect); 7005 7006 this.context.connectServiceExtensionAbility(want, connect); 7007 ``` 7008 7009 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 7010 7011 ```ts 7012 let option = new rpc.MessageOption(); 7013 let data = rpc.MessageParcel.create(); 7014 let reply = rpc.MessageParcel.create(); 7015 data.writeInt(1); 7016 data.writeString("hello"); 7017 if (proxy != undefined) { 7018 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 7019 } 7020 ``` 7021 7022### getLocalInterface<sup>9+</sup> 7023 7024getLocalInterface(interface: string): IRemoteBroker 7025 7026Obtains the **LocalInterface** object of an interface token. 7027 7028**System capability**: SystemCapability.Communication.IPC.Core 7029 7030**Parameters** 7031 7032 | Name | Type | Mandatory| Description | 7033 | --------- | ------ | ---- | ---------------------- | 7034 | interface | string | Yes | Interface descriptor.| 7035 7036**Return value** 7037 7038| Type | Description | 7039| ------------------------------- | ------------------------------------------ | 7040| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7041 7042**Error codes** 7043 7044For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 7045 7046 | ID| Error Message| 7047 | -------- | -------- | 7048 | 1900006 | only remote object permitted | 7049 7050**Example** 7051 7052 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7053 7054 ```ts 7055 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7056 // import FA from "@ohos.ability.featureAbility"; 7057 import Want from '@ohos.app.ability.Want'; 7058 import common from '@ohos.app.ability.common'; 7059 import hilog from '@ohos.hilog'; 7060 7061 let proxy: rpc.IRemoteObject | undefined; 7062 let connect: common.ConnectOptions = { 7063 onConnect: (elementName, remoteProxy) => { 7064 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7065 proxy = remoteProxy; 7066 }, 7067 onDisconnect: (elementName) => { 7068 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7069 }, 7070 onFailed: () => { 7071 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7072 } 7073 }; 7074 let want: Want = { 7075 bundleName: "com.ohos.server", 7076 abilityName: "com.ohos.server.EntryAbility", 7077 }; 7078 7079 // Use this method to connect to the ability for the FA model. 7080 // FA.connectAbility(want,connect); 7081 7082 this.context.connectServiceExtensionAbility(want, connect); 7083 ``` 7084 7085 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getLocalInterface()** of the proxy object is called to obtain the interface descriptor. 7086 7087 ```ts 7088 import hilog from '@ohos.hilog'; 7089 import { BusinessError } from '@ohos.base'; 7090 7091 if (proxy != undefined) { 7092 try { 7093 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7094 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7095 } catch(error) { 7096 let e: BusinessError = error as BusinessError; 7097 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7098 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7099 } 7100 } 7101 ``` 7102 7103### queryLocalInterface<sup>(deprecated)</sup> 7104 7105>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9-1). 7106 7107queryLocalInterface(interface: string): IRemoteBroker 7108 7109Obtains the **LocalInterface** object of an interface token. 7110 7111**System capability**: SystemCapability.Communication.IPC.Core 7112 7113**Parameters** 7114 7115 | Name | Type | Mandatory| Description | 7116 | --------- | ------ | ---- | ---------------------- | 7117 | interface | string | Yes | Interface descriptor.| 7118 7119**Return value** 7120 7121| Type | Description | 7122| ------------------------------- | ------------------------------------------ | 7123| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7124 7125**Example** 7126 7127 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7128 7129 ```ts 7130 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7131 // import FA from "@ohos.ability.featureAbility"; 7132 import Want from '@ohos.app.ability.Want'; 7133 import common from '@ohos.app.ability.common'; 7134 import hilog from '@ohos.hilog'; 7135 7136 let proxy: rpc.IRemoteObject | undefined; 7137 let connect: common.ConnectOptions = { 7138 onConnect: (elementName, remoteProxy) => { 7139 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7140 proxy = remoteProxy; 7141 }, 7142 onDisconnect: (elementName) => { 7143 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7144 }, 7145 onFailed: () => { 7146 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7147 } 7148 }; 7149 let want: Want = { 7150 bundleName: "com.ohos.server", 7151 abilityName: "com.ohos.server.EntryAbility", 7152 }; 7153 7154 // Use this method to connect to the ability for the FA model. 7155 // FA.connectAbility(want,connect); 7156 7157 this.context.connectServiceExtensionAbility(want, connect); 7158 ``` 7159 7160 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **queryLocalInterface()** of the proxy object is called to obtain the interface descriptor. 7161 7162 ```ts 7163 import hilog from '@ohos.hilog'; 7164 7165 if (proxy != undefined) { 7166 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7167 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7168 } 7169 ``` 7170 7171### registerDeathRecipient<sup>9+</sup> 7172 7173registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7174 7175Registers a callback for receiving death notifications of the remote object. The callback will be invoked when the remote object process matching the **RemoteProxy** object is killed. 7176 7177**System capability**: SystemCapability.Communication.IPC.Core 7178 7179**Parameters** 7180 7181 | Name | Type | Mandatory| Description | 7182 | --------- | --------------------------------- | ---- | -------------- | 7183| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 7184 | flags | number | Yes | Flag of the death notification.| 7185 7186**Error codes** 7187 7188For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 7189 7190 | ID| Error Message| 7191 | -------- | -------- | 7192 | 1900008 | proxy or remote object is invalid | 7193 7194**Example** 7195 7196 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7197 7198 ```ts 7199 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7200 // import FA from "@ohos.ability.featureAbility"; 7201 import Want from '@ohos.app.ability.Want'; 7202 import common from '@ohos.app.ability.common'; 7203 import hilog from '@ohos.hilog'; 7204 7205 let proxy: rpc.IRemoteObject | undefined; 7206 let connect: common.ConnectOptions = { 7207 onConnect: (elementName, remoteProxy) => { 7208 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7209 proxy = remoteProxy; 7210 }, 7211 onDisconnect: (elementName) => { 7212 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7213 }, 7214 onFailed: () => { 7215 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7216 } 7217 }; 7218 let want: Want = { 7219 bundleName: "com.ohos.server", 7220 abilityName: "com.ohos.server.EntryAbility", 7221 }; 7222 7223 // Use this method to connect to the ability for the FA model. 7224 // FA.connectAbility(want,connect); 7225 7226 this.context.connectServiceExtensionAbility(want, connect); 7227 ``` 7228 7229 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **registerDeathRecipient()** of the proxy object is called to register a callback for receiving the death notification of the remote object. 7230 7231 ```ts 7232 import hilog from '@ohos.hilog'; 7233 import { BusinessError } from '@ohos.base'; 7234 7235 class MyDeathRecipient implements rpc.DeathRecipient { 7236 onRemoteDied() { 7237 hilog.info(0x0000, 'testTag', 'server died'); 7238 } 7239 } 7240 let deathRecipient = new MyDeathRecipient(); 7241 if (proxy != undefined) { 7242 try { 7243 proxy.registerDeathRecipient(deathRecipient, 0); 7244 } catch(error) { 7245 let e: BusinessError = error as BusinessError; 7246 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7247 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7248 } 7249 } 7250 ``` 7251 7252### addDeathRecipient<sup>(deprecated)</sup> 7253 7254>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9-1). 7255 7256addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7257 7258Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 7259 7260**System capability**: SystemCapability.Communication.IPC.Core 7261 7262**Parameters** 7263 7264 | Name | Type | Mandatory| Description | 7265 | --------- | --------------------------------- | ---- | --------------------------------- | 7266 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 7267 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7268 7269**Return value** 7270 7271 | Type | Description | 7272 | ------- | ---------------------------------------- | 7273 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 7274 7275**Example** 7276 7277 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7278 7279 ```ts 7280 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7281 // import FA from "@ohos.ability.featureAbility"; 7282 import Want from '@ohos.app.ability.Want'; 7283 import common from '@ohos.app.ability.common'; 7284 import hilog from '@ohos.hilog'; 7285 7286 let proxy: rpc.IRemoteObject | undefined; 7287 let connect: common.ConnectOptions = { 7288 onConnect: (elementName, remoteProxy) => { 7289 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7290 proxy = remoteProxy; 7291 }, 7292 onDisconnect: (elementName) => { 7293 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7294 }, 7295 onFailed: () => { 7296 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7297 } 7298 }; 7299 let want: Want = { 7300 bundleName: "com.ohos.server", 7301 abilityName: "com.ohos.server.EntryAbility", 7302 }; 7303 7304 // Use this method to connect to the ability for the FA model. 7305 // FA.connectAbility(want,connect); 7306 7307 this.context.connectServiceExtensionAbility(want, connect); 7308 ``` 7309 7310 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **addDeathRecipient()** of the proxy object is called to add a callback for receiving the death notification of the remove object. 7311 7312 ```ts 7313 import hilog from '@ohos.hilog'; 7314 7315 class MyDeathRecipient implements rpc.DeathRecipient { 7316 onRemoteDied() { 7317 hilog.info(0x0000, 'testTag', 'server died'); 7318 } 7319 } 7320 let deathRecipient = new MyDeathRecipient(); 7321 if (proxy != undefined) { 7322 proxy.addDeathRecipient(deathRecipient, 0); 7323 } 7324 ``` 7325 7326### unregisterDeathRecipient<sup>9+</sup> 7327 7328unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7329 7330Unregisters the callback used to receive death notifications of the remote object. 7331 7332**System capability**: SystemCapability.Communication.IPC.Core 7333 7334**Parameters** 7335 7336 | Name | Type | Mandatory| Description | 7337 | --------- | --------------------------------- | ---- | -------------- | 7338 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 7339 | flags | number | Yes | Flag of the death notification.| 7340 7341**Error codes** 7342 7343For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 7344 7345 | ID| Error Message| 7346 | -------- | -------- | 7347 | 1900008 | proxy or remote object is invalid | 7348 7349**Example** 7350 7351 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7352 7353 ```ts 7354 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7355 // import FA from "@ohos.ability.featureAbility"; 7356 import Want from '@ohos.app.ability.Want'; 7357 import common from '@ohos.app.ability.common'; 7358 import hilog from '@ohos.hilog'; 7359 7360 let proxy: rpc.IRemoteObject | undefined; 7361 let connect: common.ConnectOptions = { 7362 onConnect: (elementName, remoteProxy) => { 7363 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7364 proxy = remoteProxy; 7365 }, 7366 onDisconnect: (elementName) => { 7367 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7368 }, 7369 onFailed: () => { 7370 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7371 } 7372 }; 7373 let want: Want = { 7374 bundleName: "com.ohos.server", 7375 abilityName: "com.ohos.server.EntryAbility", 7376 }; 7377 7378 // Use this method to connect to the ability for the FA model. 7379 // FA.connectAbility(want,connect); 7380 7381 this.context.connectServiceExtensionAbility(want, connect); 7382 ``` 7383 7384 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **unregisterDeathRecipient()** of the proxy object is called to unregister the callback for receiving the death notification of the remote object. 7385 7386 ```ts 7387 import hilog from '@ohos.hilog'; 7388 import { BusinessError } from '@ohos.base'; 7389 7390 class MyDeathRecipient implements rpc.DeathRecipient { 7391 onRemoteDied() { 7392 hilog.info(0x0000, 'testTag', 'server died'); 7393 } 7394 } 7395 let deathRecipient = new MyDeathRecipient(); 7396 if (proxy != undefined) { 7397 try { 7398 proxy.registerDeathRecipient(deathRecipient, 0); 7399 proxy.unregisterDeathRecipient(deathRecipient, 0); 7400 } catch(error) { 7401 let e: BusinessError = error as BusinessError; 7402 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7403 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7404 } 7405 } 7406 ``` 7407 7408### removeDeathRecipient<sup>(deprecated)</sup> 7409 7410>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9-1). 7411 7412removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7413 7414Removes the callback used to receive death notifications of the remote object. 7415 7416**System capability**: SystemCapability.Communication.IPC.Core 7417 7418**Parameters** 7419 7420 | Name | Type | Mandatory| Description | 7421 | --------- | --------------------------------- | ---- | --------------------------------- | 7422 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 7423 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7424 7425**Return value** 7426 7427 | Type | Description | 7428 | ------- | ---------------------------------------- | 7429 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 7430 7431**Example** 7432 7433 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7434 7435 ```ts 7436 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7437 // import FA from "@ohos.ability.featureAbility"; 7438 import Want from '@ohos.app.ability.Want'; 7439 import common from '@ohos.app.ability.common'; 7440 import hilog from '@ohos.hilog'; 7441 7442 let proxy: rpc.IRemoteObject | undefined; 7443 let connect: common.ConnectOptions = { 7444 onConnect: (elementName, remoteProxy) => { 7445 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7446 proxy = remoteProxy; 7447 }, 7448 onDisconnect: (elementName) => { 7449 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7450 }, 7451 onFailed: () => { 7452 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7453 } 7454 }; 7455 let want: Want = { 7456 bundleName: "com.ohos.server", 7457 abilityName: "com.ohos.server.EntryAbility", 7458 }; 7459 7460 // Use this method to connect to the ability for the FA model. 7461 // FA.connectAbility(want,connect); 7462 7463 this.context.connectServiceExtensionAbility(want, connect); 7464 ``` 7465 7466 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **removeDeathRecipient()** of the proxy object is called to remove the callback used to receive the death notification of the remote object. 7467 7468 ```ts 7469 import hilog from '@ohos.hilog'; 7470 7471 class MyDeathRecipient implements rpc.DeathRecipient { 7472 onRemoteDied() { 7473 hilog.info(0x0000, 'testTag', 'server died'); 7474 } 7475 } 7476 let deathRecipient = new MyDeathRecipient(); 7477 if (proxy != undefined) { 7478 proxy.addDeathRecipient(deathRecipient, 0); 7479 proxy.removeDeathRecipient(deathRecipient, 0); 7480 } 7481 ``` 7482 7483### getDescriptor<sup>9+</sup> 7484 7485getDescriptor(): string 7486 7487Obtains the interface descriptor (which is a string) of this object. 7488 7489**System capability**: SystemCapability.Communication.IPC.Core 7490 7491**Return value** 7492 7493 | Type | Description | 7494 | ------ | ---------------- | 7495 | string | Interface descriptor obtained.| 7496 7497**Error codes** 7498 7499For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 7500 7501 | ID| Error Message| 7502 | -------- | -------- | 7503 | 1900008 | proxy or remote object is invalid | 7504 | 1900007 | communication failed | 7505 7506**Example** 7507 7508 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7509 7510 ```ts 7511 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7512 // import FA from "@ohos.ability.featureAbility"; 7513 import Want from '@ohos.app.ability.Want'; 7514 import common from '@ohos.app.ability.common'; 7515 import hilog from '@ohos.hilog'; 7516 7517 let proxy: rpc.IRemoteObject | undefined; 7518 let connect: common.ConnectOptions = { 7519 onConnect: (elementName, remoteProxy) => { 7520 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7521 proxy = remoteProxy; 7522 }, 7523 onDisconnect: (elementName) => { 7524 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7525 }, 7526 onFailed: () => { 7527 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7528 } 7529 }; 7530 let want: Want = { 7531 bundleName: "com.ohos.server", 7532 abilityName: "com.ohos.server.EntryAbility", 7533 }; 7534 7535 // Use this method to connect to the ability for the FA model. 7536 // FA.connectAbility(want,connect); 7537 7538 this.context.connectServiceExtensionAbility(want, connect); 7539 ``` 7540 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getDescriptor()** of the proxy object is called to obtain the interface descriptor of the object. 7541 7542 ```ts 7543 import hilog from '@ohos.hilog'; 7544 import { BusinessError } from '@ohos.base'; 7545 7546 if (proxy != undefined) { 7547 try { 7548 let descriptor: string = proxy.getDescriptor(); 7549 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7550 } catch(error) { 7551 let e: BusinessError = error as BusinessError; 7552 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7553 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7554 } 7555 } 7556 ``` 7557 7558### getInterfaceDescriptor<sup>(deprecated)</sup> 7559 7560>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9-1). 7561 7562getInterfaceDescriptor(): string 7563 7564Obtains the interface descriptor of this proxy object. 7565 7566**System capability**: SystemCapability.Communication.IPC.Core 7567 7568**Return value** 7569 7570 | Type | Description | 7571 | ------ | ------------------ | 7572 | string | Interface descriptor obtained.| 7573 7574**Example** 7575 7576 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7577 7578 ```ts 7579 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7580 // import FA from "@ohos.ability.featureAbility"; 7581 import Want from '@ohos.app.ability.Want'; 7582 import common from '@ohos.app.ability.common'; 7583 import hilog from '@ohos.hilog'; 7584 7585 let proxy: rpc.IRemoteObject | undefined; 7586 let connect: common.ConnectOptions = { 7587 onConnect: (elementName, remoteProxy) => { 7588 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7589 proxy = remoteProxy; 7590 }, 7591 onDisconnect: (elementName) => { 7592 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7593 }, 7594 onFailed: () => { 7595 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7596 } 7597 }; 7598 let want: Want = { 7599 bundleName: "com.ohos.server", 7600 abilityName: "com.ohos.server.EntryAbility", 7601 }; 7602 7603 // Use this method to connect to the ability for the FA model. 7604 // FA.connectAbility(want,connect); 7605 7606 this.context.connectServiceExtensionAbility(want, connect); 7607 ``` 7608 7609 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getInterfaceDescriptor()** of the proxy object is called to obtain the interface descriptor of the current proxy object. 7610 7611 ```ts 7612 import hilog from '@ohos.hilog'; 7613 7614 if (proxy != undefined) { 7615 let descriptor: string = proxy.getInterfaceDescriptor(); 7616 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7617 } 7618 ``` 7619 7620### isObjectDead 7621 7622isObjectDead(): boolean 7623 7624Checks whether the **RemoteObject** is dead. 7625 7626**System capability**: SystemCapability.Communication.IPC.Core 7627 7628**Return value** 7629 7630 | Type | Description | 7631 | ------- | ------------------------------------------------- | 7632 | boolean | Returns **true** if the **RemoteObject** is dead; returns **false** otherwise.| 7633 7634**Example** 7635 7636 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7637 7638 ```ts 7639 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7640 // import FA from "@ohos.ability.featureAbility"; 7641 import Want from '@ohos.app.ability.Want'; 7642 import common from '@ohos.app.ability.common'; 7643 import hilog from '@ohos.hilog'; 7644 7645 let proxy: rpc.IRemoteObject | undefined; 7646 let connect: common.ConnectOptions = { 7647 onConnect: (elementName, remoteProxy) => { 7648 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7649 proxy = remoteProxy; 7650 }, 7651 onDisconnect: (elementName) => { 7652 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7653 }, 7654 onFailed: () => { 7655 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7656 } 7657 }; 7658 let want: Want = { 7659 bundleName: "com.ohos.server", 7660 abilityName: "com.ohos.server.EntryAbility", 7661 }; 7662 7663 // Use this method to connect to the ability for the FA model. 7664 // FA.connectAbility(want,connect); 7665 7666 this.context.connectServiceExtensionAbility(want, connect); 7667 ``` 7668 7669 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **isObjectDead()** of the proxy object is called to check whether this object is dead. 7670 7671 ```ts 7672 import hilog from '@ohos.hilog'; 7673 7674 if (proxy != undefined) { 7675 let isDead: boolean = proxy.isObjectDead(); 7676 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 7677 } 7678 ``` 7679 7680## MessageOption 7681 7682Defines the options used to construct the **MessageOption** object. 7683 7684**System capability**: SystemCapability.Communication.IPC.Core 7685 7686 | Name | Value | Description | 7687 | ------------- | --------- | ----------------------------------------------------------- | 7688 | TF_SYNC | 0 (0x00) | Synchronous call. | 7689 | TF_ASYNC | 1 (0x01) | Asynchronous call. | 7690 | TF_ACCEPT_FDS | 16 (0x10) | Indication to **sendMessageRequest<sup>9+</sup>** for returning the file descriptor.| 7691 | TF_WAIT_TIME | 4 (0x4) | RPC wait time, in seconds. This parameter is not used in IPC. | 7692 7693### constructor<sup>9+</sup> 7694 7695constructor(async?: boolean) 7696 7697A constructor used to create a **MessageOption** object. 7698 7699**System capability**: SystemCapability.Communication.IPC.Core 7700 7701**Parameters** 7702 7703| Name| Type | Mandatory| Description | 7704| ------ | ------- | ---- | ------------------------------------------------------------ | 7705| async | boolean | No | Calling mode. The value **true** indicates asynchronous calling; the value **false** (default) indicates synchronous calling.| 7706 7707**Example** 7708 7709 ```ts 7710 class TestRemoteObject extends rpc.MessageOption { 7711 constructor(async: boolean) { 7712 super(async); 7713 } 7714 } 7715 ``` 7716 7717### constructor 7718 7719constructor(syncFlags?: number, waitTime?: number) 7720 7721A constructor used to create a **MessageOption** object. 7722 7723**System capability**: SystemCapability.Communication.IPC.Core 7724 7725**Parameters** 7726 7727 | Name | Type | Mandatory| Description | 7728 | --------- | ------ | ---- | --------------------------------------------- | 7729 | syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 7730 | waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 7731 7732**Example** 7733 7734 ```ts 7735 class TestRemoteObject extends rpc.MessageOption { 7736 constructor(syncFlags?: number,waitTime?: number) { 7737 super(syncFlags,waitTime); 7738 } 7739 } 7740 ``` 7741### isAsync<sup>9+</sup> 7742 7743isAsync(): boolean 7744 7745Checks whether **SendMessageRequest** is called synchronously or asynchronously. 7746 7747**System capability**: SystemCapability.Communication.IPC.Core 7748 7749**Return value** 7750 7751 | Type | Description | 7752 | ------- | ---------------------------------------- | 7753 | boolean | Returns **true** if **SendMessageRequest** is called asynchronously; returns **false** if it is called synchronously.| 7754 7755**Example** 7756 7757 ```ts 7758 let option = new rpc.MessageOption(); 7759 option.isAsync(); 7760 ``` 7761 7762### setAsync<sup>9+</sup> 7763 7764setAsync(async: boolean): void 7765 7766Sets the calling flag in **SendMessageRequest**. 7767 7768**System capability**: SystemCapability.Communication.IPC.Core 7769 7770**Parameters** 7771 7772| Name| Type | Mandatory| Description | 7773| ------ | ------- | ---- | ------------------------------------------------- | 7774| async | boolean | Yes | Calling mode to set. The value **true** indicates asynchronous calling; the value **false** indicates synchronous calling.| 7775 7776**Example** 7777 7778 ```ts 7779 import hilog from '@ohos.hilog'; 7780 7781 let option = new rpc.MessageOption(); 7782 option.setAsync(true); 7783 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 7784 ``` 7785 7786### getFlags 7787 7788getFlags(): number 7789 7790Obtains the call flag, which can be synchronous or asynchronous. 7791 7792**System capability**: SystemCapability.Communication.IPC.Core 7793 7794**Return value** 7795 7796 | Type | Description | 7797 | ------ | ------------------------------------ | 7798 | number | Call mode obtained.| 7799 7800**Example** 7801 7802 ```ts 7803 import hilog from '@ohos.hilog'; 7804 7805 try { 7806 let option = new rpc.MessageOption(); 7807 hilog.info(0x0000, 'testTag', 'create object successfully'); 7808 let flog = option.getFlags(); 7809 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 7810 option.setFlags(1) 7811 hilog.info(0x0000, 'testTag', 'run setFlags success'); 7812 let flog2 = option.getFlags(); 7813 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 7814 } catch (error) { 7815 hilog.error(0x0000, 'testTag', 'error ' + error); 7816 } 7817 ``` 7818 7819### setFlags 7820 7821setFlags(flags: number): void 7822 7823Sets the call flag, which can be synchronous or asynchronous. 7824 7825**System capability**: SystemCapability.Communication.IPC.Core 7826 7827**Parameters** 7828 7829 | Name| Type | Mandatory| Description | 7830 | ------ | ------ | ---- | ------------------------ | 7831 | flags | number | Yes | Call flag to set.| 7832 7833**Example** 7834 7835 ```ts 7836 import hilog from '@ohos.hilog'; 7837 7838 try { 7839 let option = new rpc.MessageOption(); 7840 option.setFlags(1) 7841 hilog.info(0x0000, 'testTag', 'run setFlags success'); 7842 let flog = option.getFlags(); 7843 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 7844 } catch (error) { 7845 hilog.error(0x0000, 'testTag', 'error ' + error); 7846 } 7847 ``` 7848 7849### getWaitTime 7850 7851getWaitTime(): number 7852 7853Obtains the maximum wait time for this RPC call. 7854 7855**System capability**: SystemCapability.Communication.IPC.Core 7856 7857**Return value** 7858 7859 | Type | Description | 7860 | ------ | ----------------- | 7861 | number | Maximum wait time obtained.| 7862 7863**Example** 7864 7865 ```ts 7866 import hilog from '@ohos.hilog'; 7867 7868 try { 7869 let option = new rpc.MessageOption(); 7870 let time = option.getWaitTime(); 7871 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 7872 option.setWaitTime(16); 7873 let time2 = option.getWaitTime(); 7874 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 7875 } catch (error) { 7876 hilog.error(0x0000, 'testTag', 'error ' + error); 7877 } 7878 ``` 7879 7880### setWaitTime 7881 7882setWaitTime(waitTime: number): void 7883 7884Sets the maximum wait time for this RPC call. 7885 7886**System capability**: SystemCapability.Communication.IPC.Core 7887 7888**Parameters** 7889 7890 | Name | Type | Mandatory| Description | 7891 | -------- | ------ | ---- | --------------------- | 7892 | waitTime | number | Yes | Maximum wait time to set. The upper limit is 3000 seconds.| 7893 7894**Example** 7895 7896 ```ts 7897 import hilog from '@ohos.hilog'; 7898 7899 try { 7900 let option = new rpc.MessageOption(); 7901 option.setWaitTime(16); 7902 let time = option.getWaitTime(); 7903 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 7904 } catch (error) { 7905 hilog.error(0x0000, 'testTag', 'error ' + error); 7906 } 7907 ``` 7908 7909## IPCSkeleton 7910 7911Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 7912 7913### getContextObject 7914 7915static getContextObject(): IRemoteObject 7916 7917Obtains the system capability manager. This API is a static method. 7918 7919**System capability**: SystemCapability.Communication.IPC.Core 7920 7921**Return value** 7922 7923 | Type | Description | 7924 | ------------------------------- | -------------------- | 7925 | [IRemoteObject](#iremoteobject) | System capability manager obtained.| 7926 7927**Example** 7928 7929 ```ts 7930 import hilog from '@ohos.hilog'; 7931 7932 let samgr = rpc.IPCSkeleton.getContextObject(); 7933 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 7934 ``` 7935 7936### getCallingPid 7937 7938static getCallingPid(): number 7939 7940Obtains the PID of the caller. This API is a static method, which is invoked by the **RemoteObject** object in the **onRemoteRequest** method. If this method is not invoked in the IPC context (**onRemoteRequest**), the PID of the process will be returned. 7941 7942**System capability**: SystemCapability.Communication.IPC.Core 7943 7944**Return value** 7945 7946 | Type | Description | 7947 | ------ | ----------------- | 7948 | number | PID of the caller.| 7949 7950**Example** 7951 7952 ```ts 7953 import hilog from '@ohos.hilog'; 7954 7955 class Stub extends rpc.RemoteObject { 7956 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7957 let callerPid = rpc.IPCSkeleton.getCallingPid(); 7958 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 7959 return true; 7960 } 7961 } 7962 ``` 7963 7964### getCallingUid 7965 7966static getCallingUid(): number 7967 7968Obtains the UID of the caller. This API is a static method, which is invoked by the **RemoteObject** object in the **onRemoteRequest** method. If this method is not invoked in the IPC context (**onRemoteRequest**), the UID of the process will be returned. 7969 7970**System capability**: SystemCapability.Communication.IPC.Core 7971 7972**Return value** 7973 7974 | Type | Description | 7975 | ------ | ----------------- | 7976 | number | UID of the caller.| 7977 7978**Example** 7979 7980 ```ts 7981 import hilog from '@ohos.hilog'; 7982 7983 class Stub extends rpc.RemoteObject { 7984 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7985 let callerUid = rpc.IPCSkeleton.getCallingUid(); 7986 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 7987 return true; 7988 } 7989 } 7990 ``` 7991 7992### getCallingTokenId<sup>8+</sup> 7993 7994static getCallingTokenId(): number 7995 7996Obtains the caller's token ID, which is used to verify the caller identity. 7997 7998**System capability**: SystemCapability.Communication.IPC.Core 7999 8000**Return value** 8001 8002 | Type | Description | 8003 | ------ | --------------------- | 8004 | number | Token ID of the caller obtained.| 8005 8006**Example** 8007 8008 ```ts 8009 import hilog from '@ohos.hilog'; 8010 8011 class Stub extends rpc.RemoteObject { 8012 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8013 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8014 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8015 return true; 8016 } 8017 } 8018 ``` 8019 8020### getCallingDeviceID 8021 8022static getCallingDeviceID(): string 8023 8024Obtains the ID of the device hosting the caller's process. This API is a static method. 8025 8026**System capability**: SystemCapability.Communication.IPC.Core 8027 8028**Return value** 8029 8030 | Type | Description | 8031 | ------ | ---------------------------- | 8032 | string | Device ID obtained.| 8033 8034**Example** 8035 8036 ```ts 8037 import hilog from '@ohos.hilog'; 8038 8039 class Stub extends rpc.RemoteObject { 8040 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8041 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8042 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8043 return true; 8044 } 8045 } 8046 ``` 8047 8048### getLocalDeviceID 8049 8050static getLocalDeviceID(): string 8051 8052Obtains the local device ID. This API is a static method. 8053 8054**System capability**: SystemCapability.Communication.IPC.Core 8055 8056**Return value** 8057 8058 | Type | Description | 8059 | ------ | ------------------ | 8060 | string | Local device ID obtained.| 8061 8062**Example** 8063 8064 ```ts 8065 import hilog from '@ohos.hilog'; 8066 8067 class Stub extends rpc.RemoteObject { 8068 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8069 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8070 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8071 return true; 8072 } 8073 } 8074 ``` 8075 8076### isLocalCalling 8077 8078static isLocalCalling(): boolean 8079 8080Checks whether the remote process is a process of the local device. This API is a static method. 8081 8082**System capability**: SystemCapability.Communication.IPC.Core 8083 8084**Return value** 8085 8086 | Type | Description | 8087 | ------- | -------------------------------------------------- | 8088| boolean | Returns **true** if the local and remote processes are on the same device; returns **false** otherwise.| 8089 8090**Example** 8091 8092 ```ts 8093 import hilog from '@ohos.hilog'; 8094 8095 class Stub extends rpc.RemoteObject { 8096 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8097 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8098 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8099 return true; 8100 } 8101 } 8102 ``` 8103 8104### flushCmdBuffer<sup>9+</sup> 8105 8106static flushCmdBuffer(object: IRemoteObject): void 8107 8108Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. You are advised to call this API before performing any sensitive operation. 8109 8110**System capability**: SystemCapability.Communication.IPC.Core 8111 8112**Parameters** 8113 8114 | Name| Type | Mandatory| Description | 8115 | ------ | ------------------------------- | ---- | ------------------- | 8116 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8117 8118**Example** 8119 8120 ```ts 8121 import hilog from '@ohos.hilog'; 8122 import { BusinessError } from '@ohos.base'; 8123 8124 class TestRemoteObject extends rpc.RemoteObject { 8125 constructor(descriptor: string) { 8126 super(descriptor); 8127 } 8128 } 8129 let remoteObject = new TestRemoteObject("aaa"); 8130 try { 8131 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8132 } catch(error) { 8133 let e: BusinessError = error as BusinessError; 8134 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8135 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8136 } 8137 ``` 8138 8139### flushCommands<sup>(deprecated)</sup> 8140 8141>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [flushCmdBuffer](#flushcmdbuffer9). 8142 8143static flushCommands(object: IRemoteObject): number 8144 8145Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. You are advised to call this API before performing any sensitive operation. 8146 8147**System capability**: SystemCapability.Communication.IPC.Core 8148 8149**Parameters** 8150 8151 | Name| Type | Mandatory| Description | 8152 | ------ | ------------------------------- | ---- | ------------------- | 8153 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8154 8155**Return value** 8156 8157 | Type | Description | 8158 | ------ | --------------------------------------------------------------------------------- | 8159 | number | Returns **0** if the operation is successful; returns an error code if the input object is null or a **RemoteObject**, or if the operation fails.| 8160 8161**Example** 8162 8163 ```ts 8164 import hilog from '@ohos.hilog'; 8165 8166 class MyDeathRecipient implements rpc.DeathRecipient { 8167 onRemoteDied() { 8168 hilog.info(0x0000, 'testTag', 'server died'); 8169 } 8170 } 8171 class TestRemoteObject extends rpc.RemoteObject { 8172 constructor(descriptor: string) { 8173 super(descriptor); 8174 } 8175 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8176 return true; 8177 } 8178 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8179 return true; 8180 } 8181 isObjectDead(): boolean { 8182 return false; 8183 } 8184 } 8185 let remoteObject = new TestRemoteObject("aaa"); 8186 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8187 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8188 ``` 8189 8190### resetCallingIdentity 8191 8192static resetCallingIdentity(): string 8193 8194Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication. 8195 8196**System capability**: SystemCapability.Communication.IPC.Core 8197 8198**Return value** 8199 8200 | Type | Description | 8201 | ------ | ------------------------------------ | 8202 | string | String containing the UID and PID of the remote user.| 8203 8204**Example** 8205 8206 ```ts 8207 import hilog from '@ohos.hilog'; 8208 8209 class Stub extends rpc.RemoteObject { 8210 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8211 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8212 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8213 return true; 8214 } 8215 } 8216 ``` 8217 8218### restoreCallingIdentity<sup>9+</sup> 8219 8220static restoreCallingIdentity(identity: string): void 8221 8222Restores the UID and PID of the remote user. This API is a static method. It is called after **resetCallingIdentity**, which returns the UID and PID of the remote user. 8223 8224**System capability**: SystemCapability.Communication.IPC.Core 8225 8226**Parameters** 8227 8228 | Name | Type | Mandatory| Description | 8229 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8230 | identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 8231 8232**Example** 8233 8234 ```ts 8235 import hilog from '@ohos.hilog'; 8236 8237 class Stub extends rpc.RemoteObject { 8238 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8239 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8240 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8241 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8242 return true; 8243 } 8244 } 8245 ``` 8246 8247### setCallingIdentity<sup>(deprecated)</sup> 8248 8249>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [restoreCallingIdentity](#restorecallingidentity9). 8250 8251static setCallingIdentity(identity: string): boolean 8252 8253Sets the UID and PID to that of the remote user. This API is a static method. It is called after **resetCallingIdentity**, which returns the UID and PID of the remote user. 8254 8255**System capability**: SystemCapability.Communication.IPC.Core 8256 8257**Parameters** 8258 8259 | Name | Type | Mandatory| Description | 8260 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8261| identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**. | 8262 8263**Return value** 8264 8265 | Type | Description | 8266 | ------- | ---------------------------------| 8267 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8268 8269**Example** 8270 8271 ```ts 8272 import hilog from '@ohos.hilog'; 8273 8274 class Stub extends rpc.RemoteObject { 8275 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8276 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8277 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8278 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8279 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8280 return true; 8281 } 8282 } 8283 ``` 8284 8285## RemoteObject 8286 8287Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 8288 8289### constructor 8290 8291constructor(descriptor: string) 8292 8293A constructor used to create a **RemoteObject** object. 8294 8295**System capability**: SystemCapability.Communication.IPC.Core 8296 8297**Parameters** 8298 8299 | Name | Type | Mandatory| Description | 8300 | ---------- | ------ | ---- | ------------ | 8301 | descriptor | string | Yes | Interface descriptor.| 8302 8303### sendRequest<sup>(deprecated)</sup> 8304 8305>**NOTE**<br>This API is no longer maintained since API version 8. You are advised to use [sendRequest](#sendrequest8deprecated-4). 8306 8307sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8308 8309Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 8310 8311**System capability**: SystemCapability.Communication.IPC.Core 8312 8313**Parameters** 8314 8315 | Name | Type | Mandatory| Description | 8316 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8317 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8318 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8319 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8320 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8321 8322**Return value** 8323 8324 | Type | Description | 8325 | ------- | -------------------------------- | 8326 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 8327 8328**Example** 8329 8330 ```ts 8331 import hilog from '@ohos.hilog'; 8332 8333 class MyDeathRecipient implements rpc.DeathRecipient { 8334 onRemoteDied() { 8335 hilog.info(0x0000, 'testTag', 'server died'); 8336 } 8337 } 8338 class TestRemoteObject extends rpc.RemoteObject { 8339 constructor(descriptor: string) { 8340 super(descriptor); 8341 } 8342 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8343 return true; 8344 } 8345 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8346 return true; 8347 } 8348 isObjectDead(): boolean { 8349 return false; 8350 } 8351 } 8352 let testRemoteObject = new TestRemoteObject("testObject"); 8353 let option = new rpc.MessageOption(); 8354 let data = rpc.MessageParcel.create(); 8355 let reply = rpc.MessageParcel.create(); 8356 data.writeInt(1); 8357 data.writeString("hello"); 8358 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8359 if (ret) { 8360 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8361 let msg = reply.readString(); 8362 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8363 } else { 8364 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8365 } 8366 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8367 data.reclaim(); 8368 reply.reclaim(); 8369 ``` 8370 8371### sendMessageRequest<sup>9+</sup> 8372 8373sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8374 8375Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 8376 8377**System capability**: SystemCapability.Communication.IPC.Core 8378 8379**Parameters** 8380 8381 | Name | Type | Mandatory| Description | 8382 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8383 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8384 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8385 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8386 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8387 8388**Return value** 8389 8390| Type | Description | 8391| ----------------------------------------------- | ----------------------------------------- | 8392| Promise<[RequestResult](#requestresult9)> | Promise used to return a **RequestResult** instance.| 8393 8394**Example** 8395 8396 ```ts 8397 import hilog from '@ohos.hilog'; 8398 8399 class TestRemoteObject extends rpc.RemoteObject { 8400 constructor(descriptor: string) { 8401 super(descriptor); 8402 } 8403 } 8404 let testRemoteObject = new TestRemoteObject("testObject"); 8405 let option = new rpc.MessageOption(); 8406 let data = rpc.MessageSequence.create(); 8407 let reply = rpc.MessageSequence.create(); 8408 data.writeInt(1); 8409 data.writeString("hello"); 8410 testRemoteObject.sendMessageRequest(1, data, reply, option) 8411 .then((result: rpc.RequestResult) => { 8412 if (result.errCode === 0) { 8413 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8414 let num = result.reply.readInt(); 8415 let msg = result.reply.readString(); 8416 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8417 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8418 } else { 8419 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8420 } 8421 }).catch((e: Error) => { 8422 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8423 }).finally (() => { 8424 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8425 data.reclaim(); 8426 reply.reclaim(); 8427 }); 8428 ``` 8429 8430### sendRequest<sup>8+(deprecated)</sup> 8431 8432>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9-4). 8433 8434sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8435 8436Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a promise will be fulfilled when the response to **sendRequest** is returned, and the reply message contains the returned information. 8437 8438**System capability**: SystemCapability.Communication.IPC.Core 8439 8440**Parameters** 8441 8442 | Name | Type | Mandatory| Description | 8443 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8444 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8445 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8446 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8447 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8448 8449**Return value** 8450 8451| Type | Description | 8452| ------------------------------------------------------------ | --------------------------------------------- | 8453| Promise<[SendRequestResult](#sendrequestresult8deprecated)> | Promise used to return the **sendRequestResult** object.| 8454 8455**Example** 8456 8457 ```ts 8458 import hilog from '@ohos.hilog'; 8459 8460 class MyDeathRecipient implements rpc.DeathRecipient { 8461 onRemoteDied() { 8462 hilog.info(0x0000, 'testTag', 'server died'); 8463 } 8464 } 8465 class TestRemoteObject extends rpc.RemoteObject { 8466 constructor(descriptor: string) { 8467 super(descriptor); 8468 } 8469 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8470 return true; 8471 } 8472 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8473 return true; 8474 } 8475 isObjectDead(): boolean { 8476 return false; 8477 } 8478 } 8479 let testRemoteObject = new TestRemoteObject("testObject"); 8480 let option = new rpc.MessageOption(); 8481 let data = rpc.MessageParcel.create(); 8482 let reply = rpc.MessageParcel.create(); 8483 data.writeInt(1); 8484 data.writeString("hello"); 8485 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8486 let b = a as Promise<rpc.SendRequestResult>; 8487 b.then((result: rpc.SendRequestResult) => { 8488 if (result.errCode === 0) { 8489 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8490 let num = result.reply.readInt(); 8491 let msg = result.reply.readString(); 8492 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8493 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8494 } else { 8495 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8496 } 8497 }).catch((e: Error) => { 8498 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 8499 }).finally (() => { 8500 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8501 data.reclaim(); 8502 reply.reclaim(); 8503 }); 8504 ``` 8505 8506### sendMessageRequest<sup>9+</sup> 8507 8508sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8509 8510Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to **sendMessageRequest** is returned, and the reply message contains the returned information. 8511 8512**System capability**: SystemCapability.Communication.IPC.Core 8513 8514**Parameters** 8515 8516| Name | Type | Mandatory| Description | 8517| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8518| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8519| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8520| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8521| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8522| AsyncCallback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 8523 8524**Example** 8525 8526 ```ts 8527 import hilog from '@ohos.hilog'; 8528 import { BusinessError } from '@ohos.base'; 8529 8530 class TestRemoteObject extends rpc.RemoteObject { 8531 constructor(descriptor: string) { 8532 super(descriptor); 8533 } 8534 } 8535 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 8536 if (result.errCode === 0) { 8537 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8538 let num = result.reply.readInt(); 8539 let msg = result.reply.readString(); 8540 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8541 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8542 } else { 8543 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8544 } 8545 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8546 result.data.reclaim(); 8547 result.reply.reclaim(); 8548 } 8549 let testRemoteObject = new TestRemoteObject("testObject"); 8550 let option = new rpc.MessageOption(); 8551 let data = rpc.MessageSequence.create(); 8552 let reply = rpc.MessageSequence.create(); 8553 data.writeInt(1); 8554 data.writeString("hello"); 8555 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 8556 ``` 8557 8558### sendRequest<sup>8+(deprecated)</sup> 8559 8560>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9-5). 8561 8562sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8563 8564Sends a **MessageParcel** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information. 8565 8566**System capability**: SystemCapability.Communication.IPC.Core 8567 8568**Parameters** 8569 8570| Name | Type | Mandatory| Description | 8571| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8572| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8573| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8574| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8575| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8576| AsyncCallback | AsyncCallback<[SendRequestResult](#sendrequestresult8deprecated)> | Yes | Callback for receiving the sending result. | 8577 8578**Example** 8579 8580 ```ts 8581 import hilog from '@ohos.hilog'; 8582 import { BusinessError } from '@ohos.base'; 8583 8584 class MyDeathRecipient implements rpc.DeathRecipient { 8585 onRemoteDied() { 8586 hilog.info(0x0000, 'testTag', 'server died'); 8587 } 8588 } 8589 class TestRemoteObject extends rpc.RemoteObject { 8590 constructor(descriptor: string) { 8591 super(descriptor); 8592 } 8593 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8594 return true; 8595 } 8596 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8597 return true; 8598 } 8599 isObjectDead(): boolean { 8600 return false; 8601 } 8602 } 8603 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 8604 if (result.errCode === 0) { 8605 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8606 let num = result.reply.readInt(); 8607 let msg = result.reply.readString(); 8608 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8609 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8610 } else { 8611 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8612 } 8613 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8614 result.data.reclaim(); 8615 result.reply.reclaim(); 8616 } 8617 let testRemoteObject = new TestRemoteObject("testObject"); 8618 let option = new rpc.MessageOption(); 8619 let data = rpc.MessageParcel.create(); 8620 let reply = rpc.MessageParcel.create(); 8621 data.writeInt(1); 8622 data.writeString("hello"); 8623 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 8624 ``` 8625 8626### onRemoteMessageRequest<sup>9+</sup> 8627 8628onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 8629 8630> **NOTE** 8631> 8632>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 8633>* If both **onRemoteRequest()** and **onRemoteMessageRequest()** are overloaded, only the onRemoteMessageRequest() takes effect. 8634 8635Called to return a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 8636 8637**System capability**: SystemCapability.Communication.IPC.Core 8638 8639**Parameters** 8640 8641 | Name| Type | Mandatory| Description | 8642 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 8643 | code | number | Yes | Service request code sent by the remote end. | 8644 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 8645 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 8646 | option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8647 8648**Return value** 8649 8650 | Type | Description | 8651 | ----------------- | ----------------------------------------------------------------------------------------------- | 8652 | boolean | Returns a Boolean value if the request is processed synchronously in **onRemoteMessageRequest**. The value **true** means the operation is successful; the value **false** means the opposite.| 8653 | Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 8654 8655**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 8656 8657 ```ts 8658 import hilog from '@ohos.hilog'; 8659 8660 class TestRemoteObject extends rpc.RemoteObject { 8661 constructor(descriptor: string) { 8662 super(descriptor); 8663 } 8664 8665 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8666 if (code === 1) { 8667 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 8668 return true; 8669 } else { 8670 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8671 return false; 8672 } 8673 } 8674 } 8675 ``` 8676 8677 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 8678 8679 ```ts 8680 import hilog from '@ohos.hilog'; 8681 8682 class TestRemoteObject extends rpc.RemoteObject { 8683 constructor(descriptor: string) { 8684 super(descriptor); 8685 } 8686 8687 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 8688 if (code === 1) { 8689 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 8690 } else { 8691 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8692 return false; 8693 } 8694 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 8695 setTimeout(resolve, 100); 8696 }) 8697 return true; 8698 } 8699 } 8700 ``` 8701 8702**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 8703 8704 ```ts 8705 import hilog from '@ohos.hilog'; 8706 8707 class TestRemoteObject extends rpc.RemoteObject { 8708 constructor(descriptor: string) { 8709 super(descriptor); 8710 } 8711 8712 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 8713 if (code === 1) { 8714 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 8715 return true; 8716 } else { 8717 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8718 return false; 8719 } 8720 } 8721 // Only onRemoteMessageRequest is executed. 8722 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8723 if (code === 1) { 8724 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 8725 } else { 8726 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8727 return false; 8728 } 8729 return true; 8730 } 8731 } 8732 ``` 8733 8734 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 8735 8736 ```ts 8737 import hilog from '@ohos.hilog'; 8738 class TestRemoteObject extends rpc.RemoteObject { 8739 constructor(descriptor: string) { 8740 super(descriptor); 8741 } 8742 8743 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 8744 if (code === 1) { 8745 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 8746 return true; 8747 } else { 8748 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8749 return false; 8750 } 8751 } 8752 // Only onRemoteMessageRequest is executed. 8753 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 8754 if (code === 1) { 8755 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 8756 } else { 8757 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8758 return false; 8759 } 8760 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 8761 setTimeout(resolve, 100); 8762 }) 8763 return true; 8764 } 8765 } 8766 ``` 8767 8768### onRemoteRequest<sup>(deprecated)</sup> 8769 8770>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [onRemoteMessageRequest](#onremotemessagerequest9). 8771 8772onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8773 8774Called to return a response to **sendRequest()**. The server processes the request and returns a response in this function. 8775 8776**System capability**: SystemCapability.Communication.IPC.Core 8777 8778**Parameters** 8779 8780 | Name| Type | Mandatory| Description | 8781 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 8782 | code | number | Yes | Service request code sent by the remote end. | 8783 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 8784 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 8785 | option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8786 8787**Return value** 8788 8789 | Type | Description | 8790 | ------- | -------------------------------- | 8791 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8792 8793**Example** 8794 8795 ```ts 8796 import hilog from '@ohos.hilog'; 8797 8798 class MyDeathRecipient implements rpc.DeathRecipient { 8799 onRemoteDied() { 8800 hilog.info(0x0000, 'testTag', 'server died'); 8801 } 8802 } 8803 class TestRemoteObject extends rpc.RemoteObject { 8804 constructor(descriptor: string) { 8805 super(descriptor); 8806 } 8807 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8808 return true; 8809 } 8810 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8811 return true; 8812 } 8813 isObjectDead(): boolean { 8814 return false; 8815 } 8816 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 8817 if (code === 1) { 8818 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 8819 return true; 8820 } else { 8821 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8822 return false; 8823 } 8824 } 8825 } 8826 ``` 8827 8828### getCallingUid 8829 8830getCallingUid(): number 8831 8832Obtains the UID of the remote process. 8833 8834**System capability**: SystemCapability.Communication.IPC.Core 8835 8836**Return value** 8837 | Type | Description | 8838 | ------ | ----------------------- | 8839 | number | UID of the remote process obtained.| 8840 8841**Example** 8842 8843 ```ts 8844 import hilog from '@ohos.hilog'; 8845 8846 class TestRemoteObject extends rpc.RemoteObject { 8847 constructor(descriptor: string) { 8848 super(descriptor); 8849 } 8850 } 8851 let testRemoteObject = new TestRemoteObject("testObject"); 8852 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 8853 ``` 8854 8855### getCallingPid 8856 8857getCallingPid(): number 8858 8859Obtains the PID of the remote process. 8860 8861**System capability**: SystemCapability.Communication.IPC.Core 8862 8863**Return value** 8864 8865 | Type | Description | 8866 | ------ | ----------------------- | 8867 | number | PID of the remote process obtained.| 8868 8869**Example** 8870 8871 ```ts 8872 import hilog from '@ohos.hilog'; 8873 8874 class TestRemoteObject extends rpc.RemoteObject { 8875 constructor(descriptor: string) { 8876 super(descriptor); 8877 } 8878 } 8879 let testRemoteObject = new TestRemoteObject("testObject"); 8880 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 8881 ``` 8882 8883### getLocalInterface<sup>9+</sup> 8884 8885getLocalInterface(descriptor: string): IRemoteBroker 8886 8887Obtains the interface descriptor. 8888 8889**System capability**: SystemCapability.Communication.IPC.Core 8890 8891**Parameters** 8892 8893 | Name | Type | Mandatory| Description | 8894 | ---------- | ------ | ---- | -------------------- | 8895 | descriptor | string | Yes | Interface descriptor.| 8896 8897**Return value** 8898 8899 | Type | Description | 8900 | ------------- | --------------------------------------------- | 8901 | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 8902 8903**Example** 8904 8905 ```ts 8906 import hilog from '@ohos.hilog'; 8907 import { BusinessError } from '@ohos.base'; 8908 8909 class MyDeathRecipient implements rpc.DeathRecipient { 8910 onRemoteDied() { 8911 hilog.info(0x0000, 'testTag', 'server died'); 8912 } 8913 } 8914 class TestRemoteObject extends rpc.RemoteObject { 8915 constructor(descriptor: string) { 8916 super(descriptor); 8917 this.modifyLocalInterface(this, descriptor); 8918 } 8919 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8920 // Implement the method logic based on service requirements. 8921 } 8922 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8923 // Implement the method logic based on service requirements. 8924 } 8925 isObjectDead(): boolean { 8926 return false; 8927 } 8928 asObject(): rpc.IRemoteObject { 8929 return this; 8930 } 8931 } 8932 let testRemoteObject = new TestRemoteObject("testObject"); 8933 try { 8934 testRemoteObject.getLocalInterface("testObject"); 8935 } catch(error) { 8936 let e: BusinessError = error as BusinessError; 8937 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 8938 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 8939 } 8940 ``` 8941 8942### queryLocalInterface<sup>(deprecated)</sup> 8943 8944>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9-2). 8945 8946queryLocalInterface(descriptor: string): IRemoteBroker 8947 8948Checks whether the remote object corresponding to the specified interface token exists. 8949 8950**System capability**: SystemCapability.Communication.IPC.Core 8951 8952**Parameters** 8953 8954 | Name | Type | Mandatory| Description | 8955 | ---------- | ------ | ---- | ---------------------- | 8956 | descriptor | string | Yes | Interface descriptor.| 8957 8958**Return value** 8959 8960 | Type | Description | 8961 | ------------- | ------------------------------------------------------------------ | 8962 | IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.| 8963 8964**Example** 8965 8966 ```ts 8967 import hilog from '@ohos.hilog'; 8968 8969 class MyDeathRecipient implements rpc.DeathRecipient { 8970 onRemoteDied() { 8971 hilog.info(0x0000, 'testTag', 'server died'); 8972 } 8973 } 8974 class TestRemoteObject extends rpc.RemoteObject { 8975 constructor(descriptor: string) { 8976 super(descriptor); 8977 this.attachLocalInterface(this, descriptor); 8978 } 8979 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8980 return true; 8981 } 8982 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8983 return true; 8984 } 8985 isObjectDead(): boolean { 8986 return false; 8987 } 8988 asObject(): rpc.IRemoteObject { 8989 return this; 8990 } 8991 } 8992 let testRemoteObject = new TestRemoteObject("testObject"); 8993 testRemoteObject.queryLocalInterface("testObject"); 8994 ``` 8995 8996### getDescriptor<sup>9+</sup> 8997 8998getDescriptor(): string 8999 9000Obtains the interface descriptor of this object. The interface descriptor is a string. 9001 9002**System capability**: SystemCapability.Communication.IPC.Core 9003 9004**Return value** 9005 9006 | Type | Description | 9007 | ------ | ---------------- | 9008 | string | Interface descriptor obtained.| 9009 9010**Error codes** 9011 9012For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9013 9014 | ID| Error Message| 9015 | -------- | -------- | 9016 | 1900008 | proxy or remote object is invalid | 9017 9018**Example** 9019 9020 ```ts 9021 import hilog from '@ohos.hilog'; 9022 import { BusinessError } from '@ohos.base'; 9023 9024 class MyDeathRecipient implements rpc.DeathRecipient { 9025 onRemoteDied() { 9026 hilog.info(0x0000, 'testTag', 'server died'); 9027 } 9028 } 9029 class TestRemoteObject extends rpc.RemoteObject { 9030 constructor(descriptor: string) { 9031 super(descriptor); 9032 } 9033 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9034 // Implement the method logic based on service requirements. 9035 } 9036 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9037 // Implement the method logic based on service requirements. 9038 } 9039 isObjectDead(): boolean { 9040 return false; 9041 } 9042 } 9043 let testRemoteObject = new TestRemoteObject("testObject"); 9044 try { 9045 let descriptor = testRemoteObject.getDescriptor(); 9046 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9047 } catch(error) { 9048 let e: BusinessError = error as BusinessError; 9049 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9050 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9051 } 9052 ``` 9053 9054### getInterfaceDescriptor<sup>(deprecated)</sup> 9055 9056>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9-2). 9057 9058getInterfaceDescriptor(): string 9059 9060Obtains the interface descriptor. 9061 9062**System capability**: SystemCapability.Communication.IPC.Core 9063 9064**Return value** 9065 9066 | Type | Description | 9067 | ------ | ---------------- | 9068 | string | Interface descriptor obtained.| 9069 9070**Example** 9071 9072 ```ts 9073 import hilog from '@ohos.hilog'; 9074 9075 class MyDeathRecipient implements rpc.DeathRecipient { 9076 onRemoteDied() { 9077 hilog.info(0x0000, 'testTag', 'server died'); 9078 } 9079 } 9080 class TestRemoteObject extends rpc.RemoteObject { 9081 constructor(descriptor: string) { 9082 super(descriptor); 9083 } 9084 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9085 return true; 9086 } 9087 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9088 return true; 9089 } 9090 isObjectDead(): boolean { 9091 return false; 9092 } 9093 } 9094 let testRemoteObject = new TestRemoteObject("testObject"); 9095 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9096 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9097 ``` 9098 9099### modifyLocalInterface<sup>9+</sup> 9100 9101modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9102 9103Binds an interface descriptor to an **IRemoteBroker** object. 9104 9105**System capability**: SystemCapability.Communication.IPC.Core 9106 9107**Parameters** 9108 9109| Name | Type | Mandatory| Description | 9110| -------------- | ------------------------------- | ---- | ------------------------------------- | 9111| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9112| descriptor | string | Yes | Interface descriptor.| 9113 9114**Example** 9115 9116 ```ts 9117 import hilog from '@ohos.hilog'; 9118 import { BusinessError } from '@ohos.base'; 9119 9120 class MyDeathRecipient implements rpc.DeathRecipient { 9121 onRemoteDied() { 9122 hilog.info(0x0000, 'testTag', 'server died'); 9123 } 9124 } 9125 class TestRemoteObject extends rpc.RemoteObject { 9126 constructor(descriptor: string) { 9127 super(descriptor); 9128 try { 9129 this.modifyLocalInterface(this, descriptor); 9130 } catch(error) { 9131 let e: BusinessError = error as BusinessError; 9132 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9133 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9134 } 9135 } 9136 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9137 // Implement the method logic based on service requirements. 9138 } 9139 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9140 // Implement the method logic based on service requirements. 9141 } 9142 isObjectDead(): boolean { 9143 return false; 9144 } 9145 asObject(): rpc.IRemoteObject { 9146 return this; 9147 } 9148 } 9149 let testRemoteObject = new TestRemoteObject("testObject"); 9150 ``` 9151 9152### attachLocalInterface<sup>(deprecated)</sup> 9153 9154>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [modifyLocalInterface](#modifylocalinterface9). 9155 9156attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9157 9158Binds an interface descriptor to an **IRemoteBroker** object. 9159 9160**System capability**: SystemCapability.Communication.IPC.Core 9161 9162**Parameters** 9163 9164| Name | Type | Mandatory| Description | 9165| -------------- | ------------------------------- | ---- | ------------------------------------- | 9166| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9167| descriptor | string | Yes | Interface descriptor.| 9168 9169**Example** 9170 9171 ```ts 9172 import hilog from '@ohos.hilog'; 9173 9174 class MyDeathRecipient implements rpc.DeathRecipient { 9175 onRemoteDied() { 9176 hilog.info(0x0000, 'testTag', 'server died'); 9177 } 9178 } 9179 class TestRemoteObject extends rpc.RemoteObject { 9180 constructor(descriptor: string) { 9181 super(descriptor); 9182 this.attachLocalInterface(this, descriptor); 9183 } 9184 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9185 return true; 9186 } 9187 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9188 return true; 9189 } 9190 isObjectDead(): boolean { 9191 return false; 9192 } 9193 asObject(): rpc.IRemoteObject { 9194 return this; 9195 } 9196 } 9197 let testRemoteObject = new TestRemoteObject("testObject"); 9198 ``` 9199 9200 9201## Ashmem<sup>8+</sup> 9202 9203Provides methods related to anonymous shared memory objects, including creating, closing, mapping, and unmapping an **Ashmem** object, reading data from and writing data to an **Ashmem** object, obtaining the **Ashmem** size, and setting **Ashmem** protection. 9204 9205**System capability**: SystemCapability.Communication.IPC.Core 9206 9207The table below describes the protection types of the mapped memory. 9208 9209 | Name | Value | Description | 9210 | ---------- | --- | ------------------ | 9211 | PROT_EXEC | 4 | The mapped memory is executable. | 9212 | PROT_NONE | 0 | The mapped memory is inaccessible.| 9213 | PROT_READ | 1 | The mapped memory is readable. | 9214 | PROT_WRITE | 2 | The mapped memory is writeable. | 9215 9216### create<sup>9+</sup> 9217 9218static create(name: string, size: number): Ashmem 9219 9220Creates an **Ashmem** object with the specified name and size. This API is a static method. 9221 9222**System capability**: SystemCapability.Communication.IPC.Core 9223 9224**Parameters** 9225 9226 | Name| Type | Mandatory| Description | 9227 | ------ | ------ | ---- | ---------------------------- | 9228 | name | string | Yes | Name of the **Ashmem** object to create. | 9229 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9230 9231**Return value** 9232 9233| Type | Description | 9234| ------------------ | ---------------------------------------------- | 9235| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9236 9237**Example** 9238 9239 ```ts 9240 import hilog from '@ohos.hilog'; 9241 import { BusinessError } from '@ohos.base'; 9242 9243 let ashmem: rpc.Ashmem | undefined = undefined; 9244 try { 9245 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9246 let size = ashmem.getAshmemSize(); 9247 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9248 } catch(error) { 9249 let e: BusinessError = error as BusinessError; 9250 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9251 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9252 } 9253 ``` 9254 9255### createAshmem<sup>8+(deprecated)</sup> 9256 9257>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 9258 9259static createAshmem(name: string, size: number): Ashmem 9260 9261Creates an **Ashmem** object with the specified name and size. This API is a static method. 9262 9263**System capability**: SystemCapability.Communication.IPC.Core 9264 9265**Parameters** 9266 9267 | Name| Type | Mandatory| Description | 9268 | ------ | ------ | ---- | ---------------------------- | 9269 | name | string | Yes | Name of the **Ashmem** object to create. | 9270 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9271 9272**Return value** 9273 9274| Type | Description | 9275| ------------------ | ---------------------------------------------- | 9276| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9277 9278**Example** 9279 9280 ```ts 9281 import hilog from '@ohos.hilog'; 9282 9283 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9284 let size = ashmem.getAshmemSize(); 9285 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9286 ``` 9287 9288### create<sup>9+</sup> 9289 9290static create(ashmem: Ashmem): Ashmem 9291 9292Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9293 9294**System capability**: SystemCapability.Communication.IPC.Core 9295 9296**Parameters** 9297 9298| Name| Type | Mandatory| Description | 9299| ------ | ------------------ | ---- | -------------------- | 9300| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9301 9302**Return value** 9303 9304| Type | Description | 9305| ------------------ | ---------------------- | 9306| [Ashmem](#ashmem8) | **Ashmem** object created.| 9307 9308**Example** 9309 9310 ```ts 9311 import hilog from '@ohos.hilog'; 9312 import { BusinessError } from '@ohos.base'; 9313 9314 try { 9315 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9316 let ashmem2 = rpc.Ashmem.create(ashmem); 9317 let size = ashmem2.getAshmemSize(); 9318 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9319 } catch(error) { 9320 let e: BusinessError = error as BusinessError; 9321 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9322 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9323 } 9324 ``` 9325 9326### createAshmemFromExisting<sup>8+(deprecated)</sup> 9327 9328>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [create](#create9-1). 9329 9330static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9331 9332Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9333 9334**System capability**: SystemCapability.Communication.IPC.Core 9335 9336**Parameters** 9337 9338| Name| Type | Mandatory| Description | 9339| ------ | ------------------ | ---- | -------------------- | 9340| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9341 9342**Return value** 9343 9344| Type | Description | 9345| ------------------ | ---------------------- | 9346| [Ashmem](#ashmem8) | **Ashmem** object created.| 9347 9348**Example** 9349 9350 ```ts 9351 import hilog from '@ohos.hilog'; 9352 9353 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9354 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9355 let size = ashmem2.getAshmemSize(); 9356 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9357 ``` 9358 9359### closeAshmem<sup>8+</sup> 9360 9361closeAshmem(): void 9362 9363Closes this **Ashmem** object. 9364 9365**System capability**: SystemCapability.Communication.IPC.Core 9366 9367**Example** 9368 9369 ```ts 9370 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9371 ashmem.closeAshmem(); 9372 ``` 9373 9374### unmapAshmem<sup>8+</sup> 9375 9376unmapAshmem(): void 9377 9378Deletes the mappings for the specified address range of this **Ashmem** object. 9379 9380**System capability**: SystemCapability.Communication.IPC.Core 9381 9382**Example** 9383 9384 ```ts 9385 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9386 ashmem.unmapAshmem(); 9387 ``` 9388 9389### getAshmemSize<sup>8+</sup> 9390 9391getAshmemSize(): number 9392 9393Obtains the memory size of this **Ashmem** object. 9394 9395**System capability**: SystemCapability.Communication.IPC.Core 9396 9397**Return value** 9398 9399 | Type | Description | 9400 | ------ | -------------------------- | 9401 | number | **Ashmem** size obtained.| 9402 9403**Example** 9404 9405 ```ts 9406 import hilog from '@ohos.hilog'; 9407 9408 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9409 let size = ashmem.getAshmemSize(); 9410 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9411 ``` 9412 9413### mapTypedAshmem<sup>9+</sup> 9414 9415mapTypedAshmem(mapType: number): void 9416 9417Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9418 9419**System capability**: SystemCapability.Communication.IPC.Core 9420 9421**Parameters** 9422 9423 | Name | Type | Mandatory| Description | 9424 | ------- | ------ | ---- | ------------------------------ | 9425 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9426 9427**Error codes** 9428 9429For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9430 9431 | ID| Error Message| 9432 | -------- | -------- | 9433 | 1900001 | call mmap function failed | 9434 9435**Example** 9436 9437 ```ts 9438 import hilog from '@ohos.hilog'; 9439 import { BusinessError } from '@ohos.base'; 9440 9441 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9442 try { 9443 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9444 } catch(error) { 9445 let e: BusinessError = error as BusinessError; 9446 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9447 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9448 } 9449 ``` 9450 9451### mapAshmem<sup>8+(deprecated)</sup> 9452 9453>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [mapTypedAshmem](#maptypedashmem9). 9454 9455mapAshmem(mapType: number): boolean 9456 9457Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9458 9459**System capability**: SystemCapability.Communication.IPC.Core 9460 9461**Parameters** 9462 9463 | Name | Type | Mandatory| Description | 9464 | ------- | ------ | ---- | ------------------------------ | 9465 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9466 9467**Return value** 9468 9469 | Type | Description | 9470 | ------- | -------------------------------- | 9471 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9472 9473**Example** 9474 9475 ```ts 9476 import hilog from '@ohos.hilog'; 9477 9478 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9479 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9480 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9481 ``` 9482 9483### mapReadWriteAshmem<sup>9+</sup> 9484 9485mapReadWriteAshmem(): void 9486 9487Maps the shared file to the readable and writable virtual address space of the process. 9488 9489**System capability**: SystemCapability.Communication.IPC.Core 9490 9491**Error codes** 9492 9493For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9494 9495 | ID| Error Message| 9496 | -------- | -------- | 9497 | 1900001 | call mmap function failed | 9498 9499**Example** 9500 9501 ```ts 9502 import hilog from '@ohos.hilog'; 9503 import { BusinessError } from '@ohos.base'; 9504 9505 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9506 try { 9507 ashmem.mapReadWriteAshmem(); 9508 } catch(error) { 9509 let e: BusinessError = error as BusinessError; 9510 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9511 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9512 } 9513 ``` 9514 9515### mapReadAndWriteAshmem<sup>8+(deprecated)</sup> 9516 9517>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [mapReadWriteAshmem](#mapreadwriteashmem9). 9518 9519mapReadAndWriteAshmem(): boolean 9520 9521Maps the shared file to the readable and writable virtual address space of the process. 9522 9523**System capability**: SystemCapability.Communication.IPC.Core 9524 9525**Return value** 9526 9527 | Type | Description | 9528 | ------- | -------------------------------- | 9529 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9530 9531**Example** 9532 9533 ```ts 9534 import hilog from '@ohos.hilog'; 9535 9536 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9537 let mapResult = ashmem.mapReadAndWriteAshmem(); 9538 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 9539 ``` 9540 9541### mapReadonlyAshmem<sup>9+</sup> 9542 9543mapReadonlyAshmem(): void 9544 9545Maps the shared file to the read-only virtual address space of the process. 9546 9547**System capability**: SystemCapability.Communication.IPC.Core 9548 9549**Error codes** 9550 9551For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9552 9553 | ID| Error Message| 9554 | -------- | -------- | 9555 | 1900001 | call mmap function failed | 9556 9557**Example** 9558 9559 ```ts 9560 import hilog from '@ohos.hilog'; 9561 import { BusinessError } from '@ohos.base'; 9562 9563 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9564 try { 9565 ashmem.mapReadonlyAshmem(); 9566 } catch(error) { 9567 let e: BusinessError = error as BusinessError; 9568 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9569 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9570 } 9571 ``` 9572 9573### mapReadOnlyAshmem<sup>8+(deprecated)</sup> 9574 9575>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [mapReadonlyAshmem](#mapreadonlyashmem9). 9576 9577mapReadOnlyAshmem(): boolean 9578 9579Maps the shared file to the read-only virtual address space of the process. 9580 9581**System capability**: SystemCapability.Communication.IPC.Core 9582 9583**Return value** 9584 9585 | Type | Description | 9586 | ------- | -------------------------------- | 9587 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9588 9589**Example** 9590 9591 ```ts 9592 import hilog from '@ohos.hilog'; 9593 9594 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9595 let mapResult = ashmem.mapReadOnlyAshmem(); 9596 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 9597 ``` 9598 9599### setProtectionType<sup>9+</sup> 9600 9601setProtectionType(protectionType: number): void 9602 9603Sets the protection level of the memory region to which the shared file is mapped. 9604 9605**System capability**: SystemCapability.Communication.IPC.Core 9606 9607**Parameters** 9608 9609 | Name | Type | Mandatory| Description | 9610 | -------------- | ------ | ---- | ------------------ | 9611 | protectionType | number | Yes | Protection type to set.| 9612 9613**Error codes** 9614 9615For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9616 9617 | ID| Error Message| 9618 | -------- | -------- | 9619 | 1900002 | call os ioctl function failed | 9620 9621**Example** 9622 9623 ```ts 9624 import hilog from '@ohos.hilog'; 9625 import { BusinessError } from '@ohos.base'; 9626 9627 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9628 try { 9629 ashmem.setProtection(ashmem.PROT_READ); 9630 } catch(error) { 9631 let e: BusinessError = error as BusinessError; 9632 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 9633 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 9634 } 9635 ``` 9636 9637### setProtection<sup>8+(deprecated)</sup> 9638 9639>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [setProtectionType](#setprotectiontype9). 9640 9641setProtection(protectionType: number): boolean 9642 9643Sets the protection level of the memory region to which the shared file is mapped. 9644 9645**System capability**: SystemCapability.Communication.IPC.Core 9646 9647**Parameters** 9648 9649 | Name | Type | Mandatory| Description | 9650 | -------------- | ------ | ---- | ------------------ | 9651 | protectionType | number | Yes | Protection type to set.| 9652 9653**Return value** 9654 9655 | Type | Description | 9656 | ------- | -------------------------------- | 9657 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 9658 9659**Example** 9660 9661 ```ts 9662 import hilog from '@ohos.hilog'; 9663 9664 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9665 let result = ashmem.setProtection(ashmem.PROT_READ); 9666 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 9667 ``` 9668 9669### writeAshmem<sup>9+</sup> 9670 9671writeAshmem(buf: number[], size: number, offset: number): void 9672 9673Writes data to the shared file associated with this **Ashmem** object. 9674 9675**System capability**: SystemCapability.Communication.IPC.Core 9676 9677**Parameters** 9678 9679 | Name| Type | Mandatory| Description | 9680 | ------ | -------- | ---- | -------------------------------------------------- | 9681 | buf | number[] | Yes | Data to write. | 9682 | size | number | Yes | Size of the data to write. | 9683 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 9684 9685**Error codes** 9686 9687For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9688 9689 | ID| Error Message| 9690 | -------- | -------- | 9691 | 1900003 | write to ashmem failed | 9692 9693**Example** 9694 9695 ```ts 9696 import hilog from '@ohos.hilog'; 9697 import { BusinessError } from '@ohos.base'; 9698 9699 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9700 ashmem.mapReadWriteAshmem(); 9701 let ByteArrayVar = [1, 2, 3, 4, 5]; 9702 try { 9703 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9704 } catch(error) { 9705 let e: BusinessError = error as BusinessError; 9706 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 9707 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 9708 } 9709 ``` 9710 9711### writeToAshmem<sup>8+(deprecated)</sup> 9712 9713>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [writeAshmem](#writeashmem9). 9714 9715writeToAshmem(buf: number[], size: number, offset: number): boolean 9716 9717Writes data to the shared file associated with this **Ashmem** object. 9718 9719**System capability**: SystemCapability.Communication.IPC.Core 9720 9721**Parameters** 9722 9723 | Name| Type | Mandatory| Description | 9724 | ------ | -------- | ---- | -------------------------------------------------- | 9725 | buf | number[] | Yes | Data to write. | 9726 | size | number | Yes | Size of the data to write. | 9727 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 9728 9729**Return value** 9730 9731 | Type | Description | 9732 | ------- | ----------------------------------------------------------------------------- | 9733 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 9734 9735**Example** 9736 9737 ```ts 9738 import hilog from '@ohos.hilog'; 9739 9740 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9741 let mapResult = ashmem.mapReadAndWriteAshmem(); 9742 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 9743 let ByteArrayVar = [1, 2, 3, 4, 5]; 9744 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9745 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 9746 ``` 9747 9748### readAshmem<sup>9+</sup> 9749 9750readAshmem(size: number, offset: number): number[] 9751 9752Reads data from the shared file associated with this **Ashmem** object. 9753 9754**System capability**: SystemCapability.Communication.IPC.Core 9755 9756**Parameters** 9757 9758 | Name| Type | Mandatory| Description | 9759 | ------ | ------ | ---- | -------------------------------------------------- | 9760 | size | number | Yes | Size of the data to read. | 9761 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9762 9763**Return value** 9764 9765 | Type | Description | 9766 | -------- | ---------------- | 9767 | number[] | Data read.| 9768 9769**Error codes** 9770 9771For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9772 9773 | ID| Error Message| 9774 | -------- | -------- | 9775 | 1900004 | read from ashmem failed | 9776 9777**Example** 9778 9779 ```ts 9780 import hilog from '@ohos.hilog'; 9781 import { BusinessError } from '@ohos.base'; 9782 9783 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9784 ashmem.mapReadWriteAshmem(); 9785 let ByteArrayVar = [1, 2, 3, 4, 5]; 9786 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9787 try { 9788 let readResult = ashmem.readAshmem(5, 0); 9789 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 9790 } catch(error) { 9791 let e: BusinessError = error as BusinessError; 9792 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 9793 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 9794 } 9795 ``` 9796 9797### readFromAshmem<sup>8+(deprecated)</sup> 9798 9799>**NOTE**<br>This API is no longer maintained since API version 9. You are advised to use [readAshmem](#readashmem9). 9800 9801readFromAshmem(size: number, offset: number): number[] 9802 9803Reads data from the shared file associated with this **Ashmem** object. 9804 9805**System capability**: SystemCapability.Communication.IPC.Core 9806 9807**Parameters** 9808 9809 | Name| Type | Mandatory| Description | 9810 | ------ | ------ | ---- | -------------------------------------------------- | 9811 | size | number | Yes | Size of the data to read. | 9812 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9813 9814**Return value** 9815 9816 | Type | Description | 9817 | -------- | ---------------- | 9818 | number[] | Data read.| 9819 9820**Example** 9821 9822 ``` ts 9823 import hilog from '@ohos.hilog'; 9824 9825 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9826 let mapResult = ashmem.mapReadAndWriteAshmem(); 9827 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 9828 let ByteArrayVar = [1, 2, 3, 4, 5]; 9829 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9830 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 9831 let readResult = ashmem.readFromAshmem(5, 0); 9832 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 9833 ``` 9834 9835## Obtaining the Context 9836 9837**Example** 9838This example describes only one method of obtaining the context. For details about more methods, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 9839 ```ts 9840 import UIAbility from '@ohos.app.ability.UIAbility'; 9841 import Want from '@ohos.app.ability.Want'; 9842 import hilog from '@ohos.hilog'; 9843 import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 9844 import window from '@ohos.window'; 9845 9846 export default class MainAbility extends UIAbility { 9847 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 9848 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onCreate'); 9849 let context = this.context; 9850 } 9851 onDestroy() { 9852 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onDestroy'); 9853 } 9854 onWindowStageCreate(windowStage: window.WindowStage) { 9855 // Main window is created, set main page for this ability 9856 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onWindowStageCreate'); 9857 } 9858 onWindowStageDestroy() { 9859 // Main window is destroyed, release UI related resources 9860 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onWindowStageDestroy'); 9861 } 9862 onForeground() { 9863 // Ability has brought to foreground 9864 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onForeground'); 9865 } 9866 onBackground() { 9867 // Ability has back to background 9868 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onBackground'); 9869 } 9870 } 9871 ``` 9872