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 '@kit.IPCKit'; 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## TypeCode<sup>12+</sup> 42 43从API version 12起,IPC新增[writeArrayBuffer](#writearraybuffer12)和[readArrayBuffer](#readarraybuffer12)方法传递ArrayBuffer数据,传递数据时通过具体类型值来分辨业务是以哪一种TypedArray去进行数据的读写。类型码对应数值及含义如下。 44 45**系统能力**:SystemCapability.Communication.IPC.Core 46 47 | 名称 | 值 | 说明 | 48 | ---------------------------- | ------ | -------------------------------------------- | 49 | INT8_ARRAY | 0 | TypedArray类型为INT8_ARRAY。 | 50 | UINT8_ARRAY | 1 | TypedArray类型为UINT8_ARRAY。 | 51 | INT16_ARRAY | 2 | TypedArray类型为INT16_ARRAY。 | 52 | UINT16_ARRAY | 3 | TypedArray类型为UINT16_ARRAY。 | 53 | INT32_ARRAY | 4 | TypedArray类型为INT32_ARRAY。 | 54 | UINT32_ARRAY | 5 | TypedArray类型为UINT32_ARRAY。 | 55 | FLOAT32_ARRAY | 6 | TypedArray类型为FLOAT32_ARRAY。 | 56 | FLOAT64_ARRAY | 7 | TypedArray类型为FLOAT64_ARRAY。 | 57 | BIGINT64_ARRAY | 8 | TypedArray类型为BIGINT64_ARRAY。 | 58 | BIGUINT64_ARRAY | 9 | TypedArray类型为BIGUINT64_ARRAY。 | 59 60 61## MessageSequence<sup>9+</sup> 62 63 在RPC或IPC过程中,发送方可以使用MessageSequence提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageSequence提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 64 65### create 66 67 static create(): MessageSequence 68 69 静态方法,创建MessageSequence对象。 70 71**系统能力**:SystemCapability.Communication.IPC.Core 72 73**返回值:** 74 75| 类型 | 说明 | 76| --------------- | ------------------------------- | 77| [MessageSequence](#messagesequence9) | 返回创建的MessageSequence对象。 | 78 79**示例:** 80 81 ```ts 82 import { hilog } from '@kit.PerformanceAnalysisKit'; 83 84 let data = rpc.MessageSequence.create(); 85 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 86 87 // 当MessageSequence对象不再使用,由业务主动调用reclaim方法去释放资源。 88 data.reclaim(); 89 ``` 90 91### reclaim 92 93reclaim(): void 94 95释放不再使用的MessageSequence对象。 96 97**系统能力**:SystemCapability.Communication.IPC.Core 98 99**示例:** 100 101 ```ts 102 let reply = rpc.MessageSequence.create(); 103 reply.reclaim(); 104 ``` 105 106### writeRemoteObject 107 108writeRemoteObject(object: IRemoteObject): void 109 110序列化远程对象并将其写入MessageSequence对象。 111 112**系统能力**:SystemCapability.Communication.IPC.Core 113 114**参数:** 115 116 | 参数名 | 类型 | 必填 | 说明 | 117 | ------ | ------------------------------- | ---- | ----------------------------------------- | 118 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageSequence的远程对象。 | 119 120**错误码:** 121 122以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 123 124 | 错误码ID | 错误信息 | 125 | -------- | -------- | 126 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 127 | 1900008 | The proxy or remote object is invalid. | 128 | 1900009 | Failed to write data to the message sequence. | 129 130**示例:** 131 132 ```ts 133 import { hilog } from '@kit.PerformanceAnalysisKit'; 134 import { BusinessError } from '@kit.BasicServicesKit'; 135 136 class TestRemoteObject extends rpc.RemoteObject { 137 constructor(descriptor: string) { 138 super(descriptor); 139 } 140 } 141 let data = rpc.MessageSequence.create(); 142 let testRemoteObject = new TestRemoteObject("testObject"); 143 try { 144 data.writeRemoteObject(testRemoteObject); 145 } catch (error) { 146 let e: BusinessError = error as BusinessError; 147 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 148 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 149 } 150 ``` 151 152### readRemoteObject 153 154readRemoteObject(): IRemoteObject 155 156从MessageSequence读取远程对象。此方法用于反序列化MessageSequence对象以生成IRemoteObject。远程对象按写入MessageSequence的顺序读取。 157 158**系统能力**:SystemCapability.Communication.IPC.Core 159 160**返回值:** 161 162 | 类型 | 说明 | 163 | ------------------------------- | ------------------ | 164 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 165 166**错误码:** 167 168以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 169 170 | 错误码ID | 错误信息 | 171 | -------- | -------- | 172 | 1900008 | The proxy or remote object is invalid. | 173 | 1900010 | Failed to read data from the message sequence. | 174 175**示例:** 176 177 ```ts 178 import { hilog } from '@kit.PerformanceAnalysisKit'; 179 import { BusinessError } from '@kit.BasicServicesKit'; 180 181 class TestRemoteObject extends rpc.RemoteObject { 182 constructor(descriptor: string) { 183 super(descriptor); 184 } 185 } 186 let data = rpc.MessageSequence.create(); 187 let testRemoteObject = new TestRemoteObject("testObject"); 188 try { 189 data.writeRemoteObject(testRemoteObject); 190 let proxy = data.readRemoteObject(); 191 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 192 } catch (error) { 193 let e: BusinessError = error as BusinessError; 194 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 195 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 196 } 197 ``` 198 199### writeInterfaceToken 200 201writeInterfaceToken(token: string): void 202 203将接口描述符写入MessageSequence对象,远端对象可使用该信息校验本次通信。 204 205**系统能力**:SystemCapability.Communication.IPC.Core 206 207**参数:** 208 209 | 参数名 | 类型 | 必填 | 说明 | 210 | ------ | ------ | ---- | ------------------ | 211 | token | string | 是 | 字符串类型描述符。 | 212 213**错误码:** 214 215以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 216 217 | 错误码ID | 错误信息 | 218 | -------- | -------- | 219 | 401 | Parameter error. Possible causes:<br/> 1.The number of parameters is incorrect;<br/> 2.The parameter type does not match;<br/> 3.The string length exceeds 40960 bytes;<br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 220 | 1900009 | Failed to write data to the message sequence. | 221 222**示例:** 223 224 ```ts 225 import { hilog } from '@kit.PerformanceAnalysisKit'; 226 import { BusinessError } from '@kit.BasicServicesKit'; 227 228 let data = rpc.MessageSequence.create(); 229 try { 230 data.writeInterfaceToken("aaa"); 231 } catch (error) { 232 let e: BusinessError = error as BusinessError; 233 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 234 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 235 } 236 ``` 237 238### readInterfaceToken 239 240readInterfaceToken(): string 241 242从MessageSequence对象中读取接口描述符,接口描述符按写入MessageSequence的顺序读取,本地对象可使用该信息检验本次通信。 243 244**系统能力**:SystemCapability.Communication.IPC.Core 245 246**返回值:** 247 248 | 类型 | 说明 | 249 | ------ | ------------------------ | 250 | string | 返回读取到的接口描述符。 | 251 252**错误码:** 253 254以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 255 256 | 错误码ID | 错误信息 | 257 | -------- | -------- | 258 | 1900010 | Failed to read data from the message sequence. | 259 260**示例:** 261 262```ts 263import { hilog } from '@kit.PerformanceAnalysisKit'; 264import { BusinessError } from '@kit.BasicServicesKit'; 265 266class Stub extends rpc.RemoteObject { 267 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 268 try { 269 let interfaceToken = data.readInterfaceToken(); 270 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 271 } catch (error) { 272 let e: BusinessError = error as BusinessError; 273 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 274 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 275 } 276 return true; 277 } 278} 279``` 280 281### getSize 282 283getSize(): number 284 285获取当前创建的MessageSequence对象的数据大小。 286 287**系统能力**:SystemCapability.Communication.IPC.Core 288 289**返回值:** 290 291 | 类型 | 说明 | 292 | ------ | ----------------------------------------------- | 293 | number | 获取的MessageSequence实例的数据大小。以字节为单位。 | 294 295**示例:** 296 297 ```ts 298 import { hilog } from '@kit.PerformanceAnalysisKit'; 299 300 let data = rpc.MessageSequence.create(); 301 let size = data.getSize(); 302 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 303 ``` 304 305### getCapacity 306 307getCapacity(): number 308 309获取当前MessageSequence对象的容量大小。 310 311**系统能力**:SystemCapability.Communication.IPC.Core 312 313**返回值:** 314 315 | 类型 | 说明 | 316 | ------ | ----- | 317 | number | 获取的MessageSequence实例的容量大小。以字节为单位。 | 318 319**示例:** 320 321 ```ts 322 import { hilog } from '@kit.PerformanceAnalysisKit'; 323 324 let data = rpc.MessageSequence.create(); 325 let result = data.getCapacity(); 326 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 327 ``` 328 329### setSize 330 331setSize(size: number): void 332 333设置MessageSequence对象中包含的数据大小。 334 335**系统能力**:SystemCapability.Communication.IPC.Core 336 337**参数:** 338 339 | 参数名 | 类型 | 必填 | 说明 | 340 | ------ | ------ | ---- | ------ | 341 | size | number | 是 | MessageSequence实例的数据大小。以字节为单位。 | 342 343**错误码:** 344 345以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 346 347 | 错误码ID | 错误信息 | 348 | -------- | -------- | 349 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 350 351**示例:** 352 353 ```ts 354 import { hilog } from '@kit.PerformanceAnalysisKit'; 355 import { BusinessError } from '@kit.BasicServicesKit'; 356 357 let data = rpc.MessageSequence.create(); 358 data.writeString('Hello World'); 359 try { 360 data.setSize(16); 361 } catch (error) { 362 let e: BusinessError = error as BusinessError; 363 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 364 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 365 } 366 ``` 367 368### setCapacity 369 370setCapacity(size: number): void 371 372设置MessageSequence对象的存储容量。 373 374**系统能力**:SystemCapability.Communication.IPC.Core 375 376**参数:** 377 378 | 参数名 | 类型 | 必填 | 说明 | 379 | ------ | ------ | ---- | --------------------------------------------- | 380 | size | number | 是 | MessageSequence实例的存储容量。以字节为单位。 | 381 382**错误码:** 383 384以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 385 386 | 错误码ID | 错误信息 | 387 | -------- | -------- | 388 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 389 | 1900011 | Memory allocation failed | 390 391**示例:** 392 393 ```ts 394 import { hilog } from '@kit.PerformanceAnalysisKit'; 395 import { BusinessError } from '@kit.BasicServicesKit'; 396 397 let data = rpc.MessageSequence.create(); 398 try { 399 data.setCapacity(100); 400 } catch (error) { 401 let e: BusinessError = error as BusinessError; 402 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 403 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 404 } 405 ``` 406 407### getWritableBytes 408 409getWritableBytes(): number 410 411获取MessageSequence的可写字节空间大小。 412 413**系统能力**:SystemCapability.Communication.IPC.Core 414 415**返回值:** 416 417 | 类型 | 说明 | 418 | ------ | ------ | 419 | number | 获取到的MessageSequence实例的可写字节空间。以字节为单位。 | 420 421**示例:** 422 423```ts 424import { hilog } from '@kit.PerformanceAnalysisKit'; 425 426class Stub extends rpc.RemoteObject { 427 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 428 let getWritableBytes = data.getWritableBytes(); 429 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 430 return true; 431 } 432} 433``` 434 435### getReadableBytes 436 437getReadableBytes(): number 438 439获取MessageSequence的可读字节空间。 440 441**系统能力**:SystemCapability.Communication.IPC.Core 442 443**返回值:** 444 445 | 类型 | 说明 | 446 | ------ | ------- | 447 | number | 获取到的MessageSequence实例的可读字节空间。以字节为单位。 | 448 449**示例:** 450 451```ts 452import { hilog } from '@kit.PerformanceAnalysisKit'; 453 454class Stub extends rpc.RemoteObject { 455 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 456 let result = data.getReadableBytes(); 457 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 458 return true; 459 } 460} 461``` 462 463### getReadPosition 464 465getReadPosition(): number 466 467获取MessageSequence的读位置。 468 469**系统能力**:SystemCapability.Communication.IPC.Core 470 471**返回值:** 472 473 | 类型 | 说明 | 474 | ------ | ------ | 475 | number | 返回MessageSequence实例中的当前读取位置。 | 476 477**示例:** 478 479 ```ts 480 import { hilog } from '@kit.PerformanceAnalysisKit'; 481 482 let data = rpc.MessageSequence.create(); 483 let readPos = data.getReadPosition(); 484 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 485 ``` 486 487### getWritePosition 488 489getWritePosition(): number 490 491获取MessageSequence的写位置。 492 493**系统能力**:SystemCapability.Communication.IPC.Core 494 495**返回值:** 496 497 | 类型 | 说明 | 498 | ------ | ----- | 499 | number | 返回MessageSequence实例中的当前写入位置。 | 500 501**示例:** 502 503 ```ts 504 import { hilog } from '@kit.PerformanceAnalysisKit'; 505 506 let data = rpc.MessageSequence.create(); 507 data.writeInt(10); 508 let bwPos = data.getWritePosition(); 509 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 510 ``` 511 512### rewindRead 513 514rewindRead(pos: number): void 515 516重新偏移读取位置到指定的位置。 517 518**系统能力**:SystemCapability.Communication.IPC.Core 519 520**参数:** 521 522 | 参数名 | 类型 | 必填 | 说明 | 523 | ------ | ------ | ---- | ------- | 524 | pos | number | 是 | 开始读取数据的目标位置。 | 525 526**错误码:** 527 528以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 529 530 | 错误码ID | 错误信息 | 531 | -------- | -------- | 532 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 533 534**示例:** 535 536 ```ts 537 import { hilog } from '@kit.PerformanceAnalysisKit'; 538 import { BusinessError } from '@kit.BasicServicesKit'; 539 540 let data = rpc.MessageSequence.create(); 541 data.writeInt(12); 542 data.writeString("sequence"); 543 let number = data.readInt(); 544 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 545 try { 546 data.rewindRead(0); 547 } catch (error) { 548 let e: BusinessError = error as BusinessError; 549 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 550 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 551 } 552 let number2 = data.readInt(); 553 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 554 ``` 555 556### rewindWrite 557 558rewindWrite(pos: number): void 559 560重新偏移写位置到指定的位置。 561 562**系统能力**:SystemCapability.Communication.IPC.Core 563 564**参数:** 565 566 | 参数名 | 类型 | 必填 | 说明 | 567 | ------ | ------ | ---- | ----- | 568 | pos | number | 是 | 开始写入数据的目标位置。 | 569 570**错误码:** 571 572以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 573 574 | 错误码ID | 错误信息 | 575 | -------- | -------- | 576 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 577 578**示例:** 579 580 ```ts 581 import { hilog } from '@kit.PerformanceAnalysisKit'; 582 import { BusinessError } from '@kit.BasicServicesKit'; 583 584 let data = rpc.MessageSequence.create(); 585 data.writeInt(4); 586 try { 587 data.rewindWrite(0); 588 } catch (error) { 589 let e: BusinessError = error as BusinessError; 590 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 591 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 592 } 593 data.writeInt(5); 594 let number = data.readInt(); 595 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 596 ``` 597 598### writeByte 599 600writeByte(val: number): void 601 602将字节值写入MessageSequence实例。 603 604**系统能力**:SystemCapability.Communication.IPC.Core 605 606**参数:** 607 608 | 参数名 | 类型 | 必填 | 说明 | 609 | ------ | ------ | ---- | ----- | 610 | val | number | 是 | 要写入的字节值。 | 611 612**错误码:** 613 614以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 615 616 | 错误码ID | 错误信息 | 617 | -------- | ------- | 618 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 619 | 1900009 | Failed to write data to the message sequence. | 620 621**示例:** 622 623 ```ts 624 import { hilog } from '@kit.PerformanceAnalysisKit'; 625 import { BusinessError } from '@kit.BasicServicesKit'; 626 627 let data = rpc.MessageSequence.create(); 628 try { 629 data.writeByte(2); 630 } catch (error) { 631 let e: BusinessError = error as BusinessError; 632 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 633 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 634 } 635 ``` 636 637### readByte 638 639readByte(): number 640 641从MessageSequence实例读取字节值。 642 643**系统能力**:SystemCapability.Communication.IPC.Core 644 645**返回值:** 646 647 | 类型 | 说明 | 648 | ------ | ----- | 649 | number | 返回字节值。 | 650 651**错误码:** 652 653以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 654 655 | 错误码ID | 错误信息 | 656 | ------- | -------- | 657 | 1900010 | Failed to read data from the message sequence. | 658 659**示例:** 660 661 ```ts 662 import { hilog } from '@kit.PerformanceAnalysisKit'; 663 import { BusinessError } from '@kit.BasicServicesKit'; 664 665 let data = rpc.MessageSequence.create(); 666 try { 667 data.writeByte(2); 668 } catch (error) { 669 let e: BusinessError = error as BusinessError; 670 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 671 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 672 } 673 try { 674 let ret = data.readByte(); 675 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 676 } catch (error) { 677 let e: BusinessError = error as BusinessError; 678 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 679 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 680 } 681 ``` 682 683### writeShort 684 685writeShort(val: number): void 686 687将短整数值写入MessageSequence实例。 688 689**系统能力**:SystemCapability.Communication.IPC.Core 690 691**参数:** 692 693 | 参数名 | 类型 | 必填 | 说明 | 694 | ------ | ------ | --- | --- | 695 | val | number | 是 | 要写入的短整数值。 | 696 697**错误码:** 698 699以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 700 701 | 错误码ID | 错误信息 | 702 | -------- | -------- | 703 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 704 | 1900009 | Failed to write data to the message sequence. | 705 706**示例:** 707 708 ```ts 709 import { hilog } from '@kit.PerformanceAnalysisKit'; 710 import { BusinessError } from '@kit.BasicServicesKit'; 711 712 let data = rpc.MessageSequence.create(); 713 try { 714 data.writeShort(8); 715 } catch (error) { 716 let e: BusinessError = error as BusinessError; 717 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 718 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 719 } 720 ``` 721 722### readShort 723 724readShort(): number 725 726从MessageSequence实例读取短整数值。 727 728**系统能力**:SystemCapability.Communication.IPC.Core 729 730**返回值:** 731 732 | 类型 | 说明 | 733 | ------ | -------------- | 734 | number | 返回短整数值。 | 735 736**错误码:** 737 738以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 739 740 | 错误码ID | 错误信息 | 741 | -------- | -------- | 742 | 1900010 | Failed to read data from the message sequence. | 743 744**示例:** 745 746 ```ts 747 import { hilog } from '@kit.PerformanceAnalysisKit'; 748 import { BusinessError } from '@kit.BasicServicesKit'; 749 750 let data = rpc.MessageSequence.create(); 751 try { 752 data.writeShort(8); 753 } catch (error) { 754 let e: BusinessError = error as BusinessError; 755 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 756 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 757 } 758 try { 759 let ret = data.readShort(); 760 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 761 } catch (error) { 762 let e: BusinessError = error as BusinessError; 763 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 764 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 765 } 766 ``` 767 768### writeInt 769 770writeInt(val: number): void 771 772将整数值写入MessageSequence实例。 773 774**系统能力**:SystemCapability.Communication.IPC.Core 775 776**参数:** 777 778 | 参数名 | 类型 | 必填 | 说明 | 779 | ------ | ------ | ---- | ---------------- | 780 | val | number | 是 | 要写入的整数值。 | 781 782**错误码:** 783 784以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 785 786 | 错误码ID | 错误信息 | 787 | -------- | -------- | 788 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 789 | 1900009 | Failed to write data to the message sequence. | 790 791**示例:** 792 793 ```ts 794 import { hilog } from '@kit.PerformanceAnalysisKit'; 795 import { BusinessError } from '@kit.BasicServicesKit'; 796 797 let data = rpc.MessageSequence.create(); 798 try { 799 data.writeInt(10); 800 } catch (error) { 801 let e: BusinessError = error as BusinessError; 802 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 803 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 804 } 805 ``` 806 807### readInt 808 809readInt(): number 810 811从MessageSequence实例读取整数值。 812 813**系统能力**:SystemCapability.Communication.IPC.Core 814 815**返回值:** 816 817 | 类型 | 说明 | 818 | ------ | ------------ | 819 | number | 返回整数值。 | 820 821**错误码:** 822 823以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 824 825 | 错误码ID | 错误信息 | 826 | -------- | -------- | 827 | 1900010 | Failed to read data from the message sequence. | 828 829**示例:** 830 831 ```ts 832 import { hilog } from '@kit.PerformanceAnalysisKit'; 833 import { BusinessError } from '@kit.BasicServicesKit'; 834 835 let data = rpc.MessageSequence.create(); 836 try { 837 data.writeInt(10); 838 } catch (error) { 839 let e: BusinessError = error as BusinessError; 840 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 841 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 842 } 843 try { 844 let ret = data.readInt(); 845 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 846 } catch (error) { 847 let e: BusinessError = error as BusinessError; 848 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 849 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 850 } 851 ``` 852 853### writeLong 854 855writeLong(val: number): void 856 857将长整数值写入MessageSequence实例。 858 859**系统能力**:SystemCapability.Communication.IPC.Core 860 861**参数:** 862 863 | 参数名 | 类型 | 必填 | 说明 | 864 | ------ | ------ | ---- | ---------------- | 865 | val | number | 是 | 要写入的长整数值。 | 866 867**错误码:** 868 869以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 870 871 | 错误码ID | 错误信息 | 872 | -------- | -------- | 873 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 874 | 1900009 | Failed to write data to the message sequence. | 875 876**示例:** 877 878 ```ts 879 import { hilog } from '@kit.PerformanceAnalysisKit'; 880 import { BusinessError } from '@kit.BasicServicesKit'; 881 882 let data = rpc.MessageSequence.create(); 883 try { 884 data.writeLong(10000); 885 } catch (error) { 886 let e: BusinessError = error as BusinessError; 887 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 888 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 889 } 890 ``` 891 892### readLong 893 894readLong(): number 895 896从MessageSequence实例中读取长整数值。 897 898**系统能力**:SystemCapability.Communication.IPC.Core 899 900**返回值:** 901 902 | 类型 | 说明 | 903 | ------ | -------------- | 904 | number | 返回长整数值。 | 905 906**错误码:** 907 908以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 909 910 | 错误码ID | 错误信息 | 911 | -------- | -------- | 912 | 1900010 | Failed to read data from the message sequence. | 913 914**示例:** 915 916 ```ts 917 import { hilog } from '@kit.PerformanceAnalysisKit'; 918 import { BusinessError } from '@kit.BasicServicesKit'; 919 920 let data = rpc.MessageSequence.create(); 921 try { 922 data.writeLong(10000); 923 } catch (error) { 924 let e: BusinessError = error as BusinessError; 925 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 926 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 927 } 928 try { 929 let ret = data.readLong(); 930 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 931 } catch (error) { 932 let e: BusinessError = error as BusinessError; 933 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 934 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 935 } 936 ``` 937 938### writeFloat 939 940writeFloat(val: number): void 941 942将浮点值写入MessageSequence实例。 943 944**系统能力**:SystemCapability.Communication.IPC.Core 945 946**参数:** 947 948 | 参数名 | 类型 | 必填 | 说明 | 949 | ------ | ------ | ---- | ----- | 950 | val | number | 是 | 要写入的浮点值。 | 951 952**错误码:** 953 954以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 955 956 | 错误码ID | 错误信息 | 957 | -------- | -------- | 958 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 959 | 1900009 | Failed to write data to the message sequence. | 960 961**示例:** 962 963 ```ts 964 import { hilog } from '@kit.PerformanceAnalysisKit'; 965 import { BusinessError } from '@kit.BasicServicesKit'; 966 967 let data = rpc.MessageSequence.create(); 968 try { 969 data.writeFloat(1.2); 970 } catch (error) { 971 let e: BusinessError = error as BusinessError; 972 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 973 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 974 } 975 ``` 976 977### readFloat 978 979readFloat(): number 980 981从MessageSequence实例中读取浮点值。 982 983**系统能力**:SystemCapability.Communication.IPC.Core 984 985**返回值:** 986 987 | 类型 | 说明 | 988 | ------ | ------------ | 989 | number | 返回浮点值。 | 990 991**错误码:** 992 993以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 994 995 | 错误码ID | 错误信息 | 996 | -------- | -------- | 997 | 1900010 | Failed to read data from the message sequence. | 998 999**示例:** 1000 1001 ```ts 1002 import { hilog } from '@kit.PerformanceAnalysisKit'; 1003 import { BusinessError } from '@kit.BasicServicesKit'; 1004 1005 let data = rpc.MessageSequence.create(); 1006 try { 1007 data.writeFloat(1.2); 1008 } catch (error) { 1009 let e: BusinessError = error as BusinessError; 1010 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 1011 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 1012 } 1013 try { 1014 let ret = data.readFloat(); 1015 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 1016 } catch (error) { 1017 let e: BusinessError = error as BusinessError; 1018 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 1019 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 1020 } 1021 ``` 1022 1023### writeDouble 1024 1025writeDouble(val: number): void 1026 1027将双精度浮点值写入MessageSequence实例。 1028 1029**系统能力**:SystemCapability.Communication.IPC.Core 1030 1031**参数:** 1032 1033 | 参数名 | 类型 | 必填 | 说明 | 1034 | ------ | ------ | ---- | ---------------------- | 1035 | val | number | 是 | 要写入的双精度浮点值。 | 1036 1037**错误码:** 1038 1039以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1040 1041 | 错误码ID | 错误信息 | 1042 | -------- | -------- | 1043 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1044 | 1900009 | Failed to write data to the message sequence. | 1045 1046**示例:** 1047 1048 ```ts 1049 import { hilog } from '@kit.PerformanceAnalysisKit'; 1050 import { BusinessError } from '@kit.BasicServicesKit'; 1051 1052 let data = rpc.MessageSequence.create(); 1053 try { 1054 data.writeDouble(10.2); 1055 } catch (error) { 1056 let e: BusinessError = error as BusinessError; 1057 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1058 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1059 } 1060 ``` 1061 1062### readDouble 1063 1064readDouble(): number 1065 1066从MessageSequence实例读取双精度浮点值。 1067 1068**系统能力**:SystemCapability.Communication.IPC.Core 1069 1070**返回值:** 1071 1072 | 类型 | 说明 | 1073 | ------ | ------------------ | 1074 | number | 返回双精度浮点值。 | 1075 1076**错误码:** 1077 1078以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1079 1080 | 错误码ID | 错误信息 | 1081 | -------- | -------- | 1082 | 1900010 | Failed to read data from the message sequence. | 1083 1084**示例:** 1085 1086 ```ts 1087 import { hilog } from '@kit.PerformanceAnalysisKit'; 1088 import { BusinessError } from '@kit.BasicServicesKit'; 1089 1090 let data = rpc.MessageSequence.create(); 1091 try { 1092 data.writeDouble(10.2); 1093 } catch (error) { 1094 let e: BusinessError = error as BusinessError; 1095 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1096 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1097 } 1098 try { 1099 let ret = data.readDouble(); 1100 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1101 } catch (error) { 1102 let e: BusinessError = error as BusinessError; 1103 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1104 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1105 } 1106 ``` 1107 1108### writeBoolean 1109 1110writeBoolean(val: boolean): void 1111 1112将布尔值写入MessageSequence实例。 1113 1114**系统能力**:SystemCapability.Communication.IPC.Core 1115 1116**参数:** 1117 1118 | 参数名 | 类型 | 必填 | 说明 | 1119 | ------ | ------- | ---- | ---------------- | 1120 | val | boolean | 是 | 要写入的布尔值。 | 1121 1122**错误码:** 1123 1124以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1125 1126 | 错误码ID | 错误信息 | 1127 | -------- | -------- | 1128 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1129 | 1900009 | Failed to write data to the message sequence. | 1130 1131**示例:** 1132 1133 ```ts 1134 import { hilog } from '@kit.PerformanceAnalysisKit'; 1135 import { BusinessError } from '@kit.BasicServicesKit'; 1136 1137 let data = rpc.MessageSequence.create(); 1138 try { 1139 data.writeBoolean(false); 1140 } catch (error) { 1141 let e: BusinessError = error as BusinessError; 1142 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1143 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1144 } 1145 ``` 1146 1147### readBoolean 1148 1149readBoolean(): boolean 1150 1151从MessageSequence实例读取布尔值。 1152 1153**系统能力**:SystemCapability.Communication.IPC.Core 1154 1155**返回值:** 1156 1157 | 类型 | 说明 | 1158 | ------- | -------------------- | 1159 | boolean | 返回读取到的布尔值。 | 1160 1161**错误码:** 1162 1163以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1164 1165 | 错误码ID | 错误信息 | 1166 | -------- | -------- | 1167 | 1900010 | Failed to read data from the message sequence. | 1168 1169**示例:** 1170 1171 ```ts 1172 import { hilog } from '@kit.PerformanceAnalysisKit'; 1173 import { BusinessError } from '@kit.BasicServicesKit'; 1174 1175 let data = rpc.MessageSequence.create(); 1176 try { 1177 data.writeBoolean(false); 1178 } catch (error) { 1179 let e: BusinessError = error as BusinessError; 1180 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1181 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1182 } 1183 try { 1184 let ret = data.readBoolean(); 1185 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1186 } catch (error) { 1187 let e: BusinessError = error as BusinessError; 1188 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1189 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1190 } 1191 ``` 1192 1193### writeChar 1194 1195writeChar(val: number): void 1196 1197将单个字符值写入MessageSequence实例。 1198 1199**系统能力**:SystemCapability.Communication.IPC.Core 1200 1201**参数:** 1202 1203 | 参数名 | 类型 | 必填 | 说明 | 1204 | ------ | ------ | ---- | -------------------- | 1205 | val | number | 是 | 要写入的单个字符值。 | 1206 1207**错误码:** 1208 1209以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1210 1211 | 错误码ID | 错误信息 | 1212 | -------- | -------- | 1213 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1214 | 1900009 | Failed to write data to the message sequence. | 1215 1216**示例:** 1217 1218 ```ts 1219 import { hilog } from '@kit.PerformanceAnalysisKit'; 1220 import { BusinessError } from '@kit.BasicServicesKit'; 1221 1222 let data = rpc.MessageSequence.create(); 1223 try { 1224 data.writeChar(97); 1225 } catch (error) { 1226 let e: BusinessError = error as BusinessError; 1227 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1228 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1229 } 1230 ``` 1231 1232### readChar 1233 1234readChar(): number 1235 1236从MessageSequence实例中读取单个字符值。 1237 1238**系统能力**:SystemCapability.Communication.IPC.Core 1239 1240**返回值:** 1241 1242 | 类型 | 说明 | 1243 | ------ | ---- | 1244 | number | 返回单个字符值。 | 1245 1246**错误码:** 1247 1248以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1249 1250 | 错误码ID | 错误信息 | 1251 | -------- | -------- | 1252 | 1900010 | Failed to read data from the message sequence. | 1253 1254**示例:** 1255 1256 ```ts 1257 import { hilog } from '@kit.PerformanceAnalysisKit'; 1258 import { BusinessError } from '@kit.BasicServicesKit'; 1259 1260 let data = rpc.MessageSequence.create(); 1261 try { 1262 data.writeChar(97); 1263 } catch (error) { 1264 let e: BusinessError = error as BusinessError; 1265 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1266 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1267 } 1268 try { 1269 let ret = data.readChar(); 1270 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1271 } catch (error) { 1272 let e: BusinessError = error as BusinessError; 1273 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1274 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1275 } 1276 ``` 1277 1278### writeString 1279 1280writeString(val: string): void 1281 1282将字符串值写入MessageSequence实例。 1283 1284**系统能力**:SystemCapability.Communication.IPC.Core 1285 1286**参数:** 1287 1288 | 参数名 | 类型 | 必填 | 说明 | 1289 | ------ | ------ | ---- | ----------------------------------------- | 1290 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 1291 1292**错误码:** 1293 1294以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1295 1296 | 错误码ID | 错误信息 | 1297 | -------- | -------- | 1298 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The string length exceeds 40960 bytes; <br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 1299 | 1900009 | Failed to write data to the message sequence. | 1300 1301**示例:** 1302 1303 ```ts 1304 import { hilog } from '@kit.PerformanceAnalysisKit'; 1305 import { BusinessError } from '@kit.BasicServicesKit'; 1306 1307 let data = rpc.MessageSequence.create(); 1308 try { 1309 data.writeString('abc'); 1310 } catch (error) { 1311 let e: BusinessError = error as BusinessError; 1312 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1313 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1314 } 1315 ``` 1316 1317### readString 1318 1319readString(): string 1320 1321从MessageSequence实例读取字符串值。 1322 1323**系统能力**:SystemCapability.Communication.IPC.Core 1324 1325**返回值:** 1326 1327 | 类型 | 说明 | 1328 | ------ | -------------- | 1329 | string | 返回字符串值。 | 1330 1331**错误码:** 1332 1333以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1334 1335 | 错误码ID | 错误信息 | 1336 | -------- | -------- | 1337 | 1900010 | Failed to read data from the message sequence. | 1338 1339**示例:** 1340 1341 ```ts 1342 import { hilog } from '@kit.PerformanceAnalysisKit'; 1343 import { BusinessError } from '@kit.BasicServicesKit'; 1344 1345 let data = rpc.MessageSequence.create(); 1346 try { 1347 data.writeString('abc'); 1348 } catch (error) { 1349 let e: BusinessError = error as BusinessError; 1350 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1351 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1352 } 1353 try { 1354 let ret = data.readString(); 1355 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1356 } catch (error) { 1357 let e: BusinessError = error as BusinessError; 1358 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1359 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1360 } 1361 ``` 1362 1363### writeParcelable 1364 1365writeParcelable(val: Parcelable): void 1366 1367将自定义序列化对象写入MessageSequence实例。 1368 1369**系统能力**:SystemCapability.Communication.IPC.Core 1370 1371**参数:** 1372 1373| 参数名 | 类型 | 必填 | 说明 | 1374| ------ | --------- | ---- | ------ | 1375| val | [Parcelable](#parcelable9) | 是 | 要写入的可序列对象。 | 1376 1377**错误码:** 1378 1379以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1380 1381 | 错误码ID | 错误信息 | 1382 | -------- | -------- | 1383 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1384 | 1900009 | Failed to write data to the message sequence. | 1385 1386**示例:** 1387 1388 ```ts 1389 import { hilog } from '@kit.PerformanceAnalysisKit'; 1390 import { BusinessError } from '@kit.BasicServicesKit'; 1391 1392 class MyParcelable implements rpc.Parcelable { 1393 num: number = 0; 1394 str: string = ''; 1395 constructor( num: number, str: string) { 1396 this.num = num; 1397 this.str = str; 1398 } 1399 marshalling(messageSequence: rpc.MessageSequence): boolean { 1400 messageSequence.writeInt(this.num); 1401 messageSequence.writeString(this.str); 1402 return true; 1403 } 1404 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1405 this.num = messageSequence.readInt(); 1406 this.str = messageSequence.readString(); 1407 return true; 1408 } 1409 } 1410 let parcelable = new MyParcelable(1, "aaa"); 1411 let data = rpc.MessageSequence.create(); 1412 try { 1413 data.writeParcelable(parcelable); 1414 } catch (error) { 1415 let e: BusinessError = error as BusinessError; 1416 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1417 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1418 } 1419 ``` 1420 1421### readParcelable 1422 1423readParcelable(dataIn: Parcelable): void 1424 1425从MessageSequence实例中读取成员变量到指定的对象(dataIn)。 1426 1427**系统能力**:SystemCapability.Communication.IPC.Core 1428 1429**参数:** 1430 1431| 参数名 | 类型 | 必填 | 说明 | 1432| ------ | -------------------------- | ---- | ----------------------------------------- | 1433| dataIn | [Parcelable](#parcelable9) | 是 | 需要从MessageSequence读取成员变量的对象。 | 1434 1435**错误码:** 1436 1437以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1438 1439 | 错误码ID | 错误信息 | 1440 | -------- | -------- | 1441 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect. | 1442 | 1900010 | Failed to read data from the message sequence. | 1443 | 1900012 | Failed to call the JS callback function. | 1444 1445**示例:** 1446 1447 ```ts 1448 import { hilog } from '@kit.PerformanceAnalysisKit'; 1449 import { BusinessError } from '@kit.BasicServicesKit'; 1450 1451 class MyParcelable implements rpc.Parcelable { 1452 num: number = 0; 1453 str: string = ''; 1454 constructor(num: number, str: string) { 1455 this.num = num; 1456 this.str = str; 1457 } 1458 marshalling(messageSequence: rpc.MessageSequence): boolean { 1459 messageSequence.writeInt(this.num); 1460 messageSequence.writeString(this.str); 1461 return true; 1462 } 1463 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1464 this.num = messageSequence.readInt(); 1465 this.str = messageSequence.readString(); 1466 return true; 1467 } 1468 } 1469 let parcelable = new MyParcelable(1, "aaa"); 1470 let data = rpc.MessageSequence.create(); 1471 data.writeParcelable(parcelable); 1472 let ret = new MyParcelable(0, ""); 1473 try { 1474 data.readParcelable(ret); 1475 }catch (error) { 1476 let e: BusinessError = error as BusinessError; 1477 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1478 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1479 } 1480 ``` 1481 1482### writeByteArray 1483 1484writeByteArray(byteArray: number[]): void 1485 1486将字节数组写入MessageSequence实例。 1487 1488**系统能力**:SystemCapability.Communication.IPC.Core 1489 1490**参数:** 1491 1492 | 参数名 | 类型 | 必填 | 说明 | 1493 | --------- | -------- | ---- | ------------------ | 1494 | byteArray | number[] | 是 | 要写入的字节数组。 | 1495 1496**错误码:** 1497 1498以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1499 1500 | 错误码ID | 错误信息 | 1501 | -------- | -------- | 1502 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. <br/> 5.The type of the element in the array is incorrect. | 1503 | 1900009 | Failed to write data to the message sequence. | 1504 1505**示例:** 1506 1507 ```ts 1508 import { hilog } from '@kit.PerformanceAnalysisKit'; 1509 import { BusinessError } from '@kit.BasicServicesKit'; 1510 1511 let data = rpc.MessageSequence.create(); 1512 let ByteArrayVar = [1, 2, 3, 4, 5]; 1513 try { 1514 data.writeByteArray(ByteArrayVar); 1515 } catch (error) { 1516 let e: BusinessError = error as BusinessError; 1517 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1518 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1519 } 1520 ``` 1521 1522### readByteArray 1523 1524readByteArray(dataIn: number[]): void 1525 1526从MessageSequence实例读取字节数组。 1527 1528**系统能力**:SystemCapability.Communication.IPC.Core 1529 1530**参数:** 1531 1532 | 参数名 | 类型 | 必填 | 说明 | 1533 | ------ | -------- | ---- | ------------------ | 1534 | dataIn | number[] | 是 | 要读取的字节数组。 | 1535 1536**错误码:** 1537 1538以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1539 1540 | 错误码ID | 错误信息 | 1541 | -------- | -------- | 1542 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1543 | 1900010 | Failed to read data from the message sequence. | 1544 1545**示例:** 1546 1547 ```ts 1548 import { hilog } from '@kit.PerformanceAnalysisKit'; 1549 import { BusinessError } from '@kit.BasicServicesKit'; 1550 1551 let data = rpc.MessageSequence.create(); 1552 let ByteArrayVar = [1, 2, 3, 4, 5]; 1553 try { 1554 data.writeByteArray(ByteArrayVar); 1555 } catch (error) { 1556 let e: BusinessError = error as BusinessError; 1557 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1558 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1559 } 1560 try { 1561 let array: Array<number> = new Array(5); 1562 data.readByteArray(array); 1563 } catch (error) { 1564 let e: BusinessError = error as BusinessError; 1565 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1566 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1567 } 1568 ``` 1569 1570### readByteArray 1571 1572readByteArray(): number[] 1573 1574从MessageSequence实例中读取字节数组。 1575 1576**系统能力**:SystemCapability.Communication.IPC.Core 1577 1578**返回值:** 1579 1580 | 类型 | 说明 | 1581 | -------- | -------------- | 1582 | number[] | 返回字节数组。 | 1583 1584**错误码:** 1585 1586以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1587 1588 | 错误码ID | 错误信息 | 1589 | -------- | -------- | 1590 | 401 | check param failed | 1591 | 1900010 | Failed to read data from the message sequence. | 1592 1593**示例:** 1594 1595 ```ts 1596 import { hilog } from '@kit.PerformanceAnalysisKit'; 1597 import { BusinessError } from '@kit.BasicServicesKit'; 1598 1599 let data = rpc.MessageSequence.create(); 1600 let byteArrayVar = [1, 2, 3, 4, 5]; 1601 try { 1602 data.writeByteArray(byteArrayVar); 1603 } catch (error) { 1604 let e: BusinessError = error as BusinessError; 1605 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1606 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1607 } 1608 try { 1609 let array = data.readByteArray(); 1610 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1611 } catch (error) { 1612 let e: BusinessError = error as BusinessError; 1613 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1614 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1615 } 1616 ``` 1617 1618### writeShortArray 1619 1620writeShortArray(shortArray: number[]): void 1621 1622将短整数数组写入MessageSequence实例。 1623 1624**系统能力**:SystemCapability.Communication.IPC.Core 1625 1626**参数:** 1627 1628 | 参数名 | 类型 | 必填 | 说明 | 1629 | ---------- | -------- | ---- | -------------------- | 1630 | shortArray | number[] | 是 | 要写入的短整数数组。 | 1631 1632**错误码:** 1633 1634以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1635 1636 | 错误码ID | 错误信息 | 1637 | -------- | -------- | 1638 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 1639 | 1900009 | Failed to write data to the message sequence. | 1640 1641**示例:** 1642 1643 ```ts 1644 import { hilog } from '@kit.PerformanceAnalysisKit'; 1645 import { BusinessError } from '@kit.BasicServicesKit'; 1646 1647 let data = rpc.MessageSequence.create(); 1648 try { 1649 data.writeShortArray([11, 12, 13]); 1650 } catch (error) { 1651 let e: BusinessError = error as BusinessError; 1652 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1653 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1654 } 1655 ``` 1656 1657### readShortArray 1658 1659readShortArray(dataIn: number[]): void 1660 1661从MessageSequence实例中读取短整数数组。 1662 1663**系统能力**:SystemCapability.Communication.IPC.Core 1664 1665**参数:** 1666 1667 | 参数名 | 类型 | 必填 | 说明 | 1668 | ------ | -------- | ---- | -------------------- | 1669 | dataIn | number[] | 是 | 要读取的短整数数组。 | 1670 1671**错误码:** 1672 1673以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1674 1675 | 错误码ID | 错误信息 | 1676 | -------- | -------- | 1677 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1678 | 1900010 | Failed to read data from the message sequence. | 1679 1680**示例:** 1681 1682 ```ts 1683 import { hilog } from '@kit.PerformanceAnalysisKit'; 1684 import { BusinessError } from '@kit.BasicServicesKit'; 1685 1686 let data = rpc.MessageSequence.create(); 1687 try { 1688 data.writeShortArray([11, 12, 13]); 1689 } catch (error) { 1690 let e: BusinessError = error as BusinessError; 1691 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1692 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1693 } 1694 try { 1695 let array: Array<number> = new Array(3); 1696 data.readShortArray(array); 1697 } catch (error) { 1698 let e: BusinessError = error as BusinessError; 1699 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1700 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1701 } 1702 ``` 1703 1704### readShortArray 1705 1706readShortArray(): number[] 1707 1708从MessageSequence实例中读取短整数数组。 1709 1710**系统能力**:SystemCapability.Communication.IPC.Core 1711 1712**返回值:** 1713 1714 | 类型 | 说明 | 1715 | -------- | ---------------- | 1716 | number[] | 返回短整数数组。 | 1717 1718**错误码:** 1719 1720以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1721 1722 | 错误码ID | 错误信息 | 1723 | -------- | -------- | 1724 | 1900010 | Failed to read data from the message sequence. | 1725 1726**示例:** 1727 1728 ```ts 1729 import { hilog } from '@kit.PerformanceAnalysisKit'; 1730 import { BusinessError } from '@kit.BasicServicesKit'; 1731 1732 let data = rpc.MessageSequence.create(); 1733 try { 1734 data.writeShortArray([11, 12, 13]); 1735 } catch (error) { 1736 let e: BusinessError = error as BusinessError; 1737 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1738 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1739 } 1740 try { 1741 let array = data.readShortArray(); 1742 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1743 } catch (error) { 1744 let e: BusinessError = error as BusinessError; 1745 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1746 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1747 } 1748 ``` 1749 1750### writeIntArray 1751 1752writeIntArray(intArray: number[]): void 1753 1754将整数数组写入MessageSequence实例。 1755 1756**系统能力**:SystemCapability.Communication.IPC.Core 1757 1758**参数:** 1759 1760 | 参数名 | 类型 | 必填 | 说明 | 1761 | -------- | -------- | ---- | ------------------ | 1762 | intArray | number[] | 是 | 要写入的整数数组。 | 1763 1764**错误码:** 1765 1766以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1767 1768 | 错误码ID | 错误信息 | 1769 | -------- | -------- | 1770 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 1771 | 1900009 | Failed to write data to the message sequence. | 1772 1773**示例:** 1774 1775 ```ts 1776 import { hilog } from '@kit.PerformanceAnalysisKit'; 1777 import { BusinessError } from '@kit.BasicServicesKit'; 1778 1779 let data = rpc.MessageSequence.create(); 1780 try { 1781 data.writeIntArray([100, 111, 112]); 1782 } catch (error) { 1783 let e: BusinessError = error as BusinessError; 1784 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1785 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1786 } 1787 ``` 1788 1789### readIntArray 1790 1791readIntArray(dataIn: number[]): void 1792 1793从MessageSequence实例中读取整数数组。 1794 1795**系统能力**:SystemCapability.Communication.IPC.Core 1796 1797**参数:** 1798 1799 | 参数名 | 类型 | 必填 | 说明 | 1800 | ------ | -------- | ---- | ------------------ | 1801 | dataIn | number[] | 是 | 要读取的整数数组。 | 1802 1803**错误码:** 1804 1805以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1806 1807 | 错误码ID | 错误信息 | 1808 | -------- | -------- | 1809 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1810 | 1900010 | Failed to read data from the message sequence. | 1811 1812**示例:** 1813 1814 ```ts 1815 import { hilog } from '@kit.PerformanceAnalysisKit'; 1816 import { BusinessError } from '@kit.BasicServicesKit'; 1817 1818 let data = rpc.MessageSequence.create(); 1819 try { 1820 data.writeIntArray([100, 111, 112]); 1821 } catch (error) { 1822 let e: BusinessError = error as BusinessError; 1823 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1824 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1825 } 1826 let array: Array<number> = new Array(3); 1827 try { 1828 data.readIntArray(array); 1829 } catch (error) { 1830 let e: BusinessError = error as BusinessError; 1831 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1832 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1833 } 1834 ``` 1835 1836### readIntArray 1837 1838readIntArray(): number[] 1839 1840从MessageSequence实例中读取整数数组。 1841 1842**系统能力**:SystemCapability.Communication.IPC.Core 1843 1844**返回值:** 1845 1846 | 类型 | 说明 | 1847 | -------- | -------------- | 1848 | number[] | 返回整数数组。 | 1849 1850**错误码:** 1851 1852以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1853 1854 | 错误码ID | 错误信息 | 1855 | -------- | -------- | 1856 | 1900010 | Failed to read data from the message sequence. | 1857 1858**示例:** 1859 1860 ```ts 1861 import { hilog } from '@kit.PerformanceAnalysisKit'; 1862 import { BusinessError } from '@kit.BasicServicesKit'; 1863 1864 let data = rpc.MessageSequence.create(); 1865 try { 1866 data.writeIntArray([100, 111, 112]); 1867 } catch (error) { 1868 let e: BusinessError = error as BusinessError; 1869 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1870 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1871 } 1872 try { 1873 let array = data.readIntArray(); 1874 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1875 } catch (error) { 1876 let e: BusinessError = error as BusinessError; 1877 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1878 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1879 } 1880 ``` 1881 1882### writeLongArray 1883 1884writeLongArray(longArray: number[]): void 1885 1886将长整数数组写入MessageSequence实例。 1887 1888**系统能力**:SystemCapability.Communication.IPC.Core 1889 1890**参数:** 1891 1892 | 参数名 | 类型 | 必填 | 说明 | 1893 | --------- | -------- | ---- | -------------------- | 1894 | longArray | number[] | 是 | 要写入的长整数数组。 | 1895 1896**错误码:** 1897 1898以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1899 1900 | 错误码ID | 错误信息 | 1901 | -------- | -------- | 1902 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 1903 | 1900009 | Failed to write data to the message sequence. | 1904 1905**示例:** 1906 1907 ```ts 1908 import { hilog } from '@kit.PerformanceAnalysisKit'; 1909 import { BusinessError } from '@kit.BasicServicesKit'; 1910 1911 let data = rpc.MessageSequence.create(); 1912 try { 1913 data.writeLongArray([1111, 1112, 1113]); 1914 }catch (error){ 1915 let e: BusinessError = error as BusinessError; 1916 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1917 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1918 } 1919 ``` 1920 1921### readLongArray 1922 1923readLongArray(dataIn: number[]): void 1924 1925从MessageSequence实例读取的长整数数组。 1926 1927**系统能力**:SystemCapability.Communication.IPC.Core 1928 1929**参数:** 1930 1931 | 参数名 | 类型 | 必填 | 说明 | 1932 | ------ | -------- | ---- | -------------------- | 1933 | dataIn | number[] | 是 | 要读取的长整数数组。 | 1934 1935**错误码:** 1936 1937以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1938 1939 | 错误码ID | 错误信息 | 1940 | -------- | -------- | 1941 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1942 | 1900010 | Failed to read data from the message sequence. | 1943 1944**示例:** 1945 1946 ```ts 1947 import { hilog } from '@kit.PerformanceAnalysisKit'; 1948 import { BusinessError } from '@kit.BasicServicesKit'; 1949 1950 let data = rpc.MessageSequence.create(); 1951 try { 1952 data.writeLongArray([1111, 1112, 1113]); 1953 } catch (error) { 1954 let e: BusinessError = error as BusinessError; 1955 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1956 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1957 } 1958 let array: Array<number> = new Array(3); 1959 try { 1960 data.readLongArray(array); 1961 } catch (error) { 1962 let e: BusinessError = error as BusinessError; 1963 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1964 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1965 } 1966 ``` 1967 1968### readLongArray 1969 1970readLongArray(): number[] 1971 1972从MessageSequence实例中读取所有的长整数数组。 1973 1974**系统能力**:SystemCapability.Communication.IPC.Core 1975 1976**返回值:** 1977 1978 | 类型 | 说明 | 1979 | -------- | ---------------- | 1980 | number[] | 返回长整数数组。 | 1981 1982**错误码:** 1983 1984以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1985 1986 | 错误码ID | 错误信息 | 1987 | -------- | -------- | 1988 | 1900010 | Failed to read data from the message sequence. | 1989 1990**示例:** 1991 1992 ```ts 1993 import { hilog } from '@kit.PerformanceAnalysisKit'; 1994 import { BusinessError } from '@kit.BasicServicesKit'; 1995 1996 let data = rpc.MessageSequence.create(); 1997 try { 1998 data.writeLongArray([1111, 1112, 1113]); 1999 } catch (error) { 2000 let e: BusinessError = error as BusinessError; 2001 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 2002 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 2003 } 2004 try { 2005 let array = data.readLongArray(); 2006 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 2007 } catch (error) { 2008 let e: BusinessError = error as BusinessError; 2009 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 2010 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 2011 } 2012 ``` 2013 2014### writeFloatArray 2015 2016writeFloatArray(floatArray: number[]): void 2017 2018将浮点数组写入MessageSequence实例。 2019 2020**系统能力**:SystemCapability.Communication.IPC.Core 2021 2022**参数:** 2023 2024 | 参数名 | 类型 | 必填 | 说明 | 2025 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2026 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2027 2028**错误码:** 2029 2030以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2031 2032 | 错误码ID | 错误信息 | 2033 | -------- | -------- | 2034 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 2035 | 1900009 | Failed to write data to the message sequence. | 2036 2037**示例:** 2038 2039 ```ts 2040 import { hilog } from '@kit.PerformanceAnalysisKit'; 2041 import { BusinessError } from '@kit.BasicServicesKit'; 2042 2043 let data = rpc.MessageSequence.create(); 2044 try { 2045 data.writeFloatArray([1.2, 1.3, 1.4]); 2046 } catch (error) { 2047 let e: BusinessError = error as BusinessError; 2048 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2049 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2050 } 2051 ``` 2052 2053### readFloatArray 2054 2055readFloatArray(dataIn: number[]): void 2056 2057从MessageSequence实例中读取浮点数组。 2058 2059**系统能力**:SystemCapability.Communication.IPC.Core 2060 2061**参数:** 2062 2063 | 参数名 | 类型 | 必填 | 说明 | 2064 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2065 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2066 2067**错误码:** 2068 2069以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2070 2071 | 错误码ID | 错误信息 | 2072 | -------- | -------- | 2073 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2074 | 1900010 | Failed to read data from the message sequence. | 2075 2076**示例:** 2077 2078 ```ts 2079 import { hilog } from '@kit.PerformanceAnalysisKit'; 2080 import { BusinessError } from '@kit.BasicServicesKit'; 2081 2082 let data = rpc.MessageSequence.create(); 2083 try { 2084 data.writeFloatArray([1.2, 1.3, 1.4]); 2085 }catch (error){ 2086 let e: BusinessError = error as BusinessError; 2087 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2088 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2089 } 2090 let array: Array<number> = new Array(3); 2091 try { 2092 data.readFloatArray(array); 2093 } catch (error) { 2094 let e: BusinessError = error as BusinessError; 2095 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2096 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2097 } 2098 ``` 2099 2100### readFloatArray 2101 2102readFloatArray(): number[] 2103 2104从MessageSequence实例中读取浮点数组。 2105 2106**系统能力**:SystemCapability.Communication.IPC.Core 2107 2108**返回值:** 2109 2110 | 类型 | 说明 | 2111 | -------- | -------------- | 2112 | number[] | 返回浮点数组。 | 2113 2114**错误码:** 2115 2116以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2117 2118 | 错误码ID | 错误信息 | 2119 | -------- | -------- | 2120 | 1900010 | Failed to read data from the message sequence. | 2121 2122**示例:** 2123 2124 ```ts 2125 import { hilog } from '@kit.PerformanceAnalysisKit'; 2126 import { BusinessError } from '@kit.BasicServicesKit'; 2127 2128 let data = rpc.MessageSequence.create(); 2129 try { 2130 data.writeFloatArray([1.2, 1.3, 1.4]); 2131 } catch (error) { 2132 let e: BusinessError = error as BusinessError; 2133 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2134 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2135 } 2136 try { 2137 let array = data.readFloatArray(); 2138 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2139 } catch (error) { 2140 let e: BusinessError = error as BusinessError; 2141 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2142 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2143 } 2144 ``` 2145 2146### writeDoubleArray 2147 2148writeDoubleArray(doubleArray: number[]): void 2149 2150将双精度浮点数组写入MessageSequence实例。 2151 2152**系统能力**:SystemCapability.Communication.IPC.Core 2153 2154**参数:** 2155 2156 | 参数名 | 类型 | 必填 | 说明 | 2157 | ----------- | -------- | ---- | ------------------------ | 2158 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 2159 2160**错误码:** 2161 2162以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2163 2164 | 错误码ID | 错误信息 | 2165 | -------- | -------- | 2166 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 2167 | 1900009 | Failed to write data to the message sequence. | 2168 2169**示例:** 2170 2171 ```ts 2172 import { hilog } from '@kit.PerformanceAnalysisKit'; 2173 import { BusinessError } from '@kit.BasicServicesKit'; 2174 2175 let data = rpc.MessageSequence.create(); 2176 try { 2177 data.writeDoubleArray([11.1, 12.2, 13.3]); 2178 } catch (error) { 2179 let e: BusinessError = error as BusinessError; 2180 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2181 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2182 } 2183 ``` 2184 2185### readDoubleArray 2186 2187readDoubleArray(dataIn: number[]): void 2188 2189从MessageSequence实例中读取双精度浮点数组。 2190 2191**系统能力**:SystemCapability.Communication.IPC.Core 2192 2193**参数:** 2194 2195 | 参数名 | 类型 | 必填 | 说明 | 2196 | ------ | -------- | ---- | ------------------------ | 2197 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 2198 2199**错误码:** 2200 2201以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2202 2203 | 错误码ID | 错误信息 | 2204 | -------- | -------- | 2205 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2206 | 1900010 | Failed to read data from the message sequence. | 2207 2208**示例:** 2209 2210 ```ts 2211 import { hilog } from '@kit.PerformanceAnalysisKit'; 2212 import { BusinessError } from '@kit.BasicServicesKit'; 2213 2214 let data = rpc.MessageSequence.create(); 2215 try { 2216 data.writeDoubleArray([11.1, 12.2, 13.3]); 2217 } catch (error) { 2218 let e: BusinessError = error as BusinessError; 2219 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2220 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2221 } 2222 let array: Array<number> = new Array(3); 2223 try { 2224 data.readDoubleArray(array); 2225 } catch (error) { 2226 let e: BusinessError = error as BusinessError; 2227 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2228 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2229 } 2230 ``` 2231 2232### readDoubleArray 2233 2234readDoubleArray(): number[] 2235 2236从MessageSequence实例读取所有双精度浮点数组。 2237 2238**系统能力**:SystemCapability.Communication.IPC.Core 2239 2240**返回值:** 2241 2242 | 类型 | 说明 | 2243 | -------- | -------------------- | 2244 | number[] | 返回双精度浮点数组。 | 2245 2246**错误码:** 2247 2248以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2249 2250 | 错误码ID | 错误信息 | 2251 | -------- | -------- | 2252 | 1900010 | Failed to read data from the message sequence. | 2253 2254**示例:** 2255 2256 ```ts 2257 import { hilog } from '@kit.PerformanceAnalysisKit'; 2258 import { BusinessError } from '@kit.BasicServicesKit'; 2259 2260 let data = rpc.MessageSequence.create(); 2261 try { 2262 data.writeDoubleArray([11.1, 12.2, 13.3]); 2263 } catch (error) { 2264 let e: BusinessError = error as BusinessError; 2265 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2266 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2267 } 2268 try { 2269 let array = data.readDoubleArray(); 2270 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2271 } catch (error) { 2272 let e: BusinessError = error as BusinessError; 2273 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2274 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2275 } 2276 ``` 2277 2278### writeBooleanArray 2279 2280writeBooleanArray(booleanArray: boolean[]): void 2281 2282将布尔数组写入MessageSequence实例。 2283 2284**系统能力**:SystemCapability.Communication.IPC.Core 2285 2286**参数:** 2287 2288 | 参数名 | 类型 | 必填 | 说明 | 2289 | ------------ | --------- | ---- | ------------------ | 2290 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 2291 2292**错误码:** 2293 2294以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2295 2296 | 错误码ID | 错误信息 | 2297 | -------- | -------- | 2298 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. | 2299 | 1900009 | Failed to write data to the message sequence. | 2300 2301**示例:** 2302 2303 ```ts 2304 import { hilog } from '@kit.PerformanceAnalysisKit'; 2305 import { BusinessError } from '@kit.BasicServicesKit'; 2306 2307 let data = rpc.MessageSequence.create(); 2308 try { 2309 data.writeBooleanArray([false, true, false]); 2310 } catch (error) { 2311 let e: BusinessError = error as BusinessError; 2312 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2313 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2314 } 2315 ``` 2316 2317### readBooleanArray 2318 2319readBooleanArray(dataIn: boolean[]): void 2320 2321从MessageSequence实例中读取布尔数组。 2322 2323**系统能力**:SystemCapability.Communication.IPC.Core 2324 2325**参数:** 2326 2327 | 参数名 | 类型 | 必填 | 说明 | 2328 | ------ | --------- | ---- | ------------------ | 2329 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 2330 2331**错误码:** 2332 2333以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2334 2335 | 错误码ID | 错误信息 | 2336 | -------- | -------- | 2337 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2338 | 1900010 | Failed to read data from the message sequence. | 2339 2340**示例:** 2341 2342 ```ts 2343 import { hilog } from '@kit.PerformanceAnalysisKit'; 2344 import { BusinessError } from '@kit.BasicServicesKit'; 2345 2346 let data = rpc.MessageSequence.create(); 2347 try { 2348 data.writeBooleanArray([false, true, false]); 2349 } catch (error) { 2350 let e: BusinessError = error as BusinessError; 2351 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2352 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2353 } 2354 let array: Array<boolean> = new Array(3); 2355 try { 2356 data.readBooleanArray(array); 2357 } catch (error) { 2358 let e: BusinessError = error as BusinessError; 2359 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2360 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2361 } 2362 ``` 2363 2364### readBooleanArray 2365 2366readBooleanArray(): boolean[] 2367 2368从MessageSequence实例中读取所有布尔数组。 2369 2370**系统能力**:SystemCapability.Communication.IPC.Core 2371 2372**返回值:** 2373 2374 | 类型 | 说明 | 2375 | --------- | -------------- | 2376 | boolean[] | 返回布尔数组。 | 2377 2378**错误码:** 2379 2380以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2381 2382 | 错误码ID | 错误信息 | 2383 | -------- | -------- | 2384 | 1900010 | Failed to read data from the message sequence. | 2385 2386**示例:** 2387 2388 ```ts 2389 import { hilog } from '@kit.PerformanceAnalysisKit'; 2390 import { BusinessError } from '@kit.BasicServicesKit'; 2391 2392 let data = rpc.MessageSequence.create(); 2393 try { 2394 data.writeBooleanArray([false, true, false]); 2395 } catch (error) { 2396 let e: BusinessError = error as BusinessError; 2397 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2398 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2399 } 2400 try { 2401 let array = data.readBooleanArray(); 2402 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2403 } catch (error) { 2404 let e: BusinessError = error as BusinessError; 2405 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2406 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2407 } 2408 ``` 2409 2410### writeCharArray 2411 2412writeCharArray(charArray: number[]): void 2413 2414将单个字符数组写入MessageSequence实例。 2415 2416**系统能力**:SystemCapability.Communication.IPC.Core 2417 2418**参数:** 2419 2420 | 参数名 | 类型 | 必填 | 说明 | 2421 | --------- | -------- | ---- | ---------------------- | 2422 | charArray | number[] | 是 | 要写入的单个字符数组。 | 2423 2424**错误码:** 2425 2426以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2427 2428 | 错误码ID | 错误信息 | 2429 | -------- | -------- | 2430 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. | 2431 | 1900009 | Failed to write data to the message sequence. | 2432 2433**示例:** 2434 2435 ```ts 2436 import { hilog } from '@kit.PerformanceAnalysisKit'; 2437 import { BusinessError } from '@kit.BasicServicesKit'; 2438 2439 let data = rpc.MessageSequence.create(); 2440 try { 2441 data.writeCharArray([97, 98, 88]); 2442 } catch (error) { 2443 let e: BusinessError = error as BusinessError; 2444 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2445 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2446 } 2447 ``` 2448 2449### readCharArray 2450 2451readCharArray(dataIn: number[]): void 2452 2453从MessageSequence实例中读取单个字符数组。 2454 2455**系统能力**:SystemCapability.Communication.IPC.Core 2456 2457**参数:** 2458 2459 | 参数名 | 类型 | 必填 | 说明 | 2460 | ------ | -------- | ---- | ---------------------- | 2461 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 2462 2463**错误码:** 2464 2465以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2466 2467 | 错误码ID | 错误信息 | 2468 | -------- | -------- | 2469 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2470 | 1900010 | Failed to read data from the message sequence. | 2471 2472**示例:** 2473 2474 ```ts 2475 import { hilog } from '@kit.PerformanceAnalysisKit'; 2476 import { BusinessError } from '@kit.BasicServicesKit'; 2477 2478 let data = rpc.MessageSequence.create(); 2479 try { 2480 data.writeCharArray([97, 98, 88]); 2481 } catch (error) { 2482 let e: BusinessError = error as BusinessError; 2483 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2484 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2485 } 2486 let array: Array<number> = new Array(3); 2487 try { 2488 data.readCharArray(array); 2489 } catch (error) { 2490 let e: BusinessError = error as BusinessError; 2491 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2492 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2493 } 2494 ``` 2495 2496### readCharArray 2497 2498readCharArray(): number[] 2499 2500从MessageSequence实例读取单个字符数组。 2501 2502**系统能力**:SystemCapability.Communication.IPC.Core 2503 2504**返回值:** 2505 2506 | 类型 | 说明 | 2507 | -------- | ------------------ | 2508 | number[] | 返回单个字符数组。 | 2509 2510**错误码:** 2511 2512以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2513 2514 | 错误码ID | 错误信息 | 2515 | -------- | -------- | 2516 | 1900010 | Failed to read data from the message sequence. | 2517 2518**示例:** 2519 2520 ```ts 2521 import { hilog } from '@kit.PerformanceAnalysisKit'; 2522 import { BusinessError } from '@kit.BasicServicesKit'; 2523 2524 let data = rpc.MessageSequence.create(); 2525 try { 2526 data.writeCharArray([97, 98, 88]); 2527 } catch (error) { 2528 let e: BusinessError = error as BusinessError; 2529 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2530 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2531 } 2532 try { 2533 let array = data.readCharArray(); 2534 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2535 } catch (error) { 2536 let e: BusinessError = error as BusinessError; 2537 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2538 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2539 } 2540 ``` 2541 2542### writeStringArray 2543 2544writeStringArray(stringArray: string[]): void 2545 2546将字符串数组写入MessageSequence实例。 2547 2548**系统能力**:SystemCapability.Communication.IPC.Core 2549 2550**参数:** 2551 2552 | 参数名 | 类型 | 必填 | 说明 | 2553 | ----------- | -------- | ---- | ------------------------------------------------------- | 2554 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 2555 2556**错误码:** 2557 2558以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2559 2560 | 错误码ID | 错误信息 | 2561 | -------- | -------- | 2562 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The string length exceeds 40960 bytes; <br/> 5.The number of bytes copied to the buffer is different from the length of the obtained string. | 2563 | 1900009 | Failed to write data to the message sequence. | 2564 2565**示例:** 2566 2567 ```ts 2568 import { hilog } from '@kit.PerformanceAnalysisKit'; 2569 import { BusinessError } from '@kit.BasicServicesKit'; 2570 2571 let data = rpc.MessageSequence.create(); 2572 try { 2573 data.writeStringArray(["abc", "def"]); 2574 } catch (error) { 2575 let e: BusinessError = error as BusinessError; 2576 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2577 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2578 } 2579 ``` 2580 2581### readStringArray 2582 2583readStringArray(dataIn: string[]): void 2584 2585从MessageSequence实例读取字符串数组。 2586 2587**系统能力**:SystemCapability.Communication.IPC.Core 2588 2589**参数:** 2590 2591 | 参数名 | 类型 | 必填 | 说明 | 2592 | ------ | -------- | ---- | -------------------- | 2593 | dataIn | string[] | 是 | 要读取的字符串数组。 | 2594 2595**错误码:** 2596 2597以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2598 2599 | 错误码ID | 错误信息 | 2600 | -------- | -------- | 2601 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2602 | 1900010 | Failed to read data from the message sequence. | 2603 2604**示例:** 2605 2606 ```ts 2607 import { hilog } from '@kit.PerformanceAnalysisKit'; 2608 import { BusinessError } from '@kit.BasicServicesKit'; 2609 2610 let data = rpc.MessageSequence.create(); 2611 try { 2612 data.writeStringArray(["abc", "def"]); 2613 } catch (error) { 2614 let e: BusinessError = error as BusinessError; 2615 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2616 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2617 } 2618 let array: Array<string> = new Array(2); 2619 try { 2620 data.readStringArray(array); 2621 } catch (error) { 2622 let e: BusinessError = error as BusinessError; 2623 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2624 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2625 } 2626 ``` 2627 2628### readStringArray 2629 2630readStringArray(): string[] 2631 2632从MessageSequence实例读取字符串数组。 2633 2634**系统能力**:SystemCapability.Communication.IPC.Core 2635 2636**返回值:** 2637 2638 | 类型 | 说明 | 2639 | -------- | ---------------- | 2640 | string[] | 返回字符串数组。 | 2641 2642**错误码:** 2643 2644以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2645 2646 | 错误码ID | 错误信息 | 2647 | -------- | -------- | 2648 | 1900010 | Failed to read data from the message sequence. | 2649 2650**示例:** 2651 2652 ```ts 2653 import { hilog } from '@kit.PerformanceAnalysisKit'; 2654 import { BusinessError } from '@kit.BasicServicesKit'; 2655 2656 let data = rpc.MessageSequence.create(); 2657 try { 2658 data.writeStringArray(["abc", "def"]); 2659 } catch (error) { 2660 let e: BusinessError = error as BusinessError; 2661 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2662 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2663 } 2664 try { 2665 let array = data.readStringArray(); 2666 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2667 } catch (error) { 2668 let e: BusinessError = error as BusinessError; 2669 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2670 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2671 } 2672 ``` 2673 2674### writeNoException 2675 2676writeNoException(): void 2677 2678向MessageSequence写入“指示未发生异常”的信息。 2679 2680**系统能力**:SystemCapability.Communication.IPC.Core 2681 2682**错误码:** 2683 2684以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2685 2686 | 错误码ID | 错误信息 | 2687 | -------- | -------- | 2688 | 1900009 | Failed to write data to the message sequence. | 2689 2690**示例:** 2691 2692 ```ts 2693 import { hilog } from '@kit.PerformanceAnalysisKit'; 2694 import { BusinessError } from '@kit.BasicServicesKit'; 2695 2696 class TestRemoteObject extends rpc.RemoteObject { 2697 constructor(descriptor: string) { 2698 super(descriptor); 2699 } 2700 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2701 if (code === 1) { 2702 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2703 try { 2704 reply.writeNoException(); 2705 } catch (error) { 2706 let e: BusinessError = error as BusinessError; 2707 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2708 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2709 } 2710 return true; 2711 } else { 2712 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2713 return false; 2714 } 2715 } 2716 } 2717 ``` 2718 2719### readException 2720 2721readException(): void 2722 2723从MessageSequence中读取异常。 2724 2725**系统能力**:SystemCapability.Communication.IPC.Core 2726 2727**错误码:** 2728 2729以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2730 2731 | 错误码ID | 错误信息 | 2732 | -------- | -------- | 2733 | 1900010 | Failed to read data from the message sequence. | 2734 2735**示例:** 2736 2737 ```ts 2738 // FA模型需要从@kit.AbilityKit导入featureAbility 2739 // import { featureAbility } from '@kit.AbilityKit'; 2740 import { Want, common } from '@kit.AbilityKit'; 2741 import { hilog } from '@kit.PerformanceAnalysisKit'; 2742 2743 let proxy: rpc.IRemoteObject | undefined; 2744 let connect: common.ConnectOptions = { 2745 onConnect: (elementName, remoteProxy) => { 2746 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2747 proxy = remoteProxy; 2748 }, 2749 onDisconnect: (elementName) => { 2750 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2751 }, 2752 onFailed: () => { 2753 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2754 } 2755 }; 2756 let want: Want = { 2757 bundleName: "com.ohos.server", 2758 abilityName: "com.ohos.server.EntryAbility", 2759 }; 2760 2761 // FA模型使用此方法连接服务 2762 // FA.connectAbility(want,connect); 2763 2764 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2765 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 2766 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2767 let connectionId = context.connectServiceExtensionAbility(want, connect); 2768 ``` 2769 2770 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 2771 2772 ```ts 2773 import { BusinessError } from '@kit.BasicServicesKit'; 2774 import { hilog } from '@kit.PerformanceAnalysisKit'; 2775 2776 let option = new rpc.MessageOption(); 2777 let data = rpc.MessageSequence.create(); 2778 let reply = rpc.MessageSequence.create(); 2779 data.writeNoException(); 2780 data.writeInt(6); 2781 if (proxy != undefined) { 2782 proxy.sendMessageRequest(1, data, reply, option) 2783 .then((result: rpc.RequestResult) => { 2784 if (result.errCode === 0) { 2785 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2786 try { 2787 result.reply.readException(); 2788 } catch (error) { 2789 let e: BusinessError = error as BusinessError; 2790 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2791 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2792 } 2793 let num = result.reply.readInt(); 2794 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2795 } else { 2796 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2797 } 2798 }).catch((e: Error) => { 2799 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2800 }).finally (() => { 2801 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2802 data.reclaim(); 2803 reply.reclaim(); 2804 }); 2805 } 2806 ``` 2807 2808### writeParcelableArray 2809 2810writeParcelableArray(parcelableArray: Parcelable[]): void 2811 2812将可序列化对象数组写入MessageSequence实例。 2813 2814**系统能力**:SystemCapability.Communication.IPC.Core 2815 2816**参数:** 2817 2818| 参数名 | 类型 | 必填 | 说明 | 2819| --------------- | ------------ | ---- | -------------------------- | 2820| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要写入的可序列化对象数组。 | 2821 2822**错误码:** 2823 2824以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2825 2826 | 错误码ID | 错误信息 | 2827 | -------- | -------- | 2828 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. | 2829 | 1900009 | Failed to write data to the message sequence. | 2830 2831**示例:** 2832 2833 ```ts 2834 import { hilog } from '@kit.PerformanceAnalysisKit'; 2835 import { BusinessError } from '@kit.BasicServicesKit'; 2836 2837 class MyParcelable implements rpc.Parcelable { 2838 num: number = 0; 2839 str: string = ''; 2840 constructor(num: number, str: string) { 2841 this.num = num; 2842 this.str = str; 2843 } 2844 marshalling(messageSequence: rpc.MessageSequence): boolean { 2845 messageSequence.writeInt(this.num); 2846 messageSequence.writeString(this.str); 2847 return true; 2848 } 2849 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2850 this.num = messageSequence.readInt(); 2851 this.str = messageSequence.readString(); 2852 return true; 2853 } 2854 } 2855 let parcelable = new MyParcelable(1, "aaa"); 2856 let parcelable2 = new MyParcelable(2, "bbb"); 2857 let parcelable3 = new MyParcelable(3, "ccc"); 2858 let a = [parcelable, parcelable2, parcelable3]; 2859 let data = rpc.MessageSequence.create(); 2860 try { 2861 data.writeParcelableArray(a); 2862 } catch (error) { 2863 let e: BusinessError = error as BusinessError; 2864 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2865 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2866 } 2867 ``` 2868 2869### readParcelableArray 2870 2871readParcelableArray(parcelableArray: Parcelable[]): void 2872 2873从MessageSequence实例读取可序列化对象数组。 2874 2875**系统能力**:SystemCapability.Communication.IPC.Core 2876 2877**参数:** 2878 2879| 参数名 | 类型 | 必填 | 说明 | 2880| --------------- | ------------ | ---- | -------------------------- | 2881| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要读取的可序列化对象数组。 | 2882 2883**错误码:** 2884 2885以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2886 2887 | 错误码ID | 错误信息 | 2888 | -------- | -------- | 2889 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The length of the array passed when reading is not equal to the length passed when writing to the array; <br/> 5.The element does not exist in the array. | 2890 | 1900010 | Failed to read data from the message sequence. | 2891 | 1900012 | Failed to call the JS callback function. | 2892 2893**示例:** 2894 2895 ```ts 2896 import { hilog } from '@kit.PerformanceAnalysisKit'; 2897 import { BusinessError } from '@kit.BasicServicesKit'; 2898 2899 class MyParcelable implements rpc.Parcelable { 2900 num: number = 0; 2901 str: string = ''; 2902 constructor(num: number, str: string) { 2903 this.num = num; 2904 this.str = str; 2905 } 2906 marshalling(messageSequence: rpc.MessageSequence): boolean { 2907 messageSequence.writeInt(this.num); 2908 messageSequence.writeString(this.str); 2909 return true; 2910 } 2911 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2912 this.num = messageSequence.readInt(); 2913 this.str = messageSequence.readString(); 2914 return true; 2915 } 2916 } 2917 let parcelable = new MyParcelable(1, "aaa"); 2918 let parcelable2 = new MyParcelable(2, "bbb"); 2919 let parcelable3 = new MyParcelable(3, "ccc"); 2920 let a = [parcelable, parcelable2, parcelable3]; 2921 let data = rpc.MessageSequence.create(); 2922 data.writeParcelableArray(a); 2923 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2924 try { 2925 data.readParcelableArray(b); 2926 } catch (error) { 2927 let e: BusinessError = error as BusinessError; 2928 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2929 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2930 } 2931 ``` 2932 2933### writeRemoteObjectArray 2934 2935writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2936 2937将IRemoteObject对象数组写入MessageSequence。 2938 2939**系统能力**:SystemCapability.Communication.IPC.Core 2940 2941**参数:** 2942 2943| 参数名 | 类型 | 必填 | 说明 | 2944| ----------- | --------------- | ---- | ---------------------------------------------- | 2945| objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageSequence的IRemoteObject对象数组。 | 2946 2947**错误码:** 2948 2949以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2950 2951 | 错误码ID | 错误信息 | 2952 | -------- | -------- | 2953 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The obtained remoteObject is null. | 2954 | 1900009 | Failed to write data to the message sequence. | 2955 2956**示例:** 2957 2958 ```ts 2959 import { hilog } from '@kit.PerformanceAnalysisKit'; 2960 import { BusinessError } from '@kit.BasicServicesKit'; 2961 2962 class TestRemoteObject extends rpc.RemoteObject { 2963 constructor(descriptor: string) { 2964 super(descriptor); 2965 this.modifyLocalInterface(this, descriptor); 2966 } 2967 2968 asObject(): rpc.IRemoteObject { 2969 return this; 2970 } 2971 } 2972 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2973 let data = rpc.MessageSequence.create(); 2974 try { 2975 data.writeRemoteObjectArray(a); 2976 } catch (error) { 2977 let e: BusinessError = error as BusinessError; 2978 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2979 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2980 } 2981 ``` 2982 2983### readRemoteObjectArray 2984 2985readRemoteObjectArray(objects: IRemoteObject[]): void 2986 2987从MessageSequence读取IRemoteObject对象数组。 2988 2989**系统能力**:SystemCapability.Communication.IPC.Core 2990 2991**参数:** 2992 2993| 参数名 | 类型 | 必填 | 说明 | 2994| ------- | --------------- | ---- | ---------------------------------------------- | 2995| objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageSequence读取的IRemoteObject对象数组。 | 2996 2997**错误码:** 2998 2999以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3000 3001 | 错误码ID | 错误信息 | 3002 | -------- | -------- | 3003 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The length of the array passed when reading is not equal to the length passed when writing to the array. | 3004 | 1900010 | Failed to read data from the message sequence. | 3005 3006**示例:** 3007 3008 ```ts 3009 import { hilog } from '@kit.PerformanceAnalysisKit'; 3010 import { BusinessError } from '@kit.BasicServicesKit'; 3011 3012 class TestRemoteObject extends rpc.RemoteObject { 3013 constructor(descriptor: string) { 3014 super(descriptor); 3015 this.modifyLocalInterface(this, descriptor); 3016 } 3017 3018 asObject(): rpc.IRemoteObject { 3019 return this; 3020 } 3021 } 3022 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3023 let data = rpc.MessageSequence.create(); 3024 data.writeRemoteObjectArray(a); 3025 let b: Array<rpc.IRemoteObject> = new Array(3); 3026 try { 3027 data.readRemoteObjectArray(b); 3028 } catch (error) { 3029 let e: BusinessError = error as BusinessError; 3030 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3031 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3032 } 3033 ``` 3034 3035### readRemoteObjectArray 3036 3037readRemoteObjectArray(): IRemoteObject[] 3038 3039从MessageSequence读取IRemoteObject对象数组。 3040 3041**系统能力**:SystemCapability.Communication.IPC.Core 3042 3043**返回值:** 3044 3045| 类型 | 说明 | 3046| --------------- | --------------------------- | 3047| [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 3048 3049**错误码:** 3050 3051以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3052 3053 | 错误码ID | 错误信息 | 3054 | -------- | -------- | 3055 | 1900010 | Failed to read data from the message sequence. | 3056 3057**示例:** 3058 3059 ```ts 3060 import { hilog } from '@kit.PerformanceAnalysisKit'; 3061 import { BusinessError } from '@kit.BasicServicesKit'; 3062 3063 class TestRemoteObject extends rpc.RemoteObject { 3064 constructor(descriptor: string) { 3065 super(descriptor); 3066 this.modifyLocalInterface(this, descriptor); 3067 } 3068 3069 asObject(): rpc.IRemoteObject { 3070 return this; 3071 } 3072 } 3073 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3074 let data = rpc.MessageSequence.create(); 3075 data.writeRemoteObjectArray(a); 3076 try { 3077 let b = data.readRemoteObjectArray(); 3078 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 3079 } catch (error) { 3080 let e: BusinessError = error as BusinessError; 3081 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3082 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3083 } 3084 ``` 3085 3086### closeFileDescriptor 3087 3088static closeFileDescriptor(fd: number): void 3089 3090静态方法,关闭给定的文件描述符。 3091 3092**系统能力**:SystemCapability.Communication.IPC.Core 3093 3094**参数:** 3095 3096 | 参数名 | 类型 | 必填 | 说明 | 3097 | ------ | ------ | ---- | -------------------- | 3098 | fd | number | 是 | 要关闭的文件描述符。 | 3099 3100**错误码:** 3101 3102以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3103 3104 | 错误码ID | 错误信息 | 3105 | -------- | -------- | 3106 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3107 3108**示例:** 3109 3110 ```ts 3111 import { fileIo } from '@kit.CoreFileKit'; 3112 import { hilog } from '@kit.PerformanceAnalysisKit'; 3113 import { BusinessError } from '@kit.BasicServicesKit'; 3114 3115 let filePath = "path/to/file"; 3116 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3117 try { 3118 rpc.MessageSequence.closeFileDescriptor(file.fd); 3119 } catch (error) { 3120 let e: BusinessError = error as BusinessError; 3121 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3122 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3123 } 3124 ``` 3125 3126### dupFileDescriptor 3127 3128static dupFileDescriptor(fd: number): number 3129 3130静态方法,复制给定的文件描述符。 3131 3132**系统能力**:SystemCapability.Communication.IPC.Core 3133 3134**参数:** 3135 3136 | 参数名 | 类型 | 必填 | 说明 | 3137 | ------ | ------ | ---- | ------------------------ | 3138 | fd | number | 是 | 表示已存在的文件描述符。 | 3139 3140**返回值:** 3141 3142 | 类型 | 说明 | 3143 | ------ | -------------------- | 3144 | number | 返回新的文件描述符。 | 3145 3146**错误码:** 3147 3148以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3149 3150 | 错误码ID | 错误信息 | 3151 | -------- | -------- | 3152 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3153 | 1900013 | Failed to call dup. | 3154 3155**示例:** 3156 3157 ```ts 3158 import { fileIo } from '@kit.CoreFileKit'; 3159 import { hilog } from '@kit.PerformanceAnalysisKit'; 3160 import { BusinessError } from '@kit.BasicServicesKit'; 3161 3162 let filePath = "path/to/file"; 3163 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3164 try { 3165 rpc.MessageSequence.dupFileDescriptor(file.fd); 3166 } catch (error) { 3167 let e: BusinessError = error as BusinessError; 3168 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3169 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3170 } 3171 ``` 3172 3173### containFileDescriptors 3174 3175containFileDescriptors(): boolean 3176 3177检查此MessageSequence对象是否包含文件描述符。 3178 3179**系统能力**:SystemCapability.Communication.IPC.Core 3180 3181**返回值:** 3182 3183 | 类型 | 说明 | 3184 | ------- | -------------------------------------------------------------------- | 3185 | boolean | true:包含文件描述符,false:不包含文件描述符。| 3186 3187**示例:** 3188 3189 ```ts 3190 import { fileIo } from '@kit.CoreFileKit'; 3191 import { hilog } from '@kit.PerformanceAnalysisKit'; 3192 import { BusinessError } from '@kit.BasicServicesKit'; 3193 3194 let sequence = new rpc.MessageSequence(); 3195 let filePath = "path/to/file"; 3196 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3197 try { 3198 sequence.writeFileDescriptor(file.fd); 3199 } catch (error) { 3200 let e: BusinessError = error as BusinessError; 3201 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3202 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3203 } 3204 try { 3205 let containFD = sequence.containFileDescriptors(); 3206 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3207 } catch (error) { 3208 let e: BusinessError = error as BusinessError; 3209 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3210 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3211 } 3212 ``` 3213 3214### writeFileDescriptor 3215 3216writeFileDescriptor(fd: number): void 3217 3218写入文件描述符到MessageSequence。 3219 3220**系统能力**:SystemCapability.Communication.IPC.Core 3221 3222**参数:** 3223 3224 | 参数名 | 类型 | 必填 | 说明 | 3225 | ------ | ------ | ---- | ------------ | 3226 | fd | number | 是 | 文件描述符。 | 3227 3228**错误码:** 3229 3230以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3231 3232 | 错误码ID | 错误信息 | 3233 | -------- | -------- | 3234 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3235 | 1900009 | Failed to write data to the message sequence. | 3236 3237**示例:** 3238 3239 ```ts 3240 import { fileIo } from '@kit.CoreFileKit'; 3241 import { hilog } from '@kit.PerformanceAnalysisKit'; 3242 import { BusinessError } from '@kit.BasicServicesKit'; 3243 3244 let sequence = new rpc.MessageSequence(); 3245 let filePath = "path/to/file"; 3246 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3247 try { 3248 sequence.writeFileDescriptor(file.fd); 3249 } catch (error) { 3250 let e: BusinessError = error as BusinessError; 3251 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3252 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3253 } 3254 ``` 3255 3256### readFileDescriptor 3257 3258readFileDescriptor(): number 3259 3260从MessageSequence中读取文件描述符。 3261 3262**系统能力**:SystemCapability.Communication.IPC.Core 3263 3264**返回值:** 3265 3266 | 类型 | 说明 | 3267 | ------ | ---------------- | 3268 | number | 返回文件描述符。 | 3269 3270**错误码:** 3271 3272以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3273 3274 | 错误码ID | 错误信息 | 3275 | -------- | -------- | 3276 | 1900010 | Failed to read data from the message sequence. | 3277 3278**示例:** 3279 3280 ```ts 3281 import { fileIo } from '@kit.CoreFileKit'; 3282 import { hilog } from '@kit.PerformanceAnalysisKit'; 3283 import { BusinessError } from '@kit.BasicServicesKit'; 3284 3285 let sequence = new rpc.MessageSequence(); 3286 let filePath = "path/to/file"; 3287 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3288 try { 3289 sequence.writeFileDescriptor(file.fd); 3290 } catch (error) { 3291 let e: BusinessError = error as BusinessError; 3292 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3293 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3294 } 3295 try { 3296 let readFD = sequence.readFileDescriptor(); 3297 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3298 } catch (error) { 3299 let e: BusinessError = error as BusinessError; 3300 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3301 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3302 } 3303 ``` 3304 3305### writeAshmem 3306 3307writeAshmem(ashmem: Ashmem): void 3308 3309将指定的匿名共享对象写入此MessageSequence。 3310 3311**系统能力**:SystemCapability.Communication.IPC.Core 3312 3313**参数:** 3314 3315| 参数名 | 类型 | 必填 | 说明 | 3316| ------ | ------ | ---- | ------------------------------------- | 3317| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageSequence的匿名共享对象。 | 3318 3319**错误码:** 3320 3321以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3322 3323 | 错误码ID | 错误信息 | 3324 | -------- | ------- | 3325 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter is not an instance of the Ashmem object. | 3326 | 1900003 | Failed to write data to the shared memory. | 3327 3328**示例:** 3329 3330 ```ts 3331 import { hilog } from '@kit.PerformanceAnalysisKit'; 3332 import { BusinessError } from '@kit.BasicServicesKit'; 3333 3334 let sequence = new rpc.MessageSequence(); 3335 let ashmem: rpc.Ashmem | undefined = undefined; 3336 try { 3337 ashmem = rpc.Ashmem.create("ashmem", 1024); 3338 try { 3339 sequence.writeAshmem(ashmem); 3340 } catch (error) { 3341 let e: BusinessError = error as BusinessError; 3342 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3343 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3344 } 3345 } catch (error) { 3346 let e: BusinessError = error as BusinessError; 3347 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3348 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3349 } 3350 ``` 3351 3352### readAshmem 3353 3354readAshmem(): Ashmem 3355 3356从MessageSequence读取匿名共享对象。 3357 3358**系统能力**:SystemCapability.Communication.IPC.Core 3359 3360**返回值:** 3361 3362| 类型 | 说明 | 3363| ------ | ------------------ | 3364| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 3365 3366**错误码:** 3367 3368以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3369 3370 | 错误码ID | 错误信息 | 3371 | -------- | -------- | 3372 | 401 | check param failed | 3373 | 1900004 | Failed to read data from the shared memory. | 3374 3375**示例:** 3376 3377 ```ts 3378 import { hilog } from '@kit.PerformanceAnalysisKit'; 3379 import { BusinessError } from '@kit.BasicServicesKit'; 3380 3381 let sequence = new rpc.MessageSequence(); 3382 let ashmem: rpc.Ashmem | undefined = undefined; 3383 try { 3384 ashmem = rpc.Ashmem.create("ashmem", 1024); 3385 try { 3386 sequence.writeAshmem(ashmem); 3387 } catch (error) { 3388 let e: BusinessError = error as BusinessError; 3389 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3390 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3391 } 3392 } catch (error) { 3393 let e: BusinessError = error as BusinessError; 3394 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3395 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3396 } 3397 try { 3398 sequence.readAshmem(); 3399 } catch (error) { 3400 let e: BusinessError = error as BusinessError; 3401 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3402 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3403 } 3404 ``` 3405 3406### getRawDataCapacity 3407 3408getRawDataCapacity(): number 3409 3410获取MessageSequence可以容纳的最大原始数据量。 3411 3412**系统能力**:SystemCapability.Communication.IPC.Core 3413 3414**返回值:** 3415 3416 | 类型 | 说明 | 3417 | ------ | ------------------------------------------------------------ | 3418 | number | 返回MessageSequence可以容纳的最大原始数据量,即128MB。 | 3419 3420**示例:** 3421 3422 ```ts 3423 import { hilog } from '@kit.PerformanceAnalysisKit'; 3424 3425 let sequence = new rpc.MessageSequence(); 3426 let result = sequence.getRawDataCapacity(); 3427 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3428 ``` 3429 3430### writeRawData<sup>(deprecated)</sup> 3431 3432writeRawData(rawData: number[], size: number): void 3433 3434将原始数据写入MessageSequence对象。 3435 3436> **说明:** 3437> 3438> 从API version 11 开始废弃,建议使用[writeRawDataBuffer](#writerawdatabuffer11)替代。 3439> 3440> 该接口是一次性接口,不允许在一次parcel通信中多次调用该接口。 3441> 该接口在传输数据时,当数据量较大时(超过32KB),会使用共享内存传输数据,此时需注意selinux配置。 3442 3443**系统能力**:SystemCapability.Communication.IPC.Core 3444 3445**参数:** 3446 3447 | 参数名 | 类型 | 必填 | 说明 | 3448 | ------- | -------- | ---- | ---------------------------------- | 3449 | rawData | number[] | 是 | 要写入的原始数据,大小不能超过128MB。 | 3450 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3451 3452**错误码:** 3453 3454以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3455 3456 | 错误码ID | 错误信息 | 3457 | -------- | -------- | 3458 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The transferred size cannot be obtained; <br/> 5.The transferred size is less than or equal to 0;<br/> 6.The element does not exist in the array; <br/> 7.Failed to obtain typedArray information; <br/> 8.The array is not of type int32; <br/> 9.The length of typedarray is smaller than the size of the original data sent. | 3459 | 1900009 | Failed to write data to the message sequence. | 3460 3461**示例:** 3462 3463 ```ts 3464 import { hilog } from '@kit.PerformanceAnalysisKit'; 3465 import { BusinessError } from '@kit.BasicServicesKit'; 3466 3467 let sequence = new rpc.MessageSequence(); 3468 let arr = [1, 2, 3, 4, 5]; 3469 try { 3470 sequence.writeRawData(arr, arr.length); 3471 } catch (error) { 3472 let e: BusinessError = error as BusinessError; 3473 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3474 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3475 } 3476 ``` 3477 3478### writeRawDataBuffer<sup>11+</sup> 3479 3480writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3481 3482将原始数据写入MessageSequence对象。 3483 3484> **说明:** 3485> 3486> 该接口是一次性接口,不允许在一次parcel通信中多次调用该接口。 3487> 该接口在传输数据时,当数据量较大时(超过32KB),会使用共享内存传输数据,此时需注意selinux配置。 3488 3489**系统能力**:SystemCapability.Communication.IPC.Core 3490 3491**参数:** 3492 3493 | 参数名 | 类型 | 必填 | 说明 | 3494 | ------- | ----------- | ---- | ------------------------------------ | 3495 | rawData | ArrayBuffer | 是 | 要写入的原始数据,大小不能超过128MB。 | 3496 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3497 3498**错误码:** 3499 3500以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3501 3502 | 错误码ID | 错误信息 | 3503 | -------- | -------- | 3504 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain arrayBuffer information; <br/> 4.The transferred size cannot be obtained; <br/> 5.The transferred size is less than or equal to 0; <br/> 6.The transferred size is greater than the byte length of ArrayBuffer. | 3505 | 1900009 | Failed to write data to the message sequence. | 3506 3507**示例:** 3508 3509 ```ts 3510 import { hilog } from '@kit.PerformanceAnalysisKit'; 3511 import { BusinessError } from '@kit.BasicServicesKit'; 3512 3513 let buffer = new ArrayBuffer(64 * 1024); 3514 let int32View = new Int32Array(buffer); 3515 for (let i = 0; i < int32View.length; i++) { 3516 int32View[i] = i * 2 + 1; 3517 } 3518 let size = buffer.byteLength; 3519 let sequence = new rpc.MessageSequence(); 3520 try { 3521 sequence.writeRawDataBuffer(buffer, size); 3522 } catch (error) { 3523 let e: BusinessError = error as BusinessError; 3524 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3525 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3526 } 3527 ``` 3528 3529### readRawData<sup>(deprecated)</sup> 3530 3531readRawData(size: number): number[] 3532 3533从MessageSequence读取原始数据。 3534 3535> **说明:** 3536> 3537> 从API version 11 开始废弃,建议使用[readRawDataBuffer](#readrawdatabuffer11)替代。 3538 3539 3540**系统能力**:SystemCapability.Communication.IPC.Core 3541 3542**参数:** 3543 3544 | 参数名 | 类型 | 必填 | 说明 | 3545 | ------ | ------ | ---- | ------------------------ | 3546 | size | number | 是 | 要读取的原始数据的大小。 | 3547 3548**返回值:** 3549 3550 | 类型 | 说明 | 3551 | -------- | ------------------------------ | 3552 | number[] | 返回原始数据(以字节为单位)。 | 3553 3554**错误码:** 3555 3556以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3557 3558 | 错误码ID | 错误信息 | 3559 | -------- | -------- | 3560 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3561 | 1900010 | Failed to read data from the message sequence. | 3562 3563**示例:** 3564 3565 ```ts 3566 import { hilog } from '@kit.PerformanceAnalysisKit'; 3567 import { BusinessError } from '@kit.BasicServicesKit'; 3568 3569 let sequence = new rpc.MessageSequence(); 3570 let arr = [1, 2, 3, 4, 5]; 3571 try { 3572 sequence.writeRawData(arr, arr.length); 3573 } catch (error) { 3574 let e: BusinessError = error as BusinessError; 3575 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3576 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3577 } 3578 try { 3579 let result = sequence.readRawData(5); 3580 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3581 } catch (error) { 3582 let e: BusinessError = error as BusinessError; 3583 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3584 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3585 } 3586 ``` 3587 3588### readRawDataBuffer<sup>11+</sup> 3589 3590readRawDataBuffer(size: number): ArrayBuffer 3591 3592从MessageSequence读取原始数据。 3593 3594**系统能力**:SystemCapability.Communication.IPC.Core 3595 3596**参数:** 3597 3598 | 参数名 | 类型 | 必填 | 说明 | 3599 | ------ | ------ | ---- | ------------------------ | 3600 | size | number | 是 | 要读取的原始数据的大小。 | 3601 3602**返回值:** 3603 3604 | 类型 | 说明 | 3605 | -------- | ------------------------------ | 3606 | ArrayBuffer | 返回原始数据(以字节为单位)。 | 3607 3608**错误码:** 3609 3610以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3611 3612 | 错误码ID | 错误信息 | 3613 | -------- | -------- | 3614 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3615 | 1900010 | Failed to read data from the message sequence. | 3616 3617**示例:** 3618 3619 ```ts 3620 import { hilog } from '@kit.PerformanceAnalysisKit'; 3621 import { BusinessError } from '@kit.BasicServicesKit'; 3622 3623 let buffer = new ArrayBuffer(64 * 1024); 3624 let int32View = new Int32Array(buffer); 3625 for (let i = 0; i < int32View.length; i++) { 3626 int32View[i] = i * 2 + 1; 3627 } 3628 let size = buffer.byteLength; 3629 let sequence = new rpc.MessageSequence(); 3630 try { 3631 sequence.writeRawDataBuffer(buffer, size); 3632 } catch (error) { 3633 let e: BusinessError = error as BusinessError; 3634 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3635 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3636 } 3637 try { 3638 let result = sequence.readRawDataBuffer(size); 3639 let readInt32View = new Int32Array(result); 3640 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + readInt32View); 3641 } catch (error) { 3642 let e: BusinessError = error as BusinessError; 3643 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3644 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3645 } 3646 ``` 3647 3648### writeArrayBuffer<sup>12+</sup> 3649 3650writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3651 3652将ArrayBuffer类型数据写入MessageSequence对象。 3653 3654**系统能力**:SystemCapability.Communication.IPC.Core 3655 3656**参数:** 3657 3658 | 参数名 | 类型 | 必填 | 说明 | 3659 | --------- | ------------------------- | ---- | --------------------------- | 3660 | buf | ArrayBuffer | 是 | 要写入的ArrayBuffer数据。 | 3661 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的写入方式,需要业务正确传递枚举值。) | 3662 3663**错误码:** 3664 3665以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3666 3667 | 错误码ID | 错误信息 | 3668 | -------- | -------- | 3669 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The obtained value of typeCode is incorrect; <br/> 5.Failed to obtain arrayBuffer information. | 3670 | 1900009 | Failed to write data to the message sequence. | 3671 3672**示例:** 3673 3674 ```ts 3675 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3676 import { hilog } from '@kit.PerformanceAnalysisKit'; 3677 import { BusinessError } from '@kit.BasicServicesKit'; 3678 3679 const data = rpc.MessageSequence.create(); 3680 3681 let buffer = new ArrayBuffer(10); 3682 let int16View = new Int16Array(buffer); 3683 for (let i = 0; i < int16View.length; i++) { 3684 int16View[i] = i * 2 + 1; 3685 } 3686 3687 try { 3688 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3689 } catch (error) { 3690 let e: BusinessError = error as BusinessError; 3691 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3692 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3693 } 3694 ``` 3695 3696### readArrayBuffer<sup>12+</sup> 3697 3698readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3699 3700从MessageSequence读取ArrayBuffer类型数据。 3701 3702**系统能力**:SystemCapability.Communication.IPC.Core 3703 3704**参数:** 3705 3706 | 参数名 | 类型 | 必填 | 说明 | 3707 | -------- | ----------------------- | ---- | ------------------------| 3708 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的读取方式,需要业务正确传递枚举值,读写枚举值不匹配会导致数据异常。) | 3709 3710**返回值:** 3711 3712 | 类型 | 说明 | 3713 | -------- | -------------------------------------------- | 3714 | ArrayBuffer | 返回ArrayBuffer类型数据(以字节为单位)。 | 3715 3716**错误码:** 3717 3718以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3719 3720 | 错误码ID | 错误信息 | 3721 | -------- | -------- | 3722 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The obtained value of typeCode is incorrect; | 3723 | 1900010 | Failed to read data from the message sequence. | 3724 3725**示例:** 3726 3727 ```ts 3728 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3729 import { hilog } from '@kit.PerformanceAnalysisKit'; 3730 import { BusinessError } from '@kit.BasicServicesKit'; 3731 3732 const data = rpc.MessageSequence.create(); 3733 3734 let buffer = new ArrayBuffer(10); 3735 let int16View = new Int16Array(buffer); 3736 for (let i = 0; i < int16View.length; i++) { 3737 int16View[i] = i * 2 + 1; 3738 } 3739 3740 try { 3741 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3742 } catch (error) { 3743 let e: BusinessError = error as BusinessError; 3744 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3745 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3746 } 3747 try { 3748 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3749 let readInt16View = new Int16Array(result); 3750 hilog.info(0x0000, 'testTag', 'RpcTest: read ArrayBuffer result is ' + readInt16View); 3751 } catch (error) { 3752 let e: BusinessError = error as BusinessError; 3753 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorCode ' + e.code); 3754 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorMessage ' + e.message); 3755 } 3756 ``` 3757 3758## MessageParcel<sup>(deprecated)</sup> 3759 3760在RPC过程中,发送方可以使用MessageParcel提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageParcel提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 3761 3762> **说明:** 3763> 3764> 从API version 9 开始废弃,建议使用[MessageSequence](#messagesequence9)替代。 3765 3766### create 3767 3768static create(): MessageParcel 3769 3770静态方法,创建MessageParcel对象。 3771 3772**系统能力**:SystemCapability.Communication.IPC.Core 3773 3774**返回值:** 3775 3776 | 类型 | 说明 | 3777 | ------------- | ----------------------------- | 3778 | [MessageParcel](#messageparceldeprecated) | 返回创建的MessageParcel对象。 | 3779 3780**示例:** 3781 3782 ```ts 3783 import { hilog } from '@kit.PerformanceAnalysisKit'; 3784 3785 let data = rpc.MessageParcel.create(); 3786 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3787 3788 // 当MessageParcel对象不再使用,由业务主动调用reclaim方法去释放资源。 3789 data.reclaim(); 3790 ``` 3791 3792### reclaim 3793 3794reclaim(): void 3795 3796释放不再使用的MessageParcel对象。 3797 3798**系统能力**:SystemCapability.Communication.IPC.Core 3799 3800**示例:** 3801 3802 ```ts 3803 let reply = rpc.MessageParcel.create(); 3804 reply.reclaim(); 3805 ``` 3806 3807### writeRemoteObject 3808 3809writeRemoteObject(object: IRemoteObject): boolean 3810 3811序列化远程对象并将其写入MessageParcel对象。 3812 3813**系统能力**:SystemCapability.Communication.IPC.Core 3814 3815**参数:** 3816 3817 | 参数名 | 类型 | 必填 | 说明 | 3818 | ------ | ------------------------------- | ---- | --------------------------------------- | 3819 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageParcel的远程对象。 | 3820 3821**返回值:** 3822 3823 | 类型 | 说明 | 3824 | ------- | ----------------------------------------- | 3825 | boolean | true:操作成功,false:操作失败。| 3826 3827**示例:** 3828 3829 ```ts 3830 import { hilog } from '@kit.PerformanceAnalysisKit'; 3831 3832 class MyDeathRecipient implements rpc.DeathRecipient { 3833 onRemoteDied() { 3834 hilog.info(0x0000, 'testTag', 'server died'); 3835 } 3836 } 3837 class TestRemoteObject extends rpc.RemoteObject { 3838 constructor(descriptor: string) { 3839 super(descriptor); 3840 } 3841 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3842 return true; 3843 } 3844 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3845 return true; 3846 } 3847 isObjectDead(): boolean { 3848 return false; 3849 } 3850 } 3851 let data = rpc.MessageParcel.create(); 3852 let testRemoteObject = new TestRemoteObject("testObject"); 3853 data.writeRemoteObject(testRemoteObject); 3854 ``` 3855 3856### readRemoteObject 3857 3858readRemoteObject(): IRemoteObject 3859 3860从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。 3861 3862**系统能力**:SystemCapability.Communication.IPC.Core 3863 3864**返回值:** 3865 3866 | 类型 | 说明 | 3867 | ------------------------------- | ------------------ | 3868 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 3869 3870**示例:** 3871 3872 ```ts 3873 import { hilog } from '@kit.PerformanceAnalysisKit'; 3874 3875 class MyDeathRecipient implements rpc.DeathRecipient { 3876 onRemoteDied() { 3877 hilog.info(0x0000, 'testTag', 'server died'); 3878 } 3879 } 3880 class TestRemoteObject extends rpc.RemoteObject { 3881 constructor(descriptor: string) { 3882 super(descriptor); 3883 } 3884 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3885 return true; 3886 } 3887 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3888 return true; 3889 } 3890 isObjectDead(): boolean { 3891 return false; 3892 } 3893 } 3894 let data = rpc.MessageParcel.create(); 3895 let testRemoteObject = new TestRemoteObject("testObject"); 3896 data.writeRemoteObject(testRemoteObject); 3897 let proxy = data.readRemoteObject(); 3898 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3899 ``` 3900 3901### writeInterfaceToken 3902 3903writeInterfaceToken(token: string): boolean 3904 3905将接口描述符写入MessageParcel对象,远端对象可使用该信息校验本次通信。 3906 3907**系统能力**:SystemCapability.Communication.IPC.Core 3908 3909**参数:** 3910 3911 | 参数名 | 类型 | 必填 | 说明 | 3912 | ------ | ------ | ---- | ------------------ | 3913 | token | string | 是 | 字符串类型描述符。 | 3914 3915**返回值:** 3916 3917 | 类型 | 说明 | 3918 | ------- | ----------------------------------------- | 3919 | boolean | true:操作成功,false:操作失败。| 3920 3921**示例:** 3922 3923 ```ts 3924 import { hilog } from '@kit.PerformanceAnalysisKit'; 3925 3926 let data = rpc.MessageParcel.create(); 3927 let result = data.writeInterfaceToken("aaa"); 3928 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3929 ``` 3930 3931### readInterfaceToken 3932 3933readInterfaceToken(): string 3934 3935从MessageParcel中读取接口描述符,接口描述符按写入MessageParcel的顺序读取,本地对象可使用该信息检验本次通信。 3936 3937**系统能力**:SystemCapability.Communication.IPC.Core 3938 3939**返回值:** 3940 3941 | 类型 | 说明 | 3942 | ------ | ------------------------ | 3943 | string | 返回读取到的接口描述符。 | 3944 3945**示例:** 3946 3947 ```ts 3948 import { hilog } from '@kit.PerformanceAnalysisKit'; 3949 3950 class Stub extends rpc.RemoteObject { 3951 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3952 let interfaceToken = data.readInterfaceToken(); 3953 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3954 return true; 3955 } 3956 } 3957 ``` 3958 3959### getSize 3960 3961getSize(): number 3962 3963获取当前MessageParcel的数据大小。 3964 3965**系统能力**:SystemCapability.Communication.IPC.Core 3966 3967**返回值:** 3968 3969 | 类型 | 说明 | 3970 | ------ | --------------------------------------------- | 3971 | number | 获取的MessageParcel的数据大小。以字节为单位。 | 3972 3973**示例:** 3974 3975 ```ts 3976 import { hilog } from '@kit.PerformanceAnalysisKit'; 3977 3978 let data = rpc.MessageParcel.create(); 3979 let size = data.getSize(); 3980 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3981 ``` 3982 3983### getCapacity 3984 3985getCapacity(): number 3986 3987获取当前MessageParcel的容量。 3988 3989**系统能力**:SystemCapability.Communication.IPC.Core 3990 3991**返回值:** 3992 3993 | 类型 | 说明 | 3994 | ------ | --------------------------------------------- | 3995 | number | 获取的MessageParcel的容量大小。以字节为单位。 | 3996 3997**示例:** 3998 3999 ```ts 4000 import { hilog } from '@kit.PerformanceAnalysisKit'; 4001 4002 let data = rpc.MessageParcel.create(); 4003 let result = data.getCapacity(); 4004 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 4005 ``` 4006 4007### setSize 4008 4009setSize(size: number): boolean 4010 4011设置MessageParcel实例中包含的数据大小。 4012 4013**系统能力**:SystemCapability.Communication.IPC.Core 4014 4015**参数:** 4016 4017 | 参数名 | 类型 | 必填 | 说明 | 4018 | ------ | ------ | ---- | ------------------------------------------- | 4019 | size | number | 是 | MessageParcel实例的数据大小。以字节为单位。 | 4020 4021**返回值:** 4022 4023 | 类型 | 说明 | 4024 | ------- | --------------------------------- | 4025 | boolean | true:设置成功,false:设置失败。| 4026 4027**示例:** 4028 4029 ```ts 4030 import { hilog } from '@kit.PerformanceAnalysisKit'; 4031 4032 let data = rpc.MessageParcel.create(); 4033 let setSize = data.setSize(16); 4034 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 4035 ``` 4036 4037### setCapacity 4038 4039setCapacity(size: number): boolean 4040 4041设置MessageParcel实例的存储容量。 4042 4043**系统能力**:SystemCapability.Communication.IPC.Core 4044 4045**参数:** 4046 4047 | 参数名 | 类型 | 必填 | 说明 | 4048 | ------ | ------ | ---- | ------------------------------------------- | 4049 | size | number | 是 | MessageParcel实例的存储容量。以字节为单位。 | 4050 4051**返回值:** 4052 4053 | 类型 | 说明 | 4054 | ------- | --------------------------------- | 4055 | boolean | true:设置成功,false:设置失败。| 4056 4057**示例:** 4058 4059 ```ts 4060 import { hilog } from '@kit.PerformanceAnalysisKit'; 4061 4062 let data = rpc.MessageParcel.create(); 4063 let result = data.setCapacity(100); 4064 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 4065 ``` 4066 4067### getWritableBytes 4068 4069getWritableBytes(): number 4070 4071获取MessageParcel的可写字节空间。 4072 4073**系统能力**:SystemCapability.Communication.IPC.Core 4074 4075**返回值:** 4076 4077 | 类型 | 说明 | 4078 | ------ | --------------------------------------------------- | 4079 | number | 获取到的MessageParcel的可写字节空间。以字节为单位。 | 4080 4081**示例:** 4082 4083 ```ts 4084 import { hilog } from '@kit.PerformanceAnalysisKit'; 4085 4086 class Stub extends rpc.RemoteObject { 4087 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4088 let getWritableBytes = data.getWritableBytes(); 4089 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4090 return true; 4091 } 4092 } 4093 ``` 4094 4095### getReadableBytes 4096 4097getReadableBytes(): number 4098 4099获取MessageParcel的可读字节空间。 4100 4101**系统能力**:SystemCapability.Communication.IPC.Core 4102 4103**返回值:** 4104 4105 | 类型 | 说明 | 4106 | ------ | --------------------------------------------------- | 4107 | number | 获取到的MessageParcel的可读字节空间。以字节为单位。 | 4108 4109**示例:** 4110 4111 ```ts 4112 import { hilog } from '@kit.PerformanceAnalysisKit'; 4113 4114 class Stub extends rpc.RemoteObject { 4115 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4116 let result = data.getReadableBytes(); 4117 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4118 return true; 4119 } 4120 } 4121 ``` 4122 4123### getReadPosition 4124 4125getReadPosition(): number 4126 4127获取MessageParcel的读位置。 4128 4129**系统能力**:SystemCapability.Communication.IPC.Core 4130 4131**返回值:** 4132 4133 | 类型 | 说明 | 4134 | ------ | --------------------------------------- | 4135 | number | 返回MessageParcel实例中的当前读取位置。 | 4136 4137**示例:** 4138 4139 ```ts 4140 import { hilog } from '@kit.PerformanceAnalysisKit'; 4141 4142 let data = rpc.MessageParcel.create(); 4143 let readPos = data.getReadPosition(); 4144 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 4145 ``` 4146 4147### getWritePosition 4148 4149getWritePosition(): number 4150 4151获取MessageParcel的写位置。 4152 4153**系统能力**:SystemCapability.Communication.IPC.Core 4154 4155**返回值:** 4156 4157 | 类型 | 说明 | 4158 | ------ | --------------------------------------- | 4159 | number | 返回MessageParcel实例中的当前写入位置。 | 4160 4161**示例:** 4162 4163 ```ts 4164 import { hilog } from '@kit.PerformanceAnalysisKit'; 4165 4166 let data = rpc.MessageParcel.create(); 4167 data.writeInt(10); 4168 let bwPos = data.getWritePosition(); 4169 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 4170 ``` 4171 4172### rewindRead 4173 4174rewindRead(pos: number): boolean 4175 4176重新偏移读取位置到指定的位置。 4177 4178**系统能力**:SystemCapability.Communication.IPC.Core 4179 4180**参数:** 4181 4182 | 参数名 | 类型 | 必填 | 说明 | 4183 | ------ | ------ | ---- | ------------------------ | 4184 | pos | number | 是 | 开始读取数据的目标位置。 | 4185 4186**返回值:** 4187 4188 | 类型 | 说明 | 4189 | ------- | ------------------------------------------------- | 4190 | boolean | true:读取位置发生更改,false:读取位置未发生更改。| 4191 4192**示例:** 4193 4194 ```ts 4195 import { hilog } from '@kit.PerformanceAnalysisKit'; 4196 4197 let data = rpc.MessageParcel.create(); 4198 data.writeInt(12); 4199 data.writeString("parcel"); 4200 let number = data.readInt(); 4201 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 4202 data.rewindRead(0); 4203 let number2 = data.readInt(); 4204 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 4205 ``` 4206 4207### rewindWrite 4208 4209rewindWrite(pos: number): boolean 4210 4211重新偏移写位置到指定的位置。 4212 4213**系统能力**:SystemCapability.Communication.IPC.Core 4214 4215**参数:** 4216 4217 | 参数名 | 类型 | 必填 | 说明 | 4218 | ------ | ------ | ---- | ------------------------ | 4219 | pos | number | 是 | 开始写入数据的目标位置。 | 4220 4221**返回值:** 4222 4223 | 类型 | 说明 | 4224 | ------- | --------------------------------------------- | 4225 | boolean | true:写入位置发生更改,false:写入位置未发生更改。| 4226 4227**示例:** 4228 4229 ```ts 4230 import { hilog } from '@kit.PerformanceAnalysisKit'; 4231 4232 let data = rpc.MessageParcel.create(); 4233 data.writeInt(4); 4234 data.rewindWrite(0); 4235 data.writeInt(5); 4236 let number = data.readInt(); 4237 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 4238 ``` 4239 4240### writeByte 4241 4242writeByte(val: number): boolean 4243 4244将字节值写入MessageParcel实例。 4245 4246**系统能力**:SystemCapability.Communication.IPC.Core 4247 4248**参数:** 4249 4250 | 参数名 | 类型 | 必填 | 说明 | 4251 | ------ | ------ | ---- | ---------------- | 4252 | val | number | 是 | 要写入的字节值。 | 4253 4254**返回值:** 4255 4256 | 类型 | 说明 | 4257 | ------- | ----------------------------- | 4258 | boolean | true:写入成功,false:写入失败。 | 4259 4260**示例:** 4261 4262 ```ts 4263 import { hilog } from '@kit.PerformanceAnalysisKit'; 4264 4265 let data = rpc.MessageParcel.create(); 4266 let result = data.writeByte(2); 4267 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4268 ``` 4269 4270### readByte 4271 4272readByte(): number 4273 4274从MessageParcel实例读取字节值。 4275 4276**系统能力**:SystemCapability.Communication.IPC.Core 4277 4278**返回值:** 4279 4280 | 类型 | 说明 | 4281 | ------ | ------------ | 4282 | number | 返回字节值。 | 4283 4284**示例:** 4285 4286 ```ts 4287 import { hilog } from '@kit.PerformanceAnalysisKit'; 4288 4289 let data = rpc.MessageParcel.create(); 4290 let result = data.writeByte(2); 4291 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4292 let ret = data.readByte(); 4293 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 4294 ``` 4295 4296### writeShort 4297 4298writeShort(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 '@kit.PerformanceAnalysisKit'; 4320 4321 let data = rpc.MessageParcel.create(); 4322 let result = data.writeShort(8); 4323 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4324 ``` 4325 4326### readShort 4327 4328readShort(): 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 '@kit.PerformanceAnalysisKit'; 4344 4345 let data = rpc.MessageParcel.create(); 4346 let result = data.writeShort(8); 4347 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4348 let ret = data.readShort(); 4349 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4350 ``` 4351 4352### writeInt 4353 4354writeInt(val: number): boolean 4355 4356将整数值写入MessageParcel实例。 4357 4358**系统能力**:SystemCapability.Communication.IPC.Core 4359 4360**参数:** 4361 4362 | 参数名 | 类型 | 必填 | 说明 | 4363 | ------ | ------ | ---- | ---------------- | 4364 | val | number | 是 | 要写入的整数值。 | 4365 4366**返回值:** 4367 4368 | 类型 | 说明 | 4369 | ------- | ----------------------------- | 4370 | boolean | true:写入成功,false:写入失败。 | 4371 4372**示例:** 4373 4374 ```ts 4375 import { hilog } from '@kit.PerformanceAnalysisKit'; 4376 4377 let data = rpc.MessageParcel.create(); 4378 let result = data.writeInt(10); 4379 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4380 ``` 4381 4382### readInt 4383 4384readInt(): number 4385 4386从MessageParcel实例读取整数值。 4387 4388**系统能力**:SystemCapability.Communication.IPC.Core 4389 4390**返回值:** 4391 4392 | 类型 | 说明 | 4393 | ------ | ------------ | 4394 | number | 返回整数值。 | 4395 4396**示例:** 4397 4398 ```ts 4399 import { hilog } from '@kit.PerformanceAnalysisKit'; 4400 4401 let data = rpc.MessageParcel.create(); 4402 let result = data.writeInt(10); 4403 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4404 let ret = data.readInt(); 4405 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4406 ``` 4407 4408### writeLong 4409 4410writeLong(val: number): boolean 4411 4412将长整数值写入MessageParcel实例。 4413 4414**系统能力**:SystemCapability.Communication.IPC.Core 4415 4416**参数:** 4417 4418 | 参数名 | 类型 | 必填 | 说明 | 4419 | ------ | ------ | ---- | ---------------- | 4420 | val | number | 是 | 要写入的长整数值 | 4421 4422**返回值:** 4423 4424 | 类型 | 说明 | 4425 | ------- | --------------------------------- | 4426 | boolean | true:写入成功,false:写入失败。| 4427 4428**示例:** 4429 4430 ```ts 4431 import { hilog } from '@kit.PerformanceAnalysisKit'; 4432 4433 let data = rpc.MessageParcel.create(); 4434 let result = data.writeLong(10000); 4435 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4436 ``` 4437 4438### readLong 4439 4440readLong(): number 4441 4442从MessageParcel实例中读取长整数值。 4443 4444**系统能力**:SystemCapability.Communication.IPC.Core 4445 4446**返回值:** 4447 4448 | 类型 | 说明 | 4449 | ------ | -------------- | 4450 | number | 返回长整数值。 | 4451 4452**示例:** 4453 4454 ```ts 4455 import { hilog } from '@kit.PerformanceAnalysisKit'; 4456 4457 let data = rpc.MessageParcel.create(); 4458 let result = data.writeLong(10000); 4459 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4460 let ret = data.readLong(); 4461 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4462 ``` 4463 4464### writeFloat 4465 4466writeFloat(val: number): boolean 4467 4468将浮点值写入MessageParcel实例。 4469 4470**系统能力**:SystemCapability.Communication.IPC.Core 4471 4472**参数:** 4473 4474 | 参数名 | 类型 | 必填 | 说明 | 4475 | ------ | ------ | ---- | ---------------- | 4476 | val | number | 是 | 要写入的浮点值。 | 4477 4478**返回值:** 4479 4480 | 类型 | 说明 | 4481 | ------- | --------------------------------- | 4482 | boolean | true:写入成功,false:写入失败。| 4483 4484**示例:** 4485 4486 ```ts 4487 import { hilog } from '@kit.PerformanceAnalysisKit'; 4488 4489 let data = rpc.MessageParcel.create(); 4490 let result = data.writeFloat(1.2); 4491 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4492 ``` 4493 4494### readFloat 4495 4496readFloat(): number 4497 4498从MessageParcel实例中读取浮点值。 4499 4500**系统能力**:SystemCapability.Communication.IPC.Core 4501 4502**返回值:** 4503 4504 | 类型 | 说明 | 4505 | ------ | ------------ | 4506 | number | 返回浮点值。 | 4507 4508**示例:** 4509 4510 ```ts 4511 import { hilog } from '@kit.PerformanceAnalysisKit'; 4512 4513 let data = rpc.MessageParcel.create(); 4514 let result = data.writeFloat(1.2); 4515 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4516 let ret = data.readFloat(); 4517 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4518 ``` 4519 4520### writeDouble 4521 4522writeDouble(val: number): boolean 4523 4524将双精度浮点值写入MessageParcel实例。 4525 4526**系统能力**:SystemCapability.Communication.IPC.Core 4527 4528**参数:** 4529 4530 | 参数名 | 类型 | 必填 | 说明 | 4531 | ------ | ------ | ---- | ---------------------- | 4532 | val | number | 是 | 要写入的双精度浮点值。 | 4533 4534**返回值:** 4535 4536 | 类型 | 说明 | 4537 | ------- | --------------------------------- | 4538 | boolean | true:写入成功,false:写入失败。| 4539 4540**示例:** 4541 4542 ```ts 4543 import { hilog } from '@kit.PerformanceAnalysisKit'; 4544 4545 let data = rpc.MessageParcel.create(); 4546 let result = data.writeDouble(10.2); 4547 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4548 ``` 4549 4550### readDouble 4551 4552readDouble(): number 4553 4554从MessageParcel实例读取双精度浮点值。 4555 4556**系统能力**:SystemCapability.Communication.IPC.Core 4557 4558**返回值:** 4559 4560 | 类型 | 说明 | 4561 | ------ | ------------------ | 4562 | number | 返回双精度浮点值。 | 4563 4564**示例:** 4565 4566 ```ts 4567 import { hilog } from '@kit.PerformanceAnalysisKit'; 4568 4569 let data = rpc.MessageParcel.create(); 4570 let result = data.writeDouble(10.2); 4571 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4572 let ret = data.readDouble(); 4573 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4574 ``` 4575 4576### writeBoolean 4577 4578writeBoolean(val: boolean): boolean 4579 4580将布尔值写入MessageParcel实例。 4581 4582**系统能力**:SystemCapability.Communication.IPC.Core 4583 4584**参数:** 4585 4586 | 参数名 | 类型 | 必填 | 说明 | 4587 | ------ | ------- | ---- | ---------------- | 4588 | val | boolean | 是 | 要写入的布尔值。 | 4589 4590**返回值:** 4591 4592 | 类型 | 说明 | 4593 | ------- | --------------------------------- | 4594 | boolean | true:写入成功,false:写入失败。| 4595 4596**示例:** 4597 4598 ```ts 4599 import { hilog } from '@kit.PerformanceAnalysisKit'; 4600 4601 let data = rpc.MessageParcel.create(); 4602 let result = data.writeBoolean(false); 4603 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4604 ``` 4605 4606### readBoolean 4607 4608readBoolean(): boolean 4609 4610从MessageParcel实例读取布尔值。 4611 4612**系统能力**:SystemCapability.Communication.IPC.Core 4613 4614**返回值:** 4615 4616 | 类型 | 说明 | 4617 | ------- | -------------------- | 4618 | boolean | 返回读取到的布尔值。 | 4619 4620**示例:** 4621 4622 ```ts 4623 import { hilog } from '@kit.PerformanceAnalysisKit'; 4624 4625 let data = rpc.MessageParcel.create(); 4626 let result = data.writeBoolean(false); 4627 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4628 let ret = data.readBoolean(); 4629 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4630 ``` 4631 4632### writeChar 4633 4634writeChar(val: number): boolean 4635 4636将单个字符值写入MessageParcel实例。 4637 4638**系统能力**:SystemCapability.Communication.IPC.Core 4639 4640**参数:** 4641 4642 | 参数名 | 类型 | 必填 | 说明 | 4643 | ------ | ------ | ---- | -------------------- | 4644 | val | number | 是 | 要写入的单个字符值。 | 4645 4646**返回值:** 4647 4648 | 类型 | 说明 | 4649 | ------- | ----------------------------- | 4650 | boolean | true:写入成功,false:写入失败。| 4651 4652**示例:** 4653 4654 ```ts 4655 import { hilog } from '@kit.PerformanceAnalysisKit'; 4656 4657 let data = rpc.MessageParcel.create(); 4658 let result = data.writeChar(97); 4659 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4660 ``` 4661 4662### readChar 4663 4664readChar(): number 4665 4666从MessageParcel实例中读取单个字符值。 4667 4668**系统能力**:SystemCapability.Communication.IPC.Core 4669 4670**返回值:** 4671 4672 | 类型 | 说明 | 4673 | ------ | ---------------- | 4674 | number | 返回单个字符值。 | 4675 4676**示例:** 4677 4678 ```ts 4679 import { hilog } from '@kit.PerformanceAnalysisKit'; 4680 4681 let data = rpc.MessageParcel.create(); 4682 let result = data.writeChar(97); 4683 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4684 let ret = data.readChar(); 4685 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4686 ``` 4687 4688### writeString 4689 4690writeString(val: string): boolean 4691 4692将字符串值写入MessageParcel实例。 4693 4694**系统能力**:SystemCapability.Communication.IPC.Core 4695 4696**参数:** 4697 4698 | 参数名 | 类型 | 必填 | 说明 | 4699 | ------ | ------ | ---- | ----------------------------------------- | 4700 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 4701 4702**返回值:** 4703 4704 | 类型 | 说明 | 4705 | ------- | --------------------------------- | 4706 | boolean | true:写入成功,false:写入失败。| 4707 4708**示例:** 4709 4710 ```ts 4711 import { hilog } from '@kit.PerformanceAnalysisKit'; 4712 4713 let data = rpc.MessageParcel.create(); 4714 let result = data.writeString('abc'); 4715 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4716 ``` 4717 4718### readString 4719 4720readString(): string 4721 4722从MessageParcel实例读取字符串值。 4723 4724**系统能力**:SystemCapability.Communication.IPC.Core 4725 4726**返回值:** 4727 4728 | 类型 | 说明 | 4729 | ------ | -------------- | 4730 | string | 返回字符串值。 | 4731 4732**示例:** 4733 4734 ```ts 4735 import { hilog } from '@kit.PerformanceAnalysisKit'; 4736 4737 let data = rpc.MessageParcel.create(); 4738 let result = data.writeString('abc'); 4739 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4740 let ret = data.readString(); 4741 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4742 ``` 4743 4744### writeSequenceable 4745 4746writeSequenceable(val: Sequenceable): boolean 4747 4748将自定义序列化对象写入MessageParcel实例。 4749 4750**系统能力**:SystemCapability.Communication.IPC.Core 4751 4752**参数:** 4753 4754 | 参数名 | 类型 | 必填 | 说明 | 4755 | ------ | ----------------------------- | ---- | -------------------- | 4756 | val | [Sequenceable](#sequenceabledeprecated) | 是 | 要写入的可序列对象。 | 4757 4758**返回值:** 4759 4760 | 类型 | 说明 | 4761 | ------- | -------------------------------- | 4762 | boolean | true:写入成功,false:写入失败。| 4763 4764**示例:** 4765 4766 ```ts 4767 import { hilog } from '@kit.PerformanceAnalysisKit'; 4768 4769 class MySequenceable implements rpc.Sequenceable { 4770 num: number = 0; 4771 str: string = ''; 4772 constructor(num: number, str: string) { 4773 this.num = num; 4774 this.str = str; 4775 } 4776 marshalling(messageParcel: rpc.MessageParcel): boolean { 4777 messageParcel.writeInt(this.num); 4778 messageParcel.writeString(this.str); 4779 return true; 4780 } 4781 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4782 this.num = messageParcel.readInt(); 4783 this.str = messageParcel.readString(); 4784 return true; 4785 } 4786 } 4787 let sequenceable = new MySequenceable(1, "aaa"); 4788 let data = rpc.MessageParcel.create(); 4789 let result = data.writeSequenceable(sequenceable); 4790 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4791 ``` 4792 4793### readSequenceable 4794 4795readSequenceable(dataIn: Sequenceable): boolean 4796 4797从MessageParcel实例中读取成员变量到指定的对象(dataIn)。 4798 4799**系统能力**:SystemCapability.Communication.IPC.Core 4800 4801**参数:** 4802 4803 | 参数名 | 类型 | 必填 | 说明 | 4804 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4805 | dataIn | [Sequenceable](#sequenceabledeprecated) | 是 | 需要从MessageParcel读取成员变量的对象。 | 4806 4807**返回值:** 4808 4809 | 类型 | 说明 | 4810 | ------- | ---------------------------------------- | 4811 | boolean | true:反序列化成功,false:反序列化失败。| 4812 4813**示例:** 4814 4815 ```ts 4816 import { hilog } from '@kit.PerformanceAnalysisKit'; 4817 4818 class MySequenceable implements rpc.Sequenceable { 4819 num: number = 0; 4820 str: string = ''; 4821 constructor(num: number, str: string) { 4822 this.num = num; 4823 this.str = str; 4824 } 4825 marshalling(messageParcel: rpc.MessageParcel): boolean { 4826 messageParcel.writeInt(this.num); 4827 messageParcel.writeString(this.str); 4828 return true; 4829 } 4830 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4831 this.num = messageParcel.readInt(); 4832 this.str = messageParcel.readString(); 4833 return true; 4834 } 4835 } 4836 let sequenceable = new MySequenceable(1, "aaa"); 4837 let data = rpc.MessageParcel.create(); 4838 let result = data.writeSequenceable(sequenceable); 4839 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4840 let ret = new MySequenceable(0, ""); 4841 let result2 = data.readSequenceable(ret); 4842 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4843 ``` 4844 4845### writeByteArray 4846 4847writeByteArray(byteArray: number[]): boolean 4848 4849将字节数组写入MessageParcel实例。 4850 4851**系统能力**:SystemCapability.Communication.IPC.Core 4852 4853**参数:** 4854 4855 | 参数名 | 类型 | 必填 | 说明 | 4856 | --------- | -------- | ---- | ------------------ | 4857 | byteArray | number[] | 是 | 要写入的字节数组。 | 4858 4859**返回值:** 4860 4861 | 类型 | 说明 | 4862 | ------- | -------------------------------- | 4863 | boolean | true:写入成功,false:写入失败。| 4864 4865**示例:** 4866 4867 ```ts 4868 import { hilog } from '@kit.PerformanceAnalysisKit'; 4869 4870 let data = rpc.MessageParcel.create(); 4871 let ByteArrayVar = [1, 2, 3, 4, 5]; 4872 let result = data.writeByteArray(ByteArrayVar); 4873 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4874 ``` 4875 4876### readByteArray 4877 4878readByteArray(dataIn: number[]): void 4879 4880从MessageParcel实例读取字节数组。 4881 4882**系统能力**:SystemCapability.Communication.IPC.Core 4883 4884**参数:** 4885 4886 | 参数名 | 类型 | 必填 | 说明 | 4887 | ------ | -------- | ---- | ------------------ | 4888 | dataIn | number[] | 是 | 要读取的字节数组。 | 4889 4890**示例:** 4891 4892 ```ts 4893 import { hilog } from '@kit.PerformanceAnalysisKit'; 4894 4895 let data = rpc.MessageParcel.create(); 4896 let ByteArrayVar = [1, 2, 3, 4, 5]; 4897 let result = data.writeByteArray(ByteArrayVar); 4898 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4899 let array: Array<number> = new Array(5); 4900 data.readByteArray(array); 4901 ``` 4902 4903### readByteArray 4904 4905readByteArray(): number[] 4906 4907从MessageParcel实例中读取字节数组。 4908 4909**系统能力**:SystemCapability.Communication.IPC.Core 4910 4911**返回值:** 4912 4913 | 类型 | 说明 | 4914 | -------- | -------------- | 4915 | number[] | 返回字节数组。 | 4916 4917**示例:** 4918 4919 ```ts 4920 import { hilog } from '@kit.PerformanceAnalysisKit'; 4921 4922 let data = rpc.MessageParcel.create(); 4923 let ByteArrayVar = [1, 2, 3, 4, 5]; 4924 let result = data.writeByteArray(ByteArrayVar); 4925 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4926 let array = data.readByteArray(); 4927 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4928 ``` 4929 4930### writeShortArray 4931 4932writeShortArray(shortArray: number[]): boolean 4933 4934将短整数数组写入MessageParcel实例。 4935 4936**系统能力**:SystemCapability.Communication.IPC.Core 4937 4938**参数:** 4939 4940 | 参数名 | 类型 | 必填 | 说明 | 4941 | ---------- | -------- | ---- | -------------------- | 4942 | shortArray | number[] | 是 | 要写入的短整数数组。 | 4943 4944**返回值:** 4945 4946 | 类型 | 说明 | 4947 | ------- | -------------------------------- | 4948 | boolean | true:写入成功,false:写入失败。| 4949 4950**示例:** 4951 4952 ```ts 4953 import { hilog } from '@kit.PerformanceAnalysisKit'; 4954 4955 let data = rpc.MessageParcel.create(); 4956 let result = data.writeShortArray([11, 12, 13]); 4957 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4958 ``` 4959 4960### readShortArray 4961 4962readShortArray(dataIn: number[]): void 4963 4964从MessageParcel实例中读取短整数数组。 4965 4966**系统能力**:SystemCapability.Communication.IPC.Core 4967 4968**参数:** 4969 4970 | 参数名 | 类型 | 必填 | 说明 | 4971 | ------ | -------- | ---- | -------------------- | 4972 | dataIn | number[] | 是 | 要读取的短整数数组。 | 4973 4974**示例:** 4975 4976 ```ts 4977 import { hilog } from '@kit.PerformanceAnalysisKit'; 4978 4979 let data = rpc.MessageParcel.create(); 4980 let result = data.writeShortArray([11, 12, 13]); 4981 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4982 let array: Array<number> = new Array(3); 4983 data.readShortArray(array); 4984 ``` 4985 4986### readShortArray 4987 4988readShortArray(): number[] 4989 4990从MessageParcel实例中读取短整数数组。 4991 4992**系统能力**:SystemCapability.Communication.IPC.Core 4993 4994**返回值:** 4995 4996 | 类型 | 说明 | 4997 | -------- | ---------------- | 4998 | number[] | 返回短整数数组。 | 4999 5000**示例:** 5001 5002 ```ts 5003 import { hilog } from '@kit.PerformanceAnalysisKit'; 5004 5005 let data = rpc.MessageParcel.create(); 5006 let result = data.writeShortArray([11, 12, 13]); 5007 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 5008 let array = data.readShortArray(); 5009 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 5010 ``` 5011 5012### writeIntArray 5013 5014writeIntArray(intArray: number[]): boolean 5015 5016将整数数组写入MessageParcel实例。 5017 5018**系统能力**:SystemCapability.Communication.IPC.Core 5019 5020**参数:** 5021 5022 | 参数名 | 类型 | 必填 | 说明 | 5023 | -------- | -------- | ---- | ------------------ | 5024 | intArray | number[] | 是 | 要写入的整数数组。 | 5025 5026**返回值:** 5027 5028 | 类型 | 说明 | 5029 | ------- | -------------------------------- | 5030 | boolean | true:写入成功,false:写入失败。| 5031 5032**示例:** 5033 5034 ```ts 5035 import { hilog } from '@kit.PerformanceAnalysisKit'; 5036 5037 let data = rpc.MessageParcel.create(); 5038 let result = data.writeIntArray([100, 111, 112]); 5039 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5040 ``` 5041 5042### readIntArray 5043 5044readIntArray(dataIn: number[]): void 5045 5046从MessageParcel实例中读取整数数组。 5047 5048**系统能力**:SystemCapability.Communication.IPC.Core 5049 5050**参数:** 5051 5052 | 参数名 | 类型 | 必填 | 说明 | 5053 | ------ | -------- | ---- | ------------------ | 5054 | dataIn | number[] | 是 | 要读取的整数数组。 | 5055 5056**示例:** 5057 5058 ```ts 5059 import { hilog } from '@kit.PerformanceAnalysisKit'; 5060 5061 let data = rpc.MessageParcel.create(); 5062 let result = data.writeIntArray([100, 111, 112]); 5063 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5064 let array: Array<number> = new Array(3); 5065 data.readIntArray(array); 5066 ``` 5067 5068### readIntArray 5069 5070readIntArray(): number[] 5071 5072从MessageParcel实例中读取整数数组。 5073 5074**系统能力**:SystemCapability.Communication.IPC.Core 5075 5076**返回值:** 5077 5078 | 类型 | 说明 | 5079 | -------- | -------------- | 5080 | number[] | 返回整数数组。 | 5081 5082**示例:** 5083 5084 ```ts 5085 import { hilog } from '@kit.PerformanceAnalysisKit'; 5086 5087 let data = rpc.MessageParcel.create(); 5088 let result = data.writeIntArray([100, 111, 112]); 5089 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5090 let array = data.readIntArray(); 5091 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 5092 ``` 5093 5094### writeLongArray 5095 5096writeLongArray(longArray: number[]): boolean 5097 5098将长整数数组写入MessageParcel实例。 5099 5100**系统能力**:SystemCapability.Communication.IPC.Core 5101 5102**参数:** 5103 5104 | 参数名 | 类型 | 必填 | 说明 | 5105 | --------- | -------- | ---- | -------------------- | 5106 | longArray | number[] | 是 | 要写入的长整数数组。 | 5107 5108**返回值:** 5109 5110 | 类型 | 说明 | 5111 | ------- | ----------------------------- | 5112 | boolean | true:写入成功,false:写入失败。| 5113 5114**示例:** 5115 5116 ```ts 5117 import { hilog } from '@kit.PerformanceAnalysisKit'; 5118 5119 let data = rpc.MessageParcel.create(); 5120 let result = data.writeLongArray([1111, 1112, 1113]); 5121 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5122 ``` 5123 5124### readLongArray 5125 5126readLongArray(dataIn: number[]): void 5127 5128从MessageParcel实例读取长整数数组。 5129 5130**系统能力**:SystemCapability.Communication.IPC.Core 5131 5132**参数:** 5133 5134 | 参数名 | 类型 | 必填 | 说明 | 5135 | ------ | -------- | ---- | -------------------- | 5136 | dataIn | number[] | 是 | 要读取的长整数数组。 | 5137 5138**示例:** 5139 5140 ```ts 5141 import { hilog } from '@kit.PerformanceAnalysisKit'; 5142 5143 let data = rpc.MessageParcel.create(); 5144 let result = data.writeLongArray([1111, 1112, 1113]); 5145 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5146 let array: Array<number> = new Array(3); 5147 data.readLongArray(array); 5148 ``` 5149 5150### readLongArray 5151 5152readLongArray(): number[] 5153 5154从MessageParcel实例中读取长整数数组。 5155 5156**系统能力**:SystemCapability.Communication.IPC.Core 5157 5158**返回值:** 5159 5160 | 类型 | 说明 | 5161 | -------- | ---------------- | 5162 | number[] | 返回长整数数组。 | 5163 5164**示例:** 5165 5166 ```ts 5167 import { hilog } from '@kit.PerformanceAnalysisKit'; 5168 5169 let data = rpc.MessageParcel.create(); 5170 let result = data.writeLongArray([1111, 1112, 1113]); 5171 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5172 let array = data.readLongArray(); 5173 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 5174 ``` 5175 5176### writeFloatArray 5177 5178writeFloatArray(floatArray: number[]): boolean 5179 5180将浮点数组写入MessageParcel实例。 5181 5182**系统能力**:SystemCapability.Communication.IPC.Core 5183 5184**参数:** 5185 5186 | 参数名 | 类型 | 必填 | 说明 | 5187 | ---------- | -------- | ---- | --- | 5188 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5189 5190**返回值:** 5191 5192 | 类型 | 说明 | 5193 | ------- | -------------------------------- | 5194 | boolean | true:写入成功,false:写入失败。| 5195 5196**示例:** 5197 5198 ```ts 5199 import { hilog } from '@kit.PerformanceAnalysisKit'; 5200 5201 let data = rpc.MessageParcel.create(); 5202 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5203 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5204 ``` 5205 5206### readFloatArray 5207 5208readFloatArray(dataIn: number[]): void 5209 5210从MessageParcel实例中读取浮点数组。 5211 5212**系统能力**:SystemCapability.Communication.IPC.Core 5213 5214**参数:** 5215 5216 | 参数名 | 类型 | 必填 | 说明 | 5217 | ------ | -------- | ---- | ------ | 5218 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5219 5220**示例:** 5221 5222 ```ts 5223 import { hilog } from '@kit.PerformanceAnalysisKit'; 5224 5225 let data = rpc.MessageParcel.create(); 5226 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5227 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5228 let array: Array<number> = new Array(3); 5229 data.readFloatArray(array); 5230 ``` 5231 5232### readFloatArray 5233 5234readFloatArray(): number[] 5235 5236从MessageParcel实例中读取浮点数组。 5237 5238**系统能力**:SystemCapability.Communication.IPC.Core 5239 5240**返回值:** 5241 5242 | 类型 | 说明 | 5243 | -------- | -------------- | 5244 | number[] | 返回浮点数组。 | 5245 5246**示例:** 5247 5248 ```ts 5249 import { hilog } from '@kit.PerformanceAnalysisKit'; 5250 5251 let data = rpc.MessageParcel.create(); 5252 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5253 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5254 let array = data.readFloatArray(); 5255 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 5256 ``` 5257 5258### writeDoubleArray 5259 5260writeDoubleArray(doubleArray: number[]): boolean 5261 5262将双精度浮点数组写入MessageParcel实例。 5263 5264**系统能力**:SystemCapability.Communication.IPC.Core 5265 5266**参数:** 5267 5268 | 参数名 | 类型 | 必填 | 说明 | 5269 | ----------- | -------- | ---- | ------------------------ | 5270 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 5271 5272**返回值:** 5273 5274 | 类型 | 说明 | 5275 | ------- | -------------------------------- | 5276 | boolean | true:写入成功,false:写入失败。| 5277 5278**示例:** 5279 5280 ```ts 5281 import { hilog } from '@kit.PerformanceAnalysisKit'; 5282 5283 let data = rpc.MessageParcel.create(); 5284 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5285 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5286 ``` 5287 5288### readDoubleArray 5289 5290readDoubleArray(dataIn: number[]): void 5291 5292从MessageParcel实例中读取双精度浮点数组。 5293 5294**系统能力**:SystemCapability.Communication.IPC.Core 5295 5296**参数:** 5297 5298 | 参数名 | 类型 | 必填 | 说明 | 5299 | ------ | -------- | ---- | ------------------------ | 5300 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 5301 5302**示例:** 5303 5304 ```ts 5305 import { hilog } from '@kit.PerformanceAnalysisKit'; 5306 5307 let data = rpc.MessageParcel.create(); 5308 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5309 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5310 let array: Array<number> = new Array(3); 5311 data.readDoubleArray(array); 5312 ``` 5313 5314### readDoubleArray 5315 5316readDoubleArray(): number[] 5317 5318从MessageParcel实例读取双精度浮点数组。 5319 5320**系统能力**:SystemCapability.Communication.IPC.Core 5321 5322**返回值:** 5323 5324 | 类型 | 说明 | 5325 | -------- | -------------------- | 5326 | number[] | 返回双精度浮点数组。 | 5327 5328**示例:** 5329 5330 ```ts 5331 import { hilog } from '@kit.PerformanceAnalysisKit'; 5332 5333 let data = rpc.MessageParcel.create(); 5334 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5335 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5336 let array = data.readDoubleArray(); 5337 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5338 ``` 5339 5340### writeBooleanArray 5341 5342writeBooleanArray(booleanArray: boolean[]): boolean 5343 5344将布尔数组写入MessageParcel实例。 5345 5346**系统能力**:SystemCapability.Communication.IPC.Core 5347 5348**参数:** 5349 5350 | 参数名 | 类型 | 必填 | 说明 | 5351 | ------------ | --------- | ---- | ------------------ | 5352 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 5353 5354**返回值:** 5355 5356 | 类型 | 说明 | 5357 | ------- | -------------------------------- | 5358 | boolean | true:写入成功,false:写入失败。| 5359 5360**示例:** 5361 5362 ```ts 5363 import { hilog } from '@kit.PerformanceAnalysisKit'; 5364 5365 let data = rpc.MessageParcel.create(); 5366 let result = data.writeBooleanArray([false, true, false]); 5367 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5368 ``` 5369 5370### readBooleanArray 5371 5372readBooleanArray(dataIn: boolean[]): void 5373 5374从MessageParcel实例中读取布尔数组。 5375 5376**系统能力**:SystemCapability.Communication.IPC.Core 5377 5378**参数:** 5379 5380 | 参数名 | 类型 | 必填 | 说明 | 5381 | ------ | --------- | ---- | ------------------ | 5382 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 5383 5384**示例:** 5385 5386 ```ts 5387 import { hilog } from '@kit.PerformanceAnalysisKit'; 5388 5389 let data = rpc.MessageParcel.create(); 5390 let result = data.writeBooleanArray([false, true, false]); 5391 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5392 let array: Array<boolean> = new Array(3); 5393 data.readBooleanArray(array); 5394 ``` 5395 5396### readBooleanArray 5397 5398readBooleanArray(): boolean[] 5399 5400从MessageParcel实例中读取布尔数组。 5401 5402**系统能力**:SystemCapability.Communication.IPC.Core 5403 5404**返回值:** 5405 5406 | 类型 | 说明 | 5407 | --------- | -------------- | 5408 | boolean[] | 返回布尔数组。 | 5409 5410**示例:** 5411 5412 ```ts 5413 import { hilog } from '@kit.PerformanceAnalysisKit'; 5414 5415 let data = rpc.MessageParcel.create(); 5416 let result = data.writeBooleanArray([false, true, false]); 5417 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5418 let array = data.readBooleanArray(); 5419 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5420 ``` 5421 5422### writeCharArray 5423 5424writeCharArray(charArray: number[]): boolean 5425 5426将单个字符数组写入MessageParcel实例。 5427 5428**系统能力**:SystemCapability.Communication.IPC.Core 5429 5430**参数:** 5431 5432 | 参数名 | 类型 | 必填 | 说明 | 5433 | --------- | -------- | ---- | ---------------------- | 5434 | charArray | number[] | 是 | 要写入的单个字符数组。 | 5435 5436**返回值:** 5437 5438 | 类型 | 说明 | 5439 | ------- | -------------------------------- | 5440 | boolean | true:写入成功,false:写入失败。| 5441 5442**示例:** 5443 5444 ```ts 5445 import { hilog } from '@kit.PerformanceAnalysisKit'; 5446 5447 let data = rpc.MessageParcel.create(); 5448 let result = data.writeCharArray([97, 98, 88]); 5449 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5450 ``` 5451 5452### readCharArray 5453 5454readCharArray(dataIn: number[]): void 5455 5456从MessageParcel实例中读取单个字符数组。 5457 5458**系统能力**:SystemCapability.Communication.IPC.Core 5459 5460**参数:** 5461 5462 | 参数名 | 类型 | 必填 | 说明 | 5463 | ------ | -------- | ---- | ---------------------- | 5464 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 5465 5466**示例:** 5467 5468 ```ts 5469 import { hilog } from '@kit.PerformanceAnalysisKit'; 5470 5471 let data = rpc.MessageParcel.create(); 5472 let result = data.writeCharArray([97, 98, 99]); 5473 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5474 let array: Array<number> = new Array(3); 5475 data.readCharArray(array); 5476 ``` 5477 5478### readCharArray 5479 5480readCharArray(): number[] 5481 5482从MessageParcel实例读取单个字符数组。 5483 5484**系统能力**:SystemCapability.Communication.IPC.Core 5485 5486**返回值:** 5487 5488 | 类型 | 说明 | 5489 | -------- | ------------------ | 5490 | number[] | 返回单个字符数组。 | 5491 5492**示例:** 5493 5494 ```ts 5495 import { hilog } from '@kit.PerformanceAnalysisKit'; 5496 5497 let data = rpc.MessageParcel.create(); 5498 let result = data.writeCharArray([97, 98, 99]); 5499 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5500 let array = data.readCharArray(); 5501 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5502 ``` 5503 5504### writeStringArray 5505 5506writeStringArray(stringArray: string[]): boolean 5507 5508将字符串数组写入MessageParcel实例。 5509 5510**系统能力**:SystemCapability.Communication.IPC.Core 5511 5512**参数:** 5513 5514 | 参数名 | 类型 | 必填 | 说明 | 5515 | ----------- | -------- | ---- | ---------------- | 5516 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 5517 5518**返回值:** 5519 5520 | 类型 | 说明 | 5521 | ------- | -------------------------------- | 5522 | boolean | true:写入成功,false:写入失败。| 5523 5524**示例:** 5525 5526 ```ts 5527 import { hilog } from '@kit.PerformanceAnalysisKit'; 5528 5529 let data = rpc.MessageParcel.create(); 5530 let result = data.writeStringArray(["abc", "def"]); 5531 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5532 ``` 5533 5534### readStringArray 5535 5536readStringArray(dataIn: string[]): void 5537 5538从MessageParcel实例读取字符串数组。 5539 5540**系统能力**:SystemCapability.Communication.IPC.Core 5541 5542**参数:** 5543 5544 | 参数名 | 类型 | 必填 | 说明 | 5545 | ------ | -------- | ---- | -------------------- | 5546 | dataIn | string[] | 是 | 要读取的字符串数组。 | 5547 5548**示例:** 5549 5550 ```ts 5551 import { hilog } from '@kit.PerformanceAnalysisKit'; 5552 5553 let data = rpc.MessageParcel.create(); 5554 let result = data.writeStringArray(["abc", "def"]); 5555 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5556 let array: Array<string> = new Array(2); 5557 data.readStringArray(array); 5558 ``` 5559 5560### readStringArray 5561 5562readStringArray(): string[] 5563 5564从MessageParcel实例读取字符串数组。 5565 5566**系统能力**:SystemCapability.Communication.IPC.Core 5567 5568**返回值:** 5569 5570 | 类型 | 说明 | 5571 | -------- | ---------------- | 5572 | string[] | 返回字符串数组。 | 5573 5574**示例:** 5575 5576 ```ts 5577 import { hilog } from '@kit.PerformanceAnalysisKit'; 5578 5579 let data = rpc.MessageParcel.create(); 5580 let result = data.writeStringArray(["abc", "def"]); 5581 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5582 let array = data.readStringArray(); 5583 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5584 ``` 5585 5586### writeNoException<sup>8+</sup> 5587 5588writeNoException(): void 5589 5590向MessageParcel写入“指示未发生异常”的信息。 5591 5592**系统能力**:SystemCapability.Communication.IPC.Core 5593 5594**示例:** 5595 5596 ```ts 5597 import { hilog } from '@kit.PerformanceAnalysisKit'; 5598 5599 class MyDeathRecipient implements rpc.DeathRecipient { 5600 onRemoteDied() { 5601 hilog.info(0x0000, 'testTag', 'server died'); 5602 } 5603 } 5604 class TestRemoteObject extends rpc.RemoteObject { 5605 constructor(descriptor: string) { 5606 super(descriptor); 5607 } 5608 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5609 return true; 5610 } 5611 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5612 return true; 5613 } 5614 isObjectDead(): boolean { 5615 return false; 5616 } 5617 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5618 if (code === 1) { 5619 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5620 reply.writeNoException(); 5621 return true; 5622 } else { 5623 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5624 return false; 5625 } 5626 } 5627 } 5628 ``` 5629 5630### readException<sup>8+</sup> 5631 5632readException(): void 5633 5634从MessageParcel中读取异常。 5635 5636**系统能力**:SystemCapability.Communication.IPC.Core 5637 5638**示例:** 5639 5640 ```ts 5641 // FA模型需要从@kit.AbilityKit导入featureAbility 5642 // import { featureAbility } from '@kit.AbilityKit'; 5643 import { Want, common } from '@kit.AbilityKit'; 5644 import { hilog } from '@kit.PerformanceAnalysisKit'; 5645 5646 let proxy: rpc.IRemoteObject | undefined; 5647 let connect: common.ConnectOptions = { 5648 onConnect: (elementName, remoteProxy) => { 5649 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5650 proxy = remoteProxy; 5651 }, 5652 onDisconnect: (elementName) => { 5653 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5654 }, 5655 onFailed: () => { 5656 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5657 } 5658 }; 5659 let want: Want = { 5660 bundleName: "com.ohos.server", 5661 abilityName: "com.ohos.server.EntryAbility", 5662 }; 5663 5664 // FA模型使用此方法连接服务 5665 // FA.connectAbility(want,connect); 5666 5667 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5668 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 5669 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5670 let connectionId = context.connectServiceExtensionAbility(want, connect); 5671 ``` 5672 5673 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 5674 5675 ```ts 5676 import { hilog } from '@kit.PerformanceAnalysisKit'; 5677 5678 let option = new rpc.MessageOption(); 5679 let data = rpc.MessageParcel.create(); 5680 let reply = rpc.MessageParcel.create(); 5681 data.writeNoException(); 5682 data.writeString('hello'); 5683 if (proxy != undefined) { 5684 let a = proxy.sendRequest(1, data, reply, option) as Object; 5685 let b = a as Promise<rpc.SendRequestResult>; 5686 b.then((result: rpc.SendRequestResult) => { 5687 if (result.errCode === 0) { 5688 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5689 result.reply.readException(); 5690 let msg = result.reply.readString(); 5691 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5692 } else { 5693 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5694 } 5695 }).catch((e: Error) => { 5696 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5697 }).finally (() => { 5698 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5699 data.reclaim(); 5700 reply.reclaim(); 5701 }); 5702 } 5703 ``` 5704 5705### writeSequenceableArray 5706 5707writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5708 5709将可序列化对象数组写入MessageParcel实例。 5710 5711**系统能力**:SystemCapability.Communication.IPC.Core 5712 5713**参数:** 5714 5715| 参数名 | 类型 | 必填 | 说明 | 5716| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5717| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要写入的可序列化对象数组。 | 5718 5719**返回值:** 5720 5721 | 类型 | 说明 | 5722 | ------- | -------------------------------- | 5723 | boolean | true:写入成功,false:写入失败。| 5724 5725**示例:** 5726 5727 ```ts 5728 import { hilog } from '@kit.PerformanceAnalysisKit'; 5729 5730 class MySequenceable implements rpc.Sequenceable { 5731 num: number = 0; 5732 str: string = ''; 5733 constructor(num: number, str: string) { 5734 this.num = num; 5735 this.str = str; 5736 } 5737 marshalling(messageParcel: rpc.MessageParcel): boolean { 5738 messageParcel.writeInt(this.num); 5739 messageParcel.writeString(this.str); 5740 return true; 5741 } 5742 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5743 this.num = messageParcel.readInt(); 5744 this.str = messageParcel.readString(); 5745 return true; 5746 } 5747 } 5748 let sequenceable = new MySequenceable(1, "aaa"); 5749 let sequenceable2 = new MySequenceable(2, "bbb"); 5750 let sequenceable3 = new MySequenceable(3, "ccc"); 5751 let a = [sequenceable, sequenceable2, sequenceable3]; 5752 let data = rpc.MessageParcel.create(); 5753 let result = data.writeSequenceableArray(a); 5754 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5755 ``` 5756 5757### readSequenceableArray<sup>8+</sup> 5758 5759readSequenceableArray(sequenceableArray: Sequenceable[]): void 5760 5761从MessageParcel实例读取可序列化对象数组。 5762 5763**系统能力**:SystemCapability.Communication.IPC.Core 5764 5765**参数:** 5766 5767| 参数名 | 类型 | 必填 | 说明 | 5768| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5769| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要读取的可序列化对象数组。 | 5770 5771**示例:** 5772 5773 ```ts 5774 import { hilog } from '@kit.PerformanceAnalysisKit'; 5775 5776 class MySequenceable implements rpc.Sequenceable { 5777 num: number = 0; 5778 str: string = ''; 5779 constructor(num: number, str: string) { 5780 this.num = num; 5781 this.str = str; 5782 } 5783 marshalling(messageParcel: rpc.MessageParcel): boolean { 5784 messageParcel.writeInt(this.num); 5785 messageParcel.writeString(this.str); 5786 return true; 5787 } 5788 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5789 this.num = messageParcel.readInt(); 5790 this.str = messageParcel.readString(); 5791 return true; 5792 } 5793 } 5794 let sequenceable = new MySequenceable(1, "aaa"); 5795 let sequenceable2 = new MySequenceable(2, "bbb"); 5796 let sequenceable3 = new MySequenceable(3, "ccc"); 5797 let a = [sequenceable, sequenceable2, sequenceable3]; 5798 let data = rpc.MessageParcel.create(); 5799 let result = data.writeSequenceableArray(a); 5800 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5801 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5802 data.readSequenceableArray(b); 5803 ``` 5804 5805### writeRemoteObjectArray<sup>8+</sup> 5806 5807writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5808 5809将IRemoteObject对象数组写入MessageParcel。 5810 5811**系统能力**:SystemCapability.Communication.IPC.Core 5812 5813**参数:** 5814 5815 | 参数名 | 类型 | 必填 | 说明 | 5816 | ----------- | --------------- | ---- | ----- | 5817 | objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageParcel的IRemoteObject对象数组。 | 5818 5819**返回值:** 5820 5821 | 类型 | 说明 | 5822 | ------- | -------------------------------- | 5823 | boolean | true:写入成功,false:写入失败。| 5824 5825**示例:** 5826 5827 ```ts 5828 import { hilog } from '@kit.PerformanceAnalysisKit'; 5829 5830 class MyDeathRecipient implements rpc.DeathRecipient { 5831 onRemoteDied() { 5832 hilog.info(0x0000, 'testTag', 'server died'); 5833 } 5834 } 5835 class TestRemoteObject extends rpc.RemoteObject { 5836 constructor(descriptor: string) { 5837 super(descriptor); 5838 this.attachLocalInterface(this, descriptor); 5839 } 5840 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5841 return true; 5842 } 5843 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5844 return true; 5845 } 5846 isObjectDead(): boolean { 5847 return false; 5848 } 5849 asObject(): rpc.IRemoteObject { 5850 return this; 5851 } 5852 } 5853 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5854 let data = rpc.MessageParcel.create(); 5855 let result = data.writeRemoteObjectArray(a); 5856 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5857 ``` 5858 5859### readRemoteObjectArray<sup>8+</sup> 5860 5861readRemoteObjectArray(objects: IRemoteObject[]): void 5862 5863从MessageParcel读取IRemoteObject对象数组。 5864 5865**系统能力**:SystemCapability.Communication.IPC.Core 5866 5867**参数:** 5868 5869 | 参数名 | 类型 | 必填 | 说明 | 5870 | ------- | --------------- | ---- | --------- | 5871 | objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageParcel读取的IRemoteObject对象数组。 | 5872 5873**示例:** 5874 5875 ```ts 5876 import { hilog } from '@kit.PerformanceAnalysisKit'; 5877 5878 class MyDeathRecipient implements rpc.DeathRecipient { 5879 onRemoteDied() { 5880 hilog.info(0x0000, 'testTag', 'server died'); 5881 } 5882 } 5883 class TestRemoteObject extends rpc.RemoteObject { 5884 constructor(descriptor: string) { 5885 super(descriptor); 5886 this.attachLocalInterface(this, descriptor); 5887 } 5888 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5889 return true; 5890 } 5891 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5892 return true; 5893 } 5894 isObjectDead(): boolean { 5895 return false; 5896 } 5897 asObject(): rpc.IRemoteObject { 5898 return this; 5899 } 5900 } 5901 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5902 let data = rpc.MessageParcel.create(); 5903 data.writeRemoteObjectArray(a); 5904 let b: Array<rpc.IRemoteObject> = new Array(3); 5905 data.readRemoteObjectArray(b); 5906 ``` 5907 5908### readRemoteObjectArray<sup>8+</sup> 5909 5910readRemoteObjectArray(): IRemoteObject[] 5911 5912从MessageParcel读取IRemoteObject对象数组。 5913 5914**系统能力**:SystemCapability.Communication.IPC.Core 5915 5916**返回值:** 5917 5918 | 类型 | 说明 | 5919 | --------------- | --------------------------- | 5920 | [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 5921 5922**示例:** 5923 5924 ```ts 5925 import { hilog } from '@kit.PerformanceAnalysisKit'; 5926 5927 class MyDeathRecipient implements rpc.DeathRecipient { 5928 onRemoteDied() { 5929 hilog.info(0x0000, 'testTag', 'server died'); 5930 } 5931 } 5932 class TestRemoteObject extends rpc.RemoteObject { 5933 constructor(descriptor: string) { 5934 super(descriptor); 5935 this.attachLocalInterface(this, descriptor); 5936 } 5937 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5938 return true; 5939 } 5940 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5941 return true; 5942 } 5943 isObjectDead(): boolean { 5944 return false; 5945 } 5946 asObject(): rpc.IRemoteObject { 5947 return this; 5948 } 5949 } 5950 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5951 let data = rpc.MessageParcel.create(); 5952 let result = data.writeRemoteObjectArray(a); 5953 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5954 let b = data.readRemoteObjectArray(); 5955 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5956 ``` 5957 5958### closeFileDescriptor<sup>8+</sup> 5959 5960static closeFileDescriptor(fd: number): void 5961 5962静态方法,关闭给定的文件描述符。 5963 5964**系统能力**:SystemCapability.Communication.IPC.Core 5965 5966**参数:** 5967 5968 | 参数名 | 类型 | 必填 | 说明 | 5969 | ------ | ------ | ---- | -------------------- | 5970 | fd | number | 是 | 要关闭的文件描述符。 | 5971 5972**示例:** 5973 5974 ```ts 5975 import { fileIo } from '@kit.CoreFileKit'; 5976 5977 let filePath = "path/to/file"; 5978 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5979 rpc.MessageParcel.closeFileDescriptor(file.fd); 5980 ``` 5981 5982### dupFileDescriptor<sup>8+</sup> 5983 5984static dupFileDescriptor(fd: number) :number 5985 5986静态方法,复制给定的文件描述符。 5987 5988**系统能力**:SystemCapability.Communication.IPC.Core 5989 5990**参数:** 5991 5992 | 参数名 | 类型 | 必填 | 说明 | 5993 | ------ | ------ | ---- | ------------------------ | 5994 | fd | number | 是 | 表示已存在的文件描述符。 | 5995 5996**返回值:** 5997 5998 | 类型 | 说明 | 5999 | ------ | -------------------- | 6000 | number | 返回新的文件描述符。 | 6001 6002**示例:** 6003 6004 ```ts 6005 import { fileIo } from '@kit.CoreFileKit'; 6006 6007 let filePath = "path/to/file"; 6008 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6009 rpc.MessageParcel.dupFileDescriptor(file.fd); 6010 ``` 6011 6012### containFileDescriptors<sup>8+</sup> 6013 6014containFileDescriptors(): boolean 6015 6016检查此MessageParcel对象是否包含文件描述符。 6017 6018**系统能力**:SystemCapability.Communication.IPC.Core 6019 6020**返回值:** 6021 6022 | 类型 | 说明 | 6023 | ------- | --------------------------------------------- | 6024 | boolean |true:包含文件描述符,false:未包含文件描述符。| 6025 6026**示例:** 6027 6028 ```ts 6029 import { fileIo } from '@kit.CoreFileKit'; 6030 import { hilog } from '@kit.PerformanceAnalysisKit'; 6031 6032 let parcel = new rpc.MessageParcel(); 6033 let filePath = "path/to/file"; 6034 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6035 let writeResult = parcel.writeFileDescriptor(file.fd); 6036 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6037 let containFD = parcel.containFileDescriptors(); 6038 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 6039 ``` 6040 6041### writeFileDescriptor<sup>8+</sup> 6042 6043writeFileDescriptor(fd: number): boolean 6044 6045写入文件描述符到MessageParcel。 6046 6047**系统能力**:SystemCapability.Communication.IPC.Core 6048 6049**参数:** 6050 6051 | 参数名 | 类型 | 必填 | 说明 | 6052 | ------ | ------ | ---- | ------------ | 6053 | fd | number | 是 | 文件描述符。 | 6054 6055**返回值:** 6056 6057 | 类型 | 说明 | 6058 | ------- | -------------------------------- | 6059 | boolean | true:操作成功,false:操作失败。| 6060 6061**示例:** 6062 6063 ```ts 6064 import { fileIo } from '@kit.CoreFileKit'; 6065 import { hilog } from '@kit.PerformanceAnalysisKit'; 6066 6067 let parcel = new rpc.MessageParcel(); 6068 let filePath = "path/to/file"; 6069 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6070 let writeResult = parcel.writeFileDescriptor(file.fd); 6071 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6072 ``` 6073 6074### readFileDescriptor<sup>8+</sup> 6075 6076readFileDescriptor(): number 6077 6078从MessageParcel中读取文件描述符。 6079 6080**系统能力**:SystemCapability.Communication.IPC.Core 6081 6082**返回值:** 6083 6084 | 类型 | 说明 | 6085 | ------ | ---------------- | 6086 | number | 返回文件描述符。 | 6087 6088**示例:** 6089 6090 ```ts 6091 import { fileIo } from '@kit.CoreFileKit'; 6092 import { hilog } from '@kit.PerformanceAnalysisKit'; 6093 6094 let parcel = new rpc.MessageParcel(); 6095 let filePath = "path/to/file"; 6096 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6097 parcel.writeFileDescriptor(file.fd); 6098 let readFD = parcel.readFileDescriptor(); 6099 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 6100 ``` 6101 6102### writeAshmem<sup>8+</sup> 6103 6104writeAshmem(ashmem: Ashmem): boolean 6105 6106将指定的匿名共享对象写入此MessageParcel。 6107 6108**系统能力**:SystemCapability.Communication.IPC.Core 6109 6110**参数:** 6111 6112| 参数名 | 类型 | 必填 | 说明 | 6113| ------ | ------ | ---- | ----------------------------------- | 6114| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageParcel的匿名共享对象。 | 6115 6116**返回值:** 6117 6118 | 类型 | 说明 | 6119 | ------- | -------------------------------- | 6120 | boolean | true:写入成功,false:写入失败。| 6121 6122**示例:** 6123 6124 ```ts 6125 import { hilog } from '@kit.PerformanceAnalysisKit'; 6126 6127 let parcel = new rpc.MessageParcel(); 6128 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6129 let isWriteSuccess = parcel.writeAshmem(ashmem); 6130 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6131 ``` 6132 6133### readAshmem<sup>8+</sup> 6134 6135readAshmem(): Ashmem 6136 6137从MessageParcel读取匿名共享对象。 6138 6139**系统能力**:SystemCapability.Communication.IPC.Core 6140 6141**返回值:** 6142 6143| 类型 | 说明 | 6144| ------ | ------------------ | 6145| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 6146 6147**示例:** 6148 6149 ```ts 6150 import { hilog } from '@kit.PerformanceAnalysisKit'; 6151 6152 let parcel = new rpc.MessageParcel(); 6153 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6154 let isWriteSuccess = parcel.writeAshmem(ashmem); 6155 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6156 let readAshmem = parcel.readAshmem(); 6157 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 6158 ``` 6159 6160### getRawDataCapacity<sup>8+</sup> 6161 6162getRawDataCapacity(): number 6163 6164获取MessageParcel可以容纳的最大原始数据量。 6165 6166**系统能力**:SystemCapability.Communication.IPC.Core 6167 6168**返回值:** 6169 6170 | 类型 | 说明 | 6171 | ------ | ---------------------------------------------------------- | 6172 | number | 返回MessageParcel可以容纳的最大原始数据量,即128MB。 | 6173 6174**示例:** 6175 6176 ```ts 6177 import { hilog } from '@kit.PerformanceAnalysisKit'; 6178 6179 let parcel = new rpc.MessageParcel(); 6180 let result = parcel.getRawDataCapacity(); 6181 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 6182 ``` 6183 6184### writeRawData<sup>8+</sup> 6185 6186writeRawData(rawData: number[], size: number): boolean 6187 6188将原始数据写入MessageParcel对象。 6189 6190**系统能力**:SystemCapability.Communication.IPC.Core 6191 6192**参数:** 6193 6194 | 参数名 | 类型 | 必填 | 说明 | 6195 | ------- | -------- | ---- | ---------------------------------- | 6196 | rawData | number[] | 是 | 要写入的原始数据。 | 6197 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 6198 6199**返回值:** 6200 6201 | 类型 | 说明 | 6202 | ------- | -------------------------------- | 6203 | boolean | true:写入成功,false:写入失败。| 6204 6205**示例:** 6206 6207 ```ts 6208 import { hilog } from '@kit.PerformanceAnalysisKit'; 6209 6210 let parcel = new rpc.MessageParcel(); 6211 let arr = [1, 2, 3, 4, 5]; 6212 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6213 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6214 ``` 6215 6216### readRawData<sup>8+</sup> 6217 6218readRawData(size: number): number[] 6219 6220从MessageParcel读取原始数据。 6221 6222**系统能力**:SystemCapability.Communication.IPC.Core 6223 6224**参数:** 6225 6226 | 参数名 | 类型 | 必填 | 说明 | 6227 | ------ | ------ | ---- | ------------------------ | 6228 | size | number | 是 | 要读取的原始数据的大小。 | 6229 6230**返回值:** 6231 6232 | 类型 | 说明 | 6233 | -------- | ------------------------------ | 6234 | number[] | 返回原始数据(以字节为单位)。 | 6235 6236**示例:** 6237 6238 ```ts 6239 import { hilog } from '@kit.PerformanceAnalysisKit'; 6240 6241 let parcel = new rpc.MessageParcel(); 6242 let arr = [1, 2, 3, 4, 5]; 6243 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6244 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6245 let result = parcel.readRawData(5); 6246 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 6247 ``` 6248 6249## Parcelable<sup>9+</sup> 6250 6251在进程间通信(IPC)期间,将类的对象写入MessageSequence并从MessageSequence中恢复它们。 6252 6253### marshalling 6254 6255marshalling(dataOut: MessageSequence): boolean 6256 6257将此可序列对象封送到MessageSequence中。 6258 6259**系统能力**:SystemCapability.Communication.IPC.Core 6260 6261**参数:** 6262 6263| 参数名 | 类型 | 必填 | 说明 | 6264| ------- | --------------- | ---- | ------------------------------------------- | 6265| dataOut |[MessageSequence](#messagesequence9)| 是 | 可序列对象将被封送到的MessageSequence对象。 | 6266 6267**返回值:** 6268 6269 | 类型 | 说明 | 6270 | ------- | -------------------------------- | 6271 | boolean | true:封送成功,false:封送失败。| 6272 6273**示例:** 6274 6275 ```ts 6276 import { hilog } from '@kit.PerformanceAnalysisKit'; 6277 6278 class MyParcelable implements rpc.Parcelable { 6279 num: number = 0; 6280 str: string = ''; 6281 constructor(num: number, str: string) { 6282 this.num = num; 6283 this.str = str; 6284 } 6285 marshalling(messageSequence: rpc.MessageSequence): boolean { 6286 messageSequence.writeInt(this.num); 6287 messageSequence.writeString(this.str); 6288 return true; 6289 } 6290 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6291 this.num = messageSequence.readInt(); 6292 this.str = messageSequence.readString(); 6293 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6294 return true; 6295 } 6296 } 6297 let parcelable = new MyParcelable(1, "aaa"); 6298 let data = rpc.MessageSequence.create(); 6299 data.writeParcelable(parcelable); 6300 let ret = new MyParcelable(0, ""); 6301 data.readParcelable(ret); 6302 ``` 6303 6304### unmarshalling 6305 6306unmarshalling(dataIn: MessageSequence): boolean 6307 6308从MessageSequence中解封此可序列对象。 6309 6310**系统能力**:SystemCapability.Communication.IPC.Core 6311 6312**参数:** 6313 6314| 参数名 | 类型 | 必填 | 说明 | 6315| ------ | --------------- | ---- | ----------------------------------------------- | 6316| dataIn | [MessageSequence](#messagesequence9) | 是 | 已将可序列对象封送到其中的MessageSequence对象。 | 6317 6318**返回值:** 6319 6320 | 类型 | 说明 | 6321 | ------- | ---------------------------------------- | 6322 | boolean | true:反序列化成功,false:反序列化失败。| 6323 6324**示例:** 6325 6326 ```ts 6327 import { hilog } from '@kit.PerformanceAnalysisKit'; 6328 6329 class MyParcelable implements rpc.Parcelable { 6330 num: number = 0; 6331 str: string = ''; 6332 constructor(num: number, str: string) { 6333 this.num = num; 6334 this.str = str; 6335 } 6336 marshalling(messageSequence: rpc.MessageSequence): boolean { 6337 messageSequence.writeInt(this.num); 6338 messageSequence.writeString(this.str); 6339 return true; 6340 } 6341 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6342 this.num = messageSequence.readInt(); 6343 this.str = messageSequence.readString(); 6344 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6345 return true; 6346 } 6347 } 6348 let parcelable = new MyParcelable(1, "aaa"); 6349 let data = rpc.MessageSequence.create(); 6350 data.writeParcelable(parcelable); 6351 let ret = new MyParcelable(0, ""); 6352 data.readParcelable(ret); 6353 ``` 6354 6355## Sequenceable<sup>(deprecated)</sup> 6356 6357在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。 6358 6359> **说明:** 6360> 6361> 从API version 9 开始废弃,建议使用[Parcelable](#parcelable9)替代。 6362 6363### marshalling 6364 6365marshalling(dataOut: MessageParcel): boolean 6366 6367将此可序列对象封送到MessageParcel中。 6368 6369**系统能力**:SystemCapability.Communication.IPC.Core 6370 6371**参数:** 6372 6373 | 参数名 | 类型 | 必填 | 说明 | 6374 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6375 | dataOut | [MessageParcel](#messageparceldeprecated) | 是 | 可序列对象将被封送到的MessageParcel对象。 | 6376 6377**返回值:** 6378 6379 | 类型 | 说明 | 6380 | ------- | -------------------------------- | 6381 | boolean | true:封送成功,false:封送失败。 | 6382 6383**示例:** 6384 6385 ```ts 6386 import { hilog } from '@kit.PerformanceAnalysisKit'; 6387 6388 class MySequenceable implements rpc.Sequenceable { 6389 num: number = 0; 6390 str: string = ''; 6391 constructor(num: number, str: string) { 6392 this.num = num; 6393 this.str = str; 6394 } 6395 marshalling(messageParcel: rpc.MessageParcel): boolean { 6396 messageParcel.writeInt(this.num); 6397 messageParcel.writeString(this.str); 6398 return true; 6399 } 6400 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6401 this.num = messageParcel.readInt(); 6402 this.str = messageParcel.readString(); 6403 return true; 6404 } 6405 } 6406 let sequenceable = new MySequenceable(1, "aaa"); 6407 let data = rpc.MessageParcel.create(); 6408 let result = data.writeSequenceable(sequenceable); 6409 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6410 let ret = new MySequenceable(0, ""); 6411 let result2 = data.readSequenceable(ret); 6412 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6413 ``` 6414 6415### unmarshalling 6416 6417unmarshalling(dataIn: MessageParcel): boolean 6418 6419从MessageParcel中解封此可序列对象。 6420 6421**系统能力**:SystemCapability.Communication.IPC.Core 6422 6423**参数:** 6424 6425 | 参数名 | 类型 | 必填 | 说明 | 6426 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6427 | dataIn | [MessageParcel](#messageparceldeprecated) | 是 | 已将可序列对象封送到其中的MessageParcel对象。 | 6428 6429**返回值:** 6430 6431 | 类型 | 说明 | 6432 | ------- | ---------------------------------------- | 6433 | boolean | true:反序列化成功,false:反序列化失败。| 6434 6435**示例:** 6436 6437 ```ts 6438 import { hilog } from '@kit.PerformanceAnalysisKit'; 6439 6440 class MySequenceable implements rpc.Sequenceable { 6441 num: number = 0; 6442 str: string = ''; 6443 constructor(num: number, str: string) { 6444 this.num = num; 6445 this.str = str; 6446 } 6447 marshalling(messageParcel: rpc.MessageParcel): boolean { 6448 messageParcel.writeInt(this.num); 6449 messageParcel.writeString(this.str); 6450 return true; 6451 } 6452 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6453 this.num = messageParcel.readInt(); 6454 this.str = messageParcel.readString(); 6455 return true; 6456 } 6457 } 6458 let sequenceable = new MySequenceable(1, "aaa"); 6459 let data = rpc.MessageParcel.create(); 6460 let result = data.writeSequenceable(sequenceable); 6461 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6462 let ret = new MySequenceable(0, ""); 6463 let result2 = data.readSequenceable(ret); 6464 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6465 ``` 6466 6467## IRemoteBroker 6468 6469远端对象的代理持有者。用于获取代理对象。 6470 6471### asObject 6472 6473asObject(): IRemoteObject 6474 6475需派生类实现,获取代理或远端对象。 6476 6477**系统能力**:SystemCapability.Communication.IPC.Core 6478 6479**返回值:** 6480 6481 | 类型 | 说明 | 6482 | ----- | ----- | 6483 | [IRemoteObject](#iremoteobject) | 如果调用者是RemoteObject对象,则直接返回本身;如果调用者是[RemoteProxy](#remoteproxy)对象,则返回它的持有者[IRemoteObject](#iremoteobject)。 | 6484 6485**示例:** 6486 6487 ```ts 6488 class TestAbility extends rpc.RemoteObject { 6489 asObject() { 6490 return this; 6491 } 6492 } 6493 let remoteObject = new TestAbility("testObject").asObject(); 6494 ``` 6495 6496**示例:** 6497 6498 ```ts 6499 // FA模型需要从@kit.AbilityKit导入featureAbility 6500 // import { featureAbility } from '@kit.AbilityKit'; 6501 import { Want, common } from '@kit.AbilityKit'; 6502 import { hilog } from '@kit.PerformanceAnalysisKit'; 6503 6504 let proxy: rpc.IRemoteObject | undefined; 6505 let connect: common.ConnectOptions = { 6506 onConnect: (elementName, remoteProxy) => { 6507 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6508 proxy = remoteProxy; 6509 }, 6510 onDisconnect: (elementName) => { 6511 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6512 }, 6513 onFailed: () => { 6514 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6515 } 6516 }; 6517 let want: Want = { 6518 bundleName: "com.ohos.server", 6519 abilityName: "com.ohos.server.EntryAbility", 6520 }; 6521 6522 // FA模型使用此方法连接服务 6523 // FA.connectAbility(want,connect); 6524 6525 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6526 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 6527 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6528 let connectionId = context.connectServiceExtensionAbility(want, connect); 6529 ``` 6530 6531 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象 6532 6533 ```ts 6534 class TestProxy { 6535 remote: rpc.IRemoteObject; 6536 constructor(remote: rpc.IRemoteObject) { 6537 this.remote = remote; 6538 } 6539 asObject() { 6540 return this.remote; 6541 } 6542 } 6543 if (proxy != undefined) { 6544 let iRemoteObject = new TestProxy(proxy).asObject(); 6545 } 6546 ``` 6547 6548## DeathRecipient 6549 6550用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied](#onremotedied)接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。 6551 6552### onRemoteDied 6553 6554onRemoteDied(): void 6555 6556在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。 6557 6558**系统能力**:SystemCapability.Communication.IPC.Core 6559 6560**示例:** 6561 6562 ```ts 6563 import { hilog } from '@kit.PerformanceAnalysisKit'; 6564 6565 class MyDeathRecipient implements rpc.DeathRecipient { 6566 onRemoteDied() { 6567 hilog.info(0x0000, 'testTag', 'server died'); 6568 } 6569 } 6570 ``` 6571 6572## RequestResult<sup>9+</sup> 6573 6574发送请求的响应结果。 6575 6576**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6577 6578| 名称 | 类型 | 可读 | 可写 | 说明 | 6579| ------- | --------------- | ---- | ---- |-------------------------------------- | 6580| errCode | number | 是 | 否 | 错误码。 | 6581| code | number | 是 | 否 | 消息代码。 | 6582| data | [MessageSequence](#messagesequence9) | 是 | 否 | 发送给对端进程的MessageSequence对象。 | 6583| reply | [MessageSequence](#messagesequence9) | 是 | 否 | 对端进程返回的MessageSequence对象。 | 6584 6585## SendRequestResult<sup>(deprecated)</sup> 6586 6587发送请求的响应结果。 6588 6589> **说明:** 6590> 6591> 从API version 8 开始支持,API version 9 开始废弃,建议使用[RequestResult](#requestresult9)替代。 6592 6593**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6594 6595 | 名称 | 类型 | 可读 | 可写 | 说明 | 6596 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6597 | errCode | number | 是 | 否 | 错误码。 | 6598 | code | number | 是 | 否 | 消息代码。 | 6599 | data | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 发送给对端进程的MessageParcel对象。 | 6600 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 对端进程返回的MessageParcel对象。 | 6601 6602## IRemoteObject 6603 6604该接口可用于查询或获取接口描述符、添加或删除死亡通知、转储对象状态到特定文件、发送消息。 6605 6606### getLocalInterface<sup>9+</sup> 6607 6608getLocalInterface(descriptor: string): IRemoteBroker 6609 6610查询接口描述符的字符串。 6611 6612**系统能力**:SystemCapability.Communication.IPC.Core 6613 6614**参数:** 6615 6616 | 参数名 | 类型 | 必填 | 说明 | 6617 | ---------- | ------ | ---- | -------------------- | 6618 | descriptor | string | 是 | 接口描述符的字符串。 | 6619 6620**返回值:** 6621 6622| 类型 | 说明 | 6623| ------------- | --------------------------------------------- | 6624| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6625 6626**错误码:** 6627 6628以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6629 6630 | 错误码ID | 错误信息 | 6631 | -------- | -------- | 6632 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The string length exceeds 40960 bytes; <br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 6633 6634### queryLocalInterface<sup>(deprecated)</sup> 6635 6636queryLocalInterface(descriptor: string): IRemoteBroker 6637 6638查询接口描述符的字符串。 6639 6640> **说明:** 6641> 6642> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9)替代。 6643 6644**系统能力**:SystemCapability.Communication.IPC.Core 6645 6646**参数:** 6647 6648 | 参数名 | 类型 | 必填 | 说明 | 6649 | ---------- | ------ | ---- | -------------------- | 6650 | descriptor | string | 是 | 接口描述符的字符串。 | 6651 6652**返回值:** 6653 6654| 类型 | 说明 | 6655| ------------- | --------------------------------------------- | 6656| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6657 6658### sendRequest<sup>(deprecated)</sup> 6659 6660sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6661 6662以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6663 6664> **说明:** 6665> 6666> 从API version 8开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 6667 6668**系统能力**:SystemCapability.Communication.IPC.Core 6669 6670**参数:** 6671 6672 | 参数名 | 类型 | 必填 | 说明 | 6673 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6674 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6675 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6676 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6677 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6678 6679**返回值:** 6680 6681 | 类型 | 说明 | 6682 | ------- | -------------------------------- | 6683 | boolean | true:发送成功,false:发送失败。| 6684 6685### sendMessageRequest<sup>9+</sup> 6686 6687sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6688 6689以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 6690 6691**系统能力**:SystemCapability.Communication.IPC.Core 6692 6693**参数:** 6694 6695 | 参数名 | 类型 | 必填 | 说明 | 6696 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6697 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6698 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 6699 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6700 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6701 6702**返回值:** 6703 6704 | 类型 | 说明 | 6705 | ---------------------------- | ----------------------------------------- | 6706 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 6707 6708**错误码:** 6709 6710以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6711 6712 | 错误码ID | 错误信息 | 6713 | -------- | -------- | 6714 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain the passed object instance. | 6715 6716### sendRequest<sup>(deprecated)</sup> 6717 6718sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6719 6720以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6721 6722> **说明:** 6723> 6724> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 6725 6726**系统能力**:SystemCapability.Communication.IPC.Core 6727 6728**参数:** 6729 6730 | 参数名 | 类型 | 必填 | 说明 | 6731 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6732 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6733 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6734 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6735 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6736 6737**返回值:** 6738 6739| 类型 | 说明 | 6740| ------------------------------------------------------------ | --------------------------------------------- | 6741| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 6742 6743### sendMessageRequest<sup>9+</sup> 6744 6745sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6746 6747以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6748 6749**系统能力**:SystemCapability.Communication.IPC.Core 6750 6751**参数:** 6752 6753 | 参数名 | 类型 | 必填 | 说明 | 6754 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6755 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6756 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 6757 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6758 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6759 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 6760 6761**错误码:** 6762 6763以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6764 6765 | 错误码ID | 错误信息 | 6766 | -------- | -------- | 6767 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain the passed object instance. | 6768 6769### sendRequest<sup>(deprecated)</sup> 6770 6771sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6772 6773以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6774 6775> **说明:** 6776> 6777> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-1)替代。 6778 6779**系统能力**:SystemCapability.Communication.IPC.Core 6780 6781**参数:** 6782 6783| 参数名 | 类型 | 必填 | 说明 | 6784| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6785| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6786| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6787| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6788| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6789| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 6790 6791### registerDeathRecipient<sup>9+</sup> 6792 6793registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6794 6795注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6796 6797**系统能力**:SystemCapability.Communication.IPC.Core 6798 6799**参数:** 6800 6801 | 参数名 | 类型 | 必填 | 说明 | 6802 | --------- | --------------------------------- | ---- | -------------- | 6803 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6804 | flags | number | 是 | 死亡通知标志。 | 6805 6806**错误码:** 6807 6808以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6809 6810 | 错误码ID | 错误信息 | 6811 | -------- | -------- | 6812 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The callback used to receive remote object death notifications is empty. | 6813 | 1900008 | The proxy or remote object is invalid. | 6814 6815### addDeathrecipient<sup>(deprecated)</sup> 6816 6817addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6818 6819注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6820 6821> **说明:** 6822> 6823> 从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9)替代。 6824 6825**系统能力**:SystemCapability.Communication.IPC.Core 6826 6827**参数:** 6828 6829 | 参数名 | 类型 | 必填 | 说明 | 6830 | --------- | --------------------------------- | ---- | -------------- | 6831 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6832 | flags | number | 是 | 死亡通知标志。 | 6833 6834**返回值:** 6835 6836 | 类型 | 说明 | 6837 | ------- | ---------------------------------------- | 6838 | boolean | true:回调注册成功,false:回调注册失败。| 6839 6840### unregisterDeathRecipient<sup>9+</sup> 6841 6842unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6843 6844注销用于接收远程对象死亡通知的回调。 6845 6846**系统能力**:SystemCapability.Communication.IPC.Core 6847 6848**参数:** 6849 6850 | 参数名 | 类型 | 必填 | 说明 | 6851 | --------- | --------------------------------- | ---- | -------------- | 6852 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6853 | flags | number | 是 | 死亡通知标志。 | 6854 6855**错误码:** 6856 6857以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6858 6859 | 错误码ID | 错误信息 | 6860 | -------- | -------- | 6861 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The callback used to receive remote object death notifications is empty. | 6862 | 1900008 | The proxy or remote object is invalid. | 6863 6864### removeDeathRecipient<sup>(deprecated)</sup> 6865 6866removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6867 6868注销用于接收远程对象死亡通知的回调。 6869 6870> **说明:** 6871> 6872> 从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)替代。 6873 6874**系统能力**:SystemCapability.Communication.IPC.Core 6875 6876**参数:** 6877 6878 | 参数名 | 类型 | 必填 | 说明 | 6879 | --------- | --------------------------------- | ---- | -------------- | 6880 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6881 | flags | number | 是 | 死亡通知标志。 | 6882 6883**返回值:** 6884 6885 | 类型 | 说明 | 6886 | ------- | -----------------------------------------| 6887 | boolean | true:回调注销成功,false:回调注销失败。| 6888 6889### getDescriptor<sup>9+</sup> 6890 6891getDescriptor(): string 6892 6893获取对象的接口描述符,接口描述符为字符串。 6894 6895**系统能力**:SystemCapability.Communication.IPC.Core 6896 6897**返回值:** 6898 6899 | 类型 | 说明 | 6900 | ------ | ---------------- | 6901 | string | 返回接口描述符。 | 6902 6903**错误码:** 6904 6905以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6906 6907 | 错误码ID | 错误信息 | 6908 | -------- | -------- | 6909 | 1900008 | The proxy or remote object is invalid. | 6910 6911### getInterfaceDescriptor<sup>(deprecated)</sup> 6912 6913getInterfaceDescriptor(): string 6914 6915获取对象的接口描述符,接口描述符为字符串。 6916 6917> **说明:** 6918> 6919> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9)替代。 6920 6921**系统能力**:SystemCapability.Communication.IPC.Core 6922 6923**返回值:** 6924 6925 | 类型 | 说明 | 6926 | ------ | ---------------- | 6927 | string | 返回接口描述符。 | 6928 6929### isObjectDead 6930 6931isObjectDead(): boolean 6932 6933检查当前对象是否死亡。 6934 6935**系统能力**:SystemCapability.Communication.IPC.Core 6936 6937**返回值:** 6938 6939 | 类型 | 说明 | 6940 | ------- | ---------------------------------- | 6941 | boolean | true:对象死亡,false:对象未死亡。| 6942 6943## RemoteProxy 6944 6945实现IRemoteObject代理对象。 6946 6947### 属性 6948 6949**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6950 6951 | 名称 | 类型 | 可读 | 可写 | 说明 | 6952 | --------------------- | ----------------------------| ------|------------------------------------------------ | 6953 | PING_TRANSACTION | [RemoteProxy](#remoteproxy) | 是 | 否 | 内部指令码,用于测试IPC服务是否正常。 | 6954 | DUMP_TRANSACTION | [RemoteProxy](#remoteproxy) | 是 | 否 | 内部指令码,获取IPC服务相关的状态信息。 | 6955 | INTERFACE_TRANSACTION | [RemoteProxy](#remoteproxy) | 是 | 否 | 内部指令码,获取对端接口描述符。 | 6956 | MIN_TRANSACTION_ID | [RemoteProxy](#remoteproxy) | 是 | 否 | 最小有效指令码。 | 6957 | MAX_TRANSACTION_ID | [RemoteProxy](#remoteproxy) | 是 | 否 | 最大有效指令码。 | 6958 6959 6960### sendRequest<sup>(deprecated)</sup> 6961 6962sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6963 6964以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6965 6966> **说明:** 6967> 6968> 从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 6969 6970**系统能力**:SystemCapability.Communication.IPC.Core 6971 6972**参数:** 6973 6974 | 参数名 | 类型 | 必填 | 说明 | 6975 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6976 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6977 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6978 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6979 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6980 6981**返回值:** 6982 6983 | 类型 | 说明 | 6984 | ------- | ---------------------------------| 6985 | boolean | true:发送成功,false:发送失败。| 6986 6987**示例:** 6988 6989 ```ts 6990 // FA模型需要从@kit.AbilityKit导入featureAbility 6991 // import { featureAbility } from '@kit.AbilityKit'; 6992 import { Want, common } from '@kit.AbilityKit'; 6993 import { hilog } from '@kit.PerformanceAnalysisKit'; 6994 6995 let proxy: rpc.IRemoteObject | undefined; 6996 let connect: common.ConnectOptions = { 6997 onConnect: (elementName, remoteProxy) => { 6998 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6999 proxy = remoteProxy; 7000 }, 7001 onDisconnect: (elementName) => { 7002 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7003 }, 7004 onFailed: () => { 7005 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7006 } 7007 }; 7008 let want: Want = { 7009 bundleName: "com.ohos.server", 7010 abilityName: "com.ohos.server.EntryAbility", 7011 }; 7012 7013 // FA模型使用此方法连接服务 7014 // FA.connectAbility(want,connect); 7015 7016 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7017 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7018 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7019 let connectionId = context.connectServiceExtensionAbility(want, connect); 7020 ``` 7021 7022 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7023 7024 ```ts 7025 import { hilog } from '@kit.PerformanceAnalysisKit'; 7026 7027 let option = new rpc.MessageOption(); 7028 let data = rpc.MessageParcel.create(); 7029 let reply = rpc.MessageParcel.create(); 7030 data.writeInt(1); 7031 data.writeString("hello"); 7032 if (proxy != undefined) { 7033 let ret: boolean = proxy.sendRequest(1, data, reply, option); 7034 if (ret) { 7035 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7036 let msg = reply.readString(); 7037 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7038 } else { 7039 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 7040 } 7041 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7042 data.reclaim(); 7043 reply.reclaim(); 7044 } 7045 ``` 7046 7047### sendMessageRequest<sup>9+</sup> 7048 7049sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7050 7051以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 7052 7053**系统能力**:SystemCapability.Communication.IPC.Core 7054 7055**参数:** 7056 7057 | 参数名 | 类型 | 必填 | 说明 | 7058 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7059 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7060 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7061 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7062 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7063 7064**返回值:** 7065 7066 | 类型 | 说明 | 7067 | ---------------------------- | ----------------------------------------- | 7068 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 7069 7070 7071**错误码:** 7072 7073以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7074 7075 | 错误码ID | 错误信息 | 7076 | -------- | -------- | 7077 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain the passed object instance. | 7078 7079**示例:** 7080 7081 ```ts 7082 // FA模型需要从@kit.AbilityKit导入featureAbility 7083 // import { featureAbility } from '@kit.AbilityKit'; 7084 import { Want, common } from '@kit.AbilityKit'; 7085 import { hilog } from '@kit.PerformanceAnalysisKit'; 7086 7087 let proxy: rpc.IRemoteObject | undefined; 7088 let connect: common.ConnectOptions = { 7089 onConnect: (elementName, remoteProxy) => { 7090 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7091 proxy = remoteProxy; 7092 }, 7093 onDisconnect: (elementName) => { 7094 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7095 }, 7096 onFailed: () => { 7097 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7098 } 7099 }; 7100 let want: Want = { 7101 bundleName: "com.ohos.server", 7102 abilityName: "com.ohos.server.EntryAbility", 7103 }; 7104 7105 // FA模型使用此方法连接服务 7106 // FA.connectAbility(want,connect); 7107 7108 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7109 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7110 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7111 let connectionId = context.connectServiceExtensionAbility(want, connect); 7112 ``` 7113 7114 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 7115 7116 ```ts 7117 import { hilog } from '@kit.PerformanceAnalysisKit'; 7118 7119 let option = new rpc.MessageOption(); 7120 let data = rpc.MessageSequence.create(); 7121 let reply = rpc.MessageSequence.create(); 7122 data.writeInt(1); 7123 data.writeString("hello"); 7124 if (proxy != undefined) { 7125 proxy.sendMessageRequest(1, data, reply, option) 7126 .then((result: rpc.RequestResult) => { 7127 if (result.errCode === 0) { 7128 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7129 let num = result.reply.readInt(); 7130 let msg = result.reply.readString(); 7131 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7132 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7133 } else { 7134 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7135 } 7136 }).catch((e: Error) => { 7137 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 7138 }).finally (() => { 7139 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7140 data.reclaim(); 7141 reply.reclaim(); 7142 }); 7143 } 7144 ``` 7145 7146### sendRequest<sup>(deprecated)</sup> 7147 7148sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7149 7150以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7151 7152> **说明:** 7153> 7154> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 7155 7156**系统能力**:SystemCapability.Communication.IPC.Core 7157 7158**参数:** 7159 7160 | 参数名 | 类型 | 必填 | 说明 | 7161 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7162 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7163 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7164 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7165 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7166 7167**返回值:** 7168 7169| 类型 | 说明 | 7170| ------------------------------------------------------------ | --------------------------------------------- | 7171| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 7172 7173**示例:** 7174 7175 ```ts 7176 // FA模型需要从@kit.AbilityKit导入featureAbility 7177 // import { featureAbility } from '@kit.AbilityKit'; 7178 import { Want, common } from '@kit.AbilityKit'; 7179 import { hilog } from '@kit.PerformanceAnalysisKit'; 7180 7181 let proxy: rpc.IRemoteObject | undefined; 7182 let connect: common.ConnectOptions = { 7183 onConnect: (elementName, remoteProxy) => { 7184 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7185 proxy = remoteProxy; 7186 }, 7187 onDisconnect: (elementName) => { 7188 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7189 }, 7190 onFailed: () => { 7191 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7192 } 7193 }; 7194 let want: Want = { 7195 bundleName: "com.ohos.server", 7196 abilityName: "com.ohos.server.EntryAbility", 7197 }; 7198 7199 // FA模型使用此方法连接服务 7200 // FA.connectAbility(want,connect); 7201 7202 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7203 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7204 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7205 let connectionId = context.connectServiceExtensionAbility(want, connect); 7206 ``` 7207 7208 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7209 7210 ```ts 7211 import { hilog } from '@kit.PerformanceAnalysisKit'; 7212 7213 let option = new rpc.MessageOption(); 7214 let data = rpc.MessageParcel.create(); 7215 let reply = rpc.MessageParcel.create(); 7216 data.writeInt(1); 7217 data.writeString("hello"); 7218 if (proxy != undefined) { 7219 let a = proxy.sendRequest(1, data, reply, option) as Object; 7220 let b = a as Promise<rpc.SendRequestResult>; 7221 b.then((result: rpc.SendRequestResult) => { 7222 if (result.errCode === 0) { 7223 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7224 let num = result.reply.readInt(); 7225 let msg = result.reply.readString(); 7226 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7227 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7228 } else { 7229 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7230 } 7231 }).catch((e: Error) => { 7232 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 7233 }).finally (() => { 7234 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7235 data.reclaim(); 7236 reply.reclaim(); 7237 }); 7238 } 7239 ``` 7240 7241### sendMessageRequest<sup>9+</sup> 7242 7243sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7244 7245以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回后的某个时机执行回调,回复内容在RequestResult的reply报文里。 7246 7247**系统能力**:SystemCapability.Communication.IPC.Core 7248 7249**参数:** 7250 7251 | 参数名 | 类型 | 必填 | 说明 | 7252 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7253 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7254 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7255 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7256 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7257 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 7258 7259 7260**错误码:** 7261 7262以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7263 7264 | 错误码ID | 错误信息 | 7265 | -------- | -------- | 7266 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain the passed object instance. | 7267 7268**示例:** 7269 7270 ```ts 7271 // FA模型需要从@kit.AbilityKit导入featureAbility 7272 // import { featureAbility } from '@kit.AbilityKit'; 7273 import { Want, common } from '@kit.AbilityKit'; 7274 import { hilog } from '@kit.PerformanceAnalysisKit'; 7275 import { BusinessError } from '@kit.BasicServicesKit'; 7276 7277 let proxy: rpc.IRemoteObject | undefined; 7278 let connect: common.ConnectOptions = { 7279 onConnect: (elementName, remoteProxy) => { 7280 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7281 proxy = remoteProxy; 7282 }, 7283 onDisconnect: (elementName) => { 7284 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7285 }, 7286 onFailed: () => { 7287 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7288 } 7289 }; 7290 let want: Want = { 7291 bundleName: "com.ohos.server", 7292 abilityName: "com.ohos.server.EntryAbility", 7293 }; 7294 function sendMessageRequestCallback(err: BusinessError, result: rpc.RequestResult) { 7295 if (result.errCode === 0) { 7296 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7297 let num = result.reply.readInt(); 7298 let msg = result.reply.readString(); 7299 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7300 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7301 } else { 7302 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7303 } 7304 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7305 result.data.reclaim(); 7306 result.reply.reclaim(); 7307} 7308 7309 // FA模型使用此方法连接服务 7310 // FA.connectAbility(want,connect); 7311 7312 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7313 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7314 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7315 let connectionId = context.connectServiceExtensionAbility(want, connect); 7316 ``` 7317 7318 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 7319 7320 ```ts 7321 import { hilog } from '@kit.PerformanceAnalysisKit'; 7322 import { BusinessError } from '@kit.BasicServicesKit'; 7323 7324 let option = new rpc.MessageOption(); 7325 let data = rpc.MessageSequence.create(); 7326 let reply = rpc.MessageSequence.create(); 7327 data.writeInt(1); 7328 data.writeString("hello"); 7329 if (proxy != undefined) { 7330 try { 7331 proxy.sendMessageRequest(1, data, reply, option, sendMessageRequestCallback); 7332 } catch (error) { 7333 let e: BusinessError = error as BusinessError; 7334 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorCode ' + e.code); 7335 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorMessage ' + e.message); 7336 } 7337 } 7338 ``` 7339 7340### sendRequest<sup>(deprecated)</sup> 7341 7342sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7343 7344以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 7345 7346> **说明:** 7347> 7348> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-3)替代。 7349 7350**系统能力**:SystemCapability.Communication.IPC.Core 7351 7352**参数:** 7353 7354| 参数名 | 类型 | 必填 | 说明 | 7355| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7356| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7357| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7358| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7359| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7360| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 7361 7362**示例:** 7363 7364 ```ts 7365 // FA模型需要从@kit.AbilityKit导入featureAbility 7366 // import { featureAbility } from '@kit.AbilityKit'; 7367 import { Want, common } from '@kit.AbilityKit'; 7368 import { hilog } from '@kit.PerformanceAnalysisKit'; 7369 import { BusinessError } from '@kit.BasicServicesKit'; 7370 7371 let proxy: rpc.IRemoteObject | undefined; 7372 let connect: common.ConnectOptions = { 7373 onConnect: (elementName, remoteProxy) => { 7374 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7375 proxy = remoteProxy; 7376 }, 7377 onDisconnect: (elementName) => { 7378 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7379 }, 7380 onFailed: () => { 7381 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7382 } 7383 }; 7384 let want: Want = { 7385 bundleName: "com.ohos.server", 7386 abilityName: "com.ohos.server.EntryAbility", 7387 }; 7388 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 7389 if (result.errCode === 0) { 7390 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7391 let num = result.reply.readInt(); 7392 let msg = result.reply.readString(); 7393 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7394 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7395 } else { 7396 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7397 } 7398 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7399 result.data.reclaim(); 7400 result.reply.reclaim(); 7401} 7402 7403 // FA模型使用此方法连接服务 7404 // FA.connectAbility(want,connect); 7405 7406 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7407 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7408 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7409 let connectionId = context.connectServiceExtensionAbility(want, connect); 7410 ``` 7411 7412 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7413 7414 ```ts 7415 let option = new rpc.MessageOption(); 7416 let data = rpc.MessageParcel.create(); 7417 let reply = rpc.MessageParcel.create(); 7418 data.writeInt(1); 7419 data.writeString("hello"); 7420 if (proxy != undefined) { 7421 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 7422 } 7423 ``` 7424 7425### getLocalInterface<sup>9+</sup> 7426 7427getLocalInterface(interface: string): IRemoteBroker 7428 7429查询并获取当前接口描述符对应的本地接口对象。 7430 7431**系统能力**:SystemCapability.Communication.IPC.Core 7432 7433**参数:** 7434 7435 | 参数名 | 类型 | 必填 | 说明 | 7436 | --------- | ------ | ---- | ---------------------- | 7437 | interface | string | 是 | 需要查询的接口描述符。 | 7438 7439**返回值:** 7440 7441| 类型 | 说明 | 7442| ------------------------------- | ------------------------------------------ | 7443| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7444 7445**错误码:** 7446 7447以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7448 7449 | 错误码ID | 错误信息 | 7450 | -------- | -------- | 7451 | 401 | check param failed | 7452 | 1900006 | Operation allowed only for the remote object. | 7453 7454**示例:** 7455 7456 ```ts 7457 // FA模型需要从@kit.AbilityKit导入featureAbility 7458 // import { featureAbility } from '@kit.AbilityKit'; 7459 import { Want, common } from '@kit.AbilityKit'; 7460 import { hilog } from '@kit.PerformanceAnalysisKit'; 7461 7462 let proxy: rpc.IRemoteObject | undefined; 7463 let connect: common.ConnectOptions = { 7464 onConnect: (elementName, remoteProxy) => { 7465 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7466 proxy = remoteProxy; 7467 }, 7468 onDisconnect: (elementName) => { 7469 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7470 }, 7471 onFailed: () => { 7472 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7473 } 7474 }; 7475 let want: Want = { 7476 bundleName: "com.ohos.server", 7477 abilityName: "com.ohos.server.EntryAbility", 7478 }; 7479 7480 // FA模型使用此方法连接服务 7481 // FA.connectAbility(want,connect); 7482 7483 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7484 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7485 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7486 let connectionId = context.connectServiceExtensionAbility(want, connect); 7487 ``` 7488 7489 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象 7490 7491 ```ts 7492 import { hilog } from '@kit.PerformanceAnalysisKit'; 7493 import { BusinessError } from '@kit.BasicServicesKit'; 7494 7495 if (proxy != undefined) { 7496 try { 7497 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7498 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7499 } catch (error) { 7500 let e: BusinessError = error as BusinessError; 7501 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7502 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7503 } 7504 } 7505 ``` 7506 7507### queryLocalInterface<sup>(deprecated)</sup> 7508 7509queryLocalInterface(interface: string): IRemoteBroker 7510 7511查询并获取当前接口描述符对应的本地接口对象。 7512 7513> **说明:** 7514> 7515> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-1)替代。 7516 7517**系统能力**:SystemCapability.Communication.IPC.Core 7518 7519**参数:** 7520 7521 | 参数名 | 类型 | 必填 | 说明 | 7522 | --------- | ------ | ---- | ---------------------- | 7523 | interface | string | 是 | 需要查询的接口描述符。 | 7524 7525**返回值:** 7526 7527| 类型 | 说明 | 7528| ------------------------------- | ------------------------------------------ | 7529| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7530 7531**示例:** 7532 7533 ```ts 7534 // FA模型需要从@kit.AbilityKit导入featureAbility 7535 // import { featureAbility } from '@kit.AbilityKit'; 7536 import { Want, common } from '@kit.AbilityKit'; 7537 import { hilog } from '@kit.PerformanceAnalysisKit'; 7538 7539 let proxy: rpc.IRemoteObject | undefined; 7540 let connect: common.ConnectOptions = { 7541 onConnect: (elementName, remoteProxy) => { 7542 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7543 proxy = remoteProxy; 7544 }, 7545 onDisconnect: (elementName) => { 7546 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7547 }, 7548 onFailed: () => { 7549 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7550 } 7551 }; 7552 let want: Want = { 7553 bundleName: "com.ohos.server", 7554 abilityName: "com.ohos.server.EntryAbility", 7555 }; 7556 7557 // FA模型使用此方法连接服务 7558 // FA.connectAbility(want,connect); 7559 7560 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7561 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7562 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7563 let connectionId = context.connectServiceExtensionAbility(want, connect); 7564 ``` 7565 7566 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象 7567 7568 ```ts 7569 import { hilog } from '@kit.PerformanceAnalysisKit'; 7570 7571 if (proxy != undefined) { 7572 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7573 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7574 } 7575 ``` 7576 7577### registerDeathRecipient<sup>9+</sup> 7578 7579registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7580 7581注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7582 7583**系统能力**:SystemCapability.Communication.IPC.Core 7584 7585**参数:** 7586 7587 | 参数名 | 类型 | 必填 | 说明 | 7588 | --------- | --------------------------------- | ---- | -------------- | 7589 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7590 | flags | number | 是 | 死亡通知标志。 | 7591 7592**错误码:** 7593 7594以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7595 7596 | 错误码ID | 错误信息 | 7597 | -------- | -------- | 7598 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The callback used to receive remote object death notifications is empty. | 7599 | 1900008 | The proxy or remote object is invalid. | 7600 7601**示例:** 7602 7603 ```ts 7604 // FA模型需要从@kit.AbilityKit导入featureAbility 7605 // import { featureAbility } from '@kit.AbilityKit'; 7606 import { Want, common } from '@kit.AbilityKit'; 7607 import { hilog } from '@kit.PerformanceAnalysisKit'; 7608 7609 let proxy: rpc.IRemoteObject | undefined; 7610 let connect: common.ConnectOptions = { 7611 onConnect: (elementName, remoteProxy) => { 7612 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7613 proxy = remoteProxy; 7614 }, 7615 onDisconnect: (elementName) => { 7616 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7617 }, 7618 onFailed: () => { 7619 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7620 } 7621 }; 7622 let want: Want = { 7623 bundleName: "com.ohos.server", 7624 abilityName: "com.ohos.server.EntryAbility", 7625 }; 7626 7627 // FA模型使用此方法连接服务 7628 // FA.connectAbility(want,connect); 7629 7630 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7631 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7632 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7633 let connectionId = context.connectServiceExtensionAbility(want, connect); 7634 ``` 7635 7636 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调 7637 7638 ```ts 7639 import { hilog } from '@kit.PerformanceAnalysisKit'; 7640 import { BusinessError } from '@kit.BasicServicesKit'; 7641 7642 class MyDeathRecipient implements rpc.DeathRecipient { 7643 onRemoteDied() { 7644 hilog.info(0x0000, 'testTag', 'server died'); 7645 } 7646 } 7647 let deathRecipient = new MyDeathRecipient(); 7648 if (proxy != undefined) { 7649 try { 7650 proxy.registerDeathRecipient(deathRecipient, 0); 7651 } catch (error) { 7652 let e: BusinessError = error as BusinessError; 7653 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7654 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7655 } 7656 } 7657 ``` 7658 7659### addDeathRecipient<sup>(deprecated)</sup> 7660 7661addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7662 7663注册用于接收远程对象死亡通知的回调,增加proxy对象上的死亡通知。 7664 7665> **说明:** 7666> 7667> 从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9-1)类替代。 7668 7669**系统能力**:SystemCapability.Communication.IPC.Core 7670 7671**参数:** 7672 7673 | 参数名 | 类型 | 必填 | 说明 | 7674 | --------- | --------------------------------- | ---- | --------------------------------- | 7675 | recipient | [DeathRecipient](#deathrecipient) | 是 | 收件人表示要注册的回调。 | 7676 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7677 7678**返回值:** 7679 7680 | 类型 | 说明 | 7681 | ------- | ---------------------------------------- | 7682 | boolean | true:回调注册成功,false:回调注册失败。| 7683 7684**示例:** 7685 7686 ```ts 7687 // FA模型需要从@kit.AbilityKit导入featureAbility 7688 // import { featureAbility } from '@kit.AbilityKit'; 7689 import { Want, common } from '@kit.AbilityKit'; 7690 import { hilog } from '@kit.PerformanceAnalysisKit'; 7691 7692 let proxy: rpc.IRemoteObject | undefined; 7693 let connect: common.ConnectOptions = { 7694 onConnect: (elementName, remoteProxy) => { 7695 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7696 proxy = remoteProxy; 7697 }, 7698 onDisconnect: (elementName) => { 7699 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7700 }, 7701 onFailed: () => { 7702 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7703 } 7704 }; 7705 let want: Want = { 7706 bundleName: "com.ohos.server", 7707 abilityName: "com.ohos.server.EntryAbility", 7708 }; 7709 7710 // FA模型使用此方法连接服务 7711 // FA.connectAbility(want,connect); 7712 7713 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7714 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7715 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7716 let connectionId = context.connectServiceExtensionAbility(want, connect); 7717 ``` 7718 7719 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecipient接口方法新增死亡回调 7720 7721 ```ts 7722 import { hilog } from '@kit.PerformanceAnalysisKit'; 7723 7724 class MyDeathRecipient implements rpc.DeathRecipient { 7725 onRemoteDied() { 7726 hilog.info(0x0000, 'testTag', 'server died'); 7727 } 7728 } 7729 let deathRecipient = new MyDeathRecipient(); 7730 if (proxy != undefined) { 7731 proxy.addDeathRecipient(deathRecipient, 0); 7732 } 7733 ``` 7734 7735### unregisterDeathRecipient<sup>9+</sup> 7736 7737unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7738 7739注销用于接收远程对象死亡通知的回调。 7740 7741**系统能力**:SystemCapability.Communication.IPC.Core 7742 7743**参数:** 7744 7745 | 参数名 | 类型 | 必填 | 说明 | 7746 | --------- | --------------------------------- | ---- | -------------- | 7747 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 7748 | flags | number | 是 | 死亡通知标志。 | 7749 7750**错误码:** 7751 7752以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7753 7754 | 错误码ID | 错误信息 | 7755 | -------- | -------- | 7756 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The callback used to receive remote object death notifications is empty. | 7757 | 1900008 | The proxy or remote object is invalid. | 7758 7759**示例:** 7760 7761 ```ts 7762 // FA模型需要从@kit.AbilityKit导入featureAbility 7763 // import { featureAbility } from '@kit.AbilityKit'; 7764 import { Want, common } from '@kit.AbilityKit'; 7765 import { hilog } from '@kit.PerformanceAnalysisKit'; 7766 7767 let proxy: rpc.IRemoteObject | undefined; 7768 let connect: common.ConnectOptions = { 7769 onConnect: (elementName, remoteProxy) => { 7770 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7771 proxy = remoteProxy; 7772 }, 7773 onDisconnect: (elementName) => { 7774 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7775 }, 7776 onFailed: () => { 7777 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7778 } 7779 }; 7780 let want: Want = { 7781 bundleName: "com.ohos.server", 7782 abilityName: "com.ohos.server.EntryAbility", 7783 }; 7784 7785 // FA模型使用此方法连接服务 7786 // FA.connectAbility(want,connect); 7787 7788 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7789 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7790 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7791 let connectionId = context.connectServiceExtensionAbility(want, connect); 7792 ``` 7793 7794 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 7795 7796 ```ts 7797 import { hilog } from '@kit.PerformanceAnalysisKit'; 7798 import { BusinessError } from '@kit.BasicServicesKit'; 7799 7800 class MyDeathRecipient implements rpc.DeathRecipient { 7801 onRemoteDied() { 7802 hilog.info(0x0000, 'testTag', 'server died'); 7803 } 7804 } 7805 let deathRecipient = new MyDeathRecipient(); 7806 if (proxy != undefined) { 7807 try { 7808 proxy.registerDeathRecipient(deathRecipient, 0); 7809 proxy.unregisterDeathRecipient(deathRecipient, 0); 7810 } catch (error) { 7811 let e: BusinessError = error as BusinessError; 7812 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7813 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7814 } 7815 } 7816 ``` 7817 7818### removeDeathRecipient<sup>(deprecated)</sup> 7819 7820removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7821 7822注销用于接收远程对象死亡通知的回调。 7823 7824> **说明:** 7825> 7826> 从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9-1)替代。 7827 7828**系统能力**:SystemCapability.Communication.IPC.Core 7829 7830**参数:** 7831 7832 | 参数名 | 类型 | 必填 | 说明 | 7833 | --------- | --------------------------------- | ---- | --------------------------------- | 7834 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的死亡回调。 | 7835 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7836 7837**返回值:** 7838 7839 | 类型 | 说明 | 7840 | ------- | ---------------------------------------- | 7841 | boolean | true:回调注销成功,false:回调注销失败。| 7842 7843**示例:** 7844 7845 ```ts 7846 // FA模型需要从@kit.AbilityKit导入featureAbility 7847 // import { featureAbility } from '@kit.AbilityKit'; 7848 import { Want, common } from '@kit.AbilityKit'; 7849 import { hilog } from '@kit.PerformanceAnalysisKit'; 7850 7851 let proxy: rpc.IRemoteObject | undefined; 7852 let connect: common.ConnectOptions = { 7853 onConnect: (elementName, remoteProxy) => { 7854 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7855 proxy = remoteProxy; 7856 }, 7857 onDisconnect: (elementName) => { 7858 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7859 }, 7860 onFailed: () => { 7861 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7862 } 7863 }; 7864 let want: Want = { 7865 bundleName: "com.ohos.server", 7866 abilityName: "com.ohos.server.EntryAbility", 7867 }; 7868 7869 // FA模型使用此方法连接服务 7870 // FA.connectAbility(want,connect); 7871 7872 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7873 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7874 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7875 let connectionId = context.connectServiceExtensionAbility(want, connect); 7876 ``` 7877 7878 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调 7879 7880 ```ts 7881 import { hilog } from '@kit.PerformanceAnalysisKit'; 7882 7883 class MyDeathRecipient implements rpc.DeathRecipient { 7884 onRemoteDied() { 7885 hilog.info(0x0000, 'testTag', 'server died'); 7886 } 7887 } 7888 let deathRecipient = new MyDeathRecipient(); 7889 if (proxy != undefined) { 7890 proxy.addDeathRecipient(deathRecipient, 0); 7891 proxy.removeDeathRecipient(deathRecipient, 0); 7892 } 7893 ``` 7894 7895### getDescriptor<sup>9+</sup> 7896 7897getDescriptor(): string 7898 7899获取对象的接口描述符,接口描述符为字符串。 7900 7901**系统能力**:SystemCapability.Communication.IPC.Core 7902 7903**返回值:** 7904 7905 | 类型 | 说明 | 7906 | ------ | ---------------- | 7907 | string | 返回接口描述符。 | 7908 7909**错误码:** 7910 7911以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7912 7913 | 错误码ID | 错误信息 | 7914 | -------- | -------- | 7915 | 1900007 | communication failed. | 7916 | 1900008 | The proxy or remote object is invalid. | 7917 7918**示例:** 7919 7920 ```ts 7921 // FA模型需要从@kit.AbilityKit导入featureAbility 7922 // import { featureAbility } from '@kit.AbilityKit'; 7923 import { Want, common } from '@kit.AbilityKit'; 7924 import { hilog } from '@kit.PerformanceAnalysisKit'; 7925 7926 let proxy: rpc.IRemoteObject | undefined; 7927 let connect: common.ConnectOptions = { 7928 onConnect: (elementName, remoteProxy) => { 7929 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7930 proxy = remoteProxy; 7931 }, 7932 onDisconnect: (elementName) => { 7933 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7934 }, 7935 onFailed: () => { 7936 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7937 } 7938 }; 7939 let want: Want = { 7940 bundleName: "com.ohos.server", 7941 abilityName: "com.ohos.server.EntryAbility", 7942 }; 7943 7944 // FA模型使用此方法连接服务 7945 // FA.connectAbility(want,connect); 7946 7947 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7948 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7949 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7950 let connectionId = context.connectServiceExtensionAbility(want, connect); 7951 ``` 7952 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符 7953 7954 ```ts 7955 import { hilog } from '@kit.PerformanceAnalysisKit'; 7956 import { BusinessError } from '@kit.BasicServicesKit'; 7957 7958 if (proxy != undefined) { 7959 try { 7960 let descriptor: string = proxy.getDescriptor(); 7961 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7962 } catch (error) { 7963 let e: BusinessError = error as BusinessError; 7964 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7965 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7966 } 7967 } 7968 ``` 7969 7970### getInterfaceDescriptor<sup>(deprecated)</sup> 7971 7972getInterfaceDescriptor(): string 7973 7974查询当前代理对象接口的描述符。 7975 7976> **说明:** 7977> 7978> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-1)替代。 7979 7980**系统能力**:SystemCapability.Communication.IPC.Core 7981 7982**返回值:** 7983 7984 | 类型 | 说明 | 7985 | ------ | ------------------ | 7986 | string | 当前的接口描述符。 | 7987 7988**示例:** 7989 7990 ```ts 7991 // FA模型需要从@kit.AbilityKit导入featureAbility 7992 // import { featureAbility } from '@kit.AbilityKit'; 7993 import { Want, common } from '@kit.AbilityKit'; 7994 import { hilog } from '@kit.PerformanceAnalysisKit'; 7995 7996 let proxy: rpc.IRemoteObject | undefined; 7997 let connect: common.ConnectOptions = { 7998 onConnect: (elementName, remoteProxy) => { 7999 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8000 proxy = remoteProxy; 8001 }, 8002 onDisconnect: (elementName) => { 8003 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8004 }, 8005 onFailed: () => { 8006 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8007 } 8008 }; 8009 let want: Want = { 8010 bundleName: "com.ohos.server", 8011 abilityName: "com.ohos.server.EntryAbility", 8012 }; 8013 8014 // FA模型使用此方法连接服务 8015 // FA.connectAbility(want,connect); 8016 8017 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8018 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 8019 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8020 let connectionId = context.connectServiceExtensionAbility(want, connect); 8021 ``` 8022 8023 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符 8024 8025 ```ts 8026 import { hilog } from '@kit.PerformanceAnalysisKit'; 8027 8028 if (proxy != undefined) { 8029 let descriptor: string = proxy.getInterfaceDescriptor(); 8030 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 8031 } 8032 ``` 8033 8034### isObjectDead 8035 8036isObjectDead(): boolean 8037 8038指示对应的RemoteObject是否死亡。 8039 8040**系统能力**:SystemCapability.Communication.IPC.Core 8041 8042**返回值:** 8043 8044 | 类型 | 说明 | 8045 | ------- | ------------------------------------------------- | 8046 | boolean | true:对应的对象已经死亡,false:对应的对象未死亡。 | 8047 8048**示例:** 8049 8050 ```ts 8051 // FA模型需要从@kit.AbilityKit导入featureAbility 8052 // import { featureAbility } from '@kit.AbilityKit'; 8053 import { Want, common } from '@kit.AbilityKit'; 8054 import { hilog } from '@kit.PerformanceAnalysisKit'; 8055 8056 let proxy: rpc.IRemoteObject | undefined; 8057 let connect: common.ConnectOptions = { 8058 onConnect: (elementName, remoteProxy) => { 8059 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8060 proxy = remoteProxy; 8061 }, 8062 onDisconnect: (elementName) => { 8063 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8064 }, 8065 onFailed: () => { 8066 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8067 } 8068 }; 8069 let want: Want = { 8070 bundleName: "com.ohos.server", 8071 abilityName: "com.ohos.server.EntryAbility", 8072 }; 8073 8074 // FA模型使用此方法连接服务 8075 // FA.connectAbility(want,connect); 8076 8077 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8078 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 8079 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8080 let connectionId = context.connectServiceExtensionAbility(want, connect); 8081 ``` 8082 8083 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡 8084 8085 ```ts 8086 import { hilog } from '@kit.PerformanceAnalysisKit'; 8087 8088 if (proxy != undefined) { 8089 let isDead: boolean = proxy.isObjectDead(); 8090 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 8091 } 8092 ``` 8093 8094## MessageOption 8095 8096公共消息选项,使用指定的标志类型,构造指定的MessageOption对象。 8097 8098### 属性 8099 8100**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 8101 8102 | 名称 | 类型 | 可读 | 可写 | 说明 | 8103 | ------------- | ------------------------------- | ----- | ----- | ------------------------------------------------------------------------ | 8104 | TF_SYNC | [MessageOption](#messageoption) | 是 | 否 | 同步调用标识。 | 8105 | TF_ASYNC | [MessageOption](#messageoption) | 是 | 否 | 异步调用标识。 | 8106 | TF_ACCEPT_FDS | [MessageOption](#messageoption) | 是 | 否 | 指示sendMessageRequest<sup>9+</sup>接口可以传递文件描述符。 | 8107 | TF_WAIT_TIME | [MessageOption](#messageoption) | 是 | 是 | RPC等待时间(单位/秒),IPC场景下无效。默认等待为8秒(不建议修改等待时间)。 | 8108 8109### constructor<sup>9+</sup> 8110 8111constructor(async?: boolean) 8112 8113MessageOption构造函数。 8114 8115**系统能力**:SystemCapability.Communication.IPC.Core 8116 8117**参数:** 8118 8119| 参数名 | 类型 | 必填 | 说明 | 8120| ------ | ------- | ---- | ------------------------------------------------------------ | 8121| async | boolean | 否 | true:表示异步调用标志,false:表示同步调用标志。默认同步调用。 | 8122 8123**示例:** 8124 8125 ```ts 8126 class TestRemoteObject extends rpc.MessageOption { 8127 constructor(async: boolean) { 8128 super(async); 8129 } 8130 } 8131 ``` 8132 8133### constructor 8134 8135constructor(syncFlags?: number, waitTime?: number) 8136 8137MessageOption构造函数。 8138 8139**系统能力**:SystemCapability.Communication.IPC.Core 8140 8141**参数:** 8142 8143 | 参数名 | 类型 | 必填 | 说明 | 8144 | --------- | ------ | ---- | --------------------------------------------- | 8145 | syncFlags | number | 否 | 同步调用或异步调用标志。默认同步调用。 | 8146 | waitTime | number | 否 | 调用rpc最长等待时间。默认TF_WAIT_TIME。 | 8147 8148**示例:** 8149 8150 ```ts 8151 class TestRemoteObject extends rpc.MessageOption { 8152 constructor(syncFlags?: number,waitTime?: number) { 8153 super(syncFlags,waitTime); 8154 } 8155 } 8156 ``` 8157### isAsync<sup>9+</sup> 8158 8159isAsync(): boolean 8160 8161获取SendMessageRequest调用中确定同步或是异步的标志。 8162 8163**系统能力**:SystemCapability.Communication.IPC.Core 8164 8165**返回值:** 8166 8167 | 类型 | 说明 | 8168 | ------- | ---------------------------------------- | 8169 | boolean | true:异步调用成功,false:同步调用成功。| 8170 8171**示例:** 8172 8173 ```ts 8174 let option = new rpc.MessageOption(); 8175 option.isAsync(); 8176 ``` 8177 8178### setAsync<sup>9+</sup> 8179 8180setAsync(async: boolean): void 8181 8182设置SendMessageRequest调用中确定同步或是异步的标志。 8183 8184**系统能力**:SystemCapability.Communication.IPC.Core 8185 8186**参数:** 8187 8188| 参数名 | 类型 | 必填 | 说明 | 8189| ------ | ------- | ---- | ------------------------------------------------- | 8190| async | boolean | 是 | true:表示异步调用标志,false:表示同步调用标志。 | 8191 8192**示例:** 8193 8194 ```ts 8195 import { hilog } from '@kit.PerformanceAnalysisKit'; 8196 8197 let option = new rpc.MessageOption(); 8198 option.setAsync(true); 8199 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 8200 ``` 8201 8202### getFlags 8203 8204getFlags(): number 8205 8206获取同步调用或异步调用标志。 8207 8208**系统能力**:SystemCapability.Communication.IPC.Core 8209 8210**返回值:** 8211 8212 | 类型 | 说明 | 8213 | ------ | ------------------------------------ | 8214 | number | 调用成功返回同步调用或异步调用标志。 | 8215 8216**示例:** 8217 8218 ```ts 8219 import { hilog } from '@kit.PerformanceAnalysisKit'; 8220 8221 try { 8222 let option = new rpc.MessageOption(); 8223 hilog.info(0x0000, 'testTag', 'create object successfully'); 8224 let flog = option.getFlags(); 8225 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8226 option.setFlags(1) 8227 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8228 let flog2 = option.getFlags(); 8229 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 8230 } catch (error) { 8231 hilog.error(0x0000, 'testTag', 'error ' + error); 8232 } 8233 ``` 8234 8235### setFlags 8236 8237setFlags(flags: number): void 8238 8239设置同步调用或异步调用标志。 8240 8241**系统能力**:SystemCapability.Communication.IPC.Core 8242 8243**参数:** 8244 8245 | 参数名 | 类型 | 必填 | 说明 | 8246 | ------ | ------ | ---- | ------------------------ | 8247 | flags | number | 是 | 同步调用或异步调用标志。 | 8248 8249**示例:** 8250 8251 ```ts 8252 import { hilog } from '@kit.PerformanceAnalysisKit'; 8253 8254 try { 8255 let option = new rpc.MessageOption(); 8256 option.setFlags(1) 8257 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8258 let flog = option.getFlags(); 8259 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8260 } catch (error) { 8261 hilog.error(0x0000, 'testTag', 'error ' + error); 8262 } 8263 ``` 8264 8265### getWaitTime 8266 8267getWaitTime(): number 8268 8269获取rpc调用的最长等待时间。 8270 8271**系统能力**:SystemCapability.Communication.IPC.Core 8272 8273**返回值:** 8274 8275 | 类型 | 说明 | 8276 | ------ | ----------------- | 8277 | number | rpc最长等待时间。 | 8278 8279**示例:** 8280 8281 ```ts 8282 import { hilog } from '@kit.PerformanceAnalysisKit'; 8283 8284 try { 8285 let option = new rpc.MessageOption(); 8286 let time = option.getWaitTime(); 8287 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8288 option.setWaitTime(16); 8289 let time2 = option.getWaitTime(); 8290 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8291 } catch (error) { 8292 hilog.error(0x0000, 'testTag', 'error ' + error); 8293 } 8294 ``` 8295 8296### setWaitTime 8297 8298setWaitTime(waitTime: number): void 8299 8300设置rpc调用最长等待时间。 8301 8302**系统能力**:SystemCapability.Communication.IPC.Core 8303 8304**参数:** 8305 8306 | 参数名 | 类型 | 必填 | 说明 | 8307 | -------- | ------ | ---- | --------------------- | 8308 | waitTime | number | 是 | rpc调用最长等待时间,上限为3000秒。 | 8309 8310**示例:** 8311 8312 ```ts 8313 import { hilog } from '@kit.PerformanceAnalysisKit'; 8314 8315 try { 8316 let option = new rpc.MessageOption(); 8317 option.setWaitTime(16); 8318 let time = option.getWaitTime(); 8319 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8320 } catch (error) { 8321 hilog.error(0x0000, 'testTag', 'error ' + error); 8322 } 8323 ``` 8324 8325## IPCSkeleton 8326 8327用于获取IPC上下文信息,包括获取UID和PID、获取本端和对端设备ID、检查接口调用是否在同一设备上。 8328 8329### getContextObject 8330 8331static getContextObject(): IRemoteObject 8332 8333静态方法,获取系统能力的管理者。 8334 8335**系统能力**:SystemCapability.Communication.IPC.Core 8336 8337**返回值:** 8338 8339 | 类型 | 说明 | 8340 | ------------------------------- | -------------------- | 8341 | [IRemoteObject](#iremoteobject) | 返回系统能力管理者。 | 8342 8343**示例:** 8344 8345 ```ts 8346 import { hilog } from '@kit.PerformanceAnalysisKit'; 8347 8348 let samgr = rpc.IPCSkeleton.getContextObject(); 8349 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8350 ``` 8351 8352### getCallingPid 8353 8354static getCallingPid(): number 8355 8356静态方法,获取调用者的PID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的PID。 8357 8358**系统能力**:SystemCapability.Communication.IPC.Core 8359 8360**返回值:** 8361 8362 | 类型 | 说明 | 8363 | ------ | ----------------- | 8364 | number | 返回调用者的PID。 | 8365 8366**示例:** 8367 8368 ```ts 8369 import { hilog } from '@kit.PerformanceAnalysisKit'; 8370 8371 class Stub extends rpc.RemoteObject { 8372 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8373 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8374 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8375 return true; 8376 } 8377 } 8378 ``` 8379 8380### getCallingUid 8381 8382static getCallingUid(): number 8383 8384静态方法,获取调用者的UID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的UID。 8385 8386**系统能力**:SystemCapability.Communication.IPC.Core 8387 8388**返回值:** 8389 8390 | 类型 | 说明 | 8391 | ------ | ----------------- | 8392 | number | 返回调用者的UID。 | 8393 8394**示例:** 8395 8396 ```ts 8397 import { hilog } from '@kit.PerformanceAnalysisKit'; 8398 8399 class Stub extends rpc.RemoteObject { 8400 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8401 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8402 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8403 return true; 8404 } 8405 } 8406 ``` 8407 8408### getCallingTokenId<sup>8+</sup> 8409 8410static getCallingTokenId(): number 8411 8412静态方法,获取调用者的TokenId,用于被调用方对调用方的身份校验。 8413 8414**系统能力**:SystemCapability.Communication.IPC.Core 8415 8416**返回值:** 8417 8418 | 类型 | 说明 | 8419 | ------ | --------------------- | 8420 | number | 返回调用者的TokenId。 | 8421 8422**示例:** 8423 8424 ```ts 8425 import { hilog } from '@kit.PerformanceAnalysisKit'; 8426 8427 class Stub extends rpc.RemoteObject { 8428 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8429 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8430 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8431 return true; 8432 } 8433 } 8434 ``` 8435 8436### getCallingDeviceID 8437 8438static getCallingDeviceID(): string 8439 8440静态方法,获取调用者进程所在的设备ID。 8441 8442**系统能力**:SystemCapability.Communication.IPC.Core 8443 8444**返回值:** 8445 8446 | 类型 | 说明 | 8447 | ------ | ---------------------------- | 8448 | string | 返回调用者进程所在的设备ID。 | 8449 8450**示例:** 8451 8452 ```ts 8453 import { hilog } from '@kit.PerformanceAnalysisKit'; 8454 8455 class Stub extends rpc.RemoteObject { 8456 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8457 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8458 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8459 return true; 8460 } 8461 } 8462 ``` 8463 8464### getLocalDeviceID 8465 8466static getLocalDeviceID(): string 8467 8468静态方法,获取本端设备ID。 8469 8470**系统能力**:SystemCapability.Communication.IPC.Core 8471 8472**返回值:** 8473 8474 | 类型 | 说明 | 8475 | ------ | ------------------ | 8476 | string | 返回本地设备的ID。 | 8477 8478**示例:** 8479 8480 ```ts 8481 import { hilog } from '@kit.PerformanceAnalysisKit'; 8482 8483 class Stub extends rpc.RemoteObject { 8484 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8485 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8486 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8487 return true; 8488 } 8489 } 8490 ``` 8491 8492### isLocalCalling 8493 8494static isLocalCalling(): boolean 8495 8496静态方法,检查当前通信对端是否是本设备的进程。 8497 8498**系统能力**:SystemCapability.Communication.IPC.Core 8499 8500**返回值:** 8501 8502 | 类型 | 说明 | 8503 | ------- | -------------------------------------------------- | 8504 | boolean | true:调用在同一台设备,false:调用未在同一台设备。| 8505 8506**示例:** 8507 8508 ```ts 8509 import { hilog } from '@kit.PerformanceAnalysisKit'; 8510 8511 class Stub extends rpc.RemoteObject { 8512 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8513 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8514 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8515 return true; 8516 } 8517 } 8518 ``` 8519 8520### flushCmdBuffer<sup>9+</sup> 8521 8522static flushCmdBuffer(object: IRemoteObject): void 8523 8524静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8525 8526**系统能力**:SystemCapability.Communication.IPC.Core 8527 8528**参数:** 8529 8530 | 参数名 | 类型 | 必填 | 说明 | 8531 | ------ | ------------------------------- | ---- | ------------------- | 8532 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8533 8534**错误码:** 8535 8536以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8537 8538 | 错误码ID | 错误信息 | 8539 | -------- | -------- | 8540 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 8541 8542**示例:** 8543 8544 ```ts 8545 import { hilog } from '@kit.PerformanceAnalysisKit'; 8546 import { BusinessError } from '@kit.BasicServicesKit'; 8547 8548 class TestRemoteObject extends rpc.RemoteObject { 8549 constructor(descriptor: string) { 8550 super(descriptor); 8551 } 8552 } 8553 let remoteObject = new TestRemoteObject("aaa"); 8554 try { 8555 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8556 } catch (error) { 8557 let e: BusinessError = error as BusinessError; 8558 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8559 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8560 } 8561 ``` 8562 8563### flushCommands<sup>(deprecated)</sup> 8564 8565static flushCommands(object: IRemoteObject): number 8566 8567静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8568 8569> **说明:** 8570> 8571> 从API version 9 开始废弃,建议使用[flushCmdBuffer](#flushcmdbuffer9)替代。 8572 8573**系统能力**:SystemCapability.Communication.IPC.Core 8574 8575**参数:** 8576 8577 | 参数名 | 类型 | 必填 | 说明 | 8578 | ------ | ------------------------------- | ---- | ------------------- | 8579 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8580 8581**返回值:** 8582 8583 | 类型 | 说明 | 8584 | ------ | --------------------------------------------------------------------------------- | 8585 | number | 如果操作成功,返回0;如果输入对象为空或RemoteObject,或者操作失败,返回错误代码。 | 8586 8587**示例:** 8588 8589 ```ts 8590 import { hilog } from '@kit.PerformanceAnalysisKit'; 8591 8592 class MyDeathRecipient implements rpc.DeathRecipient { 8593 onRemoteDied() { 8594 hilog.info(0x0000, 'testTag', 'server died'); 8595 } 8596 } 8597 class TestRemoteObject extends rpc.RemoteObject { 8598 constructor(descriptor: string) { 8599 super(descriptor); 8600 } 8601 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8602 return true; 8603 } 8604 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8605 return true; 8606 } 8607 isObjectDead(): boolean { 8608 return false; 8609 } 8610 } 8611 let remoteObject = new TestRemoteObject("aaa"); 8612 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8613 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8614 ``` 8615 8616### resetCallingIdentity 8617 8618static resetCallingIdentity(): string 8619 8620静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。 8621 8622**系统能力**:SystemCapability.Communication.IPC.Core 8623 8624**返回值:** 8625 8626 | 类型 | 说明 | 8627 | ------ | ------------------------------------ | 8628 | string | 返回包含远程用户的UID和PID的字符串。 | 8629 8630**示例:** 8631 8632 ```ts 8633 import { hilog } from '@kit.PerformanceAnalysisKit'; 8634 8635 class Stub extends rpc.RemoteObject { 8636 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8637 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8638 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8639 return true; 8640 } 8641 } 8642 ``` 8643 8644### restoreCallingIdentity<sup>9+</sup> 8645 8646static restoreCallingIdentity(identity: string): void 8647 8648静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8649 8650**系统能力**:SystemCapability.Communication.IPC.Core 8651 8652**参数:** 8653 8654 | 参数名 | 类型 | 必填 | 说明 | 8655 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8656 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8657 8658**错误码:** 8659 8660以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8661 8662 | 错误码ID | 错误信息 | 8663 | -------- | -------- | 8664 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The string length exceeds 40960 bytes; <br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 8665 8666**示例:** 8667 8668 ```ts 8669 import { hilog } from '@kit.PerformanceAnalysisKit'; 8670 8671 class Stub extends rpc.RemoteObject { 8672 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8673 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8674 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8675 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8676 return true; 8677 } 8678 } 8679 ``` 8680 8681### setCallingIdentity<sup>(deprecated)</sup> 8682 8683static setCallingIdentity(identity: string): boolean 8684 8685静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8686 8687> **说明:** 8688> 8689> 从API version 9 开始废弃,建议使用[restoreCallingIdentity](#restorecallingidentity9)替代。 8690 8691**系统能力**:SystemCapability.Communication.IPC.Core 8692 8693**参数:** 8694 8695 | 参数名 | 类型 | 必填 | 说明 | 8696 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8697 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8698 8699**返回值:** 8700 8701 | 类型 | 说明 | 8702 | ------- | ---------------------------------| 8703 | boolean | true:设置成功,false:设置失败。| 8704 8705**示例:** 8706 8707 ```ts 8708 import { hilog } from '@kit.PerformanceAnalysisKit'; 8709 8710 class Stub extends rpc.RemoteObject { 8711 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8712 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8713 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8714 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8715 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8716 return true; 8717 } 8718 } 8719 ``` 8720 8721## RemoteObject 8722 8723实现远程对象。服务提供者必须继承此类。 8724 8725### constructor 8726 8727constructor(descriptor: string) 8728 8729RemoteObject构造函数。 8730 8731**系统能力**:SystemCapability.Communication.IPC.Core 8732 8733**参数:** 8734 8735 | 参数名 | 类型 | 必填 | 说明 | 8736 | ---------- | ------ | ---- | ------------ | 8737 | descriptor | string | 是 | 接口描述符。 | 8738 8739### sendRequest<sup>(deprecated)</sup> 8740 8741sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8742 8743以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8744 8745> **说明:** 8746> 8747> 从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 8748 8749**系统能力**:SystemCapability.Communication.IPC.Core 8750 8751**参数:** 8752 8753 | 参数名 | 类型 | 必填 | 说明 | 8754 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8755 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8756 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8757 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8758 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8759 8760**返回值:** 8761 8762 | 类型 | 说明 | 8763 | ------- | -------------------------------- | 8764 | boolean | true:发送成功,false:发送失败。| 8765 8766**示例:** 8767 8768 ```ts 8769 import { hilog } from '@kit.PerformanceAnalysisKit'; 8770 8771 class MyDeathRecipient implements rpc.DeathRecipient { 8772 onRemoteDied() { 8773 hilog.info(0x0000, 'testTag', 'server died'); 8774 } 8775 } 8776 class TestRemoteObject extends rpc.RemoteObject { 8777 constructor(descriptor: string) { 8778 super(descriptor); 8779 } 8780 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8781 return true; 8782 } 8783 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8784 return true; 8785 } 8786 isObjectDead(): boolean { 8787 return false; 8788 } 8789 } 8790 let testRemoteObject = new TestRemoteObject("testObject"); 8791 let option = new rpc.MessageOption(); 8792 let data = rpc.MessageParcel.create(); 8793 let reply = rpc.MessageParcel.create(); 8794 data.writeInt(1); 8795 data.writeString("hello"); 8796 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8797 if (ret) { 8798 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8799 let msg = reply.readString(); 8800 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8801 } else { 8802 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8803 } 8804 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8805 data.reclaim(); 8806 reply.reclaim(); 8807 ``` 8808 8809### sendMessageRequest<sup>9+</sup> 8810 8811sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8812 8813以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 8814 8815**系统能力**:SystemCapability.Communication.IPC.Core 8816 8817**参数:** 8818 8819 | 参数名 | 类型 | 必填 | 说明 | 8820 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8821 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8822 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 8823 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8824 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8825 8826**返回值:** 8827 8828| 类型 | 说明 | 8829| ----------------------------------------------- | ----------------------------------------- | 8830| Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是RequestResult实例。 | 8831 8832 8833**错误码:** 8834 8835以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8836 8837 | 错误码ID | 错误信息 | 8838 | -------- | -------- | 8839 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain the passed object instance. | 8840 8841**示例:** 8842 8843 ```ts 8844 import { hilog } from '@kit.PerformanceAnalysisKit'; 8845 8846 class TestRemoteObject extends rpc.RemoteObject { 8847 constructor(descriptor: string) { 8848 super(descriptor); 8849 } 8850 } 8851 let testRemoteObject = new TestRemoteObject("testObject"); 8852 let option = new rpc.MessageOption(); 8853 let data = rpc.MessageSequence.create(); 8854 let reply = rpc.MessageSequence.create(); 8855 data.writeInt(1); 8856 data.writeString("hello"); 8857 testRemoteObject.sendMessageRequest(1, data, reply, option) 8858 .then((result: rpc.RequestResult) => { 8859 if (result.errCode === 0) { 8860 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8861 let num = result.reply.readInt(); 8862 let msg = result.reply.readString(); 8863 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8864 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8865 } else { 8866 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8867 } 8868 }).catch((e: Error) => { 8869 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8870 }).finally (() => { 8871 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8872 data.reclaim(); 8873 reply.reclaim(); 8874 }); 8875 ``` 8876 8877### sendRequest<sup>(deprecated)</sup> 8878 8879sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8880 8881以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8882 8883> **说明:** 8884> 8885> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 8886 8887**系统能力**:SystemCapability.Communication.IPC.Core 8888 8889**参数:** 8890 8891 | 参数名 | 类型 | 必填 | 说明 | 8892 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8893 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8894 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8895 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8896 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8897 8898**返回值:** 8899 8900| 类型 | 说明 | 8901| ------------------------------------------------------------ | --------------------------------------------- | 8902| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 8903 8904**示例:** 8905 8906 ```ts 8907 import { hilog } from '@kit.PerformanceAnalysisKit'; 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 } 8918 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8919 return true; 8920 } 8921 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8922 return true; 8923 } 8924 isObjectDead(): boolean { 8925 return false; 8926 } 8927 } 8928 let testRemoteObject = new TestRemoteObject("testObject"); 8929 let option = new rpc.MessageOption(); 8930 let data = rpc.MessageParcel.create(); 8931 let reply = rpc.MessageParcel.create(); 8932 data.writeInt(1); 8933 data.writeString("hello"); 8934 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8935 let b = a as Promise<rpc.SendRequestResult>; 8936 b.then((result: rpc.SendRequestResult) => { 8937 if (result.errCode === 0) { 8938 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8939 let num = result.reply.readInt(); 8940 let msg = result.reply.readString(); 8941 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8942 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8943 } else { 8944 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8945 } 8946 }).catch((e: Error) => { 8947 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 8948 }).finally (() => { 8949 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8950 data.reclaim(); 8951 reply.reclaim(); 8952 }); 8953 ``` 8954 8955### sendMessageRequest<sup>9+</sup> 8956 8957sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8958 8959以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回时收到回调,回复内容在reply报文里。 8960 8961**系统能力**:SystemCapability.Communication.IPC.Core 8962 8963**参数:** 8964 8965| 参数名 | 类型 | 必填 | 说明 | 8966| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8967| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8968| data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 8969| reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8970| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8971| callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 8972 8973 8974**错误码:** 8975 8976以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8977 8978 | 错误码ID | 错误信息 | 8979 | -------- | -------- | 8980 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain the passed object instance. | 8981 8982**示例:** 8983 8984 ```ts 8985 import { hilog } from '@kit.PerformanceAnalysisKit'; 8986 import { BusinessError } from '@kit.BasicServicesKit'; 8987 8988 class TestRemoteObject extends rpc.RemoteObject { 8989 constructor(descriptor: string) { 8990 super(descriptor); 8991 } 8992 } 8993 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 8994 if (result.errCode === 0) { 8995 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8996 let num = result.reply.readInt(); 8997 let msg = result.reply.readString(); 8998 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8999 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 9000 } else { 9001 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 9002 } 9003 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 9004 result.data.reclaim(); 9005 result.reply.reclaim(); 9006 } 9007 let testRemoteObject = new TestRemoteObject("testObject"); 9008 let option = new rpc.MessageOption(); 9009 let data = rpc.MessageSequence.create(); 9010 let reply = rpc.MessageSequence.create(); 9011 data.writeInt(1); 9012 data.writeString("hello"); 9013 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 9014 ``` 9015 9016### sendRequest<sup>(deprecated)</sup> 9017 9018sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 9019 9020以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 9021 9022> **说明:** 9023> 9024> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-5)替代。 9025 9026**系统能力**:SystemCapability.Communication.IPC.Core 9027 9028**参数:** 9029 9030| 参数名 | 类型 | 必填 | 说明 | 9031| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 9032| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 9033| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 9034| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 9035| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 9036| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 9037 9038**示例:** 9039 9040 ```ts 9041 import { hilog } from '@kit.PerformanceAnalysisKit'; 9042 import { BusinessError } from '@kit.BasicServicesKit'; 9043 9044 class MyDeathRecipient implements rpc.DeathRecipient { 9045 onRemoteDied() { 9046 hilog.info(0x0000, 'testTag', 'server died'); 9047 } 9048 } 9049 class TestRemoteObject extends rpc.RemoteObject { 9050 constructor(descriptor: string) { 9051 super(descriptor); 9052 } 9053 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9054 return true; 9055 } 9056 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9057 return true; 9058 } 9059 isObjectDead(): boolean { 9060 return false; 9061 } 9062 } 9063 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 9064 if (result.errCode === 0) { 9065 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 9066 let num = result.reply.readInt(); 9067 let msg = result.reply.readString(); 9068 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 9069 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 9070 } else { 9071 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 9072 } 9073 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 9074 result.data.reclaim(); 9075 result.reply.reclaim(); 9076 } 9077 let testRemoteObject = new TestRemoteObject("testObject"); 9078 let option = new rpc.MessageOption(); 9079 let data = rpc.MessageParcel.create(); 9080 let reply = rpc.MessageParcel.create(); 9081 data.writeInt(1); 9082 data.writeString("hello"); 9083 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 9084 ``` 9085 9086### onRemoteMessageRequest<sup>9+</sup> 9087 9088onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 9089 9090> **说明:** 9091> 9092>* 开发者应优先选择重载onRemoteMessageRequest方法,其中可以自由实现同步和异步的消息处理。 9093>* 开发者同时重载onRemoteRequest和onRemoteMessageRequest方法时,仅onRemoteMessageRequest方法生效。 9094 9095sendMessageRequest请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。 9096 9097**系统能力**:SystemCapability.Communication.IPC.Core 9098 9099**参数:** 9100 9101 | 参数名 | 类型 | 必填 | 说明 | 9102 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 9103 | code | number | 是 | 对端发送的服务请求码。 | 9104 | data | [MessageSequence](#messagesequence9) | 是 | 携带客户端调用参数的MessageSequence对象。 | 9105 | reply | [MessageSequence](#messagesequence9) | 是 | 写入结果的MessageSequence对象。 | 9106 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9107 9108**返回值:** 9109 9110 | 类型 | 说明 | 9111 | ----------------- | ----------------------------------------------------------------------------------------------- | 9112 | boolean | 若在onRemoteMessageRequest中同步地处理请求,则返回一个布尔值:true:操作成功,false:操作失败。 | 9113 | Promise\<boolean> | 若在onRemoteMessageRequest中异步地处理请求,则返回一个Promise对象。 | 9114 9115**重载onRemoteMessageRequest方法同步处理请求示例:** 9116 9117 ```ts 9118 import { hilog } from '@kit.PerformanceAnalysisKit'; 9119 9120 class TestRemoteObject extends rpc.RemoteObject { 9121 constructor(descriptor: string) { 9122 super(descriptor); 9123 } 9124 9125 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9126 if (code === 1) { 9127 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9128 return true; 9129 } else { 9130 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9131 return false; 9132 } 9133 } 9134 } 9135 ``` 9136 9137 **重载onRemoteMessageRequest方法异步处理请求示例:** 9138 9139 ```ts 9140 import { hilog } from '@kit.PerformanceAnalysisKit'; 9141 9142 class TestRemoteObject extends rpc.RemoteObject { 9143 constructor(descriptor: string) { 9144 super(descriptor); 9145 } 9146 9147 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9148 if (code === 1) { 9149 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9150 } else { 9151 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9152 return false; 9153 } 9154 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9155 setTimeout(resolve, 100); 9156 }) 9157 return true; 9158 } 9159 } 9160 ``` 9161 9162**同时重载onRemoteMessageRequest和onRemoteRequest方法同步处理请求示例:** 9163 9164 ```ts 9165 import { hilog } from '@kit.PerformanceAnalysisKit'; 9166 9167 class TestRemoteObject extends rpc.RemoteObject { 9168 constructor(descriptor: string) { 9169 super(descriptor); 9170 } 9171 9172 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9173 if (code === 1) { 9174 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9175 return true; 9176 } else { 9177 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9178 return false; 9179 } 9180 } 9181 // 同时调用仅会执行onRemoteMessageRequest 9182 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9183 if (code === 1) { 9184 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9185 } else { 9186 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9187 return false; 9188 } 9189 return true; 9190 } 9191 } 9192 ``` 9193 9194 **同时重载onRemoteMessageRequest和onRemoteRequest方法异步处理请求示例:** 9195 9196 ```ts 9197 import { hilog } from '@kit.PerformanceAnalysisKit'; 9198 class TestRemoteObject extends rpc.RemoteObject { 9199 constructor(descriptor: string) { 9200 super(descriptor); 9201 } 9202 9203 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9204 if (code === 1) { 9205 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9206 return true; 9207 } else { 9208 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9209 return false; 9210 } 9211 } 9212 // 同时调用仅会执行onRemoteMessageRequest 9213 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9214 if (code === 1) { 9215 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9216 } else { 9217 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9218 return false; 9219 } 9220 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9221 setTimeout(resolve, 100); 9222 }) 9223 return true; 9224 } 9225 } 9226 ``` 9227 9228### onRemoteRequest<sup>(deprecated)</sup> 9229 9230onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9231 9232sendRequest请求的响应处理函数,服务端在该函数里处理请求,回复结果。 9233 9234> **说明:** 9235> 9236> 从API version 9 开始废弃,建议使用[onRemoteMessageRequest](#onremotemessagerequest9)替代。 9237 9238**系统能力**:SystemCapability.Communication.IPC.Core 9239 9240**参数:** 9241 9242 | 参数名 | 类型 | 必填 | 说明 | 9243 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 9244 | code | number | 是 | 对端发送的服务请求码。 | 9245 | data | [MessageParcel](#messageparceldeprecated) | 是 | 携带客户端调用参数的MessageParcel对象。 | 9246 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 写入结果的MessageParcel对象。 | 9247 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9248 9249**返回值:** 9250 9251 | 类型 | 说明 | 9252 | ------- | -------------------------------- | 9253 | boolean | true:操作成功,false:操作失败。| 9254 9255**示例:** 9256 9257 ```ts 9258 import { hilog } from '@kit.PerformanceAnalysisKit'; 9259 9260 class MyDeathRecipient implements rpc.DeathRecipient { 9261 onRemoteDied() { 9262 hilog.info(0x0000, 'testTag', 'server died'); 9263 } 9264 } 9265 class TestRemoteObject extends rpc.RemoteObject { 9266 constructor(descriptor: string) { 9267 super(descriptor); 9268 } 9269 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9270 return true; 9271 } 9272 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9273 return true; 9274 } 9275 isObjectDead(): boolean { 9276 return false; 9277 } 9278 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9279 if (code === 1) { 9280 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9281 return true; 9282 } else { 9283 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9284 return false; 9285 } 9286 } 9287 } 9288 ``` 9289 9290### getCallingUid 9291 9292getCallingUid(): number 9293 9294获取通信对端的进程Uid。 9295 9296**系统能力**:SystemCapability.Communication.IPC.Core 9297 9298**返回值:** 9299 | 类型 | 说明 | 9300 | ------ | ----------------------- | 9301 | number | 返回通信对端的进程Uid。 | 9302 9303**示例:** 9304 9305 ```ts 9306 import { hilog } from '@kit.PerformanceAnalysisKit'; 9307 9308 class TestRemoteObject extends rpc.RemoteObject { 9309 constructor(descriptor: string) { 9310 super(descriptor); 9311 } 9312 } 9313 let testRemoteObject = new TestRemoteObject("testObject"); 9314 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9315 ``` 9316 9317### getCallingPid 9318 9319getCallingPid(): number 9320 9321获取通信对端的进程Pid。 9322 9323**系统能力**:SystemCapability.Communication.IPC.Core 9324 9325**返回值:** 9326 9327 | 类型 | 说明 | 9328 | ------ | ----------------------- | 9329 | number | 返回通信对端的进程Pid。 | 9330 9331**示例:** 9332 9333 ```ts 9334 import { hilog } from '@kit.PerformanceAnalysisKit'; 9335 9336 class TestRemoteObject extends rpc.RemoteObject { 9337 constructor(descriptor: string) { 9338 super(descriptor); 9339 } 9340 } 9341 let testRemoteObject = new TestRemoteObject("testObject"); 9342 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9343 ``` 9344 9345### getLocalInterface<sup>9+</sup> 9346 9347getLocalInterface(descriptor: string): IRemoteBroker 9348 9349查询接口描述符的字符串。 9350 9351**系统能力**:SystemCapability.Communication.IPC.Core 9352 9353**参数:** 9354 9355 | 参数名 | 类型 | 必填 | 说明 | 9356 | ---------- | ------ | ---- | -------------------- | 9357 | descriptor | string | 是 | 接口描述符的字符串。 | 9358 9359**返回值:** 9360 9361 | 类型 | 说明 | 9362 | ------------- | --------------------------------------------- | 9363 | IRemoteBroker | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 9364 9365**错误码:** 9366 9367以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9368 9369 | 错误码ID | 错误信息 | 9370 | -------- | -------- | 9371 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The string length exceeds 40960 bytes; <br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9372 9373**示例:** 9374 9375 ```ts 9376 import { hilog } from '@kit.PerformanceAnalysisKit'; 9377 import { BusinessError } from '@kit.BasicServicesKit'; 9378 9379 class MyDeathRecipient implements rpc.DeathRecipient { 9380 onRemoteDied() { 9381 hilog.info(0x0000, 'testTag', 'server died'); 9382 } 9383 } 9384 class TestRemoteObject extends rpc.RemoteObject { 9385 constructor(descriptor: string) { 9386 super(descriptor); 9387 this.modifyLocalInterface(this, descriptor); 9388 } 9389 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9390 // 方法逻辑需开发者根据业务需要实现 9391 } 9392 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9393 // 方法逻辑需开发者根据业务需要实现 9394 } 9395 isObjectDead(): boolean { 9396 return false; 9397 } 9398 asObject(): rpc.IRemoteObject { 9399 return this; 9400 } 9401 } 9402 let testRemoteObject = new TestRemoteObject("testObject"); 9403 try { 9404 testRemoteObject.getLocalInterface("testObject"); 9405 } catch (error) { 9406 let e: BusinessError = error as BusinessError; 9407 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9408 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9409 } 9410 ``` 9411 9412### queryLocalInterface<sup>(deprecated)</sup> 9413 9414queryLocalInterface(descriptor: string): IRemoteBroker 9415 9416查询并获取当前接口描述符对应的远端对象是否已经存在。 9417 9418> **说明:** 9419> 9420> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-2)替代。 9421 9422**系统能力**:SystemCapability.Communication.IPC.Core 9423 9424**参数:** 9425 9426 | 参数名 | 类型 | 必填 | 说明 | 9427 | ---------- | ------ | ---- | ---------------------- | 9428 | descriptor | string | 是 | 需要查询的接口描述符。 | 9429 9430**返回值:** 9431 9432 | 类型 | 说明 | 9433 | ------------- | ------------------------------------------------------------------ | 9434 | IRemoteBroker | 如果接口描述符对应的远端对象存在,则返回该远端对象,否则返回Null。 | 9435 9436**示例:** 9437 9438 ```ts 9439 import { hilog } from '@kit.PerformanceAnalysisKit'; 9440 9441 class MyDeathRecipient implements rpc.DeathRecipient { 9442 onRemoteDied() { 9443 hilog.info(0x0000, 'testTag', 'server died'); 9444 } 9445 } 9446 class TestRemoteObject extends rpc.RemoteObject { 9447 constructor(descriptor: string) { 9448 super(descriptor); 9449 this.attachLocalInterface(this, descriptor); 9450 } 9451 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9452 return true; 9453 } 9454 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9455 return true; 9456 } 9457 isObjectDead(): boolean { 9458 return false; 9459 } 9460 asObject(): rpc.IRemoteObject { 9461 return this; 9462 } 9463 } 9464 let testRemoteObject = new TestRemoteObject("testObject"); 9465 testRemoteObject.queryLocalInterface("testObject"); 9466 ``` 9467 9468### getDescriptor<sup>9+</sup> 9469 9470getDescriptor(): string 9471 9472获取对象的接口描述符。接口描述符为字符串。 9473 9474**系统能力**:SystemCapability.Communication.IPC.Core 9475 9476**返回值:** 9477 9478 | 类型 | 说明 | 9479 | ------ | ---------------- | 9480 | string | 返回接口描述符。 | 9481 9482**错误码:** 9483 9484以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9485 9486 | 错误码ID | 错误信息 | 9487 | -------- | -------- | 9488 | 1900008 | The proxy or remote object is invalid. | 9489 9490**示例:** 9491 9492 ```ts 9493 import { hilog } from '@kit.PerformanceAnalysisKit'; 9494 import { BusinessError } from '@kit.BasicServicesKit'; 9495 9496 class MyDeathRecipient implements rpc.DeathRecipient { 9497 onRemoteDied() { 9498 hilog.info(0x0000, 'testTag', 'server died'); 9499 } 9500 } 9501 class TestRemoteObject extends rpc.RemoteObject { 9502 constructor(descriptor: string) { 9503 super(descriptor); 9504 } 9505 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9506 // 方法逻辑需开发者根据业务需要实现 9507 } 9508 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9509 // 方法逻辑需开发者根据业务需要实现 9510 } 9511 isObjectDead(): boolean { 9512 return false; 9513 } 9514 } 9515 let testRemoteObject = new TestRemoteObject("testObject"); 9516 try { 9517 let descriptor = testRemoteObject.getDescriptor(); 9518 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9519 } catch (error) { 9520 let e: BusinessError = error as BusinessError; 9521 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9522 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9523 } 9524 ``` 9525 9526### getInterfaceDescriptor<sup>(deprecated)</sup> 9527 9528getInterfaceDescriptor(): string 9529 9530查询接口描述符。 9531 9532> **说明:** 9533> 9534> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-2)替代。 9535 9536**系统能力**:SystemCapability.Communication.IPC.Core 9537 9538**返回值:** 9539 9540 | 类型 | 说明 | 9541 | ------ | ---------------- | 9542 | string | 返回接口描述符。 | 9543 9544**示例:** 9545 9546 ```ts 9547 import { hilog } from '@kit.PerformanceAnalysisKit'; 9548 9549 class MyDeathRecipient implements rpc.DeathRecipient { 9550 onRemoteDied() { 9551 hilog.info(0x0000, 'testTag', 'server died'); 9552 } 9553 } 9554 class TestRemoteObject extends rpc.RemoteObject { 9555 constructor(descriptor: string) { 9556 super(descriptor); 9557 } 9558 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9559 return true; 9560 } 9561 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9562 return true; 9563 } 9564 isObjectDead(): boolean { 9565 return false; 9566 } 9567 } 9568 let testRemoteObject = new TestRemoteObject("testObject"); 9569 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9570 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9571 ``` 9572 9573### modifyLocalInterface<sup>9+</sup> 9574 9575modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9576 9577此接口用于把接口描述符和IRemoteBroker对象绑定。 9578 9579**系统能力**:SystemCapability.Communication.IPC.Core 9580 9581**参数:** 9582 9583| 参数名 | 类型 | 必填 | 说明 | 9584| -------------- | ------------------------------- | ---- | ------------------------------------- | 9585| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9586| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9587 9588**错误码:** 9589 9590以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9591 9592 | 错误码ID | 错误信息 | 9593 | -------- | -------- | 9594 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The string length exceeds 40960 bytes; <br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9595 9596**示例:** 9597 9598 ```ts 9599 import { hilog } from '@kit.PerformanceAnalysisKit'; 9600 import { BusinessError } from '@kit.BasicServicesKit'; 9601 9602 class MyDeathRecipient implements rpc.DeathRecipient { 9603 onRemoteDied() { 9604 hilog.info(0x0000, 'testTag', 'server died'); 9605 } 9606 } 9607 class TestRemoteObject extends rpc.RemoteObject { 9608 constructor(descriptor: string) { 9609 super(descriptor); 9610 try { 9611 this.modifyLocalInterface(this, descriptor); 9612 } catch (error) { 9613 let e: BusinessError = error as BusinessError; 9614 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9615 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9616 } 9617 } 9618 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9619 // 方法逻辑需开发者根据业务需要实现 9620 } 9621 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9622 // 方法逻辑需开发者根据业务需要实现 9623 } 9624 isObjectDead(): boolean { 9625 return false; 9626 } 9627 asObject(): rpc.IRemoteObject { 9628 return this; 9629 } 9630 } 9631 let testRemoteObject = new TestRemoteObject("testObject"); 9632 ``` 9633 9634### attachLocalInterface<sup>(deprecated)</sup> 9635 9636attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9637 9638此接口用于把接口描述符和IRemoteBroker对象绑定。 9639 9640> **说明:** 9641> 9642> 从API version 9 开始废弃,建议使用[modifyLocalInterface](#modifylocalinterface9)替代。 9643 9644**系统能力**:SystemCapability.Communication.IPC.Core 9645 9646**参数:** 9647 9648| 参数名 | 类型 | 必填 | 说明 | 9649| -------------- | ------------------------------- | ---- | ------------------------------------- | 9650| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9651| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9652 9653**示例:** 9654 9655 ```ts 9656 import { hilog } from '@kit.PerformanceAnalysisKit'; 9657 9658 class MyDeathRecipient implements rpc.DeathRecipient { 9659 onRemoteDied() { 9660 hilog.info(0x0000, 'testTag', 'server died'); 9661 } 9662 } 9663 class TestRemoteObject extends rpc.RemoteObject { 9664 constructor(descriptor: string) { 9665 super(descriptor); 9666 this.attachLocalInterface(this, descriptor); 9667 } 9668 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9669 return true; 9670 } 9671 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9672 return true; 9673 } 9674 isObjectDead(): boolean { 9675 return false; 9676 } 9677 asObject(): rpc.IRemoteObject { 9678 return this; 9679 } 9680 } 9681 let testRemoteObject = new TestRemoteObject("testObject"); 9682 ``` 9683 9684## Ashmem<sup>8+</sup> 9685 9686提供与匿名共享内存对象相关的方法,包括创建、关闭、映射和取消映射Ashmem、从Ashmem读取数据和写入数据、获取Ashmem大小、设置Ashmem保护。 9687共享内存只适用与本设备内跨进程通信。 9688 9689### 属性 9690 9691**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 9692 9693 | 名称 | 类型 | 可读 | 可写 | 说明 | 9694 | ---------- | ------------------ | ----- | ----- |----------------------------------------- | 9695 | PROT_EXEC | [Ashmem](#ashmem8) | 是 | 否 | 映射内存保护类型,代表映射的内存可执行。 | 9696 | PROT_NONE | [Ashmem](#ashmem8) | 是 | 否 | 映射内存保护类型,代表映射的内存不可访问。| 9697 | PROT_READ | [Ashmem](#ashmem8) | 是 | 否 | 映射内存保护类型,代表映射的内存可读。 | 9698 | PROT_WRITE | [Ashmem](#ashmem8) | 是 | 否 | 映射内存保护类型,代表映射的内存可写。 | 9699 9700### create<sup>9+</sup> 9701 9702static create(name: string, size: number): Ashmem 9703 9704静态方法,根据指定的名称和大小创建Ashmem对象。 9705 9706**系统能力**:SystemCapability.Communication.IPC.Core 9707 9708**参数:** 9709 9710 | 参数名 | 类型 | 必填 | 说明 | 9711 | ------ | ------ | ---- | ---------------------------- | 9712 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9713 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9714 9715**返回值:** 9716 9717| 类型 | 说明 | 9718| ------------------ | ---------------------------------------------- | 9719| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9720 9721**错误码:** 9722 9723以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9724 9725 | 错误码ID | 错误信息 | 9726 | -------- | -------- | 9727 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The Ashmem name passed is empty; <br/> 4.The Ashmem size passed is less than or equal to 0. | 9728 9729**示例:** 9730 9731 ```ts 9732 import { hilog } from '@kit.PerformanceAnalysisKit'; 9733 import { BusinessError } from '@kit.BasicServicesKit'; 9734 9735 let ashmem: rpc.Ashmem | undefined = undefined; 9736 try { 9737 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9738 let size = ashmem.getAshmemSize(); 9739 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9740 } catch (error) { 9741 let e: BusinessError = error as BusinessError; 9742 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9743 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9744 } 9745 ``` 9746 9747### createAshmem<sup>(deprecated)</sup> 9748 9749static createAshmem(name: string, size: number): Ashmem 9750 9751静态方法,根据指定的名称和大小创建Ashmem对象。 9752 9753> **说明:** 9754> 9755> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9)替代。 9756 9757**系统能力**:SystemCapability.Communication.IPC.Core 9758 9759**参数:** 9760 9761 | 参数名 | 类型 | 必填 | 说明 | 9762 | ------ | ------ | ---- | ---------------------------- | 9763 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9764 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9765 9766**返回值:** 9767 9768| 类型 | 说明 | 9769| ------------------ | ---------------------------------------------- | 9770| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9771 9772**示例:** 9773 9774 ```ts 9775 import { hilog } from '@kit.PerformanceAnalysisKit'; 9776 9777 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9778 let size = ashmem.getAshmemSize(); 9779 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9780 ``` 9781 9782### create<sup>9+</sup> 9783 9784static create(ashmem: Ashmem): Ashmem 9785 9786静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9787 9788**系统能力**:SystemCapability.Communication.IPC.Core 9789 9790**参数:** 9791 9792| 参数名 | 类型 | 必填 | 说明 | 9793| ------ | ------------------ | ---- | -------------------- | 9794| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9795 9796**返回值:** 9797 9798| 类型 | 说明 | 9799| ------------------ | ---------------------- | 9800| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9801 9802**错误码:** 9803 9804以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9805 9806 | 错误码ID | 错误信息 | 9807 | -------- | -------- | 9808 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The passed parameter is not an Ahmem object; <br/> 3.The ashmem instance for obtaining packaging is empty. | 9809 9810**示例:** 9811 9812 ```ts 9813 import { hilog } from '@kit.PerformanceAnalysisKit'; 9814 import { BusinessError } from '@kit.BasicServicesKit'; 9815 9816 try { 9817 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9818 let ashmem2 = rpc.Ashmem.create(ashmem); 9819 let size = ashmem2.getAshmemSize(); 9820 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9821 } catch (error) { 9822 let e: BusinessError = error as BusinessError; 9823 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9824 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9825 } 9826 ``` 9827 9828### createAshmemFromExisting<sup>(deprecated)</sup> 9829 9830static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9831 9832静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9833 9834> **说明:** 9835> 9836> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9-1)替代。 9837 9838**系统能力**:SystemCapability.Communication.IPC.Core 9839 9840**参数:** 9841 9842| 参数名 | 类型 | 必填 | 说明 | 9843| ------ | ------------------ | ---- | -------------------- | 9844| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9845 9846**返回值:** 9847 9848| 类型 | 说明 | 9849| ------------------ | ---------------------- | 9850| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9851 9852**示例:** 9853 9854 ```ts 9855 import { hilog } from '@kit.PerformanceAnalysisKit'; 9856 9857 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9858 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9859 let size = ashmem2.getAshmemSize(); 9860 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9861 ``` 9862 9863### closeAshmem<sup>8+</sup> 9864 9865closeAshmem(): void 9866 9867关闭这个Ashmem。 9868 9869> **说明:** 9870> 9871> 关闭Ashmem对象前需要先解除地址映射。 9872 9873**系统能力**:SystemCapability.Communication.IPC.Core 9874 9875**示例:** 9876 9877 ```ts 9878 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9879 ashmem.closeAshmem(); 9880 ``` 9881 9882### unmapAshmem<sup>8+</sup> 9883 9884unmapAshmem(): void 9885 9886删除该Ashmem对象的地址映射。 9887 9888**系统能力**:SystemCapability.Communication.IPC.Core 9889 9890**示例:** 9891 9892 ```ts 9893 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9894 ashmem.unmapAshmem(); 9895 ``` 9896 9897### getAshmemSize<sup>8+</sup> 9898 9899getAshmemSize(): number 9900 9901获取Ashmem对象的内存大小。 9902 9903**系统能力**:SystemCapability.Communication.IPC.Core 9904 9905**返回值:** 9906 9907 | 类型 | 说明 | 9908 | ------ | -------------------------- | 9909 | number | 返回Ashmem对象的内存大小。 | 9910 9911**示例:** 9912 9913 ```ts 9914 import { hilog } from '@kit.PerformanceAnalysisKit'; 9915 9916 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9917 let size = ashmem.getAshmemSize(); 9918 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9919 ``` 9920 9921### mapTypedAshmem<sup>9+</sup> 9922 9923mapTypedAshmem(mapType: number): void 9924 9925在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9926 9927**系统能力**:SystemCapability.Communication.IPC.Core 9928 9929**参数:** 9930 9931 | 参数名 | 类型 | 必填 | 说明 | 9932 | ------- | ------ | ---- | ------------------------------ | 9933 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9934 9935**错误码:** 9936 9937以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9938 9939 | 错误码ID | 错误信息 | 9940 | -------- | -------- | 9941 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The passed mapType exceeds the maximum protection level. | 9942 | 1900001 | Failed to call mmap. | 9943 9944**示例:** 9945 9946 ```ts 9947 import { hilog } from '@kit.PerformanceAnalysisKit'; 9948 import { BusinessError } from '@kit.BasicServicesKit'; 9949 9950 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9951 try { 9952 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9953 } catch (error) { 9954 let e: BusinessError = error as BusinessError; 9955 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9956 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9957 } 9958 ``` 9959 9960### mapAshmem<sup>(deprecated)</sup> 9961 9962mapAshmem(mapType: number): boolean 9963 9964在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9965 9966> **说明:** 9967> 9968> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapTypedAshmem](#maptypedashmem9)替代。 9969 9970**系统能力**:SystemCapability.Communication.IPC.Core 9971 9972**参数:** 9973 9974 | 参数名 | 类型 | 必填 | 说明 | 9975 | ------- | ------ | ---- | ------------------------------ | 9976 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9977 9978**返回值:** 9979 9980 | 类型 | 说明 | 9981 | ------- | -------------------------------- | 9982 | boolean | true:映射成功,false:映射失败。| 9983 9984**示例:** 9985 9986 ```ts 9987 import { hilog } from '@kit.PerformanceAnalysisKit'; 9988 9989 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9990 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9991 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9992 ``` 9993 9994### mapReadWriteAshmem<sup>9+</sup> 9995 9996mapReadWriteAshmem(): void 9997 9998在此进程虚拟地址空间上创建可读写的共享文件映射。 9999 10000**系统能力**:SystemCapability.Communication.IPC.Core 10001 10002**错误码:** 10003 10004以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10005 10006 | 错误码ID | 错误信息 | 10007 | -------- | -------- | 10008 | 1900001 | Failed to call mmap. | 10009 10010**示例:** 10011 10012 ```ts 10013 import { hilog } from '@kit.PerformanceAnalysisKit'; 10014 import { BusinessError } from '@kit.BasicServicesKit'; 10015 10016 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10017 try { 10018 ashmem.mapReadWriteAshmem(); 10019 } catch (error) { 10020 let e: BusinessError = error as BusinessError; 10021 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 10022 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 10023 } 10024 ``` 10025 10026### mapReadAndWriteAshmem<sup>(deprecated)</sup> 10027 10028mapReadAndWriteAshmem(): boolean 10029 10030在此进程虚拟地址空间上创建可读写的共享文件映射。 10031 10032> **说明:** 10033> 10034> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadWriteAshmem](#mapreadwriteashmem9)替代。 10035 10036**系统能力**:SystemCapability.Communication.IPC.Core 10037 10038**返回值:** 10039 10040 | 类型 | 说明 | 10041 | ------- | -------------------------------- | 10042 | boolean | true:映射成功,false:映射失败。| 10043 10044**示例:** 10045 10046 ```ts 10047 import { hilog } from '@kit.PerformanceAnalysisKit'; 10048 10049 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10050 let mapResult = ashmem.mapReadAndWriteAshmem(); 10051 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 10052 ``` 10053 10054### mapReadonlyAshmem<sup>9+</sup> 10055 10056mapReadonlyAshmem(): void 10057 10058在此进程虚拟地址空间上创建只读的共享文件映射。 10059 10060**系统能力**:SystemCapability.Communication.IPC.Core 10061 10062**错误码:** 10063 10064以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10065 10066 | 错误码ID | 错误信息 | 10067 | -------- | -------- | 10068 | 1900001 | Failed to call mmap. | 10069 10070**示例:** 10071 10072 ```ts 10073 import { hilog } from '@kit.PerformanceAnalysisKit'; 10074 import { BusinessError } from '@kit.BasicServicesKit'; 10075 10076 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10077 try { 10078 ashmem.mapReadonlyAshmem(); 10079 } catch (error) { 10080 let e: BusinessError = error as BusinessError; 10081 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 10082 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 10083 } 10084 ``` 10085 10086### mapReadOnlyAshmem<sup>(deprecated)</sup> 10087 10088mapReadOnlyAshmem(): boolean 10089 10090在此进程虚拟地址空间上创建只读的共享文件映射。 10091 10092> **说明:** 10093> 10094> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadonlyAshmem](#mapreadonlyashmem9)替代。 10095 10096**系统能力**:SystemCapability.Communication.IPC.Core 10097 10098**返回值:** 10099 10100 | 类型 | 说明 | 10101 | ------- | -------------------------------- | 10102 | boolean | true:映射成功,false:映射失败。| 10103 10104**示例:** 10105 10106 ```ts 10107 import { hilog } from '@kit.PerformanceAnalysisKit'; 10108 10109 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10110 let mapResult = ashmem.mapReadOnlyAshmem(); 10111 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 10112 ``` 10113 10114### setProtectionType<sup>9+</sup> 10115 10116setProtectionType(protectionType: number): void 10117 10118设置映射内存区域的保护等级。 10119 10120**系统能力**:SystemCapability.Communication.IPC.Core 10121 10122**参数:** 10123 10124 | 参数名 | 类型 | 必填 | 说明 | 10125 | -------------- | ------ | ---- | ------------------ | 10126 | protectionType | number | 是 | 要设置的保护类型。 | 10127 10128**错误码:** 10129 10130以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10131 10132 | 错误码ID | 错误信息 | 10133 | -------- | -------- | 10134 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10135 | 1900002 | Failed to call ioctl. | 10136 10137**示例:** 10138 10139 ```ts 10140 import { hilog } from '@kit.PerformanceAnalysisKit'; 10141 import { BusinessError } from '@kit.BasicServicesKit'; 10142 10143 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10144 try { 10145 ashmem.setProtectionType(ashmem.PROT_READ); 10146 } catch (error) { 10147 let e: BusinessError = error as BusinessError; 10148 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10149 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10150 } 10151 ``` 10152 10153### setProtection<sup>(deprecated)</sup> 10154 10155setProtection(protectionType: number): boolean 10156 10157设置映射内存区域的保护等级。 10158 10159> **说明:** 10160> 10161> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[setProtectionType](#setprotectiontype9)替代。 10162 10163**系统能力**:SystemCapability.Communication.IPC.Core 10164 10165**参数:** 10166 10167 | 参数名 | 类型 | 必填 | 说明 | 10168 | -------------- | ------ | ---- | ------------------ | 10169 | protectionType | number | 是 | 要设置的保护类型。 | 10170 10171**返回值:** 10172 10173 | 类型 | 说明 | 10174 | ------- | -------------------------------- | 10175 | boolean | true:设置成功,false:设置失败。| 10176 10177**示例:** 10178 10179 ```ts 10180 import { hilog } from '@kit.PerformanceAnalysisKit'; 10181 10182 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10183 let result = ashmem.setProtection(ashmem.PROT_READ); 10184 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 10185 ``` 10186 10187### writeDataToAshmem<sup>11+</sup> 10188 10189writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10190 10191将数据写入此Ashmem对象关联的共享文件。 10192 10193> **说明:** 10194> 10195> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10196 10197**系统能力**:SystemCapability.Communication.IPC.Core 10198 10199**参数:** 10200 10201 | 参数名 | 类型 | 必填 | 说明 | 10202 | ------ | -------- | ---- | -------------------------------------------------- | 10203 | buf | ArrayBuffer | 是 | 写入Ashmem对象的数据。 | 10204 | size | number | 是 | 要写入的数据大小。 | 10205 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10206 10207**错误码:** 10208 10209以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10210 10211 | 错误码ID | 错误信息 | 10212 | -------- | -------- | 10213 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.Failed to obtain arrayBuffer information. | 10214 | 1900003 | Failed to write data to the shared memory. | 10215 10216**示例:** 10217 10218 ```ts 10219 import { hilog } from '@kit.PerformanceAnalysisKit'; 10220 import { BusinessError } from '@kit.BasicServicesKit'; 10221 10222 let buffer = new ArrayBuffer(1024); 10223 let int32View = new Int32Array(buffer); 10224 for (let i = 0; i < int32View.length; i++) { 10225 int32View[i] = i * 2 + 1; 10226 } 10227 let size = buffer.byteLength; 10228 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10229 ashmem.mapReadWriteAshmem(); 10230 try { 10231 ashmem.writeDataToAshmem(buffer, size, 0); 10232 } catch (error) { 10233 let e: BusinessError = error as BusinessError; 10234 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10235 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10236 } 10237 ``` 10238 10239### writeAshmem<sup>(deprecated)</sup> 10240 10241writeAshmem(buf: number[], size: number, offset: number): void 10242 10243将数据写入此Ashmem对象关联的共享文件。 10244 10245> **说明:** 10246> 10247> 从API version 9 开始支持,从API version 11 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10248> 10249> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10250 10251**系统能力**:SystemCapability.Communication.IPC.Core 10252 10253**参数:** 10254 10255 | 参数名 | 类型 | 必填 | 说明 | 10256 | ------ | -------- | ---- | -------------------------------------------------- | 10257 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10258 | size | number | 是 | 要写入的数据大小。 | 10259 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 | 10260 10261**错误码:** 10262 10263以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10264 10265 | 错误码ID | 错误信息 | 10266 | -------- | -------- | 10267 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The element does not exist in the array. | 10268 | 1900003 | Failed to write data to the shared memory. | 10269 10270**示例:** 10271 10272 ```ts 10273 import { hilog } from '@kit.PerformanceAnalysisKit'; 10274 import { BusinessError } from '@kit.BasicServicesKit'; 10275 10276 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10277 ashmem.mapReadWriteAshmem(); 10278 let ByteArrayVar = [1, 2, 3, 4, 5]; 10279 try { 10280 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10281 } catch (error) { 10282 let e: BusinessError = error as BusinessError; 10283 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10284 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10285 } 10286 ``` 10287 10288### writeToAshmem<sup>(deprecated)</sup> 10289 10290writeToAshmem(buf: number[], size: number, offset: number): boolean 10291 10292将数据写入此Ashmem对象关联的共享文件。 10293 10294> **说明:** 10295> 10296> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10297> 10298> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10299 10300**系统能力**:SystemCapability.Communication.IPC.Core 10301 10302**参数:** 10303 10304 | 参数名 | 类型 | 必填 | 说明 | 10305 | ------ | -------- | ---- | -------------------------------------------------- | 10306 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10307 | size | number | 是 | 要写入的数据大小。 | 10308 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10309 10310**返回值:** 10311 10312 | 类型 | 说明 | 10313 | ------- | ----------------------------------------------------------------------------- | 10314 | boolean | true:如果数据写入成功,false:在其他情况下,如数据写入越界或未获得写入权限。 | 10315 10316**示例:** 10317 10318 ```ts 10319 import { hilog } from '@kit.PerformanceAnalysisKit'; 10320 10321 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10322 let mapResult = ashmem.mapReadAndWriteAshmem(); 10323 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10324 let ByteArrayVar = [1, 2, 3, 4, 5]; 10325 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10326 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10327 ``` 10328 10329### readDataFromAshmem<sup>11+</sup> 10330 10331readDataFromAshmem(size: number, offset: number): ArrayBuffer 10332 10333从此Ashmem对象关联的共享文件中读取数据。 10334 10335> **说明:** 10336> 10337> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10338 10339**系统能力**:SystemCapability.Communication.IPC.Core 10340 10341**参数:** 10342 10343 | 参数名 | 类型 | 必填 | 说明 | 10344 | ------ | ------ | ---- | -------------------------------------------------- | 10345 | size | number | 是 | 要读取的数据的大小。 | 10346 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10347 10348**返回值:** 10349 10350 | 类型 | 说明 | 10351 | -------- | ---------------- | 10352 | ArrayBuffer | 返回读取的数据。 | 10353 10354**错误码:** 10355 10356以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10357 10358 | 错误码ID | 错误信息 | 10359 | -------- | -------- | 10360 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10361 | 1900004 | Failed to read data from the shared memory. | 10362 10363**示例:** 10364 10365 ```ts 10366 import { hilog } from '@kit.PerformanceAnalysisKit'; 10367 import { BusinessError } from '@kit.BasicServicesKit'; 10368 10369 let buffer = new ArrayBuffer(1024); 10370 let int32View = new Int32Array(buffer); 10371 for (let i = 0; i < int32View.length; i++) { 10372 int32View[i] = i * 2 + 1; 10373 } 10374 let size = buffer.byteLength; 10375 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10376 ashmem.mapReadWriteAshmem(); 10377 try { 10378 ashmem.writeDataToAshmem(buffer, size, 0); 10379 } catch (error) { 10380 let e: BusinessError = error as BusinessError; 10381 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10382 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10383 } 10384 try { 10385 let readResult = ashmem.readDataFromAshmem(size, 0); 10386 let readInt32View = new Int32Array(readResult); 10387 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readInt32View); 10388 } catch (error) { 10389 let e: BusinessError = error as BusinessError; 10390 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10391 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10392 } 10393 ``` 10394 10395### readAshmem<sup>(deprecated)</sup> 10396 10397readAshmem(size: number, offset: number): number[] 10398 10399从此Ashmem对象关联的共享文件中读取数据。 10400 10401> **说明:** 10402> 10403> 从API version 9 开始支持,从API version 11 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10404> 10405> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10406 10407 10408**系统能力**:SystemCapability.Communication.IPC.Core 10409 10410**参数:** 10411 10412 | 参数名 | 类型 | 必填 | 说明 | 10413 | ------ | ------ | ---- | -------------------------------------------------- | 10414 | size | number | 是 | 要读取的数据的大小。 | 10415 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10416 10417**返回值:** 10418 10419 | 类型 | 说明 | 10420 | -------- | ---------------- | 10421 | number[] | 返回读取的数据。 | 10422 10423**错误码:** 10424 10425以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10426 10427 | 错误码ID | 错误信息 | 10428 | -------- | -------- | 10429 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10430 | 1900004 | Failed to read data from the shared memory. | 10431 10432**示例:** 10433 10434 ```ts 10435 import { hilog } from '@kit.PerformanceAnalysisKit'; 10436 import { BusinessError } from '@kit.BasicServicesKit'; 10437 10438 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10439 ashmem.mapReadWriteAshmem(); 10440 let ByteArrayVar = [1, 2, 3, 4, 5]; 10441 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10442 try { 10443 let readResult = ashmem.readAshmem(5, 0); 10444 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 10445 } catch (error) { 10446 let e: BusinessError = error as BusinessError; 10447 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10448 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10449 } 10450 ``` 10451 10452### readFromAshmem<sup>(deprecated)</sup> 10453 10454readFromAshmem(size: number, offset: number): number[] 10455 10456从此Ashmem对象关联的共享文件中读取数据。 10457 10458> **说明:** 10459> 10460> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10461> 10462> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10463 10464**系统能力**:SystemCapability.Communication.IPC.Core 10465 10466**参数:** 10467 10468 | 参数名 | 类型 | 必填 | 说明 | 10469 | ------ | ------ | ---- | -------------------------------------------------- | 10470 | size | number | 是 | 要读取的数据的大小。 | 10471 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10472 10473**返回值:** 10474 10475 | 类型 | 说明 | 10476 | -------- | ---------------- | 10477 | number[] | 返回读取的数据。 | 10478 10479**示例:** 10480 10481 ``` ts 10482 import { hilog } from '@kit.PerformanceAnalysisKit'; 10483 10484 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10485 let mapResult = ashmem.mapReadAndWriteAshmem(); 10486 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10487 let ByteArrayVar = [1, 2, 3, 4, 5]; 10488 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10489 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10490 let readResult = ashmem.readFromAshmem(5, 0); 10491 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 10492 ```