1# @ohos.rpc (RPC通信) 2 3本模块提供进程间通信能力,包括设备内的进程间通信(IPC)和设备间的进程间通信(RPC),前者基于Binder驱动,后者基于软总线驱动。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> - 本模块从API version 9开始支持异常返回功能。 10 11## 导入模块 12 13``` 14import rpc from '@ohos.rpc'; 15``` 16 17## ErrorCode<sup>9+</sup> 18 19从API version 9起,IPC支持异常返回功能。错误码对应数值及含义如下。 20 21**系统能力**:SystemCapability.Communication.IPC.Core 22 23 | 名称 | 值 | 说明 | 24 | ------------------------------------- | ------- | --------------------------------------------- | 25 | CHECK_PARAM_ERROR | 401 | 检查参数失败。 | 26 | OS_MMAP_ERROR | 1900001 | 执行系统调用mmap失败。 | 27 | OS_IOCTL_ERROR | 1900002 | 在共享内存文件描述符上执行系统调用ioctl失败。 | 28 | WRITE_TO_ASHMEM_ERROR | 1900003 | 向共享内存写数据失败。 | 29 | READ_FROM_ASHMEM_ERROR | 1900004 | 从共享内存读数据失败。 | 30 | ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | 只有proxy对象允许该操作。 | 31 | ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | 只有remote对象允许该操作。 | 32 | COMMUNICATION_ERROR | 1900007 | 和远端对象进行进程间通信失败。 | 33 | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | 非法的代理对象或者远端对象。 | 34 | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | 向MessageSequence写数据失败。 | 35 | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | 读取MessageSequence数据失败。 | 36 | PARCEL_MEMORY_ALLOC_ERROR | 1900011 | 序列化过程中内存分配失败。 | 37 | CALL_JS_METHOD_ERROR | 1900012 | 执行JS回调方法失败。 | 38 | OS_DUP_ERROR | 1900013 | 执行系统调用dup失败。 | 39 40 41## MessageSequence<sup>9+</sup> 42 43 在RPC或IPC过程中,发送方可以使用MessageSequence提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageSequence提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 44 45### create 46 47 static create(): MessageSequence 48 49 静态方法,创建MessageSequence对象。 50 51**系统能力**:SystemCapability.Communication.IPC.Core 52 53**返回值:** 54 55| 类型 | 说明 | 56| --------------- | ------------------------------- | 57| [MessageSequence](#messagesequence9) | 返回创建的MessageSequence对象。 | 58 59**示例:** 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 72释放不再使用的MessageSequence对象。 73 74**系统能力**:SystemCapability.Communication.IPC.Core 75 76**示例:** 77 78 ```ts 79 let reply = rpc.MessageSequence.create(); 80 reply.reclaim(); 81 ``` 82 83### writeRemoteObject 84 85writeRemoteObject(object: IRemoteObject): void 86 87序列化远程对象并将其写入MessageSequence对象。 88 89**系统能力**:SystemCapability.Communication.IPC.Core 90 91**参数:** 92 93 | 参数名 | 类型 | 必填 | 说明 | 94 | ------ | ------------------------------- | ---- | ----------------------------------------- | 95 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageSequence的远程对象。 | 96 97**错误码:** 98 99以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 100 101 | 错误码ID | 错误信息 | 102 | -------- | -------- | 103 | 1900008 | proxy or remote object is invalid | 104 | 1900009 | write data to message sequence failed | 105 106**示例:** 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 132从MessageSequence读取远程对象。此方法用于反序列化MessageSequence对象以生成IRemoteObject。远程对象按写入MessageSequence的顺序读取。 133 134**系统能力**:SystemCapability.Communication.IPC.Core 135 136**返回值:** 137 138 | 类型 | 说明 | 139 | ------------------------------- | ------------------ | 140 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 141 142**错误码:** 143 144以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 145 146 | 错误码ID | 错误信息 | 147 | -------- | -------- | 148 | 1900008 | proxy or remote object is invalid | 149 | 1900010 | read data from message sequence failed | 150 151**示例:** 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 179将接口描述符写入MessageSequence对象,远端对象可使用该信息校验本次通信。 180 181**系统能力**:SystemCapability.Communication.IPC.Core 182 183**参数:** 184 185 | 参数名 | 类型 | 必填 | 说明 | 186 | ------ | ------ | ---- | ------------------ | 187 | token | string | 是 | 字符串类型描述符。 | 188 189**错误码:** 190 191以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 192 193 | 错误码ID | 错误信息 | 194 | -------- | -------- | 195 | 1900009 | write data to message sequence failed | 196 197**示例:** 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 217从MessageSequence对象中读取接口描述符,接口描述符按写入MessageSequence的顺序读取,本地对象可使用该信息检验本次通信。 218 219**系统能力**:SystemCapability.Communication.IPC.Core 220 221**返回值:** 222 223 | 类型 | 说明 | 224 | ------ | ------------------------ | 225 | string | 返回读取到的接口描述符。 | 226 227**错误码:** 228 229以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 230 231 | 错误码ID | 错误信息 | 232 | -------- | -------- | 233 | 1900010 | read data from message sequence failed | 234 235**示例:** 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 260获取当前创建的MessageSequence对象的数据大小。 261 262**系统能力**:SystemCapability.Communication.IPC.Core 263 264**返回值:** 265 266 | 类型 | 说明 | 267 | ------ | ----------------------------------------------- | 268 | number | 获取的MessageSequence实例的数据大小。以字节为单位。 | 269 270**示例:** 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 284获取当前MessageSequence对象的容量大小。 285 286**系统能力**:SystemCapability.Communication.IPC.Core 287 288**返回值:** 289 290 | 类型 | 说明 | 291 | ------ | ----- | 292 | number | 获取的MessageSequence实例的容量大小。以字节为单位。 | 293 294**示例:** 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 308设置MessageSequence对象中包含的数据大小。 309 310**系统能力**:SystemCapability.Communication.IPC.Core 311 312**参数:** 313 314 | 参数名 | 类型 | 必填 | 说明 | 315 | ------ | ------ | ---- | ------ | 316 | size | number | 是 | MessageSequence实例的数据大小。以字节为单位。 | 317 318**示例:** 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 339设置MessageSequence对象的存储容量。 340 341**系统能力**:SystemCapability.Communication.IPC.Core 342 343**参数:** 344 345 | 参数名 | 类型 | 必填 | 说明 | 346 | ------ | ------ | ---- | --------------------------------------------- | 347 | size | number | 是 | MessageSequence实例的存储容量。以字节为单位。 | 348 349**错误码:** 350 351以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 352 353 | 错误码ID | 错误信息 | 354 | -------- | -------- | 355 | 1900011 | parcel memory alloc failed | 356 357**示例:** 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 377获取MessageSequence的可写字节空间大小。 378 379**系统能力**:SystemCapability.Communication.IPC.Core 380 381**返回值:** 382 383 | 类型 | 说明 | 384 | ------ | ------ | 385 | number | 获取到的MessageSequence实例的可写字节空间。以字节为单位。 | 386 387**示例:** 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 405获取MessageSequence的可读字节空间。 406 407**系统能力**:SystemCapability.Communication.IPC.Core 408 409**返回值:** 410 411 | 类型 | 说明 | 412 | ------ | ------- | 413 | number | 获取到的MessageSequence实例的可读字节空间。以字节为单位。 | 414 415**示例:** 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 433获取MessageSequence的读位置。 434 435**系统能力**:SystemCapability.Communication.IPC.Core 436 437**返回值:** 438 439 | 类型 | 说明 | 440 | ------ | ------ | 441 | number | 返回MessageSequence实例中的当前读取位置。 | 442 443**示例:** 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 457获取MessageSequence的写位置。 458 459**系统能力**:SystemCapability.Communication.IPC.Core 460 461**返回值:** 462 463 | 类型 | 说明 | 464 | ------ | ----- | 465 | number | 返回MessageSequence实例中的当前写入位置。 | 466 467**示例:** 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 482重新偏移读取位置到指定的位置。 483 484**系统能力**:SystemCapability.Communication.IPC.Core 485 486**参数:** 487 488 | 参数名 | 类型 | 必填 | 说明 | 489 | ------ | ------ | ---- | ------- | 490 | pos | number | 是 | 开始读取数据的目标位置。 | 491 492**示例:** 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 518重新偏移写位置到指定的位置。 519 520**系统能力**:SystemCapability.Communication.IPC.Core 521 522**参数:** 523 524 | 参数名 | 类型 | 必填 | 说明 | 525 | ------ | ------ | ---- | ----- | 526 | pos | number | 是 | 开始写入数据的目标位置。 | 527 528**示例:** 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 552将字节值写入MessageSequence实例。 553 554**系统能力**:SystemCapability.Communication.IPC.Core 555 556**参数:** 557 558 | 参数名 | 类型 | 必填 | 说明 | 559 | ------ | ------ | ---- | ----- | 560 | val | number | 是 | 要写入的字节值。 | 561 562**错误码:** 563 564以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 565 566 | 错误码ID | 错误信息 | 567 | -------- | ------- | 568 | 1900009 | write data to message sequence failed | 569 570**示例:** 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 590从MessageSequence实例读取字节值。 591 592**系统能力**:SystemCapability.Communication.IPC.Core 593 594**返回值:** 595 596 | 类型 | 说明 | 597 | ------ | ----- | 598 | number | 返回字节值。 | 599 600**错误码:** 601 602以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 603 604 | 错误码ID | 错误信息 | 605 | ------- | -------- | 606 | 1900010 | read data from message sequence failed | 607 608**示例:** 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 636将短整数值写入MessageSequence实例。 637 638**系统能力**:SystemCapability.Communication.IPC.Core 639 640**参数:** 641 642 | 参数名 | 类型 | 必填 | 说明 | 643 | ------ | ------ | --- | --- | 644 | val | number | 是 | 要写入的短整数值。 | 645 646**错误码:** 647 648以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 649 650 | 错误码ID | 错误信息 | 651 | -------- | -------- | 652 | 1900009 | write data to message sequence failed | 653 654**示例:** 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 674从MessageSequence实例读取短整数值。 675 676**系统能力**:SystemCapability.Communication.IPC.Core 677 678**返回值:** 679 680 | 类型 | 说明 | 681 | ------ | -------------- | 682 | number | 返回短整数值。 | 683 684**错误码:** 685 686以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 687 688 | 错误码ID | 错误信息 | 689 | -------- | -------- | 690 | 1900010 | read data from message sequence failed | 691 692**示例:** 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 720将整数值写入MessageSequence实例。 721 722**系统能力**:SystemCapability.Communication.IPC.Core 723 724**参数:** 725 726 | 参数名 | 类型 | 必填 | 说明 | 727 | ------ | ------ | ---- | ---------------- | 728 | val | number | 是 | 要写入的整数值。 | 729 730**错误码:** 731 732以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 733 734 | 错误码ID | 错误信息 | 735 | -------- | -------- | 736 | 1900009 | write data to message sequence failed | 737 738**示例:** 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 758从MessageSequence实例读取整数值。 759 760**系统能力**:SystemCapability.Communication.IPC.Core 761 762**返回值:** 763 764 | 类型 | 说明 | 765 | ------ | ------------ | 766 | number | 返回整数值。 | 767 768**错误码:** 769 770以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 771 772 | 错误码ID | 错误信息 | 773 | -------- | -------- | 774 | 1900010 | read data from message sequence failed | 775 776**示例:** 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 804将长整数值写入MessageSequence实例。 805 806**系统能力**:SystemCapability.Communication.IPC.Core 807 808**参数:** 809 810 | 参数名 | 类型 | 必填 | 说明 | 811 | ------ | ------ | ---- | ---------------- | 812 | val | number | 是 | 要写入的长整数值 | 813 814**错误码:** 815 816以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 817 818 | 错误码ID | 错误信息 | 819 | -------- | -------- | 820 | 1900009 | write data to message sequence failed | 821 822**示例:** 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 842从MessageSequence实例中读取长整数值。 843 844**系统能力**:SystemCapability.Communication.IPC.Core 845 846**返回值:** 847 848 | 类型 | 说明 | 849 | ------ | -------------- | 850 | number | 返回长整数值。 | 851 852**错误码:** 853 854以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 855 856 | 错误码ID | 错误信息 | 857 | -------- | -------- | 858 | 1900010 | read data from message sequence failed | 859 860**示例:** 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 888将浮点值写入MessageSequence实例。 889 890**系统能力**:SystemCapability.Communication.IPC.Core 891 892**参数:** 893 894 | 参数名 | 类型 | 必填 | 说明 | 895 | ------ | ------ | ---- | ----- | 896 | val | number | 是 | 要写入的浮点值。 | 897 898**错误码:** 899 900以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 901 902 | 错误码ID | 错误信息 | 903 | -------- | -------- | 904 | 1900009 | write data to message sequence failed | 905 906**示例:** 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 926从MessageSequence实例中读取浮点值。 927 928**系统能力**:SystemCapability.Communication.IPC.Core 929 930**返回值:** 931 932 | 类型 | 说明 | 933 | ------ | ------------ | 934 | number | 返回浮点值。 | 935 936**错误码:** 937 938以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 939 940 | 错误码ID | 错误信息 | 941 | -------- | -------- | 942 | 1900010 | read data from message sequence failed | 943 944**示例:** 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 972将双精度浮点值写入MessageSequence实例。 973 974**系统能力**:SystemCapability.Communication.IPC.Core 975 976**参数:** 977 978 | 参数名 | 类型 | 必填 | 说明 | 979 | ------ | ------ | ---- | ---------------------- | 980 | val | number | 是 | 要写入的双精度浮点值。 | 981 982**错误码:** 983 984以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 985 986 | 错误码ID | 错误信息 | 987 | -------- | -------- | 988 | 1900009 | write data to message sequence failed | 989 990**示例:** 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 1010从MessageSequence实例读取双精度浮点值。 1011 1012**系统能力**:SystemCapability.Communication.IPC.Core 1013 1014**返回值:** 1015 1016 | 类型 | 说明 | 1017 | ------ | ------------------ | 1018 | number | 返回双精度浮点值。 | 1019 1020**错误码:** 1021 1022以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1023 1024 | 错误码ID | 错误信息 | 1025 | -------- | -------- | 1026 | 1900010 | read data from message sequence failed | 1027 1028**示例:** 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 1056将布尔值写入MessageSequence实例。 1057 1058**系统能力**:SystemCapability.Communication.IPC.Core 1059 1060**参数:** 1061 1062 | 参数名 | 类型 | 必填 | 说明 | 1063 | ------ | ------- | ---- | ---------------- | 1064 | val | boolean | 是 | 要写入的布尔值。 | 1065 1066**错误码:** 1067 1068以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1069 1070 | 错误码ID | 错误信息 | 1071 | -------- | -------- | 1072 | 1900009 | write data to message sequence failed | 1073 1074**示例:** 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 1094从MessageSequence实例读取布尔值。 1095 1096**系统能力**:SystemCapability.Communication.IPC.Core 1097 1098**返回值:** 1099 1100 | 类型 | 说明 | 1101 | ------- | -------------------- | 1102 | boolean | 返回读取到的布尔值。 | 1103 1104**错误码:** 1105 1106以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1107 1108 | 错误码ID | 错误信息 | 1109 | -------- | -------- | 1110 | 1900010 | read data from message sequence failed | 1111 1112**示例:** 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 1140将单个字符值写入MessageSequence实例。 1141 1142**系统能力**:SystemCapability.Communication.IPC.Core 1143 1144**参数:** 1145 1146 | 参数名 | 类型 | 必填 | 说明 | 1147 | ------ | ------ | ---- | -------------------- | 1148 | val | number | 是 | 要写入的单个字符值。 | 1149 1150**错误码:** 1151 1152以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1153 1154 | 错误码ID | 错误信息 | 1155 | -------- | -------- | 1156 | 1900009 | write data to message sequence failed | 1157 1158**示例:** 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 1178从MessageSequence实例中读取单个字符值。 1179 1180**系统能力**:SystemCapability.Communication.IPC.Core 1181 1182**返回值:** 1183 1184 | 类型 | 说明 | 1185 | ------ | ---- | 1186 | number | 返回单个字符值。 | 1187 1188**错误码:** 1189 1190以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1191 1192 | 错误码ID | 错误信息 | 1193 | -------- | -------- | 1194 | 1900010 | read data from message sequence failed | 1195 1196**示例:** 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 1224将字符串值写入MessageSequence实例。 1225 1226**系统能力**:SystemCapability.Communication.IPC.Core 1227 1228**参数:** 1229 1230 | 参数名 | 类型 | 必填 | 说明 | 1231 | ------ | ------ | ---- | ----------------------------------------- | 1232 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 1233 1234**错误码:** 1235 1236以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1237 1238 | 错误码ID | 错误信息 | 1239 | -------- | -------- | 1240 | 1900009 | write data to message sequence failed | 1241 1242**示例:** 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 1262从MessageSequence实例读取字符串值。 1263 1264**系统能力**:SystemCapability.Communication.IPC.Core 1265 1266**返回值:** 1267 1268 | 类型 | 说明 | 1269 | ------ | -------------- | 1270 | string | 返回字符串值。 | 1271 1272**错误码:** 1273 1274以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1275 1276 | 错误码ID | 错误信息 | 1277 | -------- | -------- | 1278 | 1900010 | read data from message sequence failed | 1279 1280**示例:** 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 1308将自定义序列化对象写入MessageSequence实例。 1309 1310**系统能力**:SystemCapability.Communication.IPC.Core 1311 1312**参数:** 1313 1314| 参数名 | 类型 | 必填 | 说明 | 1315| ------ | --------- | ---- | ------ | 1316| val | [Parcelable](#parcelable9) | 是 | 要写入的可序列对象。 | 1317 1318**错误码:** 1319 1320以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1321 1322 | 错误码ID | 错误信息 | 1323 | -------- | -------- | 1324 | 1900009 | write data to message sequence failed | 1325 1326**示例:** 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 1365从MessageSequence实例中读取成员变量到指定的对象(dataIn)。 1366 1367**系统能力**:SystemCapability.Communication.IPC.Core 1368 1369**参数:** 1370 1371| 参数名 | 类型 | 必填 | 说明 | 1372| ------ | -------------------------- | ---- | ----------------------------------------- | 1373| dataIn | [Parcelable](#parcelable9) | 是 | 需要从MessageSequence读取成员变量的对象。 | 1374 1375**错误码:** 1376 1377以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1378 1379 | 错误码ID | 错误信息 | 1380 | -------- | -------- | 1381 | 1900010 | read data from message sequence failed | 1382 | 1900012 | call js callback function failed | 1383 1384**示例:** 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 1425将字节数组写入MessageSequence实例。 1426 1427**系统能力**:SystemCapability.Communication.IPC.Core 1428 1429**参数:** 1430 1431 | 参数名 | 类型 | 必填 | 说明 | 1432 | --------- | -------- | ---- | ------------------ | 1433 | byteArray | number[] | 是 | 要写入的字节数组。 | 1434 1435**错误码:** 1436 1437以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1438 1439 | 错误码ID | 错误信息 | 1440 | -------- | -------- | 1441 | 1900009 | write data to message sequence failed | 1442 1443**示例:** 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 1464从MessageSequence实例读取字节数组。 1465 1466**系统能力**:SystemCapability.Communication.IPC.Core 1467 1468**参数:** 1469 1470 | 参数名 | 类型 | 必填 | 说明 | 1471 | ------ | -------- | ---- | ------------------ | 1472 | dataIn | number[] | 是 | 要读取的字节数组。 | 1473 1474**错误码:** 1475 1476以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1477 1478 | 错误码ID | 错误信息 | 1479 | -------- | -------- | 1480 | 1900010 | read data from message sequence failed | 1481 1482**示例:** 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 1511从MessageSequence实例中读取字节数组。 1512 1513**系统能力**:SystemCapability.Communication.IPC.Core 1514 1515**返回值:** 1516 1517 | 类型 | 说明 | 1518 | -------- | -------------- | 1519 | number[] | 返回字节数组。 | 1520 1521**错误码:** 1522 1523以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1524 1525 | 错误码ID | 错误信息 | 1526 | -------- | -------- | 1527 | 1900010 | read data from message sequence failed | 1528 1529**示例:** 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 1558将短整数数组写入MessageSequence实例。 1559 1560**系统能力**:SystemCapability.Communication.IPC.Core 1561 1562**参数:** 1563 1564 | 参数名 | 类型 | 必填 | 说明 | 1565 | ---------- | -------- | ---- | -------------------- | 1566 | shortArray | number[] | 是 | 要写入的短整数数组。 | 1567 1568**错误码:** 1569 1570以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1571 1572 | 错误码ID | 错误信息 | 1573 | -------- | -------- | 1574 | 1900009 | write data to message sequence failed | 1575 1576**示例:** 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 1596从MessageSequence实例中读取短整数数组。 1597 1598**系统能力**:SystemCapability.Communication.IPC.Core 1599 1600**参数:** 1601 1602 | 参数名 | 类型 | 必填 | 说明 | 1603 | ------ | -------- | ---- | -------------------- | 1604 | dataIn | number[] | 是 | 要读取的短整数数组。 | 1605 1606**错误码:** 1607 1608以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1609 1610 | 错误码ID | 错误信息 | 1611 | -------- | -------- | 1612 | 1900010 | read data from message sequence failed | 1613 1614**示例:** 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 1642从MessageSequence实例中读取短整数数组。 1643 1644**系统能力**:SystemCapability.Communication.IPC.Core 1645 1646**返回值:** 1647 1648 | 类型 | 说明 | 1649 | -------- | ---------------- | 1650 | number[] | 返回短整数数组。 | 1651 1652**错误码:** 1653 1654以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1655 1656 | 错误码ID | 错误信息 | 1657 | -------- | -------- | 1658 | 1900010 | read data from message sequence failed | 1659 1660**示例:** 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 1688将整数数组写入MessageSequence实例。 1689 1690**系统能力**:SystemCapability.Communication.IPC.Core 1691 1692**参数:** 1693 1694 | 参数名 | 类型 | 必填 | 说明 | 1695 | -------- | -------- | ---- | ------------------ | 1696 | intArray | number[] | 是 | 要写入的整数数组。 | 1697 1698**错误码:** 1699 1700以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1701 1702 | 错误码ID | 错误信息 | 1703 | -------- | -------- | 1704 | 1900009 | write data to message sequence failed | 1705 1706**示例:** 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 1726从MessageSequence实例中读取整数数组。 1727 1728**系统能力**:SystemCapability.Communication.IPC.Core 1729 1730**参数:** 1731 1732 | 参数名 | 类型 | 必填 | 说明 | 1733 | ------ | -------- | ---- | ------------------ | 1734 | dataIn | number[] | 是 | 要读取的整数数组。 | 1735 1736**错误码:** 1737 1738以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1739 1740 | 错误码ID | 错误信息 | 1741 | -------- | -------- | 1742 | 1900010 | read data from message sequence failed | 1743 1744**示例:** 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 1772从MessageSequence实例中读取整数数组。 1773 1774**系统能力**:SystemCapability.Communication.IPC.Core 1775 1776**返回值:** 1777 1778 | 类型 | 说明 | 1779 | -------- | -------------- | 1780 | number[] | 返回整数数组。 | 1781 1782**错误码:** 1783 1784以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1785 1786 | 错误码ID | 错误信息 | 1787 | -------- | -------- | 1788 | 1900010 | read data from message sequence failed | 1789 1790**示例:** 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 1818将长整数数组写入MessageSequence实例。 1819 1820**系统能力**:SystemCapability.Communication.IPC.Core 1821 1822**参数:** 1823 1824 | 参数名 | 类型 | 必填 | 说明 | 1825 | --------- | -------- | ---- | -------------------- | 1826 | longArray | number[] | 是 | 要写入的长整数数组。 | 1827 1828**错误码:** 1829 1830以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1831 1832 | 错误码ID | 错误信息 | 1833 | -------- | -------- | 1834 | 1900009 | write data to message sequence failed | 1835 1836**示例:** 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 1856从MessageSequence实例读取的长整数数组。 1857 1858**系统能力**:SystemCapability.Communication.IPC.Core 1859 1860**参数:** 1861 1862 | 参数名 | 类型 | 必填 | 说明 | 1863 | ------ | -------- | ---- | -------------------- | 1864 | dataIn | number[] | 是 | 要读取的长整数数组。 | 1865 1866**错误码:** 1867 1868以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1869 1870 | 错误码ID | 错误信息 | 1871 | -------- | -------- | 1872 | 1900010 | read data from message sequence failed | 1873 1874**示例:** 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 1902从MessageSequence实例中读取所有的长整数数组。 1903 1904**系统能力**:SystemCapability.Communication.IPC.Core 1905 1906**返回值:** 1907 1908 | 类型 | 说明 | 1909 | -------- | ---------------- | 1910 | number[] | 返回长整数数组。 | 1911 1912**错误码:** 1913 1914以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1915 1916 | 错误码ID | 错误信息 | 1917 | -------- | -------- | 1918 | 1900010 | read data from message sequence failed | 1919 1920**示例:** 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 1948将浮点数组写入MessageSequence实例。 1949 1950**系统能力**:SystemCapability.Communication.IPC.Core 1951 1952**参数:** 1953 1954 | 参数名 | 类型 | 必填 | 说明 | 1955 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1956 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 1957 1958**错误码:** 1959 1960以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1961 1962 | 错误码ID | 错误信息 | 1963 | -------- | -------- | 1964 | 1900009 | write data to message sequence failed | 1965 1966**示例:** 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 1986从MessageSequence实例中读取浮点数组。 1987 1988**系统能力**:SystemCapability.Communication.IPC.Core 1989 1990**参数:** 1991 1992 | 参数名 | 类型 | 必填 | 说明 | 1993 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1994 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 1995 1996**错误码:** 1997 1998以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 1999 2000 | 错误码ID | 错误信息 | 2001 | -------- | -------- | 2002 | 1900010 | read data from message sequence failed | 2003 2004**示例:** 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 2032从MessageSequence实例中读取浮点数组。 2033 2034**系统能力**:SystemCapability.Communication.IPC.Core 2035 2036**返回值:** 2037 2038 | 类型 | 说明 | 2039 | -------- | -------------- | 2040 | number[] | 返回浮点数组。 | 2041 2042**错误码:** 2043 2044以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2045 2046 | 错误码ID | 错误信息 | 2047 | -------- | -------- | 2048 | 1900010 | read data from message sequence failed | 2049 2050**示例:** 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 2078将双精度浮点数组写入MessageSequence实例。 2079 2080**系统能力**:SystemCapability.Communication.IPC.Core 2081 2082**参数:** 2083 2084 | 参数名 | 类型 | 必填 | 说明 | 2085 | ----------- | -------- | ---- | ------------------------ | 2086 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 2087 2088**错误码:** 2089 2090以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2091 2092 | 错误码ID | 错误信息 | 2093 | -------- | -------- | 2094 | 1900009 | write data to message sequence failed | 2095 2096**示例:** 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 2116从MessageSequence实例中读取双精度浮点数组。 2117 2118**系统能力**:SystemCapability.Communication.IPC.Core 2119 2120**参数:** 2121 2122 | 参数名 | 类型 | 必填 | 说明 | 2123 | ------ | -------- | ---- | ------------------------ | 2124 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 2125 2126**错误码:** 2127 2128以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2129 2130 | 错误码ID | 错误信息 | 2131 | -------- | -------- | 2132 | 1900010 | read data from message sequence failed | 2133 2134**示例:** 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 2162从MessageSequence实例读取所有双精度浮点数组。 2163 2164**系统能力**:SystemCapability.Communication.IPC.Core 2165 2166**返回值:** 2167 2168 | 类型 | 说明 | 2169 | -------- | -------------------- | 2170 | number[] | 返回双精度浮点数组。 | 2171 2172**错误码:** 2173 2174以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2175 2176 | 错误码ID | 错误信息 | 2177 | -------- | -------- | 2178 | 1900010 | read data from message sequence failed | 2179 2180**示例:** 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 2208将布尔数组写入MessageSequence实例。 2209 2210**系统能力**:SystemCapability.Communication.IPC.Core 2211 2212**参数:** 2213 2214 | 参数名 | 类型 | 必填 | 说明 | 2215 | ------------ | --------- | ---- | ------------------ | 2216 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 2217 2218**错误码:** 2219 2220以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2221 2222 | 错误码ID | 错误信息 | 2223 | -------- | -------- | 2224 | 1900009 | write data to message sequence failed | 2225 2226**示例:** 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 2246从MessageSequence实例中读取布尔数组。 2247 2248**系统能力**:SystemCapability.Communication.IPC.Core 2249 2250**参数:** 2251 2252 | 参数名 | 类型 | 必填 | 说明 | 2253 | ------ | --------- | ---- | ------------------ | 2254 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 2255 2256**错误码:** 2257 2258以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2259 2260 | 错误码ID | 错误信息 | 2261 | -------- | -------- | 2262 | 1900010 | read data from message sequence failed | 2263 2264**示例:** 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 2292从MessageSequence实例中读取所有布尔数组。 2293 2294**系统能力**:SystemCapability.Communication.IPC.Core 2295 2296**返回值:** 2297 2298 | 类型 | 说明 | 2299 | --------- | -------------- | 2300 | boolean[] | 返回布尔数组。 | 2301 2302**错误码:** 2303 2304以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2305 2306 | 错误码ID | 错误信息 | 2307 | -------- | -------- | 2308 | 1900010 | read data from message sequence failed | 2309 2310**示例:** 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 2338将单个字符数组写入MessageSequence实例。 2339 2340**系统能力**:SystemCapability.Communication.IPC.Core 2341 2342**参数:** 2343 2344 | 参数名 | 类型 | 必填 | 说明 | 2345 | --------- | -------- | ---- | ---------------------- | 2346 | charArray | number[] | 是 | 要写入的单个字符数组。 | 2347 2348**错误码:** 2349 2350以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2351 2352 | 错误码ID | 错误信息 | 2353 | -------- | -------- | 2354 | 1900009 | write data to message sequence failed | 2355 2356**示例:** 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 2376从MessageSequence实例中读取单个字符数组。 2377 2378**系统能力**:SystemCapability.Communication.IPC.Core 2379 2380**参数:** 2381 2382 | 参数名 | 类型 | 必填 | 说明 | 2383 | ------ | -------- | ---- | ---------------------- | 2384 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 2385 2386**错误码:** 2387 2388以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2389 2390 | 错误码ID | 错误信息 | 2391 | -------- | -------- | 2392 | 1900010 | read data from message sequence failed | 2393 2394**示例:** 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 2422从MessageSequence实例读取单个字符数组。 2423 2424**系统能力**:SystemCapability.Communication.IPC.Core 2425 2426**返回值:** 2427 2428 | 类型 | 说明 | 2429 | -------- | ------------------ | 2430 | number[] | 返回单个字符数组。 | 2431 2432**错误码:** 2433 2434以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2435 2436 | 错误码ID | 错误信息 | 2437 | -------- | -------- | 2438 | 1900010 | read data from message sequence failed | 2439 2440**示例:** 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 2468将字符串数组写入MessageSequence实例。 2469 2470**系统能力**:SystemCapability.Communication.IPC.Core 2471 2472**参数:** 2473 2474 | 参数名 | 类型 | 必填 | 说明 | 2475 | ----------- | -------- | ---- | ------------------------------------------------------- | 2476 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 2477 2478**错误码:** 2479 2480以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2481 2482 | 错误码ID | 错误信息 | 2483 | -------- | -------- | 2484 | 1900009 | write data to message sequence failed | 2485 2486**示例:** 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 2506从MessageSequence实例读取字符串数组。 2507 2508**系统能力**:SystemCapability.Communication.IPC.Core 2509 2510**参数:** 2511 2512 | 参数名 | 类型 | 必填 | 说明 | 2513 | ------ | -------- | ---- | -------------------- | 2514 | dataIn | string[] | 是 | 要读取的字符串数组。 | 2515 2516**错误码:** 2517 2518以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2519 2520 | 错误码ID | 错误信息 | 2521 | -------- | -------- | 2522 | 1900010 | read data from message sequence failed | 2523 2524**示例:** 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 2552从MessageSequence实例读取字符串数组。 2553 2554**系统能力**:SystemCapability.Communication.IPC.Core 2555 2556**返回值:** 2557 2558 | 类型 | 说明 | 2559 | -------- | ---------------- | 2560 | string[] | 返回字符串数组。 | 2561 2562**错误码:** 2563 2564以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2565 2566 | 错误码ID | 错误信息 | 2567 | -------- | -------- | 2568 | 1900010 | read data from message sequence failed | 2569 2570**示例:** 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 2598向MessageSequence写入“指示未发生异常”的信息。 2599 2600**系统能力**:SystemCapability.Communication.IPC.Core 2601 2602**错误码:** 2603 2604以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2605 2606 | 错误码ID | 错误信息 | 2607 | -------- | -------- | 2608 | 1900009 | write data to message sequence failed | 2609 2610**示例:** 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 2643从MessageSequence中读取异常。 2644 2645**系统能力**:SystemCapability.Communication.IPC.Core 2646 2647**错误码:** 2648 2649以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2650 2651 | 错误码ID | 错误信息 | 2652 | -------- | -------- | 2653 | 1900010 | read data from message sequence failed | 2654 2655**示例:** 2656 2657 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 2658 2659 ```ts 2660 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 2685 // FA.connectAbility(want,connect); 2686 2687 this.context.connectServiceExtensionAbility(want, connect); 2688 ``` 2689 2690 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 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 2732将可序列化对象数组写入MessageSequence实例。 2733 2734**系统能力**:SystemCapability.Communication.IPC.Core 2735 2736**参数:** 2737 2738| 参数名 | 类型 | 必填 | 说明 | 2739| --------------- | ------------ | ---- | -------------------------- | 2740| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要写入的可序列化对象数组。 | 2741 2742**错误码:** 2743 2744以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2745 2746 | 错误码ID | 错误信息 | 2747 | -------- | -------- | 2748 | 1900009 | write data to message sequence failed | 2749 2750**示例:** 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 2792从MessageSequence实例读取可序列化对象数组。 2793 2794**系统能力**:SystemCapability.Communication.IPC.Core 2795 2796**参数:** 2797 2798| 参数名 | 类型 | 必填 | 说明 | 2799| --------------- | ------------ | ---- | -------------------------- | 2800| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要读取的可序列化对象数组。 | 2801 2802**错误码:** 2803 2804以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2805 2806 | 错误码ID | 错误信息 | 2807 | -------- | -------- | 2808 | 1900010 | read data from message sequence failed | 2809 | 1900012 | call js callback function failed | 2810 2811**示例:** 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 2855将IRemoteObject对象数组写入MessageSequence。 2856 2857**系统能力**:SystemCapability.Communication.IPC.Core 2858 2859**参数:** 2860 2861| 参数名 | 类型 | 必填 | 说明 | 2862| ----------- | --------------- | ---- | ---------------------------------------------- | 2863| objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageSequence的IRemoteObject对象数组。 | 2864 2865**错误码:** 2866 2867以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2868 2869 | 错误码ID | 错误信息 | 2870 | -------- | -------- | 2871 | 1900009 | write data to message sequence failed | 2872 2873**示例:** 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 2904从MessageSequence读取IRemoteObject对象数组。 2905 2906**系统能力**:SystemCapability.Communication.IPC.Core 2907 2908**参数:** 2909 2910| 参数名 | 类型 | 必填 | 说明 | 2911| ------- | --------------- | ---- | ---------------------------------------------- | 2912| objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageSequence读取的IRemoteObject对象数组。 | 2913 2914**错误码:** 2915 2916以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2917 2918 | 错误码ID | 错误信息 | 2919 | -------- | -------- | 2920 | 1900010 | read data from message sequence failed | 2921 2922**示例:** 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 2955从MessageSequence读取IRemoteObject对象数组。 2956 2957**系统能力**:SystemCapability.Communication.IPC.Core 2958 2959**返回值:** 2960 2961| 类型 | 说明 | 2962| --------------- | --------------------------- | 2963| [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 2964 2965**错误码:** 2966 2967以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 2968 2969 | 错误码ID | 错误信息 | 2970 | -------- | -------- | 2971 | 1900010 | read data from message sequence failed | 2972 2973**示例:** 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 3006静态方法,关闭给定的文件描述符。 3007 3008**系统能力**:SystemCapability.Communication.IPC.Core 3009 3010**参数:** 3011 3012 | 参数名 | 类型 | 必填 | 说明 | 3013 | ------ | ------ | ---- | -------------------- | 3014 | fd | number | 是 | 要关闭的文件描述符。 | 3015 3016**示例:** 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 3038静态方法,复制给定的文件描述符。 3039 3040**系统能力**:SystemCapability.Communication.IPC.Core 3041 3042**参数:** 3043 3044 | 参数名 | 类型 | 必填 | 说明 | 3045 | ------ | ------ | ---- | ------------------------ | 3046 | fd | number | 是 | 表示已存在的文件描述符。 | 3047 3048**返回值:** 3049 3050 | 类型 | 说明 | 3051 | ------ | -------------------- | 3052 | number | 返回新的文件描述符。 | 3053 3054**错误码:** 3055 3056以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3057 3058 | 错误码ID | 错误信息 | 3059 | -------- | -------- | 3060 | 1900013 | call os dup function failed | 3061 3062**示例:** 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 3084检查此MessageSequence对象是否包含文件描述符。 3085 3086**系统能力**:SystemCapability.Communication.IPC.Core 3087 3088**返回值:** 3089 3090 | 类型 | 说明 | 3091 | ------- | -------------------------------------------------------------------- | 3092 | boolean | true:包含文件描述符,false:不包含文件描述符。| 3093 3094**示例:** 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 3125写入文件描述符到MessageSequence。 3126 3127**系统能力**:SystemCapability.Communication.IPC.Core 3128 3129**参数:** 3130 3131 | 参数名 | 类型 | 必填 | 说明 | 3132 | ------ | ------ | ---- | ------------ | 3133 | fd | number | 是 | 文件描述符。 | 3134 3135**错误码:** 3136 3137以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3138 3139 | 错误码ID | 错误信息 | 3140 | -------- | -------- | 3141 | 1900009 | write data to message sequence failed | 3142 3143**示例:** 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 3166从MessageSequence中读取文件描述符。 3167 3168**系统能力**:SystemCapability.Communication.IPC.Core 3169 3170**返回值:** 3171 3172 | 类型 | 说明 | 3173 | ------ | ---------------- | 3174 | number | 返回文件描述符。 | 3175 3176**错误码:** 3177 3178以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3179 3180 | 错误码ID | 错误信息 | 3181 | -------- | -------- | 3182 | 1900010 | read data from message sequence failed | 3183 3184**示例:** 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 3215将指定的匿名共享对象写入此MessageSequence。 3216 3217**系统能力**:SystemCapability.Communication.IPC.Core 3218 3219**参数:** 3220 3221| 参数名 | 类型 | 必填 | 说明 | 3222| ------ | ------ | ---- | ------------------------------------- | 3223| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageSequence的匿名共享对象。 | 3224 3225**错误码:** 3226 3227以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3228 3229 | 错误码ID | 错误信息 | 3230 | -------- | ------- | 3231 | 1900003 | write to ashmem failed | 3232 3233**示例:** 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 3261从MessageSequence读取匿名共享对象。 3262 3263**系统能力**:SystemCapability.Communication.IPC.Core 3264 3265**返回值:** 3266 3267| 类型 | 说明 | 3268| ------ | ------------------ | 3269| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 3270 3271**错误码:** 3272 3273以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3274 3275 | 错误码ID | 错误信息 | 3276 | -------- | -------- | 3277 | 1900004 | read from ashmem failed | 3278 3279**示例:** 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 3314获取MessageSequence可以容纳的最大原始数据量。 3315 3316**系统能力**:SystemCapability.Communication.IPC.Core 3317 3318**返回值:** 3319 3320 | 类型 | 说明 | 3321 | ------ | ------------------------------------------------------------ | 3322 | number | 返回MessageSequence可以容纳的最大原始数据量,即128 Mb。 | 3323 3324**示例:** 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 3338将原始数据写入MessageSequence对象。 3339 3340**系统能力**:SystemCapability.Communication.IPC.Core 3341 3342**参数:** 3343 3344 | 参数名 | 类型 | 必填 | 说明 | 3345 | ------- | -------- | ---- | ---------------------------------- | 3346 | rawData | number[] | 是 | 要写入的原始数据。 | 3347 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3348 3349**错误码:** 3350 3351以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3352 3353 | 错误码ID | 错误信息 | 3354 | -------- | -------- | 3355 | 1900009 | write data to message sequence failed | 3356 3357**示例:** 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 3378从MessageSequence读取原始数据。 3379 3380**系统能力**:SystemCapability.Communication.IPC.Core 3381 3382**参数:** 3383 3384 | 参数名 | 类型 | 必填 | 说明 | 3385 | ------ | ------ | ---- | ------------------------ | 3386 | size | number | 是 | 要读取的原始数据的大小。 | 3387 3388**返回值:** 3389 3390 | 类型 | 说明 | 3391 | -------- | ------------------------------ | 3392 | number[] | 返回原始数据(以字节为单位)。 | 3393 3394**错误码:** 3395 3396以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 3397 3398 | 错误码ID | 错误信息 | 3399 | -------- | -------- | 3400 | 1900010 | read data from message sequence failed | 3401 3402**示例:** 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>从API version 9 开始不再维护,建议使用[MessageSequence](#messagesequence9)类替代。 3430 3431在RPC过程中,发送方可以使用MessageParcel提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageParcel提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 3432 3433### create 3434 3435static create(): MessageParcel 3436 3437静态方法,创建MessageParcel对象。 3438 3439**系统能力**:SystemCapability.Communication.IPC.Core 3440 3441**返回值:** 3442 3443 | 类型 | 说明 | 3444 | ------------- | ----------------------------- | 3445 | [MessageParcel](#messageparceldeprecated) | 返回创建的MessageParcel对象。 | 3446 3447**示例:** 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 3460释放不再使用的MessageParcel对象。 3461 3462**系统能力**:SystemCapability.Communication.IPC.Core 3463 3464**示例:** 3465 3466 ```ts 3467 let reply = rpc.MessageParcel.create(); 3468 reply.reclaim(); 3469 ``` 3470 3471### writeRemoteObject 3472 3473writeRemoteObject(object: IRemoteObject): boolean 3474 3475序列化远程对象并将其写入MessageParcel对象。 3476 3477**系统能力**:SystemCapability.Communication.IPC.Core 3478 3479**参数:** 3480 3481 | 参数名 | 类型 | 必填 | 说明 | 3482 | ------ | ------------------------------- | ---- | --------------------------------------- | 3483 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageParcel的远程对象。 | 3484 3485**返回值:** 3486 3487 | 类型 | 说明 | 3488 | ------- | ----------------------------------------- | 3489 | boolean | true:操作成功,false:操作失败。| 3490 3491**示例:** 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 3524从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。 3525 3526**系统能力**:SystemCapability.Communication.IPC.Core 3527 3528**返回值:** 3529 3530 | 类型 | 说明 | 3531 | ------------------------------- | ------------------ | 3532 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 3533 3534**示例:** 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 3569将接口描述符写入MessageParcel对象,远端对象可使用该信息校验本次通信。 3570 3571**系统能力**:SystemCapability.Communication.IPC.Core 3572 3573**参数:** 3574 3575 | 参数名 | 类型 | 必填 | 说明 | 3576 | ------ | ------ | ---- | ------------------ | 3577 | token | string | 是 | 字符串类型描述符。 | 3578 3579**返回值:** 3580 3581 | 类型 | 说明 | 3582 | ------- | ----------------------------------------- | 3583 | boolean | true:操作成功,false:操作失败。| 3584 3585**示例:** 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 3599从MessageParcel中读取接口描述符,接口描述符按写入MessageParcel的顺序读取,本地对象可使用该信息检验本次通信。 3600 3601**系统能力**:SystemCapability.Communication.IPC.Core 3602 3603**返回值:** 3604 3605 | 类型 | 说明 | 3606 | ------ | ------------------------ | 3607 | string | 返回读取到的接口描述符。 | 3608 3609**示例:** 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 3627获取当前MessageParcel的数据大小。 3628 3629**系统能力**:SystemCapability.Communication.IPC.Core 3630 3631**返回值:** 3632 3633 | 类型 | 说明 | 3634 | ------ | --------------------------------------------- | 3635 | number | 获取的MessageParcel的数据大小。以字节为单位。 | 3636 3637**示例:** 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 3651获取当前MessageParcel的容量。 3652 3653**系统能力**:SystemCapability.Communication.IPC.Core 3654 3655**返回值:** 3656 3657 | 类型 | 说明 | 3658 | ------ | --------------------------------------------- | 3659 | number | 获取的MessageParcel的容量大小。以字节为单位。 | 3660 3661**示例:** 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 3675设置MessageParcel实例中包含的数据大小。 3676 3677**系统能力**:SystemCapability.Communication.IPC.Core 3678 3679**参数:** 3680 3681 | 参数名 | 类型 | 必填 | 说明 | 3682 | ------ | ------ | ---- | ------------------------------------------- | 3683 | size | number | 是 | MessageParcel实例的数据大小。以字节为单位。 | 3684 3685**返回值:** 3686 3687 | 类型 | 说明 | 3688 | ------- | --------------------------------- | 3689 | boolean | true:设置成功,false:设置失败。| 3690 3691**示例:** 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 3705设置MessageParcel实例的存储容量。 3706 3707**系统能力**:SystemCapability.Communication.IPC.Core 3708 3709**参数:** 3710 3711 | 参数名 | 类型 | 必填 | 说明 | 3712 | ------ | ------ | ---- | ------------------------------------------- | 3713 | size | number | 是 | MessageParcel实例的存储容量。以字节为单位。 | 3714 3715**返回值:** 3716 3717 | 类型 | 说明 | 3718 | ------- | --------------------------------- | 3719 | boolean | true:设置成功,false:设置失败。| 3720 3721**示例:** 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 3735获取MessageParcel的可写字节空间。 3736 3737**系统能力**:SystemCapability.Communication.IPC.Core 3738 3739**返回值:** 3740 3741 | 类型 | 说明 | 3742 | ------ | --------------------------------------------------- | 3743 | number | 获取到的MessageParcel的可写字节空间。以字节为单位。 | 3744 3745**示例:** 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 3763获取MessageParcel的可读字节空间。 3764 3765**系统能力**:SystemCapability.Communication.IPC.Core 3766 3767**返回值:** 3768 3769 | 类型 | 说明 | 3770 | ------ | --------------------------------------------------- | 3771 | number | 获取到的MessageParcel的可读字节空间。以字节为单位。 | 3772 3773**示例:** 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 3791获取MessageParcel的读位置。 3792 3793**系统能力**:SystemCapability.Communication.IPC.Core 3794 3795**返回值:** 3796 3797 | 类型 | 说明 | 3798 | ------ | --------------------------------------- | 3799 | number | 返回MessageParcel实例中的当前读取位置。 | 3800 3801**示例:** 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 3815获取MessageParcel的写位置。 3816 3817**系统能力**:SystemCapability.Communication.IPC.Core 3818 3819**返回值:** 3820 3821 | 类型 | 说明 | 3822 | ------ | --------------------------------------- | 3823 | number | 返回MessageParcel实例中的当前写入位置。 | 3824 3825**示例:** 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 3840重新偏移读取位置到指定的位置。 3841 3842**系统能力**:SystemCapability.Communication.IPC.Core 3843 3844**参数:** 3845 3846 | 参数名 | 类型 | 必填 | 说明 | 3847 | ------ | ------ | ---- | ------------------------ | 3848 | pos | number | 是 | 开始读取数据的目标位置。 | 3849 3850**返回值:** 3851 3852 | 类型 | 说明 | 3853 | ------- | ------------------------------------------------- | 3854 | boolean | true:读取位置发生更改,false:读取位置未发生更改。| 3855 3856**示例:** 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 3875重新偏移写位置到指定的位置。 3876 3877**系统能力**:SystemCapability.Communication.IPC.Core 3878 3879**参数:** 3880 3881 | 参数名 | 类型 | 必填 | 说明 | 3882 | ------ | ------ | ---- | ------------------------ | 3883 | pos | number | 是 | 开始写入数据的目标位置。 | 3884 3885**返回值:** 3886 3887 | 类型 | 说明 | 3888 | ------- | --------------------------------------------- | 3889 | boolean | true:写入位置发生更改,false:写入位置未发生更改。| 3890 3891**示例:** 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 3908将字节值写入MessageParcel实例。 3909 3910**系统能力**:SystemCapability.Communication.IPC.Core 3911 3912**参数:** 3913 3914 | 参数名 | 类型 | 必填 | 说明 | 3915 | ------ | ------ | ---- | ---------------- | 3916 | val | number | 是 | 要写入的字节值。 | 3917 3918**返回值:** 3919 3920 | 类型 | 说明 | 3921 | ------- | ----------------------------- | 3922 | boolean | true:写入成功,false:写入失败。 | 3923 3924**示例:** 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 3938从MessageParcel实例读取字节值。 3939 3940**系统能力**:SystemCapability.Communication.IPC.Core 3941 3942**返回值:** 3943 3944 | 类型 | 说明 | 3945 | ------ | ------------ | 3946 | number | 返回字节值。 | 3947 3948**示例:** 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 3964将短整数值写入MessageParcel实例。 3965 3966**系统能力**:SystemCapability.Communication.IPC.Core 3967 3968**参数:** 3969 3970 | 参数名 | 类型 | 必填 | 说明 | 3971 | ------ | ------ | ---- | ------------------ | 3972 | val | number | 是 | 要写入的短整数值。 | 3973 3974**返回值:** 3975 3976 | 类型 | 说明 | 3977 | ------- | ----------------------------- | 3978 | boolean | true:写入成功,false:写入失败。| 3979 3980**示例:** 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 3994从MessageParcel实例读取短整数值。 3995 3996**系统能力**:SystemCapability.Communication.IPC.Core 3997 3998**返回值:** 3999 4000 | 类型 | 说明 | 4001 | ------ | -------------- | 4002 | number | 返回短整数值。 | 4003 4004**示例:** 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 4020将整数值写入MessageParcel实例。 4021 4022**系统能力**:SystemCapability.Communication.IPC.Core 4023 4024**参数:** 4025 4026 | 参数名 | 类型 | 必填 | 说明 | 4027 | ------ | ------ | ---- | ---------------- | 4028 | val | number | 是 | 要写入的整数值。 | 4029 4030**返回值:** 4031 4032 | 类型 | 说明 | 4033 | ------- | ----------------------------- | 4034 | boolean | true:写入成功,false:写入失败。 | 4035 4036**示例:** 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 4050从MessageParcel实例读取整数值。 4051 4052**系统能力**:SystemCapability.Communication.IPC.Core 4053 4054**返回值:** 4055 4056 | 类型 | 说明 | 4057 | ------ | ------------ | 4058 | number | 返回整数值。 | 4059 4060**示例:** 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 4076将长整数值写入MessageParcel实例。 4077 4078**系统能力**:SystemCapability.Communication.IPC.Core 4079 4080**参数:** 4081 4082 | 参数名 | 类型 | 必填 | 说明 | 4083 | ------ | ------ | ---- | ---------------- | 4084 | val | number | 是 | 要写入的长整数值 | 4085 4086**返回值:** 4087 4088 | 类型 | 说明 | 4089 | ------- | --------------------------------- | 4090 | boolean | true:写入成功,false:写入失败。| 4091 4092**示例:** 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 4106从MessageParcel实例中读取长整数值。 4107 4108**系统能力**:SystemCapability.Communication.IPC.Core 4109 4110**返回值:** 4111 4112 | 类型 | 说明 | 4113 | ------ | -------------- | 4114 | number | 返回长整数值。 | 4115 4116**示例:** 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 4132将浮点值写入MessageParcel实例。 4133 4134**系统能力**:SystemCapability.Communication.IPC.Core 4135 4136**参数:** 4137 4138 | 参数名 | 类型 | 必填 | 说明 | 4139 | ------ | ------ | ---- | ---------------- | 4140 | val | number | 是 | 要写入的浮点值。 | 4141 4142**返回值:** 4143 4144 | 类型 | 说明 | 4145 | ------- | --------------------------------- | 4146 | boolean | true:写入成功,false:写入失败。| 4147 4148**示例:** 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 4162从MessageParcel实例中读取浮点值。 4163 4164**系统能力**:SystemCapability.Communication.IPC.Core 4165 4166**返回值:** 4167 4168 | 类型 | 说明 | 4169 | ------ | ------------ | 4170 | number | 返回浮点值。 | 4171 4172**示例:** 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 4188将双精度浮点值写入MessageParcel实例。 4189 4190**系统能力**:SystemCapability.Communication.IPC.Core 4191 4192**参数:** 4193 4194 | 参数名 | 类型 | 必填 | 说明 | 4195 | ------ | ------ | ---- | ---------------------- | 4196 | val | number | 是 | 要写入的双精度浮点值。 | 4197 4198**返回值:** 4199 4200 | 类型 | 说明 | 4201 | ------- | --------------------------------- | 4202 | boolean | true:写入成功,false:写入失败。| 4203 4204**示例:** 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 4218从MessageParcel实例读取双精度浮点值。 4219 4220**系统能力**:SystemCapability.Communication.IPC.Core 4221 4222**返回值:** 4223 4224 | 类型 | 说明 | 4225 | ------ | ------------------ | 4226 | number | 返回双精度浮点值。 | 4227 4228**示例:** 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 4244将布尔值写入MessageParcel实例。 4245 4246**系统能力**:SystemCapability.Communication.IPC.Core 4247 4248**参数:** 4249 4250 | 参数名 | 类型 | 必填 | 说明 | 4251 | ------ | ------- | ---- | ---------------- | 4252 | val | boolean | 是 | 要写入的布尔值。 | 4253 4254**返回值:** 4255 4256 | 类型 | 说明 | 4257 | ------- | --------------------------------- | 4258 | boolean | true:写入成功,false:写入失败。| 4259 4260**示例:** 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 4274从MessageParcel实例读取布尔值。 4275 4276**系统能力**:SystemCapability.Communication.IPC.Core 4277 4278**返回值:** 4279 4280 | 类型 | 说明 | 4281 | ------- | -------------------- | 4282 | boolean | 返回读取到的布尔值。 | 4283 4284**示例:** 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 4300将单个字符值写入MessageParcel实例。 4301 4302**系统能力**:SystemCapability.Communication.IPC.Core 4303 4304**参数:** 4305 4306 | 参数名 | 类型 | 必填 | 说明 | 4307 | ------ | ------ | ---- | -------------------- | 4308 | val | number | 是 | 要写入的单个字符值。 | 4309 4310**返回值:** 4311 4312 | 类型 | 说明 | 4313 | ------- | ----------------------------- | 4314 | boolean | true:写入成功,false:写入失败。| 4315 4316**示例:** 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 4330从MessageParcel实例中读取单个字符值。 4331 4332**系统能力**:SystemCapability.Communication.IPC.Core 4333 4334**返回值:** 4335 4336 | 类型 | 说明 | 4337 | ------ | ---------------- | 4338 | number | 返回单个字符值。 | 4339 4340**示例:** 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 4356将字符串值写入MessageParcel实例。 4357 4358**系统能力**:SystemCapability.Communication.IPC.Core 4359 4360**参数:** 4361 4362 | 参数名 | 类型 | 必填 | 说明 | 4363 | ------ | ------ | ---- | ----------------------------------------- | 4364 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 4365 4366**返回值:** 4367 4368 | 类型 | 说明 | 4369 | ------- | --------------------------------- | 4370 | boolean | true:写入成功,false:写入失败。| 4371 4372**示例:** 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 4386从MessageParcel实例读取字符串值。 4387 4388**系统能力**:SystemCapability.Communication.IPC.Core 4389 4390**返回值:** 4391 4392 | 类型 | 说明 | 4393 | ------ | -------------- | 4394 | string | 返回字符串值。 | 4395 4396**示例:** 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 4412将自定义序列化对象写入MessageParcel实例。 4413 4414**系统能力**:SystemCapability.Communication.IPC.Core 4415 4416**参数:** 4417 4418 | 参数名 | 类型 | 必填 | 说明 | 4419 | ------ | ----------------------------- | ---- | -------------------- | 4420 | val | [Sequenceable](#sequenceabledeprecated) | 是 | 要写入的可序列对象。 | 4421 4422**返回值:** 4423 4424 | 类型 | 说明 | 4425 | ------- | -------------------------------- | 4426 | boolean | true:写入成功,false:写入失败。| 4427 4428**示例:** 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 4461从MessageParcel实例中读取成员变量到指定的对象(dataIn)。 4462 4463**系统能力**:SystemCapability.Communication.IPC.Core 4464 4465**参数:** 4466 4467 | 参数名 | 类型 | 必填 | 说明 | 4468 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4469 | dataIn | [Sequenceable](#sequenceabledeprecated) | 是 | 需要从MessageParcel读取成员变量的对象。 | 4470 4471**返回值:** 4472 4473 | 类型 | 说明 | 4474 | ------- | ---------------------------------------- | 4475 | boolean | true:反序列化成功,false:反序列化失败。| 4476 4477**示例:** 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 4513将字节数组写入MessageParcel实例。 4514 4515**系统能力**:SystemCapability.Communication.IPC.Core 4516 4517**参数:** 4518 4519 | 参数名 | 类型 | 必填 | 说明 | 4520 | --------- | -------- | ---- | ------------------ | 4521 | byteArray | number[] | 是 | 要写入的字节数组。 | 4522 4523**返回值:** 4524 4525 | 类型 | 说明 | 4526 | ------- | -------------------------------- | 4527 | boolean | true:写入成功,false:写入失败。| 4528 4529**示例:** 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 4544从MessageParcel实例读取字节数组。 4545 4546**系统能力**:SystemCapability.Communication.IPC.Core 4547 4548**参数:** 4549 4550 | 参数名 | 类型 | 必填 | 说明 | 4551 | ------ | -------- | ---- | ------------------ | 4552 | dataIn | number[] | 是 | 要读取的字节数组。 | 4553 4554**示例:** 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 4571从MessageParcel实例中读取字节数组。 4572 4573**系统能力**:SystemCapability.Communication.IPC.Core 4574 4575**返回值:** 4576 4577 | 类型 | 说明 | 4578 | -------- | -------------- | 4579 | number[] | 返回字节数组。 | 4580 4581**示例:** 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 4598将短整数数组写入MessageParcel实例。 4599 4600**系统能力**:SystemCapability.Communication.IPC.Core 4601 4602**参数:** 4603 4604 | 参数名 | 类型 | 必填 | 说明 | 4605 | ---------- | -------- | ---- | -------------------- | 4606 | shortArray | number[] | 是 | 要写入的短整数数组。 | 4607 4608**返回值:** 4609 4610 | 类型 | 说明 | 4611 | ------- | -------------------------------- | 4612 | boolean | true:写入成功,false:写入失败。| 4613 4614**示例:** 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 4628从MessageParcel实例中读取短整数数组。 4629 4630**系统能力**:SystemCapability.Communication.IPC.Core 4631 4632**参数:** 4633 4634 | 参数名 | 类型 | 必填 | 说明 | 4635 | ------ | -------- | ---- | -------------------- | 4636 | dataIn | number[] | 是 | 要读取的短整数数组。 | 4637 4638**示例:** 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 4654从MessageParcel实例中读取短整数数组。 4655 4656**系统能力**:SystemCapability.Communication.IPC.Core 4657 4658**返回值:** 4659 4660 | 类型 | 说明 | 4661 | -------- | ---------------- | 4662 | number[] | 返回短整数数组。 | 4663 4664**示例:** 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 4680将整数数组写入MessageParcel实例。 4681 4682**系统能力**:SystemCapability.Communication.IPC.Core 4683 4684**参数:** 4685 4686 | 参数名 | 类型 | 必填 | 说明 | 4687 | -------- | -------- | ---- | ------------------ | 4688 | intArray | number[] | 是 | 要写入的整数数组。 | 4689 4690**返回值:** 4691 4692 | 类型 | 说明 | 4693 | ------- | -------------------------------- | 4694 | boolean | true:写入成功,false:写入失败。| 4695 4696**示例:** 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 4710从MessageParcel实例中读取整数数组。 4711 4712**系统能力**:SystemCapability.Communication.IPC.Core 4713 4714**参数:** 4715 4716 | 参数名 | 类型 | 必填 | 说明 | 4717 | ------ | -------- | ---- | ------------------ | 4718 | dataIn | number[] | 是 | 要读取的整数数组。 | 4719 4720**示例:** 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 4736从MessageParcel实例中读取整数数组。 4737 4738**系统能力**:SystemCapability.Communication.IPC.Core 4739 4740**返回值:** 4741 4742 | 类型 | 说明 | 4743 | -------- | -------------- | 4744 | number[] | 返回整数数组。 | 4745 4746**示例:** 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 4762将长整数数组写入MessageParcel实例。 4763 4764**系统能力**:SystemCapability.Communication.IPC.Core 4765 4766**参数:** 4767 4768 | 参数名 | 类型 | 必填 | 说明 | 4769 | --------- | -------- | ---- | -------------------- | 4770 | longArray | number[] | 是 | 要写入的长整数数组。 | 4771 4772**返回值:** 4773 4774 | 类型 | 说明 | 4775 | ------- | ----------------------------- | 4776 | boolean | true:写入成功,false:写入失败。| 4777 4778**示例:** 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 4792从MessageParcel实例读取长整数数组。 4793 4794**系统能力**:SystemCapability.Communication.IPC.Core 4795 4796**参数:** 4797 4798 | 参数名 | 类型 | 必填 | 说明 | 4799 | ------ | -------- | ---- | -------------------- | 4800 | dataIn | number[] | 是 | 要读取的长整数数组。 | 4801 4802**示例:** 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 4818从MessageParcel实例中读取长整数数组。 4819 4820**系统能力**:SystemCapability.Communication.IPC.Core 4821 4822**返回值:** 4823 4824 | 类型 | 说明 | 4825 | -------- | ---------------- | 4826 | number[] | 返回长整数数组。 | 4827 4828**示例:** 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 4844将浮点数组写入MessageParcel实例。 4845 4846**系统能力**:SystemCapability.Communication.IPC.Core 4847 4848**参数:** 4849 4850 | 参数名 | 类型 | 必填 | 说明 | 4851 | ---------- | -------- | ---- | --- | 4852 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 4853 4854**返回值:** 4855 4856 | 类型 | 说明 | 4857 | ------- | -------------------------------- | 4858 | boolean | true:写入成功,false:写入失败。| 4859 4860**示例:** 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 4874从MessageParcel实例中读取浮点数组。 4875 4876**系统能力**:SystemCapability.Communication.IPC.Core 4877 4878**参数:** 4879 4880 | 参数名 | 类型 | 必填 | 说明 | 4881 | ------ | -------- | ---- | ------ | 4882 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 4883 4884**示例:** 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 4900从MessageParcel实例中读取浮点数组。 4901 4902**系统能力**:SystemCapability.Communication.IPC.Core 4903 4904**返回值:** 4905 4906 | 类型 | 说明 | 4907 | -------- | -------------- | 4908 | number[] | 返回浮点数组。 | 4909 4910**示例:** 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 4926将双精度浮点数组写入MessageParcel实例。 4927 4928**系统能力**:SystemCapability.Communication.IPC.Core 4929 4930**参数:** 4931 4932 | 参数名 | 类型 | 必填 | 说明 | 4933 | ----------- | -------- | ---- | ------------------------ | 4934 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 4935 4936**返回值:** 4937 4938 | 类型 | 说明 | 4939 | ------- | -------------------------------- | 4940 | boolean | true:写入成功,false:写入失败。| 4941 4942**示例:** 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 4956从MessageParcel实例中读取双精度浮点数组。 4957 4958**系统能力**:SystemCapability.Communication.IPC.Core 4959 4960**参数:** 4961 4962 | 参数名 | 类型 | 必填 | 说明 | 4963 | ------ | -------- | ---- | ------------------------ | 4964 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 4965 4966**示例:** 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 4982从MessageParcel实例读取双精度浮点数组。 4983 4984**系统能力**:SystemCapability.Communication.IPC.Core 4985 4986**返回值:** 4987 4988 | 类型 | 说明 | 4989 | -------- | -------------------- | 4990 | number[] | 返回双精度浮点数组。 | 4991 4992**示例:** 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 5008将布尔数组写入MessageParcel实例。 5009 5010**系统能力**:SystemCapability.Communication.IPC.Core 5011 5012**参数:** 5013 5014 | 参数名 | 类型 | 必填 | 说明 | 5015 | ------------ | --------- | ---- | ------------------ | 5016 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 5017 5018**返回值:** 5019 5020 | 类型 | 说明 | 5021 | ------- | -------------------------------- | 5022 | boolean | true:写入成功,false:写入失败。| 5023 5024**示例:** 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 5038从MessageParcel实例中读取布尔数组。 5039 5040**系统能力**:SystemCapability.Communication.IPC.Core 5041 5042**参数:** 5043 5044 | 参数名 | 类型 | 必填 | 说明 | 5045 | ------ | --------- | ---- | ------------------ | 5046 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 5047 5048**示例:** 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 5064从MessageParcel实例中读取布尔数组。 5065 5066**系统能力**:SystemCapability.Communication.IPC.Core 5067 5068**返回值:** 5069 5070 | 类型 | 说明 | 5071 | --------- | -------------- | 5072 | boolean[] | 返回布尔数组。 | 5073 5074**示例:** 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 5090将单个字符数组写入MessageParcel实例。 5091 5092**系统能力**:SystemCapability.Communication.IPC.Core 5093 5094**参数:** 5095 5096 | 参数名 | 类型 | 必填 | 说明 | 5097 | --------- | -------- | ---- | ---------------------- | 5098 | charArray | number[] | 是 | 要写入的单个字符数组。 | 5099 5100**返回值:** 5101 5102 | 类型 | 说明 | 5103 | ------- | -------------------------------- | 5104 | boolean | true:写入成功,false:写入失败。| 5105 5106**示例:** 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 5120从MessageParcel实例中读取单个字符数组。 5121 5122**系统能力**:SystemCapability.Communication.IPC.Core 5123 5124**参数:** 5125 5126 | 参数名 | 类型 | 必填 | 说明 | 5127 | ------ | -------- | ---- | ---------------------- | 5128 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 5129 5130**示例:** 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 5146从MessageParcel实例读取单个字符数组。 5147 5148**系统能力**:SystemCapability.Communication.IPC.Core 5149 5150**返回值:** 5151 5152 | 类型 | 说明 | 5153 | -------- | ------------------ | 5154 | number[] | 返回单个字符数组。 | 5155 5156**示例:** 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 5172将字符串数组写入MessageParcel实例。 5173 5174**系统能力**:SystemCapability.Communication.IPC.Core 5175 5176**参数:** 5177 5178 | 参数名 | 类型 | 必填 | 说明 | 5179 | ----------- | -------- | ---- | ---------------- | 5180 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 5181 5182**返回值:** 5183 5184 | 类型 | 说明 | 5185 | ------- | -------------------------------- | 5186 | boolean | true:写入成功,false:写入失败。| 5187 5188**示例:** 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 5202从MessageParcel实例读取字符串数组。 5203 5204**系统能力**:SystemCapability.Communication.IPC.Core 5205 5206**参数:** 5207 5208 | 参数名 | 类型 | 必填 | 说明 | 5209 | ------ | -------- | ---- | -------------------- | 5210 | dataIn | string[] | 是 | 要读取的字符串数组。 | 5211 5212**示例:** 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 5228从MessageParcel实例读取字符串数组。 5229 5230**系统能力**:SystemCapability.Communication.IPC.Core 5231 5232**返回值:** 5233 5234 | 类型 | 说明 | 5235 | -------- | ---------------- | 5236 | string[] | 返回字符串数组。 | 5237 5238**示例:** 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 5254向MessageParcel写入“指示未发生异常”的信息。 5255 5256**系统能力**:SystemCapability.Communication.IPC.Core 5257 5258**示例:** 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 5298从MessageParcel中读取异常。 5299 5300**系统能力**:SystemCapability.Communication.IPC.Core 5301 5302**示例:** 5303 5304 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 5305 5306 ```ts 5307 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 5332 // FA.connectAbility(want,connect); 5333 5334 this.context.connectServiceExtensionAbility(want, connect); 5335 ``` 5336 5337 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 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 5373将可序列化对象数组写入MessageParcel实例。 5374 5375**系统能力**:SystemCapability.Communication.IPC.Core 5376 5377**参数:** 5378 5379| 参数名 | 类型 | 必填 | 说明 | 5380| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5381| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要写入的可序列化对象数组。 | 5382 5383**返回值:** 5384 5385 | 类型 | 说明 | 5386 | ------- | -------------------------------- | 5387 | boolean | true:写入成功,false:写入失败。| 5388 5389**示例:** 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 5425从MessageParcel实例读取可序列化对象数组。 5426 5427**系统能力**:SystemCapability.Communication.IPC.Core 5428 5429**参数:** 5430 5431| 参数名 | 类型 | 必填 | 说明 | 5432| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5433| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要读取的可序列化对象数组。 | 5434 5435**示例:** 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 5473将IRemoteObject对象数组写入MessageParcel。 5474 5475**系统能力**:SystemCapability.Communication.IPC.Core 5476 5477**参数:** 5478 5479 | 参数名 | 类型 | 必填 | 说明 | 5480 | ----------- | --------------- | ---- | ----- | 5481 | objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageParcel的IRemoteObject对象数组。 | 5482 5483**返回值:** 5484 5485 | 类型 | 说明 | 5486 | ------- | -------------------------------- | 5487 | boolean | true:写入成功,false:写入失败。| 5488 5489**示例:** 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 5527从MessageParcel读取IRemoteObject对象数组。 5528 5529**系统能力**:SystemCapability.Communication.IPC.Core 5530 5531**参数:** 5532 5533 | 参数名 | 类型 | 必填 | 说明 | 5534 | ------- | --------------- | ---- | --------- | 5535 | objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageParcel读取的IRemoteObject对象数组。 | 5536 5537**示例:** 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 5576从MessageParcel读取IRemoteObject对象数组。 5577 5578**系统能力**:SystemCapability.Communication.IPC.Core 5579 5580**返回值:** 5581 5582 | 类型 | 说明 | 5583 | --------------- | --------------------------- | 5584 | [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 5585 5586**示例:** 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 5626静态方法,关闭给定的文件描述符。 5627 5628**系统能力**:SystemCapability.Communication.IPC.Core 5629 5630**参数:** 5631 5632 | 参数名 | 类型 | 必填 | 说明 | 5633 | ------ | ------ | ---- | -------------------- | 5634 | fd | number | 是 | 要关闭的文件描述符。 | 5635 5636**示例:** 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 5650静态方法,复制给定的文件描述符。 5651 5652**系统能力**:SystemCapability.Communication.IPC.Core 5653 5654**参数:** 5655 5656 | 参数名 | 类型 | 必填 | 说明 | 5657 | ------ | ------ | ---- | ------------------------ | 5658 | fd | number | 是 | 表示已存在的文件描述符。 | 5659 5660**返回值:** 5661 5662 | 类型 | 说明 | 5663 | ------ | -------------------- | 5664 | number | 返回新的文件描述符。 | 5665 5666**示例:** 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 5680检查此MessageParcel对象是否包含文件描述符。 5681 5682**系统能力**:SystemCapability.Communication.IPC.Core 5683 5684**返回值:** 5685 5686 | 类型 | 说明 | 5687 | ------- | --------------------------------------------- | 5688 | boolean |true:包含文件描述符,false:未包含文件描述符。| 5689 5690**示例:** 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 5709写入文件描述符到MessageParcel。 5710 5711**系统能力**:SystemCapability.Communication.IPC.Core 5712 5713**参数:** 5714 5715 | 参数名 | 类型 | 必填 | 说明 | 5716 | ------ | ------ | ---- | ------------ | 5717 | fd | number | 是 | 文件描述符。 | 5718 5719**返回值:** 5720 5721 | 类型 | 说明 | 5722 | ------- | -------------------------------- | 5723 | boolean | true:操作成功,false:操作失败。| 5724 5725**示例:** 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 5742从MessageParcel中读取文件描述符。 5743 5744**系统能力**:SystemCapability.Communication.IPC.Core 5745 5746**返回值:** 5747 5748 | 类型 | 说明 | 5749 | ------ | ---------------- | 5750 | number | 返回文件描述符。 | 5751 5752**示例:** 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 5770将指定的匿名共享对象写入此MessageParcel。 5771 5772**系统能力**:SystemCapability.Communication.IPC.Core 5773 5774**参数:** 5775 5776| 参数名 | 类型 | 必填 | 说明 | 5777| ------ | ------ | ---- | ----------------------------------- | 5778| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageParcel的匿名共享对象。 | 5779 5780**返回值:** 5781 5782 | 类型 | 说明 | 5783 | ------- | -------------------------------- | 5784 | boolean | true:写入成功,false:写入失败。| 5785 5786**示例:** 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 5801从MessageParcel读取匿名共享对象。 5802 5803**系统能力**:SystemCapability.Communication.IPC.Core 5804 5805**返回值:** 5806 5807| 类型 | 说明 | 5808| ------ | ------------------ | 5809| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 5810 5811**示例:** 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 5828获取MessageParcel可以容纳的最大原始数据量。 5829 5830**系统能力**:SystemCapability.Communication.IPC.Core 5831 5832**返回值:** 5833 5834 | 类型 | 说明 | 5835 | ------ | ---------------------------------------------------------- | 5836 | number | 返回MessageParcel可以容纳的最大原始数据量,即128 Mb。 | 5837 5838**示例:** 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 5852将原始数据写入MessageParcel对象。 5853 5854**系统能力**:SystemCapability.Communication.IPC.Core 5855 5856**参数:** 5857 5858 | 参数名 | 类型 | 必填 | 说明 | 5859 | ------- | -------- | ---- | ---------------------------------- | 5860 | rawData | number[] | 是 | 要写入的原始数据。 | 5861 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 5862 5863**返回值:** 5864 5865 | 类型 | 说明 | 5866 | ------- | -------------------------------- | 5867 | boolean | true:写入成功,false:写入失败。| 5868 5869**示例:** 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 5884从MessageParcel读取原始数据。 5885 5886**系统能力**:SystemCapability.Communication.IPC.Core 5887 5888**参数:** 5889 5890 | 参数名 | 类型 | 必填 | 说明 | 5891 | ------ | ------ | ---- | ------------------------ | 5892 | size | number | 是 | 要读取的原始数据的大小。 | 5893 5894**返回值:** 5895 5896 | 类型 | 说明 | 5897 | -------- | ------------------------------ | 5898 | number[] | 返回原始数据(以字节为单位)。 | 5899 5900**示例:** 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 5915在进程间通信(IPC)期间,将类的对象写入MessageSequence并从MessageSequence中恢复它们。 5916 5917### marshalling 5918 5919marshalling(dataOut: MessageSequence): boolean 5920 5921将此可序列对象封送到MessageSequence中。 5922 5923**系统能力**:SystemCapability.Communication.IPC.Core 5924 5925**参数:** 5926 5927| 参数名 | 类型 | 必填 | 说明 | 5928| ------- | --------------- | ---- | ------------------------------------------- | 5929| dataOut |[MessageSequence](#messagesequence9)| 是 | 可序列对象将被封送到的MessageSequence对象。 | 5930 5931**返回值:** 5932 5933 | 类型 | 说明 | 5934 | ------- | -------------------------------- | 5935 | boolean | true:封送成功,false:封送失败。| 5936 5937**示例:** 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 5973从MessageSequence中解封此可序列对象。 5974 5975**系统能力**:SystemCapability.Communication.IPC.Core 5976 5977**参数:** 5978 5979| 参数名 | 类型 | 必填 | 说明 | 5980| ------ | --------------- | ---- | ----------------------------------------------- | 5981| dataIn | [MessageSequence](#messagesequence9) | 是 | 已将可序列对象封送到其中的MessageSequence对象。 | 5982 5983**返回值:** 5984 5985 | 类型 | 说明 | 5986 | ------- | ---------------------------------------- | 5987 | boolean | true:反序列化成功,false:反序列化失败。| 5988 5989**示例:** 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>从API version 9 开始不再维护,建议使用[Parcelable](#parcelable9)类替代。 6024 6025在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。 6026 6027### marshalling 6028 6029marshalling(dataOut: MessageParcel): boolean 6030 6031将此可序列对象封送到MessageParcel中。 6032 6033**系统能力**:SystemCapability.Communication.IPC.Core 6034 6035**参数:** 6036 6037 | 参数名 | 类型 | 必填 | 说明 | 6038 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6039 | dataOut | [MessageParcel](#messageparceldeprecated) | 是 | 可序列对象将被封送到的MessageParcel对象。 | 6040 6041**返回值:** 6042 6043 | 类型 | 说明 | 6044 | ------- | -------------------------------- | 6045 | boolean | true:封送成功,false:封送失败。 | 6046 6047**示例:** 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 6083从MessageParcel中解封此可序列对象。 6084 6085**系统能力**:SystemCapability.Communication.IPC.Core 6086 6087**参数:** 6088 6089 | 参数名 | 类型 | 必填 | 说明 | 6090 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6091 | dataIn | [MessageParcel](#messageparceldeprecated) | 是 | 已将可序列对象封送到其中的MessageParcel对象。 | 6092 6093**返回值:** 6094 6095 | 类型 | 说明 | 6096 | ------- | ---------------------------------------- | 6097 | boolean | true:反序列化成功,false:反序列化失败。| 6098 6099**示例:** 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 6133远端对象的代理持有者。用于获取代理对象。 6134 6135### asObject 6136 6137asObject(): IRemoteObject 6138 6139需派生类实现,获取代理或远端对象。 6140 6141**系统能力**:SystemCapability.Communication.IPC.Core 6142 6143**返回值:** 6144 6145 | 类型 | 说明 | 6146 | ----- | ----- | 6147 | [IRemoteObject](#iremoteobject) | 如果调用者是RemoteObject对象,则直接返回本身;如果调用者是[RemoteProxy](#remoteproxy)对象,则返回它的持有者[IRemoteObject](#iremoteobject)。 | 6148 6149**示例:** 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**示例:** 6161 6162 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 6163 6164 ```ts 6165 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 6191 // FA.connectAbility(want,connect); 6192 6193 this.context.connectServiceExtensionAbility(want, connect); 6194 ``` 6195 6196 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象 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 6215用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied](#onremotedied)接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。 6216 6217### onRemoteDied 6218 6219onRemoteDied(): void 6220 6221在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。 6222 6223**系统能力**:SystemCapability.Communication.IPC.Core 6224 6225**示例:** 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 6239发送请求的响应结果。 6240 6241**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6242 6243| 名称 | 类型 | 可读 | 可写 | 说明 | 6244| ------- | --------------- | ---- | ---- |-------------------------------------- | 6245| errCode | number | 是 | 否 | 错误码。 | 6246| code | number | 是 | 否 | 消息代码。 | 6247| data | [MessageSequence](#messagesequence9) | 是 | 否 | 发送给对端进程的MessageSequence对象。 | 6248| reply | [MessageSequence](#messagesequence9) | 是 | 否 | 对端进程返回的MessageSequence对象。 | 6249 6250## SendRequestResult<sup>8+(deprecated)</sup> 6251 6252>从API version 9 开始不再维护,建议使用[RequestResult](#requestresult9)类替代。 6253 6254发送请求的响应结果。 6255 6256**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6257 6258 | 名称 | 类型 | 可读 | 可写 | 说明 | 6259 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6260 | errCode | number | 是 | 否 | 错误码。 | 6261 | code | number | 是 | 否 | 消息代码。 | 6262 | data | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 发送给对端进程的MessageParcel对象。 | 6263 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 对端进程返回的MessageParcel对象。 | 6264 6265## IRemoteObject 6266 6267该接口可用于查询或获取接口描述符、添加或删除死亡通知、转储对象状态到特定文件、发送消息。 6268 6269### getLocalInterface<sup>9+</sup> 6270 6271getLocalInterface(descriptor: string): IRemoteBroker 6272 6273查询接口描述符的字符串。 6274 6275**系统能力**:SystemCapability.Communication.IPC.Core 6276 6277**参数:** 6278 6279 | 参数名 | 类型 | 必填 | 说明 | 6280 | ---------- | ------ | ---- | -------------------- | 6281 | descriptor | string | 是 | 接口描述符的字符串。 | 6282 6283**返回值:** 6284 6285| 类型 | 说明 | 6286| ------------- | --------------------------------------------- | 6287| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6288 6289### queryLocalInterface<sup>(deprecated)</sup> 6290 6291>从API version 9 开始不再维护,建议使用[getLocalInterface](#getlocalinterface9)类替代。 6292 6293queryLocalInterface(descriptor: string): IRemoteBroker 6294 6295查询接口描述符的字符串。 6296 6297**系统能力**:SystemCapability.Communication.IPC.Core 6298 6299**参数:** 6300 6301 | 参数名 | 类型 | 必填 | 说明 | 6302 | ---------- | ------ | ---- | -------------------- | 6303 | descriptor | string | 是 | 接口描述符的字符串。 | 6304 6305**返回值:** 6306 6307| 类型 | 说明 | 6308| ------------- | --------------------------------------------- | 6309| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6310 6311### sendRequest<sup>(deprecated)</sup> 6312 6313>从API version 8开始不再维护,建议使用[sendRequest](#sendrequest8deprecated)类替代。 6314 6315sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6316 6317以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6318 6319**系统能力**:SystemCapability.Communication.IPC.Core 6320 6321**参数:** 6322 6323 | 参数名 | 类型 | 必填 | 说明 | 6324 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6325 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6326 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 6327 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6328 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6329 6330**返回值:** 6331 6332 | 类型 | 说明 | 6333 | ------- | -------------------------------- | 6334 | boolean | true:发送成功,false:发送失败。| 6335 6336### sendMessageRequest<sup>9+</sup> 6337 6338sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6339 6340以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 6341 6342**系统能力**:SystemCapability.Communication.IPC.Core 6343 6344**参数:** 6345 6346 | 参数名 | 类型 | 必填 | 说明 | 6347 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6348 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6349 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的 MessageSequence对象。 | 6350 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6351 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6352 6353**返回值:** 6354 6355 | 类型 | 说明 | 6356 | ---------------------------- | ----------------------------------------- | 6357 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 6358 6359### sendRequest<sup>8+(deprecated)</sup> 6360 6361>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9)类替代。 6362 6363sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6364 6365以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6366 6367**系统能力**:SystemCapability.Communication.IPC.Core 6368 6369**参数:** 6370 6371 | 参数名 | 类型 | 必填 | 说明 | 6372 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6373 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6374 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 6375 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6376 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6377 6378**返回值:** 6379 6380| 类型 | 说明 | 6381| ------------------------------------------------------------ | --------------------------------------------- | 6382| Promise<[SendRequestResult](#sendrequestresult8deprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 6383 6384### sendMessageRequest<sup>9+</sup> 6385 6386sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6387 6388以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6389 6390**系统能力**:SystemCapability.Communication.IPC.Core 6391 6392**参数:** 6393 6394 | 参数名 | 类型 | 必填 | 说明 | 6395 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6396 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6397 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的 MessageSequence对象。 | 6398 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6399 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6400 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 6401 6402### sendRequest<sup>8+(deprecated)</sup> 6403 6404>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9-1)类替代。 6405 6406sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6407 6408以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6409 6410**系统能力**:SystemCapability.Communication.IPC.Core 6411 6412**参数:** 6413 6414| 参数名 | 类型 | 必填 | 说明 | 6415| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6416| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6417| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 6418| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6419| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6420| callback | AsyncCallback<[SendRequestResult](#sendrequestresult8deprecated)> | 是 | 接收发送结果的回调。 | 6421 6422### registerDeathRecipient<sup>9+</sup> 6423 6424registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6425 6426注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6427 6428**系统能力**:SystemCapability.Communication.IPC.Core 6429 6430**参数:** 6431 6432 | 参数名 | 类型 | 必填 | 说明 | 6433 | --------- | --------------------------------- | ---- | -------------- | 6434 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6435 | flags | number | 是 | 死亡通知标志。 | 6436 6437**错误码:** 6438 6439以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 6440 6441 | 错误码ID | 错误信息 | 6442 | -------- | -------- | 6443 | 1900008 | proxy or remote object is invalid | 6444 6445### addDeathrecipient<sup>(deprecated)</sup> 6446 6447>从API version 9 开始不再维护,建议使用[registerDeathRecipient](#registerdeathrecipient9)类替代。 6448 6449addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6450 6451注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6452 6453**系统能力**:SystemCapability.Communication.IPC.Core 6454 6455**参数:** 6456 6457 | 参数名 | 类型 | 必填 | 说明 | 6458 | --------- | --------------------------------- | ---- | -------------- | 6459 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6460 | flags | number | 是 | 死亡通知标志。 | 6461 6462**返回值:** 6463 6464 | 类型 | 说明 | 6465 | ------- | ---------------------------------------- | 6466 | boolean | true:回调注册成功,false:回调注册失败。| 6467 6468### unregisterDeathRecipient<sup>9+</sup> 6469 6470unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6471 6472注销用于接收远程对象死亡通知的回调。 6473 6474**系统能力**:SystemCapability.Communication.IPC.Core 6475 6476**参数:** 6477 6478 | 参数名 | 类型 | 必填 | 说明 | 6479 | --------- | --------------------------------- | ---- | -------------- | 6480 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6481 | flags | number | 是 | 死亡通知标志。 | 6482 6483**错误码:** 6484 6485以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 6486 6487 | 错误码ID | 错误信息 | 6488 | -------- | -------- | 6489 | 1900008 | proxy or remote object is invalid | 6490 6491### removeDeathRecipient<sup>(deprecated)</sup> 6492 6493>从API version 9 开始不再维护,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)类替代。 6494 6495removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6496 6497注销用于接收远程对象死亡通知的回调。 6498 6499**系统能力**:SystemCapability.Communication.IPC.Core 6500 6501**参数:** 6502 6503 | 参数名 | 类型 | 必填 | 说明 | 6504 | --------- | --------------------------------- | ---- | -------------- | 6505 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6506 | flags | number | 是 | 死亡通知标志。 | 6507 6508**返回值:** 6509 6510 | 类型 | 说明 | 6511 | ------- | -----------------------------------------| 6512 | boolean | true:回调注销成功,false:回调注销失败。| 6513 6514### getDescriptor<sup>9+</sup> 6515 6516getDescriptor(): string 6517 6518获取对象的接口描述符,接口描述符为字符串。 6519 6520**系统能力**:SystemCapability.Communication.IPC.Core 6521 6522**返回值:** 6523 6524 | 类型 | 说明 | 6525 | ------ | ---------------- | 6526 | string | 返回接口描述符。 | 6527 6528**错误码:** 6529 6530以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 6531 6532 | 错误码ID | 错误信息 | 6533 | -------- | -------- | 6534 | 1900008 | proxy or remote object is invalid | 6535 6536### getInterfaceDescriptor<sup>(deprecated)</sup> 6537 6538>从API version 9 开始不再维护,建议使用[getDescriptor](#getdescriptor9)类替代。 6539 6540getInterfaceDescriptor(): string 6541 6542获取对象的接口描述符,接口描述符为字符串。 6543 6544**系统能力**:SystemCapability.Communication.IPC.Core 6545 6546**返回值:** 6547 6548 | 类型 | 说明 | 6549 | ------ | ---------------- | 6550 | string | 返回接口描述符。 | 6551 6552### isObjectDead 6553 6554isObjectDead(): boolean 6555 6556检查当前对象是否死亡。 6557 6558**系统能力**:SystemCapability.Communication.IPC.Core 6559 6560**返回值:** 6561 6562 | 类型 | 说明 | 6563 | ------- | ---------------------------------- | 6564 | boolean | true:对象死亡,false:对象未死亡。| 6565 6566## RemoteProxy 6567 6568实现IRemoteObject代理对象。 6569 6570**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6571 6572| 名称 | 值 | 说明 | 6573| --------------------- | ----------------------- | --------------------------------- | 6574| PING_TRANSACTION | 1599098439 (0x5f504e47) | 内部指令码,用于测试IPC服务正常。 | 6575| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | 内部指令码,获取Binder内部状态。 | 6576| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | 内部指令码,获取对端接口描述符。 | 6577| MIN_TRANSACTION_ID | 1 (0x00000001) | 最小有效指令码。 | 6578| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | 最大有效指令码。 | 6579 6580### sendRequest<sup>(deprecated)</sup> 6581 6582>从API version 8 开始不再维护,建议使用[sendRequest](#sendrequest8deprecated-2)类替代。 6583 6584sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6585 6586以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6587 6588**系统能力**:SystemCapability.Communication.IPC.Core 6589 6590**参数:** 6591 6592 | 参数名 | 类型 | 必填 | 说明 | 6593 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6594 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6595 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 6596 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6597 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6598 6599**返回值:** 6600 6601 | 类型 | 说明 | 6602 | ------- | ---------------------------------| 6603 | boolean | true:发送成功,false:发送失败。| 6604 6605**示例:** 6606 6607 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 6608 6609 ```ts 6610 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 6636 // FA.connectAbility(want,connect); 6637 6638 this.context.connectServiceExtensionAbility(want, connect); 6639 ``` 6640 6641 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 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 6670以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 6671 6672**系统能力**:SystemCapability.Communication.IPC.Core 6673 6674**参数:** 6675 6676 | 参数名 | 类型 | 必填 | 说明 | 6677 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6678 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6679 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的 MessageSequence对象。 | 6680 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6681 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6682 6683**返回值:** 6684 6685 | 类型 | 说明 | 6686 | ---------------------------- | ----------------------------------------- | 6687 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 6688 6689**示例:** 6690 6691 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 6692 6693 ```ts 6694 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 6719 // FA.connectAbility(want,connect); 6720 6721 this.context.connectServiceExtensionAbility(want, connect); 6722 ``` 6723 6724 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 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>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9-2)类替代。 6759 6760sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6761 6762以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6763 6764**系统能力**:SystemCapability.Communication.IPC.Core 6765 6766**参数:** 6767 6768 | 参数名 | 类型 | 必填 | 说明 | 6769 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6770 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6771 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 6772 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6773 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6774 6775**返回值:** 6776 6777| 类型 | 说明 | 6778| ------------------------------------------------------------ | --------------------------------------------- | 6779| Promise<[SendRequestResult](#sendrequestresult8deprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 6780 6781**示例:** 6782 6783 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 6784 6785 ```ts 6786 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 6811 // FA.connectAbility(want,connect); 6812 6813 this.context.connectServiceExtensionAbility(want, connect); 6814 ``` 6815 6816 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 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 6853以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回后的某个时机执行回调,回复内容在RequestResult的reply报文里。 6854 6855**系统能力**:SystemCapability.Communication.IPC.Core 6856 6857**参数:** 6858 6859 | 参数名 | 类型 | 必填 | 说明 | 6860 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6861 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6862 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的 MessageSequence对象。 | 6863 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6864 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6865 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 6866 6867**示例:** 6868 6869 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 6870 6871 ```ts 6872 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 6912 // FA.connectAbility(want,connect); 6913 6914 this.context.connectServiceExtensionAbility(want, connect); 6915 ``` 6916 6917 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 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>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9-3)类替代。 6942 6943sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6944 6945以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6946 6947**系统能力**:SystemCapability.Communication.IPC.Core 6948 6949**参数:** 6950 6951| 参数名 | 类型 | 必填 | 说明 | 6952| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6953| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6954| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 6955| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6956| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6957| callback | AsyncCallback<[SendRequestResult](#sendrequestresult8deprecated)> | 是 | 接收发送结果的回调。 | 6958 6959**示例:** 6960 6961 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 6962 6963 ```ts 6964 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7004 // FA.connectAbility(want,connect); 7005 7006 this.context.connectServiceExtensionAbility(want, connect); 7007 ``` 7008 7009 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 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 7026查询并获取当前接口描述符对应的本地接口对象。 7027 7028**系统能力**:SystemCapability.Communication.IPC.Core 7029 7030**参数:** 7031 7032 | 参数名 | 类型 | 必填 | 说明 | 7033 | --------- | ------ | ---- | ---------------------- | 7034 | interface | string | 是 | 需要查询的接口描述符。 | 7035 7036**返回值:** 7037 7038| 类型 | 说明 | 7039| ------------------------------- | ------------------------------------------ | 7040| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7041 7042**错误码:** 7043 7044以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 7045 7046 | 错误码ID | 错误信息 | 7047 | -------- | -------- | 7048 | 1900006 | only remote object permitted | 7049 7050**示例:** 7051 7052 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7053 7054 ```ts 7055 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7080 // FA.connectAbility(want,connect); 7081 7082 this.context.connectServiceExtensionAbility(want, connect); 7083 ``` 7084 7085 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象 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>从API version 9 开始不再维护,建议使用[getLocalInterface](#getlocalinterface9-1)类替代。 7106 7107queryLocalInterface(interface: string): IRemoteBroker 7108 7109查询并获取当前接口描述符对应的本地接口对象。 7110 7111**系统能力**:SystemCapability.Communication.IPC.Core 7112 7113**参数:** 7114 7115 | 参数名 | 类型 | 必填 | 说明 | 7116 | --------- | ------ | ---- | ---------------------- | 7117 | interface | string | 是 | 需要查询的接口描述符。 | 7118 7119**返回值:** 7120 7121| 类型 | 说明 | 7122| ------------------------------- | ------------------------------------------ | 7123| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7124 7125**示例:** 7126 7127 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7128 7129 ```ts 7130 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7155 // FA.connectAbility(want,connect); 7156 7157 this.context.connectServiceExtensionAbility(want, connect); 7158 ``` 7159 7160 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象 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 7175注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7176 7177**系统能力**:SystemCapability.Communication.IPC.Core 7178 7179**参数:** 7180 7181 | 参数名 | 类型 | 必填 | 说明 | 7182 | --------- | --------------------------------- | ---- | -------------- | 7183 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7184 | flags | number | 是 | 死亡通知标志。 | 7185 7186**错误码:** 7187 7188以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 7189 7190 | 错误码ID | 错误信息 | 7191 | -------- | -------- | 7192 | 1900008 | proxy or remote object is invalid | 7193 7194**示例:** 7195 7196 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7197 7198 ```ts 7199 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7224 // FA.connectAbility(want,connect); 7225 7226 this.context.connectServiceExtensionAbility(want, connect); 7227 ``` 7228 7229 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调 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>从API version 9 开始不再维护,建议使用[registerDeathRecipient](#registerdeathrecipient9-1)类替代。 7255 7256addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7257 7258注册用于接收远程对象死亡通知的回调,增加proxy对象上的死亡通知。 7259 7260**系统能力**:SystemCapability.Communication.IPC.Core 7261 7262**参数:** 7263 7264 | 参数名 | 类型 | 必填 | 说明 | 7265 | --------- | --------------------------------- | ---- | --------------------------------- | 7266 | recipient | [DeathRecipient](#deathrecipient) | 是 | 收件人表示要注册的回调。 | 7267 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7268 7269**返回值:** 7270 7271 | 类型 | 说明 | 7272 | ------- | ---------------------------------------- | 7273 | boolean | true:回调注册成功,false:回调注册失败。| 7274 7275**示例:** 7276 7277 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7278 7279 ```ts 7280 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7305 // FA.connectAbility(want,connect); 7306 7307 this.context.connectServiceExtensionAbility(want, connect); 7308 ``` 7309 7310 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecipient接口方法新增死亡回调 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 7330注销用于接收远程对象死亡通知的回调。 7331 7332**系统能力**:SystemCapability.Communication.IPC.Core 7333 7334**参数:** 7335 7336 | 参数名 | 类型 | 必填 | 说明 | 7337 | --------- | --------------------------------- | ---- | -------------- | 7338 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 7339 | flags | number | 是 | 死亡通知标志。 | 7340 7341**错误码:** 7342 7343以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 7344 7345 | 错误码ID | 错误信息 | 7346 | -------- | -------- | 7347 | 1900008 | proxy or remote object is invalid | 7348 7349**示例:** 7350 7351 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7352 7353 ```ts 7354 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7379 // FA.connectAbility(want,connect); 7380 7381 this.context.connectServiceExtensionAbility(want, connect); 7382 ``` 7383 7384 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 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>从API version 9 开始不再维护,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9-1)类替代。 7411 7412removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7413 7414注销用于接收远程对象死亡通知的回调。 7415 7416**系统能力**:SystemCapability.Communication.IPC.Core 7417 7418**参数:** 7419 7420 | 参数名 | 类型 | 必填 | 说明 | 7421 | --------- | --------------------------------- | ---- | --------------------------------- | 7422 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的死亡回调。 | 7423 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7424 7425**返回值:** 7426 7427 | 类型 | 说明 | 7428 | ------- | ---------------------------------------- | 7429 | boolean | true:回调注销成功,false:回调注销失败。| 7430 7431**示例:** 7432 7433 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7434 7435 ```ts 7436 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7461 // FA.connectAbility(want,connect); 7462 7463 this.context.connectServiceExtensionAbility(want, connect); 7464 ``` 7465 7466 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调 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 7487获取对象的接口描述符,接口描述符为字符串。 7488 7489**系统能力**:SystemCapability.Communication.IPC.Core 7490 7491**返回值:** 7492 7493 | 类型 | 说明 | 7494 | ------ | ---------------- | 7495 | string | 返回接口描述符。 | 7496 7497**错误码:** 7498 7499以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 7500 7501 | 错误码ID | 错误信息 | 7502 | -------- | -------- | 7503 | 1900008 | proxy or remote object is invalid | 7504 | 1900007 | communication failed | 7505 7506**示例:** 7507 7508 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7509 7510 ```ts 7511 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7536 // FA.connectAbility(want,connect); 7537 7538 this.context.connectServiceExtensionAbility(want, connect); 7539 ``` 7540 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符 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>从API version 9 开始不再维护,建议使用[getDescriptor](#getdescriptor9-1)类替代。 7561 7562getInterfaceDescriptor(): string 7563 7564查询当前代理对象接口的描述符。 7565 7566**系统能力**:SystemCapability.Communication.IPC.Core 7567 7568**返回值:** 7569 7570 | 类型 | 说明 | 7571 | ------ | ------------------ | 7572 | string | 当前的接口描述符。 | 7573 7574**示例:** 7575 7576 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7577 7578 ```ts 7579 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7604 // FA.connectAbility(want,connect); 7605 7606 this.context.connectServiceExtensionAbility(want, connect); 7607 ``` 7608 7609 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符 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 7624指示对应的RemoteObject是否死亡。 7625 7626**系统能力**:SystemCapability.Communication.IPC.Core 7627 7628**返回值:** 7629 7630 | 类型 | 说明 | 7631 | ------- | ------------------------------------------------- | 7632 | boolean | true:对应的对象已经死亡,false:对应的对象未死亡 | 7633 7634**示例:** 7635 7636 Stage模型的应用在获取服务前需要先获取context,具体方法可参考[获取context](#获取context) 7637 7638 ```ts 7639 // 仅FA模型需要导入@ohos.ability.featureAbility 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 // FA模型使用此方法连接服务 7664 // FA.connectAbility(want,connect); 7665 7666 this.context.connectServiceExtensionAbility(want, connect); 7667 ``` 7668 7669 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡 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 7682公共消息选项,使用指定的标志类型,构造指定的MessageOption对象。 7683 7684**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 7685 7686 | 名称 | 值 | 说明 | 7687 | ------------- | --------- | ----------------------------------------------------------- | 7688 | TF_SYNC | 0 (0x00) | 同步调用标识。 | 7689 | TF_ASYNC | 1 (0x01) | 异步调用标识。 | 7690 | TF_ACCEPT_FDS | 16 (0x10) | 指示sendMessageRequest<sup>9+</sup>接口可以返回文件描述符。 | 7691 | TF_WAIT_TIME | 4 (0x4) | RPC等待时间(单位/秒),不用于IPC的情况。 | 7692 7693### constructor<sup>9+</sup> 7694 7695constructor(async?: boolean) 7696 7697MessageOption构造函数。 7698 7699**系统能力**:SystemCapability.Communication.IPC.Core 7700 7701**参数:** 7702 7703| 参数名 | 类型 | 必填 | 说明 | 7704| ------ | ------- | ---- | ------------------------------------------------------------ | 7705| async | boolean | 否 | true:表示异步调用标志,false:表示同步调用标志。默认同步调用。 | 7706 7707**示例:** 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 7721MessageOption构造函数。 7722 7723**系统能力**:SystemCapability.Communication.IPC.Core 7724 7725**参数:** 7726 7727 | 参数名 | 类型 | 必填 | 说明 | 7728 | --------- | ------ | ---- | --------------------------------------------- | 7729 | syncFlags | number | 否 | 同步调用或异步调用标志。默认同步调用。 | 7730 | waitTime | number | 否 | 调用rpc最长等待时间。默认 TF_WAIT_TIME。 | 7731 7732**示例:** 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 7745获取SendMessageRequest调用中确定同步或是异步的标志。 7746 7747**系统能力**:SystemCapability.Communication.IPC.Core 7748 7749**返回值:** 7750 7751 | 类型 | 说明 | 7752 | ------- | ---------------------------------------- | 7753 | boolean | true:异步调用成功,false:同步调用成功。| 7754 7755**示例:** 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 7766设置SendMessageRequest调用中确定同步或是异步的标志。 7767 7768**系统能力**:SystemCapability.Communication.IPC.Core 7769 7770**参数:** 7771 7772| 参数名 | 类型 | 必填 | 说明 | 7773| ------ | ------- | ---- | ------------------------------------------------- | 7774| async | boolean | 是 | true:表示异步调用标志,false:表示同步调用标志。 | 7775 7776**示例:** 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 7790获取同步调用或异步调用标志。 7791 7792**系统能力**:SystemCapability.Communication.IPC.Core 7793 7794**返回值:** 7795 7796 | 类型 | 说明 | 7797 | ------ | ------------------------------------ | 7798 | number | 调用成功返回同步调用或异步调用标志。 | 7799 7800**示例:** 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 7823设置同步调用或异步调用标志。 7824 7825**系统能力**:SystemCapability.Communication.IPC.Core 7826 7827**参数:** 7828 7829 | 参数名 | 类型 | 必填 | 说明 | 7830 | ------ | ------ | ---- | ------------------------ | 7831 | flags | number | 是 | 同步调用或异步调用标志。 | 7832 7833**示例:** 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 7853获取rpc调用的最长等待时间。 7854 7855**系统能力**:SystemCapability.Communication.IPC.Core 7856 7857**返回值:** 7858 7859 | 类型 | 说明 | 7860 | ------ | ----------------- | 7861 | number | rpc最长等待时间。 | 7862 7863**示例:** 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 7884设置rpc调用最长等待时间。 7885 7886**系统能力**:SystemCapability.Communication.IPC.Core 7887 7888**参数:** 7889 7890 | 参数名 | 类型 | 必填 | 说明 | 7891 | -------- | ------ | ---- | --------------------- | 7892 | waitTime | number | 是 | rpc调用最长等待时间,上限为3000秒。 | 7893 7894**示例:** 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 7911用于获取IPC上下文信息,包括获取UID和PID、获取本端和对端设备ID、检查接口调用是否在同一设备上。 7912 7913### getContextObject 7914 7915static getContextObject(): IRemoteObject 7916 7917静态方法,获取系统能力的管理者。 7918 7919**系统能力**:SystemCapability.Communication.IPC.Core 7920 7921**返回值:** 7922 7923 | 类型 | 说明 | 7924 | ------------------------------- | -------------------- | 7925 | [IRemoteObject](#iremoteobject) | 返回系统能力管理者。 | 7926 7927**示例:** 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 7940静态方法,获取调用者的PID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的PID。 7941 7942**系统能力**:SystemCapability.Communication.IPC.Core 7943 7944**返回值:** 7945 7946 | 类型 | 说明 | 7947 | ------ | ----------------- | 7948 | number | 返回调用者的PID。 | 7949 7950**示例:** 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 7968静态方法,获取调用者的UID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的UID。 7969 7970**系统能力**:SystemCapability.Communication.IPC.Core 7971 7972**返回值:** 7973 7974 | 类型 | 说明 | 7975 | ------ | ----------------- | 7976 | number | 返回调用者的UID。 | 7977 7978**示例:** 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 7996静态方法,获取调用者的TokenId,用于被调用方对调用方的身份校验。 7997 7998**系统能力**:SystemCapability.Communication.IPC.Core 7999 8000**返回值:** 8001 8002 | 类型 | 说明 | 8003 | ------ | --------------------- | 8004 | number | 返回调用者的TokenId。 | 8005 8006**示例:** 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 8024静态方法,获取调用者进程所在的设备ID。 8025 8026**系统能力**:SystemCapability.Communication.IPC.Core 8027 8028**返回值:** 8029 8030 | 类型 | 说明 | 8031 | ------ | ---------------------------- | 8032 | string | 返回调用者进程所在的设备ID。 | 8033 8034**示例:** 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 8052静态方法,获取本端设备ID。 8053 8054**系统能力**:SystemCapability.Communication.IPC.Core 8055 8056**返回值:** 8057 8058 | 类型 | 说明 | 8059 | ------ | ------------------ | 8060 | string | 返回本地设备的ID。 | 8061 8062**示例:** 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 8080静态方法,检查当前通信对端是否是本设备的进程。 8081 8082**系统能力**:SystemCapability.Communication.IPC.Core 8083 8084**返回值:** 8085 8086 | 类型 | 说明 | 8087 | ------- | -------------------------------------------------- | 8088 | boolean | true:调用在同一台设备,false:调用未在同一台设备。| 8089 8090**示例:** 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 8108静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8109 8110**系统能力**:SystemCapability.Communication.IPC.Core 8111 8112**参数:** 8113 8114 | 参数名 | 类型 | 必填 | 说明 | 8115 | ------ | ------------------------------- | ---- | ------------------- | 8116 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8117 8118**示例:** 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>从API version 9 开始不再维护,建议使用[flushCmdBuffer](#flushcmdbuffer9)类替代。 8142 8143static flushCommands(object: IRemoteObject): number 8144 8145静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8146 8147**系统能力**:SystemCapability.Communication.IPC.Core 8148 8149**参数:** 8150 8151 | 参数名 | 类型 | 必填 | 说明 | 8152 | ------ | ------------------------------- | ---- | ------------------- | 8153 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8154 8155**返回值:** 8156 8157 | 类型 | 说明 | 8158 | ------ | --------------------------------------------------------------------------------- | 8159 | number | 如果操作成功,返回0;如果输入对象为空或RemoteObject,或者操作失败,返回错误代码。 | 8160 8161**示例:** 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 8194静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。 8195 8196**系统能力**:SystemCapability.Communication.IPC.Core 8197 8198**返回值:** 8199 8200 | 类型 | 说明 | 8201 | ------ | ------------------------------------ | 8202 | string | 返回包含远程用户的UID和PID的字符串。 | 8203 8204**示例:** 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 8222静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8223 8224**系统能力**:SystemCapability.Communication.IPC.Core 8225 8226**参数:** 8227 8228 | 参数名 | 类型 | 必填 | 说明 | 8229 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8230 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8231 8232**示例:** 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>从API version 9 开始不再维护,建议使用[restoreCallingIdentity](#restorecallingidentity9)类替代。 8250 8251static setCallingIdentity(identity: string): boolean 8252 8253静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8254 8255**系统能力**:SystemCapability.Communication.IPC.Core 8256 8257**参数:** 8258 8259 | 参数名 | 类型 | 必填 | 说明 | 8260 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8261 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8262 8263**返回值:** 8264 8265 | 类型 | 说明 | 8266 | ------- | ---------------------------------| 8267 | boolean | true:设置成功,false:设置失败。| 8268 8269**示例:** 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 8287实现远程对象。服务提供者必须继承此类。 8288 8289### constructor 8290 8291constructor(descriptor: string) 8292 8293RemoteObject构造函数。 8294 8295**系统能力**:SystemCapability.Communication.IPC.Core 8296 8297**参数:** 8298 8299 | 参数名 | 类型 | 必填 | 说明 | 8300 | ---------- | ------ | ---- | ------------ | 8301 | descriptor | string | 是 | 接口描述符。 | 8302 8303### sendRequest<sup>(deprecated)</sup> 8304 8305>从API version 8 开始不再维护,建议使用[sendRequest](#sendrequest8deprecated-4)类替代。 8306 8307sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8308 8309以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8310 8311**系统能力**:SystemCapability.Communication.IPC.Core 8312 8313**参数:** 8314 8315 | 参数名 | 类型 | 必填 | 说明 | 8316 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8317 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8318 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 8319 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8320 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8321 8322**返回值:** 8323 8324 | 类型 | 说明 | 8325 | ------- | -------------------------------- | 8326 | boolean | true:发送成功,false:发送失败。| 8327 8328**示例:** 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 8375以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 8376 8377**系统能力**:SystemCapability.Communication.IPC.Core 8378 8379**参数:** 8380 8381 | 参数名 | 类型 | 必填 | 说明 | 8382 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8383 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8384 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的 MessageSequence对象。 | 8385 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8386 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8387 8388**返回值:** 8389 8390| 类型 | 说明 | 8391| ----------------------------------------------- | ----------------------------------------- | 8392| Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是RequestResult实例。 | 8393 8394**示例:** 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>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9-4)类替代。 8433 8434sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8435 8436以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8437 8438**系统能力**:SystemCapability.Communication.IPC.Core 8439 8440**参数:** 8441 8442 | 参数名 | 类型 | 必填 | 说明 | 8443 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8444 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8445 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 8446 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8447 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8448 8449**返回值:** 8450 8451| 类型 | 说明 | 8452| ------------------------------------------------------------ | --------------------------------------------- | 8453| Promise<[SendRequestResult](#sendrequestresult8deprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 8454 8455**示例:** 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 8510以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回时收到回调,回复内容在reply报文里。 8511 8512**系统能力**:SystemCapability.Communication.IPC.Core 8513 8514**参数:** 8515 8516| 参数名 | 类型 | 必填 | 说明 | 8517| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8518| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8519| data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的 MessageSequence对象。 | 8520| reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8521| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8522| AsyncCallback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 8523 8524**示例:** 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>从API version 9 开始不再维护,建议使用[sendMessageRequest](#sendmessagerequest9-5)类替代。 8561 8562sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8563 8564以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 8565 8566**系统能力**:SystemCapability.Communication.IPC.Core 8567 8568**参数:** 8569 8570| 参数名 | 类型 | 必填 | 说明 | 8571| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8572| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8573| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的 MessageParcel对象。 | 8574| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8575| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8576| AsyncCallback | AsyncCallback<[SendRequestResult](#sendrequestresult8deprecated)> | 是 | 接收发送结果的回调。 | 8577 8578**示例:** 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> **说明:** 8631> 8632>* 开发者应优先选择重载onRemoteMessageRequest方法,其中可以自由实现同步和异步的消息处理。 8633>* 开发者同时重载onRemoteRequest和onRemoteMessageRequest方法时,仅onRemoteMessageRequest方法生效。 8634 8635sendMessageRequest请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。 8636 8637**系统能力**:SystemCapability.Communication.IPC.Core 8638 8639**参数:** 8640 8641 | 参数名 | 类型 | 必填 | 说明 | 8642 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 8643 | code | number | 是 | 对端发送的服务请求码。 | 8644 | data | [MessageSequence](#messagesequence9) | 是 | 携带客户端调用参数的MessageSequence对象。 | 8645 | reply | [MessageSequence](#messagesequence9) | 是 | 写入结果的MessageSequence对象。 | 8646 | option | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 8647 8648**返回值:** 8649 8650 | 类型 | 说明 | 8651 | ----------------- | ----------------------------------------------------------------------------------------------- | 8652 | boolean | 若在onRemoteMessageRequest中同步地处理请求,则返回一个布尔值:true:操作成功,false:操作失败。 | 8653 | Promise\<boolean> | 若在onRemoteMessageRequest中异步地处理请求,则返回一个Promise对象。 | 8654 8655**重载onRemoteMessageRequest方法同步处理请求示例:** 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 **重载onRemoteMessageRequest方法异步处理请求示例:** 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**同时重载onRemoteMessageRequest和onRemoteRequest方法同步处理请求示例:** 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 // 同时调用仅会执行onRemoteMessageRequest 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 **同时重载onRemoteMessageRequest和onRemoteRequest方法异步处理请求示例:** 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 // 同时调用仅会执行onRemoteMessageRequest 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>从API version 9 开始不再维护,建议使用[onRemoteMessageRequest](#onremotemessagerequest9)类替代。 8771 8772onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8773 8774sendRequest请求的响应处理函数,服务端在该函数里处理请求,回复结果。 8775 8776**系统能力**:SystemCapability.Communication.IPC.Core 8777 8778**参数:** 8779 8780 | 参数名 | 类型 | 必填 | 说明 | 8781 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 8782 | code | number | 是 | 对端发送的服务请求码。 | 8783 | data | [MessageParcel](#messageparceldeprecated) | 是 | 携带客户端调用参数的MessageParcel对象。 | 8784 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 写入结果的MessageParcel对象。 | 8785 | option | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 8786 8787**返回值:** 8788 8789 | 类型 | 说明 | 8790 | ------- | -------------------------------- | 8791 | boolean | true:操作成功,false:操作失败。| 8792 8793**示例:** 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 8832获取通信对端的进程Uid。 8833 8834**系统能力**:SystemCapability.Communication.IPC.Core 8835 8836**返回值:** 8837 | 类型 | 说明 | 8838 | ------ | ----------------------- | 8839 | number | 返回通信对端的进程Uid。 | 8840 8841**示例:** 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 8859获取通信对端的进程Pid。 8860 8861**系统能力**:SystemCapability.Communication.IPC.Core 8862 8863**返回值:** 8864 8865 | 类型 | 说明 | 8866 | ------ | ----------------------- | 8867 | number | 返回通信对端的进程Pid。 | 8868 8869**示例:** 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 8887查询接口描述符的字符串。 8888 8889**系统能力**:SystemCapability.Communication.IPC.Core 8890 8891**参数:** 8892 8893 | 参数名 | 类型 | 必填 | 说明 | 8894 | ---------- | ------ | ---- | -------------------- | 8895 | descriptor | string | 是 | 接口描述符的字符串。 | 8896 8897**返回值:** 8898 8899 | 类型 | 说明 | 8900 | ------------- | --------------------------------------------- | 8901 | IRemoteBroker | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 8902 8903**示例:** 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 // 方法逻辑需开发者根据业务需要实现 8921 } 8922 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8923 // 方法逻辑需开发者根据业务需要实现 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>从API version 9 开始不再维护,建议使用[getLocalInterface](#getlocalinterface9-2)类替代。 8945 8946queryLocalInterface(descriptor: string): IRemoteBroker 8947 8948查询并获取当前接口描述符对应的远端对象是否已经存在。 8949 8950**系统能力**:SystemCapability.Communication.IPC.Core 8951 8952**参数:** 8953 8954 | 参数名 | 类型 | 必填 | 说明 | 8955 | ---------- | ------ | ---- | ---------------------- | 8956 | descriptor | string | 是 | 需要查询的接口描述符。 | 8957 8958**返回值:** 8959 8960 | 类型 | 说明 | 8961 | ------------- | ------------------------------------------------------------------ | 8962 | IRemoteBroker | 如果接口描述符对应的远端对象存在,则返回该远端对象,否则返回Null。 | 8963 8964**示例:** 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 9000获取对象的接口描述符。接口描述符为字符串。 9001 9002**系统能力**:SystemCapability.Communication.IPC.Core 9003 9004**返回值:** 9005 9006 | 类型 | 说明 | 9007 | ------ | ---------------- | 9008 | string | 返回接口描述符。 | 9009 9010**错误码:** 9011 9012以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9013 9014 | 错误码ID | 错误信息 | 9015 | -------- | -------- | 9016 | 1900008 | proxy or remote object is invalid | 9017 9018**示例:** 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 // 方法逻辑需开发者根据业务需要实现 9035 } 9036 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9037 // 方法逻辑需开发者根据业务需要实现 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>从API version 9 开始不再维护,建议使用[getDescriptor](#getdescriptor9-2)类替代。 9057 9058getInterfaceDescriptor(): string 9059 9060查询接口描述符。 9061 9062**系统能力**:SystemCapability.Communication.IPC.Core 9063 9064**返回值:** 9065 9066 | 类型 | 说明 | 9067 | ------ | ---------------- | 9068 | string | 返回接口描述符。 | 9069 9070**示例:** 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 9103此接口用于把接口描述符和IRemoteBroker对象绑定。 9104 9105**系统能力**:SystemCapability.Communication.IPC.Core 9106 9107**参数:** 9108 9109| 参数名 | 类型 | 必填 | 说明 | 9110| -------------- | ------------------------------- | ---- | ------------------------------------- | 9111| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9112| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9113 9114**示例:** 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 // 方法逻辑需开发者根据业务需要实现 9138 } 9139 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9140 // 方法逻辑需开发者根据业务需要实现 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>从API version 9 开始不再维护,建议使用[modifyLocalInterface](#modifylocalinterface9)类替代。 9155 9156attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9157 9158此接口用于把接口描述符和IRemoteBroker对象绑定。 9159 9160**系统能力**:SystemCapability.Communication.IPC.Core 9161 9162**参数:** 9163 9164| 参数名 | 类型 | 必填 | 说明 | 9165| -------------- | ------------------------------- | ---- | ------------------------------------- | 9166| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9167| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9168 9169**示例:** 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## Ashmem<sup>8+</sup> 9201 9202提供与匿名共享内存对象相关的方法,包括创建、关闭、映射和取消映射Ashmem、从Ashmem读取数据和写入数据、获取Ashmem大小、设置Ashmem保护。 9203 9204**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 9205 9206映射内存保护类型: 9207 9208 | 名称 | 值 | 说明 | 9209 | ---------- | --- | ------------------ | 9210 | PROT_EXEC | 4 | 映射的内存可执行 | 9211 | PROT_NONE | 0 | 映射的内存不可访问 | 9212 | PROT_READ | 1 | 映射的内存可读 | 9213 | PROT_WRITE | 2 | 映射的内存可写 | 9214 9215### create<sup>9+</sup> 9216 9217static create(name: string, size: number): Ashmem 9218 9219静态方法,根据指定的名称和大小创建Ashmem对象。 9220 9221**系统能力**:SystemCapability.Communication.IPC.Core 9222 9223**参数:** 9224 9225 | 参数名 | 类型 | 必填 | 说明 | 9226 | ------ | ------ | ---- | ---------------------------- | 9227 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9228 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9229 9230**返回值:** 9231 9232| 类型 | 说明 | 9233| ------------------ | ---------------------------------------------- | 9234| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9235 9236**示例:** 9237 9238 ```ts 9239 import hilog from '@ohos.hilog'; 9240 import { BusinessError } from '@ohos.base'; 9241 9242 let ashmem: rpc.Ashmem | undefined = undefined; 9243 try { 9244 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9245 let size = ashmem.getAshmemSize(); 9246 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9247 } catch (error) { 9248 let e: BusinessError = error as BusinessError; 9249 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9250 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9251 } 9252 ``` 9253 9254### createAshmem<sup>8+(deprecated)</sup> 9255 9256>从API version 9 开始不再维护,建议使用[create](#create9)类替代。 9257 9258static createAshmem(name: string, size: number): Ashmem 9259 9260静态方法,根据指定的名称和大小创建Ashmem对象。 9261 9262**系统能力**:SystemCapability.Communication.IPC.Core 9263 9264**参数:** 9265 9266 | 参数名 | 类型 | 必填 | 说明 | 9267 | ------ | ------ | ---- | ---------------------------- | 9268 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9269 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9270 9271**返回值:** 9272 9273| 类型 | 说明 | 9274| ------------------ | ---------------------------------------------- | 9275| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9276 9277**示例:** 9278 9279 ```ts 9280 import hilog from '@ohos.hilog'; 9281 9282 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9283 let size = ashmem.getAshmemSize(); 9284 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9285 ``` 9286 9287### create<sup>9+</sup> 9288 9289static create(ashmem: Ashmem): Ashmem 9290 9291静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9292 9293**系统能力**:SystemCapability.Communication.IPC.Core 9294 9295**参数:** 9296 9297| 参数名 | 类型 | 必填 | 说明 | 9298| ------ | ------------------ | ---- | -------------------- | 9299| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9300 9301**返回值:** 9302 9303| 类型 | 说明 | 9304| ------------------ | ---------------------- | 9305| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9306 9307**示例:** 9308 9309 ```ts 9310 import hilog from '@ohos.hilog'; 9311 import { BusinessError } from '@ohos.base'; 9312 9313 try { 9314 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9315 let ashmem2 = rpc.Ashmem.create(ashmem); 9316 let size = ashmem2.getAshmemSize(); 9317 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9318 } catch (error) { 9319 let e: BusinessError = error as BusinessError; 9320 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9321 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9322 } 9323 ``` 9324 9325### createAshmemFromExisting<sup>8+(deprecated)</sup> 9326 9327>从API version 9 开始不再维护,建议使用[create](#create9-1)替代。 9328 9329static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9330 9331静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9332 9333**系统能力**:SystemCapability.Communication.IPC.Core 9334 9335**参数:** 9336 9337| 参数名 | 类型 | 必填 | 说明 | 9338| ------ | ------------------ | ---- | -------------------- | 9339| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9340 9341**返回值:** 9342 9343| 类型 | 说明 | 9344| ------------------ | ---------------------- | 9345| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9346 9347**示例:** 9348 9349 ```ts 9350 import hilog from '@ohos.hilog'; 9351 9352 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9353 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9354 let size = ashmem2.getAshmemSize(); 9355 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9356 ``` 9357 9358### closeAshmem<sup>8+</sup> 9359 9360closeAshmem(): void 9361 9362关闭这个Ashmem。 9363 9364**系统能力**:SystemCapability.Communication.IPC.Core 9365 9366**示例:** 9367 9368 ```ts 9369 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9370 ashmem.closeAshmem(); 9371 ``` 9372 9373### unmapAshmem<sup>8+</sup> 9374 9375unmapAshmem(): void 9376 9377删除该Ashmem对象的地址映射。 9378 9379**系统能力**:SystemCapability.Communication.IPC.Core 9380 9381**示例:** 9382 9383 ```ts 9384 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9385 ashmem.unmapAshmem(); 9386 ``` 9387 9388### getAshmemSize<sup>8+</sup> 9389 9390getAshmemSize(): number 9391 9392获取Ashmem对象的内存大小。 9393 9394**系统能力**:SystemCapability.Communication.IPC.Core 9395 9396**返回值:** 9397 9398 | 类型 | 说明 | 9399 | ------ | -------------------------- | 9400 | number | 返回Ashmem对象的内存大小。 | 9401 9402**示例:** 9403 9404 ```ts 9405 import hilog from '@ohos.hilog'; 9406 9407 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9408 let size = ashmem.getAshmemSize(); 9409 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9410 ``` 9411 9412### mapTypedAshmem<sup>9+</sup> 9413 9414mapTypedAshmem(mapType: number): void 9415 9416在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9417 9418**系统能力**:SystemCapability.Communication.IPC.Core 9419 9420**参数:** 9421 9422 | 参数名 | 类型 | 必填 | 说明 | 9423 | ------- | ------ | ---- | ------------------------------ | 9424 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9425 9426**错误码:** 9427 9428以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9429 9430 | 错误码ID | 错误信息 | 9431 | -------- | -------- | 9432 | 1900001 | call mmap function failed | 9433 9434**示例:** 9435 9436 ```ts 9437 import hilog from '@ohos.hilog'; 9438 import { BusinessError } from '@ohos.base'; 9439 9440 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9441 try { 9442 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9443 } catch (error) { 9444 let e: BusinessError = error as BusinessError; 9445 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9446 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9447 } 9448 ``` 9449 9450### mapAshmem<sup>8+(deprecated)</sup> 9451 9452>从API version 9 开始不再维护,建议使用[mapTypedAshmem](#maptypedashmem9)类替代。 9453 9454mapAshmem(mapType: number): boolean 9455 9456在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9457 9458**系统能力**:SystemCapability.Communication.IPC.Core 9459 9460**参数:** 9461 9462 | 参数名 | 类型 | 必填 | 说明 | 9463 | ------- | ------ | ---- | ------------------------------ | 9464 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9465 9466**返回值:** 9467 9468 | 类型 | 说明 | 9469 | ------- | -------------------------------- | 9470 | boolean | true:映射成功,false:映射失败。| 9471 9472**示例:** 9473 9474 ```ts 9475 import hilog from '@ohos.hilog'; 9476 9477 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9478 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9479 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9480 ``` 9481 9482### mapReadWriteAshmem<sup>9+</sup> 9483 9484mapReadWriteAshmem(): void 9485 9486在此进程虚拟地址空间上创建可读写的共享文件映射。 9487 9488**系统能力**:SystemCapability.Communication.IPC.Core 9489 9490**错误码:** 9491 9492以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9493 9494 | 错误码ID | 错误信息 | 9495 | -------- | -------- | 9496 | 1900001 | call mmap function failed | 9497 9498**示例:** 9499 9500 ```ts 9501 import hilog from '@ohos.hilog'; 9502 import { BusinessError } from '@ohos.base'; 9503 9504 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9505 try { 9506 ashmem.mapReadWriteAshmem(); 9507 } catch (error) { 9508 let e: BusinessError = error as BusinessError; 9509 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9510 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9511 } 9512 ``` 9513 9514### mapReadAndWriteAshmem<sup>8+(deprecated)</sup> 9515 9516>从API version 9 开始不再维护,建议使用[mapReadWriteAshmem](#mapreadwriteashmem9)类替代。 9517 9518mapReadAndWriteAshmem(): boolean 9519 9520在此进程虚拟地址空间上创建可读写的共享文件映射。 9521 9522**系统能力**:SystemCapability.Communication.IPC.Core 9523 9524**返回值:** 9525 9526 | 类型 | 说明 | 9527 | ------- | -------------------------------- | 9528 | boolean | true:映射成功,false:映射失败。| 9529 9530**示例:** 9531 9532 ```ts 9533 import hilog from '@ohos.hilog'; 9534 9535 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9536 let mapResult = ashmem.mapReadAndWriteAshmem(); 9537 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 9538 ``` 9539 9540### mapReadonlyAshmem<sup>9+</sup> 9541 9542mapReadonlyAshmem(): void 9543 9544在此进程虚拟地址空间上创建只读的共享文件映射。 9545 9546**系统能力**:SystemCapability.Communication.IPC.Core 9547 9548**错误码:** 9549 9550以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9551 9552 | 错误码ID | 错误信息 | 9553 | -------- | -------- | 9554 | 1900001 | call mmap function failed | 9555 9556**示例:** 9557 9558 ```ts 9559 import hilog from '@ohos.hilog'; 9560 import { BusinessError } from '@ohos.base'; 9561 9562 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9563 try { 9564 ashmem.mapReadonlyAshmem(); 9565 } catch (error) { 9566 let e: BusinessError = error as BusinessError; 9567 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9568 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9569 } 9570 ``` 9571 9572### mapReadOnlyAshmem<sup>8+(deprecated)</sup> 9573 9574>从API version 9 开始不再维护,建议使用[mapReadonlyAshmem](#mapreadonlyashmem9)类替代。 9575 9576mapReadOnlyAshmem(): boolean 9577 9578在此进程虚拟地址空间上创建只读的共享文件映射。 9579 9580**系统能力**:SystemCapability.Communication.IPC.Core 9581 9582**返回值:** 9583 9584 | 类型 | 说明 | 9585 | ------- | -------------------------------- | 9586 | boolean | true:映射成功,false:映射失败。| 9587 9588**示例:** 9589 9590 ```ts 9591 import hilog from '@ohos.hilog'; 9592 9593 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9594 let mapResult = ashmem.mapReadOnlyAshmem(); 9595 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 9596 ``` 9597 9598### setProtectionType<sup>9+</sup> 9599 9600setProtectionType(protectionType: number): void 9601 9602设置映射内存区域的保护等级。 9603 9604**系统能力**:SystemCapability.Communication.IPC.Core 9605 9606**参数:** 9607 9608 | 参数名 | 类型 | 必填 | 说明 | 9609 | -------------- | ------ | ---- | ------------------ | 9610 | protectionType | number | 是 | 要设置的保护类型。 | 9611 9612**错误码:** 9613 9614以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9615 9616 | 错误码ID | 错误信息 | 9617 | -------- | -------- | 9618 | 1900002 | call os ioctl function failed | 9619 9620**示例:** 9621 9622 ```ts 9623 import hilog from '@ohos.hilog'; 9624 import { BusinessError } from '@ohos.base'; 9625 9626 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9627 try { 9628 ashmem.setProtection(ashmem.PROT_READ); 9629 } catch (error) { 9630 let e: BusinessError = error as BusinessError; 9631 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 9632 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 9633 } 9634 ``` 9635 9636### setProtection<sup>8+(deprecated)</sup> 9637 9638>从API version 9 开始不再维护,建议使用[setProtectionType](#setprotectiontype9)类替代。 9639 9640setProtection(protectionType: number): boolean 9641 9642设置映射内存区域的保护等级。 9643 9644**系统能力**:SystemCapability.Communication.IPC.Core 9645 9646**参数:** 9647 9648 | 参数名 | 类型 | 必填 | 说明 | 9649 | -------------- | ------ | ---- | ------------------ | 9650 | protectionType | number | 是 | 要设置的保护类型。 | 9651 9652**返回值:** 9653 9654 | 类型 | 说明 | 9655 | ------- | -------------------------------- | 9656 | boolean | true:设置成功,false:设置失败。| 9657 9658**示例:** 9659 9660 ```ts 9661 import hilog from '@ohos.hilog'; 9662 9663 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9664 let result = ashmem.setProtection(ashmem.PROT_READ); 9665 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 9666 ``` 9667 9668### writeAshmem<sup>9+</sup> 9669 9670writeAshmem(buf: number[], size: number, offset: number): void 9671 9672将数据写入此Ashmem对象关联的共享文件。 9673 9674**系统能力**:SystemCapability.Communication.IPC.Core 9675 9676**参数:** 9677 9678 | 参数名 | 类型 | 必填 | 说明 | 9679 | ------ | -------- | ---- | -------------------------------------------------- | 9680 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 9681 | size | number | 是 | 要写入的数据大小。 | 9682 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 | 9683 9684**错误码:** 9685 9686以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9687 9688 | 错误码ID | 错误信息 | 9689 | -------- | -------- | 9690 | 1900003 | write to ashmem failed | 9691 9692**示例:** 9693 9694 ```ts 9695 import hilog from '@ohos.hilog'; 9696 import { BusinessError } from '@ohos.base'; 9697 9698 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9699 ashmem.mapReadWriteAshmem(); 9700 let ByteArrayVar = [1, 2, 3, 4, 5]; 9701 try { 9702 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9703 } catch (error) { 9704 let e: BusinessError = error as BusinessError; 9705 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 9706 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 9707 } 9708 ``` 9709 9710### writeToAshmem<sup>8+(deprecated)</sup> 9711 9712>从API version 9 开始不再维护,建议使用[writeAshmem](#writeashmem9)类替代。 9713 9714writeToAshmem(buf: number[], size: number, offset: number): boolean 9715 9716将数据写入此Ashmem对象关联的共享文件。 9717 9718**系统能力**:SystemCapability.Communication.IPC.Core 9719 9720**参数:** 9721 9722 | 参数名 | 类型 | 必填 | 说明 | 9723 | ------ | -------- | ---- | -------------------------------------------------- | 9724 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 9725 | size | number | 是 | 要写入的数据大小。 | 9726 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 | 9727 9728**返回值:** 9729 9730 | 类型 | 说明 | 9731 | ------- | ----------------------------------------------------------------------------- | 9732 | boolean | true:如果数据写入成功,false:在其他情况下,如数据写入越界或未获得写入权限。 | 9733 9734**示例:** 9735 9736 ```ts 9737 import hilog from '@ohos.hilog'; 9738 9739 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9740 let mapResult = ashmem.mapReadAndWriteAshmem(); 9741 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 9742 let ByteArrayVar = [1, 2, 3, 4, 5]; 9743 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9744 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 9745 ``` 9746 9747### readAshmem<sup>9+</sup> 9748 9749readAshmem(size: number, offset: number): number[] 9750 9751从此Ashmem对象关联的共享文件中读取数据。 9752 9753**系统能力**:SystemCapability.Communication.IPC.Core 9754 9755**参数:** 9756 9757 | 参数名 | 类型 | 必填 | 说明 | 9758 | ------ | ------ | ---- | -------------------------------------------------- | 9759 | size | number | 是 | 要读取的数据的大小。 | 9760 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置 | 9761 9762**返回值:** 9763 9764 | 类型 | 说明 | 9765 | -------- | ---------------- | 9766 | number[] | 返回读取的数据。 | 9767 9768**错误码:** 9769 9770以下错误码的详细介绍请参见[ohos.rpc错误码](../errorcodes/errorcode-rpc.md) 9771 9772 | 错误码ID | 错误信息 | 9773 | -------- | -------- | 9774 | 1900004 | read from ashmem failed | 9775 9776**示例:** 9777 9778 ```ts 9779 import hilog from '@ohos.hilog'; 9780 import { BusinessError } from '@ohos.base'; 9781 9782 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9783 ashmem.mapReadWriteAshmem(); 9784 let ByteArrayVar = [1, 2, 3, 4, 5]; 9785 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9786 try { 9787 let readResult = ashmem.readAshmem(5, 0); 9788 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 9789 } catch (error) { 9790 let e: BusinessError = error as BusinessError; 9791 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 9792 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 9793 } 9794 ``` 9795 9796### readFromAshmem<sup>8+(deprecated)</sup> 9797 9798>从API version 9 开始不再维护,建议使用[readAshmem](#readashmem9)类替代。 9799 9800readFromAshmem(size: number, offset: number): number[] 9801 9802从此Ashmem对象关联的共享文件中读取数据。 9803 9804**系统能力**:SystemCapability.Communication.IPC.Core 9805 9806**参数:** 9807 9808 | 参数名 | 类型 | 必填 | 说明 | 9809 | ------ | ------ | ---- | -------------------------------------------------- | 9810 | size | number | 是 | 要读取的数据的大小。 | 9811 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置 | 9812 9813**返回值:** 9814 9815 | 类型 | 说明 | 9816 | -------- | ---------------- | 9817 | number[] | 返回读取的数据。 | 9818 9819**示例:** 9820 9821 ``` ts 9822 import hilog from '@ohos.hilog'; 9823 9824 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9825 let mapResult = ashmem.mapReadAndWriteAshmem(); 9826 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 9827 let ByteArrayVar = [1, 2, 3, 4, 5]; 9828 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9829 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 9830 let readResult = ashmem.readFromAshmem(5, 0); 9831 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 9832 ``` 9833 9834## 获取context 9835 9836**示例:** 9837此处只介绍一种获取context的方式,更多获取方式请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 9838 ```ts 9839 import UIAbility from '@ohos.app.ability.UIAbility'; 9840 import Want from '@ohos.app.ability.Want'; 9841 import hilog from '@ohos.hilog'; 9842 import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 9843 import window from '@ohos.window'; 9844 9845 export default class MainAbility extends UIAbility { 9846 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 9847 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onCreate'); 9848 let context = this.context; 9849 } 9850 onDestroy() { 9851 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onDestroy'); 9852 } 9853 onWindowStageCreate(windowStage: window.WindowStage) { 9854 // Main window is created, set main page for this ability 9855 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onWindowStageCreate'); 9856 } 9857 onWindowStageDestroy() { 9858 // Main window is destroyed, release UI related resources 9859 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onWindowStageDestroy'); 9860 } 9861 onForeground() { 9862 // Ability has brought to foreground 9863 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onForeground'); 9864 } 9865 onBackground() { 9866 // Ability has back to background 9867 hilog.info(0x0000, 'testTag', '%{public}s', 'UIAbility onBackground'); 9868 } 9869 } 9870 ```