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 88### reclaim 89 90reclaim(): void 91 92释放不再使用的MessageSequence对象。 93 94**系统能力**:SystemCapability.Communication.IPC.Core 95 96**示例:** 97 98 ```ts 99 let reply = rpc.MessageSequence.create(); 100 reply.reclaim(); 101 ``` 102 103### writeRemoteObject 104 105writeRemoteObject(object: IRemoteObject): void 106 107序列化远程对象并将其写入MessageSequence对象。 108 109**系统能力**:SystemCapability.Communication.IPC.Core 110 111**参数:** 112 113 | 参数名 | 类型 | 必填 | 说明 | 114 | ------ | ------------------------------- | ---- | ----------------------------------------- | 115 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageSequence的远程对象。 | 116 117**错误码:** 118 119以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 120 121 | 错误码ID | 错误信息 | 122 | -------- | -------- | 123 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 124 | 1900008 | The proxy or remote object is invalid. | 125 | 1900009 | Failed to write data to the message sequence. | 126 127**示例:** 128 129 ```ts 130 import { hilog } from '@kit.PerformanceAnalysisKit'; 131 import { BusinessError } from '@kit.BasicServicesKit'; 132 133 class TestRemoteObject extends rpc.RemoteObject { 134 constructor(descriptor: string) { 135 super(descriptor); 136 } 137 } 138 let data = rpc.MessageSequence.create(); 139 let testRemoteObject = new TestRemoteObject("testObject"); 140 try { 141 data.writeRemoteObject(testRemoteObject); 142 } catch (error) { 143 let e: BusinessError = error as BusinessError; 144 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 145 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 146 } 147 ``` 148 149### readRemoteObject 150 151readRemoteObject(): IRemoteObject 152 153从MessageSequence读取远程对象。此方法用于反序列化MessageSequence对象以生成IRemoteObject。远程对象按写入MessageSequence的顺序读取。 154 155**系统能力**:SystemCapability.Communication.IPC.Core 156 157**返回值:** 158 159 | 类型 | 说明 | 160 | ------------------------------- | ------------------ | 161 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 162 163**错误码:** 164 165以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 166 167 | 错误码ID | 错误信息 | 168 | -------- | -------- | 169 | 1900008 | The proxy or remote object is invalid. | 170 | 1900010 | Failed to read data from the message sequence. | 171 172**示例:** 173 174 ```ts 175 import { hilog } from '@kit.PerformanceAnalysisKit'; 176 import { BusinessError } from '@kit.BasicServicesKit'; 177 178 class TestRemoteObject extends rpc.RemoteObject { 179 constructor(descriptor: string) { 180 super(descriptor); 181 } 182 } 183 let data = rpc.MessageSequence.create(); 184 let testRemoteObject = new TestRemoteObject("testObject"); 185 try { 186 data.writeRemoteObject(testRemoteObject); 187 let proxy = data.readRemoteObject(); 188 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 189 } catch (error) { 190 let e: BusinessError = error as BusinessError; 191 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 192 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 193 } 194 ``` 195 196### writeInterfaceToken 197 198writeInterfaceToken(token: string): void 199 200将接口描述符写入MessageSequence对象,远端对象可使用该信息校验本次通信。 201 202**系统能力**:SystemCapability.Communication.IPC.Core 203 204**参数:** 205 206 | 参数名 | 类型 | 必填 | 说明 | 207 | ------ | ------ | ---- | ------------------ | 208 | token | string | 是 | 字符串类型描述符。 | 209 210**错误码:** 211 212以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 213 214 | 错误码ID | 错误信息 | 215 | -------- | -------- | 216 | 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. | 217 | 1900009 | Failed to write data to the message sequence. | 218 219**示例:** 220 221 ```ts 222 import { hilog } from '@kit.PerformanceAnalysisKit'; 223 import { BusinessError } from '@kit.BasicServicesKit'; 224 225 let data = rpc.MessageSequence.create(); 226 try { 227 data.writeInterfaceToken("aaa"); 228 } catch (error) { 229 let e: BusinessError = error as BusinessError; 230 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 231 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 232 } 233 ``` 234 235### readInterfaceToken 236 237readInterfaceToken(): string 238 239从MessageSequence对象中读取接口描述符,接口描述符按写入MessageSequence的顺序读取,本地对象可使用该信息检验本次通信。 240 241**系统能力**:SystemCapability.Communication.IPC.Core 242 243**返回值:** 244 245 | 类型 | 说明 | 246 | ------ | ------------------------ | 247 | string | 返回读取到的接口描述符。 | 248 249**错误码:** 250 251以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 252 253 | 错误码ID | 错误信息 | 254 | -------- | -------- | 255 | 1900010 | Failed to read data from the message sequence. | 256 257**示例:** 258 259```ts 260import { hilog } from '@kit.PerformanceAnalysisKit'; 261import { BusinessError } from '@kit.BasicServicesKit'; 262 263class Stub extends rpc.RemoteObject { 264 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 265 try { 266 let interfaceToken = data.readInterfaceToken(); 267 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 268 } catch (error) { 269 let e: BusinessError = error as BusinessError; 270 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 271 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 272 } 273 return true; 274 } 275} 276``` 277 278### getSize 279 280getSize(): number 281 282获取当前创建的MessageSequence对象的数据大小。 283 284**系统能力**:SystemCapability.Communication.IPC.Core 285 286**返回值:** 287 288 | 类型 | 说明 | 289 | ------ | ----------------------------------------------- | 290 | number | 获取的MessageSequence实例的数据大小。以字节为单位。 | 291 292**示例:** 293 294 ```ts 295 import { hilog } from '@kit.PerformanceAnalysisKit'; 296 297 let data = rpc.MessageSequence.create(); 298 let size = data.getSize(); 299 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 300 ``` 301 302### getCapacity 303 304getCapacity(): number 305 306获取当前MessageSequence对象的容量大小。 307 308**系统能力**:SystemCapability.Communication.IPC.Core 309 310**返回值:** 311 312 | 类型 | 说明 | 313 | ------ | ----- | 314 | number | 获取的MessageSequence实例的容量大小。以字节为单位。 | 315 316**示例:** 317 318 ```ts 319 import { hilog } from '@kit.PerformanceAnalysisKit'; 320 321 let data = rpc.MessageSequence.create(); 322 let result = data.getCapacity(); 323 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 324 ``` 325 326### setSize 327 328setSize(size: number): void 329 330设置MessageSequence对象中包含的数据大小。 331 332**系统能力**:SystemCapability.Communication.IPC.Core 333 334**参数:** 335 336 | 参数名 | 类型 | 必填 | 说明 | 337 | ------ | ------ | ---- | ------ | 338 | size | number | 是 | MessageSequence实例的数据大小。以字节为单位。 | 339 340**错误码:** 341 342以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 343 344 | 错误码ID | 错误信息 | 345 | -------- | -------- | 346 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 347 348**示例:** 349 350 ```ts 351 import { hilog } from '@kit.PerformanceAnalysisKit'; 352 import { BusinessError } from '@kit.BasicServicesKit'; 353 354 let data = rpc.MessageSequence.create(); 355 data.writeString('Hello World'); 356 try { 357 data.setSize(16); 358 } catch (error) { 359 let e: BusinessError = error as BusinessError; 360 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 361 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 362 } 363 ``` 364 365### setCapacity 366 367setCapacity(size: number): void 368 369设置MessageSequence对象的存储容量。 370 371**系统能力**:SystemCapability.Communication.IPC.Core 372 373**参数:** 374 375 | 参数名 | 类型 | 必填 | 说明 | 376 | ------ | ------ | ---- | --------------------------------------------- | 377 | size | number | 是 | MessageSequence实例的存储容量。以字节为单位。 | 378 379**错误码:** 380 381以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 382 383 | 错误码ID | 错误信息 | 384 | -------- | -------- | 385 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 386 | 1900011 | Memory allocation failed | 387 388**示例:** 389 390 ```ts 391 import { hilog } from '@kit.PerformanceAnalysisKit'; 392 import { BusinessError } from '@kit.BasicServicesKit'; 393 394 let data = rpc.MessageSequence.create(); 395 try { 396 data.setCapacity(100); 397 } catch (error) { 398 let e: BusinessError = error as BusinessError; 399 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 400 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 401 } 402 ``` 403 404### getWritableBytes 405 406getWritableBytes(): number 407 408获取MessageSequence的可写字节空间大小。 409 410**系统能力**:SystemCapability.Communication.IPC.Core 411 412**返回值:** 413 414 | 类型 | 说明 | 415 | ------ | ------ | 416 | number | 获取到的MessageSequence实例的可写字节空间。以字节为单位。 | 417 418**示例:** 419 420```ts 421import { hilog } from '@kit.PerformanceAnalysisKit'; 422 423class Stub extends rpc.RemoteObject { 424 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 425 let getWritableBytes = data.getWritableBytes(); 426 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 427 return true; 428 } 429} 430``` 431 432### getReadableBytes 433 434getReadableBytes(): number 435 436获取MessageSequence的可读字节空间。 437 438**系统能力**:SystemCapability.Communication.IPC.Core 439 440**返回值:** 441 442 | 类型 | 说明 | 443 | ------ | ------- | 444 | number | 获取到的MessageSequence实例的可读字节空间。以字节为单位。 | 445 446**示例:** 447 448```ts 449import { hilog } from '@kit.PerformanceAnalysisKit'; 450 451class Stub extends rpc.RemoteObject { 452 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 453 let result = data.getReadableBytes(); 454 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 455 return true; 456 } 457} 458``` 459 460### getReadPosition 461 462getReadPosition(): number 463 464获取MessageSequence的读位置。 465 466**系统能力**:SystemCapability.Communication.IPC.Core 467 468**返回值:** 469 470 | 类型 | 说明 | 471 | ------ | ------ | 472 | number | 返回MessageSequence实例中的当前读取位置。 | 473 474**示例:** 475 476 ```ts 477 import { hilog } from '@kit.PerformanceAnalysisKit'; 478 479 let data = rpc.MessageSequence.create(); 480 let readPos = data.getReadPosition(); 481 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 482 ``` 483 484### getWritePosition 485 486getWritePosition(): number 487 488获取MessageSequence的写位置。 489 490**系统能力**:SystemCapability.Communication.IPC.Core 491 492**返回值:** 493 494 | 类型 | 说明 | 495 | ------ | ----- | 496 | number | 返回MessageSequence实例中的当前写入位置。 | 497 498**示例:** 499 500 ```ts 501 import { hilog } from '@kit.PerformanceAnalysisKit'; 502 503 let data = rpc.MessageSequence.create(); 504 data.writeInt(10); 505 let bwPos = data.getWritePosition(); 506 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 507 ``` 508 509### rewindRead 510 511rewindRead(pos: number): void 512 513重新偏移读取位置到指定的位置。 514 515**系统能力**:SystemCapability.Communication.IPC.Core 516 517**参数:** 518 519 | 参数名 | 类型 | 必填 | 说明 | 520 | ------ | ------ | ---- | ------- | 521 | pos | number | 是 | 开始读取数据的目标位置。 | 522 523**错误码:** 524 525以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 526 527 | 错误码ID | 错误信息 | 528 | -------- | -------- | 529 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 530 531**示例:** 532 533 ```ts 534 import { hilog } from '@kit.PerformanceAnalysisKit'; 535 import { BusinessError } from '@kit.BasicServicesKit'; 536 537 let data = rpc.MessageSequence.create(); 538 data.writeInt(12); 539 data.writeString("sequence"); 540 let number = data.readInt(); 541 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 542 try { 543 data.rewindRead(0); 544 } catch (error) { 545 let e: BusinessError = error as BusinessError; 546 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 547 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 548 } 549 let number2 = data.readInt(); 550 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 551 ``` 552 553### rewindWrite 554 555rewindWrite(pos: number): void 556 557重新偏移写位置到指定的位置。 558 559**系统能力**:SystemCapability.Communication.IPC.Core 560 561**参数:** 562 563 | 参数名 | 类型 | 必填 | 说明 | 564 | ------ | ------ | ---- | ----- | 565 | pos | number | 是 | 开始写入数据的目标位置。 | 566 567**错误码:** 568 569以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 570 571 | 错误码ID | 错误信息 | 572 | -------- | -------- | 573 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 574 575**示例:** 576 577 ```ts 578 import { hilog } from '@kit.PerformanceAnalysisKit'; 579 import { BusinessError } from '@kit.BasicServicesKit'; 580 581 let data = rpc.MessageSequence.create(); 582 data.writeInt(4); 583 try { 584 data.rewindWrite(0); 585 } catch (error) { 586 let e: BusinessError = error as BusinessError; 587 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 588 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 589 } 590 data.writeInt(5); 591 let number = data.readInt(); 592 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 593 ``` 594 595### writeByte 596 597writeByte(val: number): void 598 599将字节值写入MessageSequence实例。 600 601**系统能力**:SystemCapability.Communication.IPC.Core 602 603**参数:** 604 605 | 参数名 | 类型 | 必填 | 说明 | 606 | ------ | ------ | ---- | ----- | 607 | val | number | 是 | 要写入的字节值。 | 608 609**错误码:** 610 611以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 612 613 | 错误码ID | 错误信息 | 614 | -------- | ------- | 615 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 616 | 1900009 | Failed to write data to the message sequence. | 617 618**示例:** 619 620 ```ts 621 import { hilog } from '@kit.PerformanceAnalysisKit'; 622 import { BusinessError } from '@kit.BasicServicesKit'; 623 624 let data = rpc.MessageSequence.create(); 625 try { 626 data.writeByte(2); 627 } catch (error) { 628 let e: BusinessError = error as BusinessError; 629 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 630 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 631 } 632 ``` 633 634### readByte 635 636readByte(): number 637 638从MessageSequence实例读取字节值。 639 640**系统能力**:SystemCapability.Communication.IPC.Core 641 642**返回值:** 643 644 | 类型 | 说明 | 645 | ------ | ----- | 646 | number | 返回字节值。 | 647 648**错误码:** 649 650以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 651 652 | 错误码ID | 错误信息 | 653 | ------- | -------- | 654 | 1900010 | Failed to read data from the message sequence. | 655 656**示例:** 657 658 ```ts 659 import { hilog } from '@kit.PerformanceAnalysisKit'; 660 import { BusinessError } from '@kit.BasicServicesKit'; 661 662 let data = rpc.MessageSequence.create(); 663 try { 664 data.writeByte(2); 665 } catch (error) { 666 let e: BusinessError = error as BusinessError; 667 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 668 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 669 } 670 try { 671 let ret = data.readByte(); 672 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 673 } catch (error) { 674 let e: BusinessError = error as BusinessError; 675 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 676 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 677 } 678 ``` 679 680### writeShort 681 682writeShort(val: number): void 683 684将短整数值写入MessageSequence实例。 685 686**系统能力**:SystemCapability.Communication.IPC.Core 687 688**参数:** 689 690 | 参数名 | 类型 | 必填 | 说明 | 691 | ------ | ------ | --- | --- | 692 | val | number | 是 | 要写入的短整数值。 | 693 694**错误码:** 695 696以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 697 698 | 错误码ID | 错误信息 | 699 | -------- | -------- | 700 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 701 | 1900009 | Failed to write data to the message sequence. | 702 703**示例:** 704 705 ```ts 706 import { hilog } from '@kit.PerformanceAnalysisKit'; 707 import { BusinessError } from '@kit.BasicServicesKit'; 708 709 let data = rpc.MessageSequence.create(); 710 try { 711 data.writeShort(8); 712 } catch (error) { 713 let e: BusinessError = error as BusinessError; 714 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 715 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 716 } 717 ``` 718 719### readShort 720 721readShort(): number 722 723从MessageSequence实例读取短整数值。 724 725**系统能力**:SystemCapability.Communication.IPC.Core 726 727**返回值:** 728 729 | 类型 | 说明 | 730 | ------ | -------------- | 731 | number | 返回短整数值。 | 732 733**错误码:** 734 735以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 736 737 | 错误码ID | 错误信息 | 738 | -------- | -------- | 739 | 1900010 | Failed to read data from the message sequence. | 740 741**示例:** 742 743 ```ts 744 import { hilog } from '@kit.PerformanceAnalysisKit'; 745 import { BusinessError } from '@kit.BasicServicesKit'; 746 747 let data = rpc.MessageSequence.create(); 748 try { 749 data.writeShort(8); 750 } catch (error) { 751 let e: BusinessError = error as BusinessError; 752 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 753 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 754 } 755 try { 756 let ret = data.readShort(); 757 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 758 } catch (error) { 759 let e: BusinessError = error as BusinessError; 760 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 761 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 762 } 763 ``` 764 765### writeInt 766 767writeInt(val: number): void 768 769将整数值写入MessageSequence实例。 770 771**系统能力**:SystemCapability.Communication.IPC.Core 772 773**参数:** 774 775 | 参数名 | 类型 | 必填 | 说明 | 776 | ------ | ------ | ---- | ---------------- | 777 | val | number | 是 | 要写入的整数值。 | 778 779**错误码:** 780 781以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 782 783 | 错误码ID | 错误信息 | 784 | -------- | -------- | 785 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 786 | 1900009 | Failed to write data to the message sequence. | 787 788**示例:** 789 790 ```ts 791 import { hilog } from '@kit.PerformanceAnalysisKit'; 792 import { BusinessError } from '@kit.BasicServicesKit'; 793 794 let data = rpc.MessageSequence.create(); 795 try { 796 data.writeInt(10); 797 } catch (error) { 798 let e: BusinessError = error as BusinessError; 799 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 800 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 801 } 802 ``` 803 804### readInt 805 806readInt(): number 807 808从MessageSequence实例读取整数值。 809 810**系统能力**:SystemCapability.Communication.IPC.Core 811 812**返回值:** 813 814 | 类型 | 说明 | 815 | ------ | ------------ | 816 | number | 返回整数值。 | 817 818**错误码:** 819 820以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 821 822 | 错误码ID | 错误信息 | 823 | -------- | -------- | 824 | 1900010 | Failed to read data from the message sequence. | 825 826**示例:** 827 828 ```ts 829 import { hilog } from '@kit.PerformanceAnalysisKit'; 830 import { BusinessError } from '@kit.BasicServicesKit'; 831 832 let data = rpc.MessageSequence.create(); 833 try { 834 data.writeInt(10); 835 } catch (error) { 836 let e: BusinessError = error as BusinessError; 837 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 838 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 839 } 840 try { 841 let ret = data.readInt(); 842 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 843 } catch (error) { 844 let e: BusinessError = error as BusinessError; 845 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 846 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 847 } 848 ``` 849 850### writeLong 851 852writeLong(val: number): void 853 854将长整数值写入MessageSequence实例。 855 856**系统能力**:SystemCapability.Communication.IPC.Core 857 858**参数:** 859 860 | 参数名 | 类型 | 必填 | 说明 | 861 | ------ | ------ | ---- | ---------------- | 862 | val | number | 是 | 要写入的长整数值。 | 863 864**错误码:** 865 866以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 867 868 | 错误码ID | 错误信息 | 869 | -------- | -------- | 870 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 871 | 1900009 | Failed to write data to the message sequence. | 872 873**示例:** 874 875 ```ts 876 import { hilog } from '@kit.PerformanceAnalysisKit'; 877 import { BusinessError } from '@kit.BasicServicesKit'; 878 879 let data = rpc.MessageSequence.create(); 880 try { 881 data.writeLong(10000); 882 } catch (error) { 883 let e: BusinessError = error as BusinessError; 884 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 885 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 886 } 887 ``` 888 889### readLong 890 891readLong(): number 892 893从MessageSequence实例中读取长整数值。 894 895**系统能力**:SystemCapability.Communication.IPC.Core 896 897**返回值:** 898 899 | 类型 | 说明 | 900 | ------ | -------------- | 901 | number | 返回长整数值。 | 902 903**错误码:** 904 905以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 906 907 | 错误码ID | 错误信息 | 908 | -------- | -------- | 909 | 1900010 | Failed to read data from the message sequence. | 910 911**示例:** 912 913 ```ts 914 import { hilog } from '@kit.PerformanceAnalysisKit'; 915 import { BusinessError } from '@kit.BasicServicesKit'; 916 917 let data = rpc.MessageSequence.create(); 918 try { 919 data.writeLong(10000); 920 } catch (error) { 921 let e: BusinessError = error as BusinessError; 922 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 923 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 924 } 925 try { 926 let ret = data.readLong(); 927 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 928 } catch (error) { 929 let e: BusinessError = error as BusinessError; 930 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 931 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 932 } 933 ``` 934 935### writeFloat 936 937writeFloat(val: number): void 938 939将浮点值写入MessageSequence实例。 940 941**系统能力**:SystemCapability.Communication.IPC.Core 942 943**参数:** 944 945 | 参数名 | 类型 | 必填 | 说明 | 946 | ------ | ------ | ---- | ----- | 947 | val | number | 是 | 要写入的浮点值。 | 948 949**错误码:** 950 951以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 952 953 | 错误码ID | 错误信息 | 954 | -------- | -------- | 955 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 956 | 1900009 | Failed to write data to the message sequence. | 957 958**示例:** 959 960 ```ts 961 import { hilog } from '@kit.PerformanceAnalysisKit'; 962 import { BusinessError } from '@kit.BasicServicesKit'; 963 964 let data = rpc.MessageSequence.create(); 965 try { 966 data.writeFloat(1.2); 967 } catch (error) { 968 let e: BusinessError = error as BusinessError; 969 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 970 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 971 } 972 ``` 973 974### readFloat 975 976readFloat(): number 977 978从MessageSequence实例中读取浮点值。 979 980**系统能力**:SystemCapability.Communication.IPC.Core 981 982**返回值:** 983 984 | 类型 | 说明 | 985 | ------ | ------------ | 986 | number | 返回浮点值。 | 987 988**错误码:** 989 990以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 991 992 | 错误码ID | 错误信息 | 993 | -------- | -------- | 994 | 1900010 | Failed to read data from the message sequence. | 995 996**示例:** 997 998 ```ts 999 import { hilog } from '@kit.PerformanceAnalysisKit'; 1000 import { BusinessError } from '@kit.BasicServicesKit'; 1001 1002 let data = rpc.MessageSequence.create(); 1003 try { 1004 data.writeFloat(1.2); 1005 } catch (error) { 1006 let e: BusinessError = error as BusinessError; 1007 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 1008 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 1009 } 1010 try { 1011 let ret = data.readFloat(); 1012 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 1013 } catch (error) { 1014 let e: BusinessError = error as BusinessError; 1015 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 1016 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 1017 } 1018 ``` 1019 1020### writeDouble 1021 1022writeDouble(val: number): void 1023 1024将双精度浮点值写入MessageSequence实例。 1025 1026**系统能力**:SystemCapability.Communication.IPC.Core 1027 1028**参数:** 1029 1030 | 参数名 | 类型 | 必填 | 说明 | 1031 | ------ | ------ | ---- | ---------------------- | 1032 | val | number | 是 | 要写入的双精度浮点值。 | 1033 1034**错误码:** 1035 1036以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1037 1038 | 错误码ID | 错误信息 | 1039 | -------- | -------- | 1040 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1041 | 1900009 | Failed to write data to the message sequence. | 1042 1043**示例:** 1044 1045 ```ts 1046 import { hilog } from '@kit.PerformanceAnalysisKit'; 1047 import { BusinessError } from '@kit.BasicServicesKit'; 1048 1049 let data = rpc.MessageSequence.create(); 1050 try { 1051 data.writeDouble(10.2); 1052 } catch (error) { 1053 let e: BusinessError = error as BusinessError; 1054 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1055 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1056 } 1057 ``` 1058 1059### readDouble 1060 1061readDouble(): number 1062 1063从MessageSequence实例读取双精度浮点值。 1064 1065**系统能力**:SystemCapability.Communication.IPC.Core 1066 1067**返回值:** 1068 1069 | 类型 | 说明 | 1070 | ------ | ------------------ | 1071 | number | 返回双精度浮点值。 | 1072 1073**错误码:** 1074 1075以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1076 1077 | 错误码ID | 错误信息 | 1078 | -------- | -------- | 1079 | 1900010 | Failed to read data from the message sequence. | 1080 1081**示例:** 1082 1083 ```ts 1084 import { hilog } from '@kit.PerformanceAnalysisKit'; 1085 import { BusinessError } from '@kit.BasicServicesKit'; 1086 1087 let data = rpc.MessageSequence.create(); 1088 try { 1089 data.writeDouble(10.2); 1090 } catch (error) { 1091 let e: BusinessError = error as BusinessError; 1092 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1093 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1094 } 1095 try { 1096 let ret = data.readDouble(); 1097 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1098 } catch (error) { 1099 let e: BusinessError = error as BusinessError; 1100 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1101 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1102 } 1103 ``` 1104 1105### writeBoolean 1106 1107writeBoolean(val: boolean): void 1108 1109将布尔值写入MessageSequence实例。 1110 1111**系统能力**:SystemCapability.Communication.IPC.Core 1112 1113**参数:** 1114 1115 | 参数名 | 类型 | 必填 | 说明 | 1116 | ------ | ------- | ---- | ---------------- | 1117 | val | boolean | 是 | 要写入的布尔值。 | 1118 1119**错误码:** 1120 1121以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1122 1123 | 错误码ID | 错误信息 | 1124 | -------- | -------- | 1125 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1126 | 1900009 | Failed to write data to the message sequence. | 1127 1128**示例:** 1129 1130 ```ts 1131 import { hilog } from '@kit.PerformanceAnalysisKit'; 1132 import { BusinessError } from '@kit.BasicServicesKit'; 1133 1134 let data = rpc.MessageSequence.create(); 1135 try { 1136 data.writeBoolean(false); 1137 } catch (error) { 1138 let e: BusinessError = error as BusinessError; 1139 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1140 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1141 } 1142 ``` 1143 1144### readBoolean 1145 1146readBoolean(): boolean 1147 1148从MessageSequence实例读取布尔值。 1149 1150**系统能力**:SystemCapability.Communication.IPC.Core 1151 1152**返回值:** 1153 1154 | 类型 | 说明 | 1155 | ------- | -------------------- | 1156 | boolean | 返回读取到的布尔值。 | 1157 1158**错误码:** 1159 1160以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1161 1162 | 错误码ID | 错误信息 | 1163 | -------- | -------- | 1164 | 1900010 | Failed to read data from the message sequence. | 1165 1166**示例:** 1167 1168 ```ts 1169 import { hilog } from '@kit.PerformanceAnalysisKit'; 1170 import { BusinessError } from '@kit.BasicServicesKit'; 1171 1172 let data = rpc.MessageSequence.create(); 1173 try { 1174 data.writeBoolean(false); 1175 } catch (error) { 1176 let e: BusinessError = error as BusinessError; 1177 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1178 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1179 } 1180 try { 1181 let ret = data.readBoolean(); 1182 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1183 } catch (error) { 1184 let e: BusinessError = error as BusinessError; 1185 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1186 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1187 } 1188 ``` 1189 1190### writeChar 1191 1192writeChar(val: number): void 1193 1194将单个字符值写入MessageSequence实例。 1195 1196**系统能力**:SystemCapability.Communication.IPC.Core 1197 1198**参数:** 1199 1200 | 参数名 | 类型 | 必填 | 说明 | 1201 | ------ | ------ | ---- | -------------------- | 1202 | val | number | 是 | 要写入的单个字符值。 | 1203 1204**错误码:** 1205 1206以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1207 1208 | 错误码ID | 错误信息 | 1209 | -------- | -------- | 1210 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1211 | 1900009 | Failed to write data to the message sequence. | 1212 1213**示例:** 1214 1215 ```ts 1216 import { hilog } from '@kit.PerformanceAnalysisKit'; 1217 import { BusinessError } from '@kit.BasicServicesKit'; 1218 1219 let data = rpc.MessageSequence.create(); 1220 try { 1221 data.writeChar(97); 1222 } catch (error) { 1223 let e: BusinessError = error as BusinessError; 1224 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1225 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1226 } 1227 ``` 1228 1229### readChar 1230 1231readChar(): number 1232 1233从MessageSequence实例中读取单个字符值。 1234 1235**系统能力**:SystemCapability.Communication.IPC.Core 1236 1237**返回值:** 1238 1239 | 类型 | 说明 | 1240 | ------ | ---- | 1241 | number | 返回单个字符值。 | 1242 1243**错误码:** 1244 1245以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1246 1247 | 错误码ID | 错误信息 | 1248 | -------- | -------- | 1249 | 1900010 | Failed to read data from the message sequence. | 1250 1251**示例:** 1252 1253 ```ts 1254 import { hilog } from '@kit.PerformanceAnalysisKit'; 1255 import { BusinessError } from '@kit.BasicServicesKit'; 1256 1257 let data = rpc.MessageSequence.create(); 1258 try { 1259 data.writeChar(97); 1260 } catch (error) { 1261 let e: BusinessError = error as BusinessError; 1262 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1263 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1264 } 1265 try { 1266 let ret = data.readChar(); 1267 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1268 } catch (error) { 1269 let e: BusinessError = error as BusinessError; 1270 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1271 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1272 } 1273 ``` 1274 1275### writeString 1276 1277writeString(val: string): void 1278 1279将字符串值写入MessageSequence实例。 1280 1281**系统能力**:SystemCapability.Communication.IPC.Core 1282 1283**参数:** 1284 1285 | 参数名 | 类型 | 必填 | 说明 | 1286 | ------ | ------ | ---- | ----------------------------------------- | 1287 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 1288 1289**错误码:** 1290 1291以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1292 1293 | 错误码ID | 错误信息 | 1294 | -------- | -------- | 1295 | 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. | 1296 | 1900009 | Failed to write data to the message sequence. | 1297 1298**示例:** 1299 1300 ```ts 1301 import { hilog } from '@kit.PerformanceAnalysisKit'; 1302 import { BusinessError } from '@kit.BasicServicesKit'; 1303 1304 let data = rpc.MessageSequence.create(); 1305 try { 1306 data.writeString('abc'); 1307 } catch (error) { 1308 let e: BusinessError = error as BusinessError; 1309 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1310 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1311 } 1312 ``` 1313 1314### readString 1315 1316readString(): string 1317 1318从MessageSequence实例读取字符串值。 1319 1320**系统能力**:SystemCapability.Communication.IPC.Core 1321 1322**返回值:** 1323 1324 | 类型 | 说明 | 1325 | ------ | -------------- | 1326 | string | 返回字符串值。 | 1327 1328**错误码:** 1329 1330以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1331 1332 | 错误码ID | 错误信息 | 1333 | -------- | -------- | 1334 | 1900010 | Failed to read data from the message sequence. | 1335 1336**示例:** 1337 1338 ```ts 1339 import { hilog } from '@kit.PerformanceAnalysisKit'; 1340 import { BusinessError } from '@kit.BasicServicesKit'; 1341 1342 let data = rpc.MessageSequence.create(); 1343 try { 1344 data.writeString('abc'); 1345 } catch (error) { 1346 let e: BusinessError = error as BusinessError; 1347 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1348 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1349 } 1350 try { 1351 let ret = data.readString(); 1352 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1353 } catch (error) { 1354 let e: BusinessError = error as BusinessError; 1355 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1356 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1357 } 1358 ``` 1359 1360### writeParcelable 1361 1362writeParcelable(val: Parcelable): void 1363 1364将自定义序列化对象写入MessageSequence实例。 1365 1366**系统能力**:SystemCapability.Communication.IPC.Core 1367 1368**参数:** 1369 1370| 参数名 | 类型 | 必填 | 说明 | 1371| ------ | --------- | ---- | ------ | 1372| val | [Parcelable](#parcelable9) | 是 | 要写入的可序列对象。 | 1373 1374**错误码:** 1375 1376以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1377 1378 | 错误码ID | 错误信息 | 1379 | -------- | -------- | 1380 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1381 | 1900009 | Failed to write data to the message sequence. | 1382 1383**示例:** 1384 1385 ```ts 1386 import { hilog } from '@kit.PerformanceAnalysisKit'; 1387 import { BusinessError } from '@kit.BasicServicesKit'; 1388 1389 class MyParcelable implements rpc.Parcelable { 1390 num: number = 0; 1391 str: string = ''; 1392 constructor( num: number, str: string) { 1393 this.num = num; 1394 this.str = str; 1395 } 1396 marshalling(messageSequence: rpc.MessageSequence): boolean { 1397 messageSequence.writeInt(this.num); 1398 messageSequence.writeString(this.str); 1399 return true; 1400 } 1401 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1402 this.num = messageSequence.readInt(); 1403 this.str = messageSequence.readString(); 1404 return true; 1405 } 1406 } 1407 let parcelable = new MyParcelable(1, "aaa"); 1408 let data = rpc.MessageSequence.create(); 1409 try { 1410 data.writeParcelable(parcelable); 1411 } catch (error) { 1412 let e: BusinessError = error as BusinessError; 1413 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1414 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1415 } 1416 ``` 1417 1418### readParcelable 1419 1420readParcelable(dataIn: Parcelable): void 1421 1422从MessageSequence实例中读取成员变量到指定的对象(dataIn)。 1423 1424**系统能力**:SystemCapability.Communication.IPC.Core 1425 1426**参数:** 1427 1428| 参数名 | 类型 | 必填 | 说明 | 1429| ------ | -------------------------- | ---- | ----------------------------------------- | 1430| dataIn | [Parcelable](#parcelable9) | 是 | 需要从MessageSequence读取成员变量的对象。 | 1431 1432**错误码:** 1433 1434以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1435 1436 | 错误码ID | 错误信息 | 1437 | -------- | -------- | 1438 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect. | 1439 | 1900010 | Failed to read data from the message sequence. | 1440 | 1900012 | Failed to call the JS callback function. | 1441 1442**示例:** 1443 1444 ```ts 1445 import { hilog } from '@kit.PerformanceAnalysisKit'; 1446 import { BusinessError } from '@kit.BasicServicesKit'; 1447 1448 class MyParcelable implements rpc.Parcelable { 1449 num: number = 0; 1450 str: string = ''; 1451 constructor(num: number, str: string) { 1452 this.num = num; 1453 this.str = str; 1454 } 1455 marshalling(messageSequence: rpc.MessageSequence): boolean { 1456 messageSequence.writeInt(this.num); 1457 messageSequence.writeString(this.str); 1458 return true; 1459 } 1460 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1461 this.num = messageSequence.readInt(); 1462 this.str = messageSequence.readString(); 1463 return true; 1464 } 1465 } 1466 let parcelable = new MyParcelable(1, "aaa"); 1467 let data = rpc.MessageSequence.create(); 1468 data.writeParcelable(parcelable); 1469 let ret = new MyParcelable(0, ""); 1470 try { 1471 data.readParcelable(ret); 1472 }catch (error) { 1473 let e: BusinessError = error as BusinessError; 1474 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1475 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1476 } 1477 ``` 1478 1479### writeByteArray 1480 1481writeByteArray(byteArray: number[]): void 1482 1483将字节数组写入MessageSequence实例。 1484 1485**系统能力**:SystemCapability.Communication.IPC.Core 1486 1487**参数:** 1488 1489 | 参数名 | 类型 | 必填 | 说明 | 1490 | --------- | -------- | ---- | ------------------ | 1491 | byteArray | number[] | 是 | 要写入的字节数组。 | 1492 1493**错误码:** 1494 1495以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1496 1497 | 错误码ID | 错误信息 | 1498 | -------- | -------- | 1499 | 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. | 1500 | 1900009 | Failed to write data to the message sequence. | 1501 1502**示例:** 1503 1504 ```ts 1505 import { hilog } from '@kit.PerformanceAnalysisKit'; 1506 import { BusinessError } from '@kit.BasicServicesKit'; 1507 1508 let data = rpc.MessageSequence.create(); 1509 let ByteArrayVar = [1, 2, 3, 4, 5]; 1510 try { 1511 data.writeByteArray(ByteArrayVar); 1512 } catch (error) { 1513 let e: BusinessError = error as BusinessError; 1514 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1515 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1516 } 1517 ``` 1518 1519### readByteArray 1520 1521readByteArray(dataIn: number[]): void 1522 1523从MessageSequence实例读取字节数组。 1524 1525**系统能力**:SystemCapability.Communication.IPC.Core 1526 1527**参数:** 1528 1529 | 参数名 | 类型 | 必填 | 说明 | 1530 | ------ | -------- | ---- | ------------------ | 1531 | dataIn | number[] | 是 | 要读取的字节数组。 | 1532 1533**错误码:** 1534 1535以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1536 1537 | 错误码ID | 错误信息 | 1538 | -------- | -------- | 1539 | 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. | 1540 | 1900010 | Failed to read data from the message sequence. | 1541 1542**示例:** 1543 1544 ```ts 1545 import { hilog } from '@kit.PerformanceAnalysisKit'; 1546 import { BusinessError } from '@kit.BasicServicesKit'; 1547 1548 let data = rpc.MessageSequence.create(); 1549 let ByteArrayVar = [1, 2, 3, 4, 5]; 1550 try { 1551 data.writeByteArray(ByteArrayVar); 1552 } catch (error) { 1553 let e: BusinessError = error as BusinessError; 1554 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1555 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1556 } 1557 try { 1558 let array: Array<number> = new Array(5); 1559 data.readByteArray(array); 1560 } catch (error) { 1561 let e: BusinessError = error as BusinessError; 1562 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1563 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1564 } 1565 ``` 1566 1567### readByteArray 1568 1569readByteArray(): number[] 1570 1571从MessageSequence实例中读取字节数组。 1572 1573**系统能力**:SystemCapability.Communication.IPC.Core 1574 1575**返回值:** 1576 1577 | 类型 | 说明 | 1578 | -------- | -------------- | 1579 | number[] | 返回字节数组。 | 1580 1581**错误码:** 1582 1583以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1584 1585 | 错误码ID | 错误信息 | 1586 | -------- | -------- | 1587 | 401 | check param failed | 1588 | 1900010 | Failed to read data from the message sequence. | 1589 1590**示例:** 1591 1592 ```ts 1593 import { hilog } from '@kit.PerformanceAnalysisKit'; 1594 import { BusinessError } from '@kit.BasicServicesKit'; 1595 1596 let data = rpc.MessageSequence.create(); 1597 let byteArrayVar = [1, 2, 3, 4, 5]; 1598 try { 1599 data.writeByteArray(byteArrayVar); 1600 } catch (error) { 1601 let e: BusinessError = error as BusinessError; 1602 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1603 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1604 } 1605 try { 1606 let array = data.readByteArray(); 1607 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1608 } catch (error) { 1609 let e: BusinessError = error as BusinessError; 1610 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1611 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1612 } 1613 ``` 1614 1615### writeShortArray 1616 1617writeShortArray(shortArray: number[]): void 1618 1619将短整数数组写入MessageSequence实例。 1620 1621**系统能力**:SystemCapability.Communication.IPC.Core 1622 1623**参数:** 1624 1625 | 参数名 | 类型 | 必填 | 说明 | 1626 | ---------- | -------- | ---- | -------------------- | 1627 | shortArray | number[] | 是 | 要写入的短整数数组。 | 1628 1629**错误码:** 1630 1631以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1632 1633 | 错误码ID | 错误信息 | 1634 | -------- | -------- | 1635 | 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. | 1636 | 1900009 | Failed to write data to the message sequence. | 1637 1638**示例:** 1639 1640 ```ts 1641 import { hilog } from '@kit.PerformanceAnalysisKit'; 1642 import { BusinessError } from '@kit.BasicServicesKit'; 1643 1644 let data = rpc.MessageSequence.create(); 1645 try { 1646 data.writeShortArray([11, 12, 13]); 1647 } catch (error) { 1648 let e: BusinessError = error as BusinessError; 1649 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1650 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1651 } 1652 ``` 1653 1654### readShortArray 1655 1656readShortArray(dataIn: number[]): void 1657 1658从MessageSequence实例中读取短整数数组。 1659 1660**系统能力**:SystemCapability.Communication.IPC.Core 1661 1662**参数:** 1663 1664 | 参数名 | 类型 | 必填 | 说明 | 1665 | ------ | -------- | ---- | -------------------- | 1666 | dataIn | number[] | 是 | 要读取的短整数数组。 | 1667 1668**错误码:** 1669 1670以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1671 1672 | 错误码ID | 错误信息 | 1673 | -------- | -------- | 1674 | 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. | 1675 | 1900010 | Failed to read data from the message sequence. | 1676 1677**示例:** 1678 1679 ```ts 1680 import { hilog } from '@kit.PerformanceAnalysisKit'; 1681 import { BusinessError } from '@kit.BasicServicesKit'; 1682 1683 let data = rpc.MessageSequence.create(); 1684 try { 1685 data.writeShortArray([11, 12, 13]); 1686 } catch (error) { 1687 let e: BusinessError = error as BusinessError; 1688 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1689 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1690 } 1691 try { 1692 let array: Array<number> = new Array(3); 1693 data.readShortArray(array); 1694 } catch (error) { 1695 let e: BusinessError = error as BusinessError; 1696 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1697 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1698 } 1699 ``` 1700 1701### readShortArray 1702 1703readShortArray(): number[] 1704 1705从MessageSequence实例中读取短整数数组。 1706 1707**系统能力**:SystemCapability.Communication.IPC.Core 1708 1709**返回值:** 1710 1711 | 类型 | 说明 | 1712 | -------- | ---------------- | 1713 | number[] | 返回短整数数组。 | 1714 1715**错误码:** 1716 1717以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1718 1719 | 错误码ID | 错误信息 | 1720 | -------- | -------- | 1721 | 1900010 | Failed to read data from the message sequence. | 1722 1723**示例:** 1724 1725 ```ts 1726 import { hilog } from '@kit.PerformanceAnalysisKit'; 1727 import { BusinessError } from '@kit.BasicServicesKit'; 1728 1729 let data = rpc.MessageSequence.create(); 1730 try { 1731 data.writeShortArray([11, 12, 13]); 1732 } catch (error) { 1733 let e: BusinessError = error as BusinessError; 1734 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1735 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1736 } 1737 try { 1738 let array = data.readShortArray(); 1739 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1740 } catch (error) { 1741 let e: BusinessError = error as BusinessError; 1742 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1743 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1744 } 1745 ``` 1746 1747### writeIntArray 1748 1749writeIntArray(intArray: number[]): void 1750 1751将整数数组写入MessageSequence实例。 1752 1753**系统能力**:SystemCapability.Communication.IPC.Core 1754 1755**参数:** 1756 1757 | 参数名 | 类型 | 必填 | 说明 | 1758 | -------- | -------- | ---- | ------------------ | 1759 | intArray | number[] | 是 | 要写入的整数数组。 | 1760 1761**错误码:** 1762 1763以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1764 1765 | 错误码ID | 错误信息 | 1766 | -------- | -------- | 1767 | 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. | 1768 | 1900009 | Failed to write data to the message sequence. | 1769 1770**示例:** 1771 1772 ```ts 1773 import { hilog } from '@kit.PerformanceAnalysisKit'; 1774 import { BusinessError } from '@kit.BasicServicesKit'; 1775 1776 let data = rpc.MessageSequence.create(); 1777 try { 1778 data.writeIntArray([100, 111, 112]); 1779 } catch (error) { 1780 let e: BusinessError = error as BusinessError; 1781 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1782 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1783 } 1784 ``` 1785 1786### readIntArray 1787 1788readIntArray(dataIn: number[]): void 1789 1790从MessageSequence实例中读取整数数组。 1791 1792**系统能力**:SystemCapability.Communication.IPC.Core 1793 1794**参数:** 1795 1796 | 参数名 | 类型 | 必填 | 说明 | 1797 | ------ | -------- | ---- | ------------------ | 1798 | dataIn | number[] | 是 | 要读取的整数数组。 | 1799 1800**错误码:** 1801 1802以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1803 1804 | 错误码ID | 错误信息 | 1805 | -------- | -------- | 1806 | 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. | 1807 | 1900010 | Failed to read data from the message sequence. | 1808 1809**示例:** 1810 1811 ```ts 1812 import { hilog } from '@kit.PerformanceAnalysisKit'; 1813 import { BusinessError } from '@kit.BasicServicesKit'; 1814 1815 let data = rpc.MessageSequence.create(); 1816 try { 1817 data.writeIntArray([100, 111, 112]); 1818 } catch (error) { 1819 let e: BusinessError = error as BusinessError; 1820 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1821 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1822 } 1823 let array: Array<number> = new Array(3); 1824 try { 1825 data.readIntArray(array); 1826 } catch (error) { 1827 let e: BusinessError = error as BusinessError; 1828 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1829 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1830 } 1831 ``` 1832 1833### readIntArray 1834 1835readIntArray(): number[] 1836 1837从MessageSequence实例中读取整数数组。 1838 1839**系统能力**:SystemCapability.Communication.IPC.Core 1840 1841**返回值:** 1842 1843 | 类型 | 说明 | 1844 | -------- | -------------- | 1845 | number[] | 返回整数数组。 | 1846 1847**错误码:** 1848 1849以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1850 1851 | 错误码ID | 错误信息 | 1852 | -------- | -------- | 1853 | 1900010 | Failed to read data from the message sequence. | 1854 1855**示例:** 1856 1857 ```ts 1858 import { hilog } from '@kit.PerformanceAnalysisKit'; 1859 import { BusinessError } from '@kit.BasicServicesKit'; 1860 1861 let data = rpc.MessageSequence.create(); 1862 try { 1863 data.writeIntArray([100, 111, 112]); 1864 } catch (error) { 1865 let e: BusinessError = error as BusinessError; 1866 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1867 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1868 } 1869 try { 1870 let array = data.readIntArray(); 1871 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1872 } catch (error) { 1873 let e: BusinessError = error as BusinessError; 1874 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1875 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1876 } 1877 ``` 1878 1879### writeLongArray 1880 1881writeLongArray(longArray: number[]): void 1882 1883将长整数数组写入MessageSequence实例。 1884 1885**系统能力**:SystemCapability.Communication.IPC.Core 1886 1887**参数:** 1888 1889 | 参数名 | 类型 | 必填 | 说明 | 1890 | --------- | -------- | ---- | -------------------- | 1891 | longArray | number[] | 是 | 要写入的长整数数组。 | 1892 1893**错误码:** 1894 1895以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1896 1897 | 错误码ID | 错误信息 | 1898 | -------- | -------- | 1899 | 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. | 1900 | 1900009 | Failed to write data to the message sequence. | 1901 1902**示例:** 1903 1904 ```ts 1905 import { hilog } from '@kit.PerformanceAnalysisKit'; 1906 import { BusinessError } from '@kit.BasicServicesKit'; 1907 1908 let data = rpc.MessageSequence.create(); 1909 try { 1910 data.writeLongArray([1111, 1112, 1113]); 1911 }catch (error){ 1912 let e: BusinessError = error as BusinessError; 1913 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1914 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1915 } 1916 ``` 1917 1918### readLongArray 1919 1920readLongArray(dataIn: number[]): void 1921 1922从MessageSequence实例读取的长整数数组。 1923 1924**系统能力**:SystemCapability.Communication.IPC.Core 1925 1926**参数:** 1927 1928 | 参数名 | 类型 | 必填 | 说明 | 1929 | ------ | -------- | ---- | -------------------- | 1930 | dataIn | number[] | 是 | 要读取的长整数数组。 | 1931 1932**错误码:** 1933 1934以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1935 1936 | 错误码ID | 错误信息 | 1937 | -------- | -------- | 1938 | 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. | 1939 | 1900010 | Failed to read data from the message sequence. | 1940 1941**示例:** 1942 1943 ```ts 1944 import { hilog } from '@kit.PerformanceAnalysisKit'; 1945 import { BusinessError } from '@kit.BasicServicesKit'; 1946 1947 let data = rpc.MessageSequence.create(); 1948 try { 1949 data.writeLongArray([1111, 1112, 1113]); 1950 } catch (error) { 1951 let e: BusinessError = error as BusinessError; 1952 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1953 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1954 } 1955 let array: Array<number> = new Array(3); 1956 try { 1957 data.readLongArray(array); 1958 } catch (error) { 1959 let e: BusinessError = error as BusinessError; 1960 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1961 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1962 } 1963 ``` 1964 1965### readLongArray 1966 1967readLongArray(): number[] 1968 1969从MessageSequence实例中读取所有的长整数数组。 1970 1971**系统能力**:SystemCapability.Communication.IPC.Core 1972 1973**返回值:** 1974 1975 | 类型 | 说明 | 1976 | -------- | ---------------- | 1977 | number[] | 返回长整数数组。 | 1978 1979**错误码:** 1980 1981以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1982 1983 | 错误码ID | 错误信息 | 1984 | -------- | -------- | 1985 | 1900010 | Failed to read data from the message sequence. | 1986 1987**示例:** 1988 1989 ```ts 1990 import { hilog } from '@kit.PerformanceAnalysisKit'; 1991 import { BusinessError } from '@kit.BasicServicesKit'; 1992 1993 let data = rpc.MessageSequence.create(); 1994 try { 1995 data.writeLongArray([1111, 1112, 1113]); 1996 } catch (error) { 1997 let e: BusinessError = error as BusinessError; 1998 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1999 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 2000 } 2001 try { 2002 let array = data.readLongArray(); 2003 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 2004 } catch (error) { 2005 let e: BusinessError = error as BusinessError; 2006 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 2007 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 2008 } 2009 ``` 2010 2011### writeFloatArray 2012 2013writeFloatArray(floatArray: number[]): void 2014 2015将浮点数组写入MessageSequence实例。 2016 2017**系统能力**:SystemCapability.Communication.IPC.Core 2018 2019**参数:** 2020 2021 | 参数名 | 类型 | 必填 | 说明 | 2022 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2023 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2024 2025**错误码:** 2026 2027以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2028 2029 | 错误码ID | 错误信息 | 2030 | -------- | -------- | 2031 | 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. | 2032 | 1900009 | Failed to write data to the message sequence. | 2033 2034**示例:** 2035 2036 ```ts 2037 import { hilog } from '@kit.PerformanceAnalysisKit'; 2038 import { BusinessError } from '@kit.BasicServicesKit'; 2039 2040 let data = rpc.MessageSequence.create(); 2041 try { 2042 data.writeFloatArray([1.2, 1.3, 1.4]); 2043 } catch (error) { 2044 let e: BusinessError = error as BusinessError; 2045 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2046 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2047 } 2048 ``` 2049 2050### readFloatArray 2051 2052readFloatArray(dataIn: number[]): void 2053 2054从MessageSequence实例中读取浮点数组。 2055 2056**系统能力**:SystemCapability.Communication.IPC.Core 2057 2058**参数:** 2059 2060 | 参数名 | 类型 | 必填 | 说明 | 2061 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2062 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2063 2064**错误码:** 2065 2066以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2067 2068 | 错误码ID | 错误信息 | 2069 | -------- | -------- | 2070 | 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. | 2071 | 1900010 | Failed to read data from the message sequence. | 2072 2073**示例:** 2074 2075 ```ts 2076 import { hilog } from '@kit.PerformanceAnalysisKit'; 2077 import { BusinessError } from '@kit.BasicServicesKit'; 2078 2079 let data = rpc.MessageSequence.create(); 2080 try { 2081 data.writeFloatArray([1.2, 1.3, 1.4]); 2082 }catch (error){ 2083 let e: BusinessError = error as BusinessError; 2084 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2085 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2086 } 2087 let array: Array<number> = new Array(3); 2088 try { 2089 data.readFloatArray(array); 2090 } catch (error) { 2091 let e: BusinessError = error as BusinessError; 2092 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2093 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2094 } 2095 ``` 2096 2097### readFloatArray 2098 2099readFloatArray(): number[] 2100 2101从MessageSequence实例中读取浮点数组。 2102 2103**系统能力**:SystemCapability.Communication.IPC.Core 2104 2105**返回值:** 2106 2107 | 类型 | 说明 | 2108 | -------- | -------------- | 2109 | number[] | 返回浮点数组。 | 2110 2111**错误码:** 2112 2113以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2114 2115 | 错误码ID | 错误信息 | 2116 | -------- | -------- | 2117 | 1900010 | Failed to read data from the message sequence. | 2118 2119**示例:** 2120 2121 ```ts 2122 import { hilog } from '@kit.PerformanceAnalysisKit'; 2123 import { BusinessError } from '@kit.BasicServicesKit'; 2124 2125 let data = rpc.MessageSequence.create(); 2126 try { 2127 data.writeFloatArray([1.2, 1.3, 1.4]); 2128 } catch (error) { 2129 let e: BusinessError = error as BusinessError; 2130 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2131 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2132 } 2133 try { 2134 let array = data.readFloatArray(); 2135 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2136 } catch (error) { 2137 let e: BusinessError = error as BusinessError; 2138 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2139 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2140 } 2141 ``` 2142 2143### writeDoubleArray 2144 2145writeDoubleArray(doubleArray: number[]): void 2146 2147将双精度浮点数组写入MessageSequence实例。 2148 2149**系统能力**:SystemCapability.Communication.IPC.Core 2150 2151**参数:** 2152 2153 | 参数名 | 类型 | 必填 | 说明 | 2154 | ----------- | -------- | ---- | ------------------------ | 2155 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 2156 2157**错误码:** 2158 2159以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2160 2161 | 错误码ID | 错误信息 | 2162 | -------- | -------- | 2163 | 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. | 2164 | 1900009 | Failed to write data to the message sequence. | 2165 2166**示例:** 2167 2168 ```ts 2169 import { hilog } from '@kit.PerformanceAnalysisKit'; 2170 import { BusinessError } from '@kit.BasicServicesKit'; 2171 2172 let data = rpc.MessageSequence.create(); 2173 try { 2174 data.writeDoubleArray([11.1, 12.2, 13.3]); 2175 } catch (error) { 2176 let e: BusinessError = error as BusinessError; 2177 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2178 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2179 } 2180 ``` 2181 2182### readDoubleArray 2183 2184readDoubleArray(dataIn: number[]): void 2185 2186从MessageSequence实例中读取双精度浮点数组。 2187 2188**系统能力**:SystemCapability.Communication.IPC.Core 2189 2190**参数:** 2191 2192 | 参数名 | 类型 | 必填 | 说明 | 2193 | ------ | -------- | ---- | ------------------------ | 2194 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 2195 2196**错误码:** 2197 2198以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2199 2200 | 错误码ID | 错误信息 | 2201 | -------- | -------- | 2202 | 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. | 2203 | 1900010 | Failed to read data from the message sequence. | 2204 2205**示例:** 2206 2207 ```ts 2208 import { hilog } from '@kit.PerformanceAnalysisKit'; 2209 import { BusinessError } from '@kit.BasicServicesKit'; 2210 2211 let data = rpc.MessageSequence.create(); 2212 try { 2213 data.writeDoubleArray([11.1, 12.2, 13.3]); 2214 } catch (error) { 2215 let e: BusinessError = error as BusinessError; 2216 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2217 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2218 } 2219 let array: Array<number> = new Array(3); 2220 try { 2221 data.readDoubleArray(array); 2222 } catch (error) { 2223 let e: BusinessError = error as BusinessError; 2224 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2225 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2226 } 2227 ``` 2228 2229### readDoubleArray 2230 2231readDoubleArray(): number[] 2232 2233从MessageSequence实例读取所有双精度浮点数组。 2234 2235**系统能力**:SystemCapability.Communication.IPC.Core 2236 2237**返回值:** 2238 2239 | 类型 | 说明 | 2240 | -------- | -------------------- | 2241 | number[] | 返回双精度浮点数组。 | 2242 2243**错误码:** 2244 2245以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2246 2247 | 错误码ID | 错误信息 | 2248 | -------- | -------- | 2249 | 1900010 | Failed to read data from the message sequence. | 2250 2251**示例:** 2252 2253 ```ts 2254 import { hilog } from '@kit.PerformanceAnalysisKit'; 2255 import { BusinessError } from '@kit.BasicServicesKit'; 2256 2257 let data = rpc.MessageSequence.create(); 2258 try { 2259 data.writeDoubleArray([11.1, 12.2, 13.3]); 2260 } catch (error) { 2261 let e: BusinessError = error as BusinessError; 2262 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2263 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2264 } 2265 try { 2266 let array = data.readDoubleArray(); 2267 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2268 } catch (error) { 2269 let e: BusinessError = error as BusinessError; 2270 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2271 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2272 } 2273 ``` 2274 2275### writeBooleanArray 2276 2277writeBooleanArray(booleanArray: boolean[]): void 2278 2279将布尔数组写入MessageSequence实例。 2280 2281**系统能力**:SystemCapability.Communication.IPC.Core 2282 2283**参数:** 2284 2285 | 参数名 | 类型 | 必填 | 说明 | 2286 | ------------ | --------- | ---- | ------------------ | 2287 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 2288 2289**错误码:** 2290 2291以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2292 2293 | 错误码ID | 错误信息 | 2294 | -------- | -------- | 2295 | 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. | 2296 | 1900009 | Failed to write data to the message sequence. | 2297 2298**示例:** 2299 2300 ```ts 2301 import { hilog } from '@kit.PerformanceAnalysisKit'; 2302 import { BusinessError } from '@kit.BasicServicesKit'; 2303 2304 let data = rpc.MessageSequence.create(); 2305 try { 2306 data.writeBooleanArray([false, true, false]); 2307 } catch (error) { 2308 let e: BusinessError = error as BusinessError; 2309 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2310 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2311 } 2312 ``` 2313 2314### readBooleanArray 2315 2316readBooleanArray(dataIn: boolean[]): void 2317 2318从MessageSequence实例中读取布尔数组。 2319 2320**系统能力**:SystemCapability.Communication.IPC.Core 2321 2322**参数:** 2323 2324 | 参数名 | 类型 | 必填 | 说明 | 2325 | ------ | --------- | ---- | ------------------ | 2326 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 2327 2328**错误码:** 2329 2330以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2331 2332 | 错误码ID | 错误信息 | 2333 | -------- | -------- | 2334 | 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. | 2335 | 1900010 | Failed to read data from the message sequence. | 2336 2337**示例:** 2338 2339 ```ts 2340 import { hilog } from '@kit.PerformanceAnalysisKit'; 2341 import { BusinessError } from '@kit.BasicServicesKit'; 2342 2343 let data = rpc.MessageSequence.create(); 2344 try { 2345 data.writeBooleanArray([false, true, false]); 2346 } catch (error) { 2347 let e: BusinessError = error as BusinessError; 2348 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2349 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2350 } 2351 let array: Array<boolean> = new Array(3); 2352 try { 2353 data.readBooleanArray(array); 2354 } catch (error) { 2355 let e: BusinessError = error as BusinessError; 2356 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2357 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2358 } 2359 ``` 2360 2361### readBooleanArray 2362 2363readBooleanArray(): boolean[] 2364 2365从MessageSequence实例中读取所有布尔数组。 2366 2367**系统能力**:SystemCapability.Communication.IPC.Core 2368 2369**返回值:** 2370 2371 | 类型 | 说明 | 2372 | --------- | -------------- | 2373 | boolean[] | 返回布尔数组。 | 2374 2375**错误码:** 2376 2377以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2378 2379 | 错误码ID | 错误信息 | 2380 | -------- | -------- | 2381 | 1900010 | Failed to read data from the message sequence. | 2382 2383**示例:** 2384 2385 ```ts 2386 import { hilog } from '@kit.PerformanceAnalysisKit'; 2387 import { BusinessError } from '@kit.BasicServicesKit'; 2388 2389 let data = rpc.MessageSequence.create(); 2390 try { 2391 data.writeBooleanArray([false, true, false]); 2392 } catch (error) { 2393 let e: BusinessError = error as BusinessError; 2394 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2395 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2396 } 2397 try { 2398 let array = data.readBooleanArray(); 2399 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2400 } catch (error) { 2401 let e: BusinessError = error as BusinessError; 2402 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2403 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2404 } 2405 ``` 2406 2407### writeCharArray 2408 2409writeCharArray(charArray: number[]): void 2410 2411将单个字符数组写入MessageSequence实例。 2412 2413**系统能力**:SystemCapability.Communication.IPC.Core 2414 2415**参数:** 2416 2417 | 参数名 | 类型 | 必填 | 说明 | 2418 | --------- | -------- | ---- | ---------------------- | 2419 | charArray | number[] | 是 | 要写入的单个字符数组。 | 2420 2421**错误码:** 2422 2423以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2424 2425 | 错误码ID | 错误信息 | 2426 | -------- | -------- | 2427 | 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. | 2428 | 1900009 | Failed to write data to the message sequence. | 2429 2430**示例:** 2431 2432 ```ts 2433 import { hilog } from '@kit.PerformanceAnalysisKit'; 2434 import { BusinessError } from '@kit.BasicServicesKit'; 2435 2436 let data = rpc.MessageSequence.create(); 2437 try { 2438 data.writeCharArray([97, 98, 88]); 2439 } catch (error) { 2440 let e: BusinessError = error as BusinessError; 2441 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2442 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2443 } 2444 ``` 2445 2446### readCharArray 2447 2448readCharArray(dataIn: number[]): void 2449 2450从MessageSequence实例中读取单个字符数组。 2451 2452**系统能力**:SystemCapability.Communication.IPC.Core 2453 2454**参数:** 2455 2456 | 参数名 | 类型 | 必填 | 说明 | 2457 | ------ | -------- | ---- | ---------------------- | 2458 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 2459 2460**错误码:** 2461 2462以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2463 2464 | 错误码ID | 错误信息 | 2465 | -------- | -------- | 2466 | 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. | 2467 | 1900010 | Failed to read data from the message sequence. | 2468 2469**示例:** 2470 2471 ```ts 2472 import { hilog } from '@kit.PerformanceAnalysisKit'; 2473 import { BusinessError } from '@kit.BasicServicesKit'; 2474 2475 let data = rpc.MessageSequence.create(); 2476 try { 2477 data.writeCharArray([97, 98, 88]); 2478 } catch (error) { 2479 let e: BusinessError = error as BusinessError; 2480 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2481 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2482 } 2483 let array: Array<number> = new Array(3); 2484 try { 2485 data.readCharArray(array); 2486 } catch (error) { 2487 let e: BusinessError = error as BusinessError; 2488 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2489 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2490 } 2491 ``` 2492 2493### readCharArray 2494 2495readCharArray(): number[] 2496 2497从MessageSequence实例读取单个字符数组。 2498 2499**系统能力**:SystemCapability.Communication.IPC.Core 2500 2501**返回值:** 2502 2503 | 类型 | 说明 | 2504 | -------- | ------------------ | 2505 | number[] | 返回单个字符数组。 | 2506 2507**错误码:** 2508 2509以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2510 2511 | 错误码ID | 错误信息 | 2512 | -------- | -------- | 2513 | 1900010 | Failed to read data from the message sequence. | 2514 2515**示例:** 2516 2517 ```ts 2518 import { hilog } from '@kit.PerformanceAnalysisKit'; 2519 import { BusinessError } from '@kit.BasicServicesKit'; 2520 2521 let data = rpc.MessageSequence.create(); 2522 try { 2523 data.writeCharArray([97, 98, 88]); 2524 } catch (error) { 2525 let e: BusinessError = error as BusinessError; 2526 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2527 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2528 } 2529 try { 2530 let array = data.readCharArray(); 2531 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2532 } catch (error) { 2533 let e: BusinessError = error as BusinessError; 2534 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2535 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2536 } 2537 ``` 2538 2539### writeStringArray 2540 2541writeStringArray(stringArray: string[]): void 2542 2543将字符串数组写入MessageSequence实例。 2544 2545**系统能力**:SystemCapability.Communication.IPC.Core 2546 2547**参数:** 2548 2549 | 参数名 | 类型 | 必填 | 说明 | 2550 | ----------- | -------- | ---- | ------------------------------------------------------- | 2551 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 2552 2553**错误码:** 2554 2555以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2556 2557 | 错误码ID | 错误信息 | 2558 | -------- | -------- | 2559 | 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. | 2560 | 1900009 | Failed to write data to the message sequence. | 2561 2562**示例:** 2563 2564 ```ts 2565 import { hilog } from '@kit.PerformanceAnalysisKit'; 2566 import { BusinessError } from '@kit.BasicServicesKit'; 2567 2568 let data = rpc.MessageSequence.create(); 2569 try { 2570 data.writeStringArray(["abc", "def"]); 2571 } catch (error) { 2572 let e: BusinessError = error as BusinessError; 2573 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2574 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2575 } 2576 ``` 2577 2578### readStringArray 2579 2580readStringArray(dataIn: string[]): void 2581 2582从MessageSequence实例读取字符串数组。 2583 2584**系统能力**:SystemCapability.Communication.IPC.Core 2585 2586**参数:** 2587 2588 | 参数名 | 类型 | 必填 | 说明 | 2589 | ------ | -------- | ---- | -------------------- | 2590 | dataIn | string[] | 是 | 要读取的字符串数组。 | 2591 2592**错误码:** 2593 2594以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2595 2596 | 错误码ID | 错误信息 | 2597 | -------- | -------- | 2598 | 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. | 2599 | 1900010 | Failed to read data from the message sequence. | 2600 2601**示例:** 2602 2603 ```ts 2604 import { hilog } from '@kit.PerformanceAnalysisKit'; 2605 import { BusinessError } from '@kit.BasicServicesKit'; 2606 2607 let data = rpc.MessageSequence.create(); 2608 try { 2609 data.writeStringArray(["abc", "def"]); 2610 } catch (error) { 2611 let e: BusinessError = error as BusinessError; 2612 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2613 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2614 } 2615 let array: Array<string> = new Array(2); 2616 try { 2617 data.readStringArray(array); 2618 } catch (error) { 2619 let e: BusinessError = error as BusinessError; 2620 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2621 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2622 } 2623 ``` 2624 2625### readStringArray 2626 2627readStringArray(): string[] 2628 2629从MessageSequence实例读取字符串数组。 2630 2631**系统能力**:SystemCapability.Communication.IPC.Core 2632 2633**返回值:** 2634 2635 | 类型 | 说明 | 2636 | -------- | ---------------- | 2637 | string[] | 返回字符串数组。 | 2638 2639**错误码:** 2640 2641以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2642 2643 | 错误码ID | 错误信息 | 2644 | -------- | -------- | 2645 | 1900010 | Failed to read data from the message sequence. | 2646 2647**示例:** 2648 2649 ```ts 2650 import { hilog } from '@kit.PerformanceAnalysisKit'; 2651 import { BusinessError } from '@kit.BasicServicesKit'; 2652 2653 let data = rpc.MessageSequence.create(); 2654 try { 2655 data.writeStringArray(["abc", "def"]); 2656 } catch (error) { 2657 let e: BusinessError = error as BusinessError; 2658 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2659 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2660 } 2661 try { 2662 let array = data.readStringArray(); 2663 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2664 } catch (error) { 2665 let e: BusinessError = error as BusinessError; 2666 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2667 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2668 } 2669 ``` 2670 2671### writeNoException 2672 2673writeNoException(): void 2674 2675向MessageSequence写入“指示未发生异常”的信息。 2676 2677**系统能力**:SystemCapability.Communication.IPC.Core 2678 2679**错误码:** 2680 2681以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2682 2683 | 错误码ID | 错误信息 | 2684 | -------- | -------- | 2685 | 1900009 | Failed to write data to the message sequence. | 2686 2687**示例:** 2688 2689 ```ts 2690 import { hilog } from '@kit.PerformanceAnalysisKit'; 2691 import { BusinessError } from '@kit.BasicServicesKit'; 2692 2693 class TestRemoteObject extends rpc.RemoteObject { 2694 constructor(descriptor: string) { 2695 super(descriptor); 2696 } 2697 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2698 if (code === 1) { 2699 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2700 try { 2701 reply.writeNoException(); 2702 } catch (error) { 2703 let e: BusinessError = error as BusinessError; 2704 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2705 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2706 } 2707 return true; 2708 } else { 2709 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2710 return false; 2711 } 2712 } 2713 } 2714 ``` 2715 2716### readException 2717 2718readException(): void 2719 2720从MessageSequence中读取异常。 2721 2722**系统能力**:SystemCapability.Communication.IPC.Core 2723 2724**错误码:** 2725 2726以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2727 2728 | 错误码ID | 错误信息 | 2729 | -------- | -------- | 2730 | 1900010 | Failed to read data from the message sequence. | 2731 2732**示例:** 2733 2734 ```ts 2735 // FA模型需要从@kit.AbilityKit导入featureAbility 2736 // import { featureAbility } from '@kit.AbilityKit'; 2737 import { Want, common } from '@kit.AbilityKit'; 2738 import { hilog } from '@kit.PerformanceAnalysisKit'; 2739 2740 let proxy: rpc.IRemoteObject | undefined; 2741 let connect: common.ConnectOptions = { 2742 onConnect: (elementName, remoteProxy) => { 2743 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2744 proxy = remoteProxy; 2745 }, 2746 onDisconnect: (elementName) => { 2747 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2748 }, 2749 onFailed: () => { 2750 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2751 } 2752 }; 2753 let want: Want = { 2754 bundleName: "com.ohos.server", 2755 abilityName: "com.ohos.server.EntryAbility", 2756 }; 2757 2758 // FA模型使用此方法连接服务 2759 // FA.connectAbility(want,connect); 2760 2761 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2762 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 2763 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2764 let connectionId = context.connectServiceExtensionAbility(want, connect); 2765 ``` 2766 2767 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 2768 2769 ```ts 2770 import { BusinessError } from '@kit.BasicServicesKit'; 2771 import { hilog } from '@kit.PerformanceAnalysisKit'; 2772 2773 let option = new rpc.MessageOption(); 2774 let data = rpc.MessageSequence.create(); 2775 let reply = rpc.MessageSequence.create(); 2776 data.writeNoException(); 2777 data.writeInt(6); 2778 if (proxy != undefined) { 2779 proxy.sendMessageRequest(1, data, reply, option) 2780 .then((result: rpc.RequestResult) => { 2781 if (result.errCode === 0) { 2782 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2783 try { 2784 result.reply.readException(); 2785 } catch (error) { 2786 let e: BusinessError = error as BusinessError; 2787 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2788 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2789 } 2790 let num = result.reply.readInt(); 2791 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2792 } else { 2793 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2794 } 2795 }).catch((e: Error) => { 2796 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2797 }).finally (() => { 2798 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2799 data.reclaim(); 2800 reply.reclaim(); 2801 }); 2802 } 2803 ``` 2804 2805### writeParcelableArray 2806 2807writeParcelableArray(parcelableArray: Parcelable[]): void 2808 2809将可序列化对象数组写入MessageSequence实例。 2810 2811**系统能力**:SystemCapability.Communication.IPC.Core 2812 2813**参数:** 2814 2815| 参数名 | 类型 | 必填 | 说明 | 2816| --------------- | ------------ | ---- | -------------------------- | 2817| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要写入的可序列化对象数组。 | 2818 2819**错误码:** 2820 2821以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2822 2823 | 错误码ID | 错误信息 | 2824 | -------- | -------- | 2825 | 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. | 2826 | 1900009 | Failed to write data to the message sequence. | 2827 2828**示例:** 2829 2830 ```ts 2831 import { hilog } from '@kit.PerformanceAnalysisKit'; 2832 import { BusinessError } from '@kit.BasicServicesKit'; 2833 2834 class MyParcelable implements rpc.Parcelable { 2835 num: number = 0; 2836 str: string = ''; 2837 constructor(num: number, str: string) { 2838 this.num = num; 2839 this.str = str; 2840 } 2841 marshalling(messageSequence: rpc.MessageSequence): boolean { 2842 messageSequence.writeInt(this.num); 2843 messageSequence.writeString(this.str); 2844 return true; 2845 } 2846 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2847 this.num = messageSequence.readInt(); 2848 this.str = messageSequence.readString(); 2849 return true; 2850 } 2851 } 2852 let parcelable = new MyParcelable(1, "aaa"); 2853 let parcelable2 = new MyParcelable(2, "bbb"); 2854 let parcelable3 = new MyParcelable(3, "ccc"); 2855 let a = [parcelable, parcelable2, parcelable3]; 2856 let data = rpc.MessageSequence.create(); 2857 try { 2858 data.writeParcelableArray(a); 2859 } catch (error) { 2860 let e: BusinessError = error as BusinessError; 2861 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2862 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2863 } 2864 ``` 2865 2866### readParcelableArray 2867 2868readParcelableArray(parcelableArray: Parcelable[]): void 2869 2870从MessageSequence实例读取可序列化对象数组。 2871 2872**系统能力**:SystemCapability.Communication.IPC.Core 2873 2874**参数:** 2875 2876| 参数名 | 类型 | 必填 | 说明 | 2877| --------------- | ------------ | ---- | -------------------------- | 2878| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要读取的可序列化对象数组。 | 2879 2880**错误码:** 2881 2882以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2883 2884 | 错误码ID | 错误信息 | 2885 | -------- | -------- | 2886 | 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. | 2887 | 1900010 | Failed to read data from the message sequence. | 2888 | 1900012 | Failed to call the JS callback function. | 2889 2890**示例:** 2891 2892 ```ts 2893 import { hilog } from '@kit.PerformanceAnalysisKit'; 2894 import { BusinessError } from '@kit.BasicServicesKit'; 2895 2896 class MyParcelable implements rpc.Parcelable { 2897 num: number = 0; 2898 str: string = ''; 2899 constructor(num: number, str: string) { 2900 this.num = num; 2901 this.str = str; 2902 } 2903 marshalling(messageSequence: rpc.MessageSequence): boolean { 2904 messageSequence.writeInt(this.num); 2905 messageSequence.writeString(this.str); 2906 return true; 2907 } 2908 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2909 this.num = messageSequence.readInt(); 2910 this.str = messageSequence.readString(); 2911 return true; 2912 } 2913 } 2914 let parcelable = new MyParcelable(1, "aaa"); 2915 let parcelable2 = new MyParcelable(2, "bbb"); 2916 let parcelable3 = new MyParcelable(3, "ccc"); 2917 let a = [parcelable, parcelable2, parcelable3]; 2918 let data = rpc.MessageSequence.create(); 2919 data.writeParcelableArray(a); 2920 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2921 try { 2922 data.readParcelableArray(b); 2923 } catch (error) { 2924 let e: BusinessError = error as BusinessError; 2925 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2926 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2927 } 2928 ``` 2929 2930### writeRemoteObjectArray 2931 2932writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2933 2934将IRemoteObject对象数组写入MessageSequence。 2935 2936**系统能力**:SystemCapability.Communication.IPC.Core 2937 2938**参数:** 2939 2940| 参数名 | 类型 | 必填 | 说明 | 2941| ----------- | --------------- | ---- | ---------------------------------------------- | 2942| objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageSequence的IRemoteObject对象数组。 | 2943 2944**错误码:** 2945 2946以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2947 2948 | 错误码ID | 错误信息 | 2949 | -------- | -------- | 2950 | 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. | 2951 | 1900009 | Failed to write data to the message sequence. | 2952 2953**示例:** 2954 2955 ```ts 2956 import { hilog } from '@kit.PerformanceAnalysisKit'; 2957 import { BusinessError } from '@kit.BasicServicesKit'; 2958 2959 class TestRemoteObject extends rpc.RemoteObject { 2960 constructor(descriptor: string) { 2961 super(descriptor); 2962 this.modifyLocalInterface(this, descriptor); 2963 } 2964 2965 asObject(): rpc.IRemoteObject { 2966 return this; 2967 } 2968 } 2969 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2970 let data = rpc.MessageSequence.create(); 2971 try { 2972 data.writeRemoteObjectArray(a); 2973 } catch (error) { 2974 let e: BusinessError = error as BusinessError; 2975 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2976 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2977 } 2978 ``` 2979 2980### readRemoteObjectArray 2981 2982readRemoteObjectArray(objects: IRemoteObject[]): void 2983 2984从MessageSequence读取IRemoteObject对象数组。 2985 2986**系统能力**:SystemCapability.Communication.IPC.Core 2987 2988**参数:** 2989 2990| 参数名 | 类型 | 必填 | 说明 | 2991| ------- | --------------- | ---- | ---------------------------------------------- | 2992| objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageSequence读取的IRemoteObject对象数组。 | 2993 2994**错误码:** 2995 2996以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2997 2998 | 错误码ID | 错误信息 | 2999 | -------- | -------- | 3000 | 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. | 3001 | 1900010 | Failed to read data from the message sequence. | 3002 3003**示例:** 3004 3005 ```ts 3006 import { hilog } from '@kit.PerformanceAnalysisKit'; 3007 import { BusinessError } from '@kit.BasicServicesKit'; 3008 3009 class TestRemoteObject extends rpc.RemoteObject { 3010 constructor(descriptor: string) { 3011 super(descriptor); 3012 this.modifyLocalInterface(this, descriptor); 3013 } 3014 3015 asObject(): rpc.IRemoteObject { 3016 return this; 3017 } 3018 } 3019 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3020 let data = rpc.MessageSequence.create(); 3021 data.writeRemoteObjectArray(a); 3022 let b: Array<rpc.IRemoteObject> = new Array(3); 3023 try { 3024 data.readRemoteObjectArray(b); 3025 } catch (error) { 3026 let e: BusinessError = error as BusinessError; 3027 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3028 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3029 } 3030 ``` 3031 3032### readRemoteObjectArray 3033 3034readRemoteObjectArray(): IRemoteObject[] 3035 3036从MessageSequence读取IRemoteObject对象数组。 3037 3038**系统能力**:SystemCapability.Communication.IPC.Core 3039 3040**返回值:** 3041 3042| 类型 | 说明 | 3043| --------------- | --------------------------- | 3044| [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 3045 3046**错误码:** 3047 3048以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3049 3050 | 错误码ID | 错误信息 | 3051 | -------- | -------- | 3052 | 1900010 | Failed to read data from the message sequence. | 3053 3054**示例:** 3055 3056 ```ts 3057 import { hilog } from '@kit.PerformanceAnalysisKit'; 3058 import { BusinessError } from '@kit.BasicServicesKit'; 3059 3060 class TestRemoteObject extends rpc.RemoteObject { 3061 constructor(descriptor: string) { 3062 super(descriptor); 3063 this.modifyLocalInterface(this, descriptor); 3064 } 3065 3066 asObject(): rpc.IRemoteObject { 3067 return this; 3068 } 3069 } 3070 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3071 let data = rpc.MessageSequence.create(); 3072 data.writeRemoteObjectArray(a); 3073 try { 3074 let b = data.readRemoteObjectArray(); 3075 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 3076 } catch (error) { 3077 let e: BusinessError = error as BusinessError; 3078 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3079 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3080 } 3081 ``` 3082 3083### closeFileDescriptor 3084 3085static closeFileDescriptor(fd: number): void 3086 3087静态方法,关闭给定的文件描述符。 3088 3089**系统能力**:SystemCapability.Communication.IPC.Core 3090 3091**参数:** 3092 3093 | 参数名 | 类型 | 必填 | 说明 | 3094 | ------ | ------ | ---- | -------------------- | 3095 | fd | number | 是 | 要关闭的文件描述符。 | 3096 3097**错误码:** 3098 3099以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3100 3101 | 错误码ID | 错误信息 | 3102 | -------- | -------- | 3103 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3104 3105**示例:** 3106 3107 ```ts 3108 import { fileIo } from '@kit.CoreFileKit'; 3109 import { hilog } from '@kit.PerformanceAnalysisKit'; 3110 import { BusinessError } from '@kit.BasicServicesKit'; 3111 3112 let filePath = "path/to/file"; 3113 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3114 try { 3115 rpc.MessageSequence.closeFileDescriptor(file.fd); 3116 } catch (error) { 3117 let e: BusinessError = error as BusinessError; 3118 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3119 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3120 } 3121 ``` 3122 3123### dupFileDescriptor 3124 3125static dupFileDescriptor(fd: number): number 3126 3127静态方法,复制给定的文件描述符。 3128 3129**系统能力**:SystemCapability.Communication.IPC.Core 3130 3131**参数:** 3132 3133 | 参数名 | 类型 | 必填 | 说明 | 3134 | ------ | ------ | ---- | ------------------------ | 3135 | fd | number | 是 | 表示已存在的文件描述符。 | 3136 3137**返回值:** 3138 3139 | 类型 | 说明 | 3140 | ------ | -------------------- | 3141 | number | 返回新的文件描述符。 | 3142 3143**错误码:** 3144 3145以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3146 3147 | 错误码ID | 错误信息 | 3148 | -------- | -------- | 3149 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3150 | 1900013 | Failed to call dup. | 3151 3152**示例:** 3153 3154 ```ts 3155 import { fileIo } from '@kit.CoreFileKit'; 3156 import { hilog } from '@kit.PerformanceAnalysisKit'; 3157 import { BusinessError } from '@kit.BasicServicesKit'; 3158 3159 let filePath = "path/to/file"; 3160 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3161 try { 3162 rpc.MessageSequence.dupFileDescriptor(file.fd); 3163 } catch (error) { 3164 let e: BusinessError = error as BusinessError; 3165 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3166 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3167 } 3168 ``` 3169 3170### containFileDescriptors 3171 3172containFileDescriptors(): boolean 3173 3174检查此MessageSequence对象是否包含文件描述符。 3175 3176**系统能力**:SystemCapability.Communication.IPC.Core 3177 3178**返回值:** 3179 3180 | 类型 | 说明 | 3181 | ------- | -------------------------------------------------------------------- | 3182 | boolean | true:包含文件描述符,false:不包含文件描述符。| 3183 3184**示例:** 3185 3186 ```ts 3187 import { fileIo } from '@kit.CoreFileKit'; 3188 import { hilog } from '@kit.PerformanceAnalysisKit'; 3189 import { BusinessError } from '@kit.BasicServicesKit'; 3190 3191 let sequence = new rpc.MessageSequence(); 3192 let filePath = "path/to/file"; 3193 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3194 try { 3195 sequence.writeFileDescriptor(file.fd); 3196 } catch (error) { 3197 let e: BusinessError = error as BusinessError; 3198 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3199 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3200 } 3201 try { 3202 let containFD = sequence.containFileDescriptors(); 3203 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3204 } catch (error) { 3205 let e: BusinessError = error as BusinessError; 3206 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3207 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3208 } 3209 ``` 3210 3211### writeFileDescriptor 3212 3213writeFileDescriptor(fd: number): void 3214 3215写入文件描述符到MessageSequence。 3216 3217**系统能力**:SystemCapability.Communication.IPC.Core 3218 3219**参数:** 3220 3221 | 参数名 | 类型 | 必填 | 说明 | 3222 | ------ | ------ | ---- | ------------ | 3223 | fd | number | 是 | 文件描述符。 | 3224 3225**错误码:** 3226 3227以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3228 3229 | 错误码ID | 错误信息 | 3230 | -------- | -------- | 3231 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3232 | 1900009 | Failed to write data to the message sequence. | 3233 3234**示例:** 3235 3236 ```ts 3237 import { fileIo } from '@kit.CoreFileKit'; 3238 import { hilog } from '@kit.PerformanceAnalysisKit'; 3239 import { BusinessError } from '@kit.BasicServicesKit'; 3240 3241 let sequence = new rpc.MessageSequence(); 3242 let filePath = "path/to/file"; 3243 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3244 try { 3245 sequence.writeFileDescriptor(file.fd); 3246 } catch (error) { 3247 let e: BusinessError = error as BusinessError; 3248 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3249 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3250 } 3251 ``` 3252 3253### readFileDescriptor 3254 3255readFileDescriptor(): number 3256 3257从MessageSequence中读取文件描述符。 3258 3259**系统能力**:SystemCapability.Communication.IPC.Core 3260 3261**返回值:** 3262 3263 | 类型 | 说明 | 3264 | ------ | ---------------- | 3265 | number | 返回文件描述符。 | 3266 3267**错误码:** 3268 3269以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3270 3271 | 错误码ID | 错误信息 | 3272 | -------- | -------- | 3273 | 1900010 | Failed to read data from the message sequence. | 3274 3275**示例:** 3276 3277 ```ts 3278 import { fileIo } from '@kit.CoreFileKit'; 3279 import { hilog } from '@kit.PerformanceAnalysisKit'; 3280 import { BusinessError } from '@kit.BasicServicesKit'; 3281 3282 let sequence = new rpc.MessageSequence(); 3283 let filePath = "path/to/file"; 3284 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3285 try { 3286 sequence.writeFileDescriptor(file.fd); 3287 } catch (error) { 3288 let e: BusinessError = error as BusinessError; 3289 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3290 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3291 } 3292 try { 3293 let readFD = sequence.readFileDescriptor(); 3294 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3295 } catch (error) { 3296 let e: BusinessError = error as BusinessError; 3297 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3298 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3299 } 3300 ``` 3301 3302### writeAshmem 3303 3304writeAshmem(ashmem: Ashmem): void 3305 3306将指定的匿名共享对象写入此MessageSequence。 3307 3308**系统能力**:SystemCapability.Communication.IPC.Core 3309 3310**参数:** 3311 3312| 参数名 | 类型 | 必填 | 说明 | 3313| ------ | ------ | ---- | ------------------------------------- | 3314| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageSequence的匿名共享对象。 | 3315 3316**错误码:** 3317 3318以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3319 3320 | 错误码ID | 错误信息 | 3321 | -------- | ------- | 3322 | 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. | 3323 | 1900003 | Failed to write data to the shared memory. | 3324 3325**示例:** 3326 3327 ```ts 3328 import { hilog } from '@kit.PerformanceAnalysisKit'; 3329 import { BusinessError } from '@kit.BasicServicesKit'; 3330 3331 let sequence = new rpc.MessageSequence(); 3332 let ashmem: rpc.Ashmem | undefined = undefined; 3333 try { 3334 ashmem = rpc.Ashmem.create("ashmem", 1024); 3335 try { 3336 sequence.writeAshmem(ashmem); 3337 } catch (error) { 3338 let e: BusinessError = error as BusinessError; 3339 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3340 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3341 } 3342 } catch (error) { 3343 let e: BusinessError = error as BusinessError; 3344 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3345 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3346 } 3347 ``` 3348 3349### readAshmem 3350 3351readAshmem(): Ashmem 3352 3353从MessageSequence读取匿名共享对象。 3354 3355**系统能力**:SystemCapability.Communication.IPC.Core 3356 3357**返回值:** 3358 3359| 类型 | 说明 | 3360| ------ | ------------------ | 3361| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 3362 3363**错误码:** 3364 3365以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3366 3367 | 错误码ID | 错误信息 | 3368 | -------- | -------- | 3369 | 401 | check param failed | 3370 | 1900004 | Failed to read data from the shared memory. | 3371 3372**示例:** 3373 3374 ```ts 3375 import { hilog } from '@kit.PerformanceAnalysisKit'; 3376 import { BusinessError } from '@kit.BasicServicesKit'; 3377 3378 let sequence = new rpc.MessageSequence(); 3379 let ashmem: rpc.Ashmem | undefined = undefined; 3380 try { 3381 ashmem = rpc.Ashmem.create("ashmem", 1024); 3382 try { 3383 sequence.writeAshmem(ashmem); 3384 } catch (error) { 3385 let e: BusinessError = error as BusinessError; 3386 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3387 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3388 } 3389 } catch (error) { 3390 let e: BusinessError = error as BusinessError; 3391 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3392 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3393 } 3394 try { 3395 sequence.readAshmem(); 3396 } catch (error) { 3397 let e: BusinessError = error as BusinessError; 3398 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3399 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3400 } 3401 ``` 3402 3403### getRawDataCapacity 3404 3405getRawDataCapacity(): number 3406 3407获取MessageSequence可以容纳的最大原始数据量。 3408 3409**系统能力**:SystemCapability.Communication.IPC.Core 3410 3411**返回值:** 3412 3413 | 类型 | 说明 | 3414 | ------ | ------------------------------------------------------------ | 3415 | number | 返回MessageSequence可以容纳的最大原始数据量,即128MB。 | 3416 3417**示例:** 3418 3419 ```ts 3420 import { hilog } from '@kit.PerformanceAnalysisKit'; 3421 3422 let sequence = new rpc.MessageSequence(); 3423 let result = sequence.getRawDataCapacity(); 3424 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3425 ``` 3426 3427### writeRawData<sup>(deprecated)</sup> 3428 3429>从API version 11 开始废弃,建议使用[writeRawDataBuffer](#writerawdatabuffer11)替代。 3430 3431writeRawData(rawData: number[], size: number): void 3432 3433将原始数据写入MessageSequence对象。 3434 3435**系统能力**:SystemCapability.Communication.IPC.Core 3436 3437**参数:** 3438 3439 | 参数名 | 类型 | 必填 | 说明 | 3440 | ------- | -------- | ---- | ---------------------------------- | 3441 | rawData | number[] | 是 | 要写入的原始数据。 | 3442 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3443 3444**错误码:** 3445 3446以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3447 3448 | 错误码ID | 错误信息 | 3449 | -------- | -------- | 3450 | 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. | 3451 | 1900009 | Failed to write data to the message sequence. | 3452 3453**示例:** 3454 3455 ```ts 3456 import { hilog } from '@kit.PerformanceAnalysisKit'; 3457 import { BusinessError } from '@kit.BasicServicesKit'; 3458 3459 let sequence = new rpc.MessageSequence(); 3460 let arr = [1, 2, 3, 4, 5]; 3461 try { 3462 sequence.writeRawData(arr, arr.length); 3463 } catch (error) { 3464 let e: BusinessError = error as BusinessError; 3465 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3466 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3467 } 3468 ``` 3469 3470### writeRawDataBuffer<sup>11+</sup> 3471 3472writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3473 3474将原始数据写入MessageSequence对象。 3475 3476**系统能力**:SystemCapability.Communication.IPC.Core 3477 3478**参数:** 3479 3480 | 参数名 | 类型 | 必填 | 说明 | 3481 | ------- | -------- | ---- | ---------------------------------- | 3482 | rawData | ArrayBuffer | 是 | 要写入的原始数据。 | 3483 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3484 3485**错误码:** 3486 3487以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3488 3489 | 错误码ID | 错误信息 | 3490 | -------- | -------- | 3491 | 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. | 3492 | 1900009 | Failed to write data to the message sequence. | 3493 3494**示例:** 3495 3496 ```ts 3497 import { hilog } from '@kit.PerformanceAnalysisKit'; 3498 import { BusinessError } from '@kit.BasicServicesKit'; 3499 3500 let buffer = new ArrayBuffer(64 * 1024); 3501 let int32View = new Int32Array(buffer); 3502 for (let i = 0; i < int32View.length; i++) { 3503 int32View[i] = i * 2 + 1; 3504 } 3505 let size = buffer.byteLength; 3506 let sequence = new rpc.MessageSequence(); 3507 try { 3508 sequence.writeRawDataBuffer(buffer, size); 3509 } catch (error) { 3510 let e: BusinessError = error as BusinessError; 3511 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3512 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3513 } 3514 ``` 3515 3516### readRawData<sup>(deprecated)</sup> 3517 3518>从API version 11 开始废弃,建议使用[readRawDataBuffer](#readrawdatabuffer11)替代。 3519 3520readRawData(size: number): number[] 3521 3522从MessageSequence读取原始数据。 3523 3524**系统能力**:SystemCapability.Communication.IPC.Core 3525 3526**参数:** 3527 3528 | 参数名 | 类型 | 必填 | 说明 | 3529 | ------ | ------ | ---- | ------------------------ | 3530 | size | number | 是 | 要读取的原始数据的大小。 | 3531 3532**返回值:** 3533 3534 | 类型 | 说明 | 3535 | -------- | ------------------------------ | 3536 | number[] | 返回原始数据(以字节为单位)。 | 3537 3538**错误码:** 3539 3540以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3541 3542 | 错误码ID | 错误信息 | 3543 | -------- | -------- | 3544 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3545 | 1900010 | Failed to read data from the message sequence. | 3546 3547**示例:** 3548 3549 ```ts 3550 import { hilog } from '@kit.PerformanceAnalysisKit'; 3551 import { BusinessError } from '@kit.BasicServicesKit'; 3552 3553 let sequence = new rpc.MessageSequence(); 3554 let arr = [1, 2, 3, 4, 5]; 3555 try { 3556 sequence.writeRawData(arr, arr.length); 3557 } catch (error) { 3558 let e: BusinessError = error as BusinessError; 3559 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3560 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3561 } 3562 try { 3563 let result = sequence.readRawData(5); 3564 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3565 } catch (error) { 3566 let e: BusinessError = error as BusinessError; 3567 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3568 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3569 } 3570 ``` 3571 3572### readRawDataBuffer<sup>11+</sup> 3573 3574readRawDataBuffer(size: number): ArrayBuffer 3575 3576从MessageSequence读取原始数据。 3577 3578**系统能力**:SystemCapability.Communication.IPC.Core 3579 3580**参数:** 3581 3582 | 参数名 | 类型 | 必填 | 说明 | 3583 | ------ | ------ | ---- | ------------------------ | 3584 | size | number | 是 | 要读取的原始数据的大小。 | 3585 3586**返回值:** 3587 3588 | 类型 | 说明 | 3589 | -------- | ------------------------------ | 3590 | ArrayBuffer | 返回原始数据(以字节为单位)。 | 3591 3592**错误码:** 3593 3594以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3595 3596 | 错误码ID | 错误信息 | 3597 | -------- | -------- | 3598 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3599 | 1900010 | Failed to read data from the message sequence. | 3600 3601**示例:** 3602 3603 ```ts 3604 import { hilog } from '@kit.PerformanceAnalysisKit'; 3605 import { BusinessError } from '@kit.BasicServicesKit'; 3606 3607 let buffer = new ArrayBuffer(64 * 1024); 3608 let int32View = new Int32Array(buffer); 3609 for (let i = 0; i < int32View.length; i++) { 3610 int32View[i] = i * 2 + 1; 3611 } 3612 let size = buffer.byteLength; 3613 let sequence = new rpc.MessageSequence(); 3614 try { 3615 sequence.writeRawDataBuffer(buffer, size); 3616 } catch (error) { 3617 let e: BusinessError = error as BusinessError; 3618 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3619 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3620 } 3621 try { 3622 let result = sequence.readRawDataBuffer(size); 3623 let readInt32View = new Int32Array(result); 3624 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + readInt32View); 3625 } catch (error) { 3626 let e: BusinessError = error as BusinessError; 3627 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3628 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3629 } 3630 ``` 3631 3632### writeArrayBuffer<sup>12+</sup> 3633 3634writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3635 3636将ArrayBuffer类型数据写入MessageSequence对象。 3637 3638**系统能力**:SystemCapability.Communication.IPC.Core 3639 3640**参数:** 3641 3642 | 参数名 | 类型 | 必填 | 说明 | 3643 | --------- | ------------------------- | ---- | --------------------------- | 3644 | buf | ArrayBuffer | 是 | 要写入的ArrayBuffer数据。 | 3645 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的写入方式,需要业务正确传递枚举值。) | 3646 3647**错误码:** 3648 3649以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3650 3651 | 错误码ID | 错误信息 | 3652 | -------- | -------- | 3653 | 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. | 3654 | 1900009 | Failed to write data to the message sequence. | 3655 3656**示例:** 3657 3658 ```ts 3659 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3660 import { hilog } from '@kit.PerformanceAnalysisKit'; 3661 import { BusinessError } from '@kit.BasicServicesKit'; 3662 3663 const data = rpc.MessageSequence.create(); 3664 3665 let buffer = new ArrayBuffer(10); 3666 let int16View = new Int16Array(buffer); 3667 for (let i = 0; i < int16View.length; i++) { 3668 int16View[i] = i * 2 + 1; 3669 } 3670 3671 try { 3672 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3673 } catch (error) { 3674 let e: BusinessError = error as BusinessError; 3675 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3676 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3677 } 3678 ``` 3679 3680### readArrayBuffer<sup>12+</sup> 3681 3682readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3683 3684从MessageSequence读取ArrayBuffer类型数据。 3685 3686**系统能力**:SystemCapability.Communication.IPC.Core 3687 3688**参数:** 3689 3690 | 参数名 | 类型 | 必填 | 说明 | 3691 | -------- | ----------------------- | ---- | ------------------------| 3692 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的读取方式,需要业务正确传递枚举值,读写枚举值不匹配会导致数据异常。) | 3693 3694**返回值:** 3695 3696 | 类型 | 说明 | 3697 | -------- | -------------------------------------------- | 3698 | ArrayBuffer | 返回ArrayBuffer类型数据(以字节为单位)。 | 3699 3700**错误码:** 3701 3702以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3703 3704 | 错误码ID | 错误信息 | 3705 | -------- | -------- | 3706 | 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; | 3707 | 1900010 | Failed to read data from the message sequence. | 3708 3709**示例:** 3710 3711 ```ts 3712 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3713 import { hilog } from '@kit.PerformanceAnalysisKit'; 3714 import { BusinessError } from '@kit.BasicServicesKit'; 3715 3716 const data = rpc.MessageSequence.create(); 3717 3718 let buffer = new ArrayBuffer(10); 3719 let int16View = new Int16Array(buffer); 3720 for (let i = 0; i < int16View.length; i++) { 3721 int16View[i] = i * 2 + 1; 3722 } 3723 3724 try { 3725 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3726 } catch (error) { 3727 let e: BusinessError = error as BusinessError; 3728 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3729 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3730 } 3731 try { 3732 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3733 let readInt16View = new Int16Array(result); 3734 hilog.info(0x0000, 'testTag', 'RpcTest: read ArrayBuffer result is ' + readInt16View); 3735 } catch (error) { 3736 let e: BusinessError = error as BusinessError; 3737 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorCode ' + e.code); 3738 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorMessage ' + e.message); 3739 } 3740 ``` 3741 3742## MessageParcel<sup>(deprecated)</sup> 3743 3744>从API version 9 开始废弃,建议使用[MessageSequence](#messagesequence9)替代。 3745 3746在RPC过程中,发送方可以使用MessageParcel提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageParcel提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 3747 3748### create 3749 3750static create(): MessageParcel 3751 3752静态方法,创建MessageParcel对象。 3753 3754**系统能力**:SystemCapability.Communication.IPC.Core 3755 3756**返回值:** 3757 3758 | 类型 | 说明 | 3759 | ------------- | ----------------------------- | 3760 | [MessageParcel](#messageparceldeprecated) | 返回创建的MessageParcel对象。 | 3761 3762**示例:** 3763 3764 ```ts 3765 import { hilog } from '@kit.PerformanceAnalysisKit'; 3766 3767 let data = rpc.MessageParcel.create(); 3768 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3769 ``` 3770 3771### reclaim 3772 3773reclaim(): void 3774 3775释放不再使用的MessageParcel对象。 3776 3777**系统能力**:SystemCapability.Communication.IPC.Core 3778 3779**示例:** 3780 3781 ```ts 3782 let reply = rpc.MessageParcel.create(); 3783 reply.reclaim(); 3784 ``` 3785 3786### writeRemoteObject 3787 3788writeRemoteObject(object: IRemoteObject): boolean 3789 3790序列化远程对象并将其写入MessageParcel对象。 3791 3792**系统能力**:SystemCapability.Communication.IPC.Core 3793 3794**参数:** 3795 3796 | 参数名 | 类型 | 必填 | 说明 | 3797 | ------ | ------------------------------- | ---- | --------------------------------------- | 3798 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageParcel的远程对象。 | 3799 3800**返回值:** 3801 3802 | 类型 | 说明 | 3803 | ------- | ----------------------------------------- | 3804 | boolean | true:操作成功,false:操作失败。| 3805 3806**示例:** 3807 3808 ```ts 3809 import { hilog } from '@kit.PerformanceAnalysisKit'; 3810 3811 class MyDeathRecipient implements rpc.DeathRecipient { 3812 onRemoteDied() { 3813 hilog.info(0x0000, 'testTag', 'server died'); 3814 } 3815 } 3816 class TestRemoteObject extends rpc.RemoteObject { 3817 constructor(descriptor: string) { 3818 super(descriptor); 3819 } 3820 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3821 return true; 3822 } 3823 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3824 return true; 3825 } 3826 isObjectDead(): boolean { 3827 return false; 3828 } 3829 } 3830 let data = rpc.MessageParcel.create(); 3831 let testRemoteObject = new TestRemoteObject("testObject"); 3832 data.writeRemoteObject(testRemoteObject); 3833 ``` 3834 3835### readRemoteObject 3836 3837readRemoteObject(): IRemoteObject 3838 3839从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。 3840 3841**系统能力**:SystemCapability.Communication.IPC.Core 3842 3843**返回值:** 3844 3845 | 类型 | 说明 | 3846 | ------------------------------- | ------------------ | 3847 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 3848 3849**示例:** 3850 3851 ```ts 3852 import { hilog } from '@kit.PerformanceAnalysisKit'; 3853 3854 class MyDeathRecipient implements rpc.DeathRecipient { 3855 onRemoteDied() { 3856 hilog.info(0x0000, 'testTag', 'server died'); 3857 } 3858 } 3859 class TestRemoteObject extends rpc.RemoteObject { 3860 constructor(descriptor: string) { 3861 super(descriptor); 3862 } 3863 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3864 return true; 3865 } 3866 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3867 return true; 3868 } 3869 isObjectDead(): boolean { 3870 return false; 3871 } 3872 } 3873 let data = rpc.MessageParcel.create(); 3874 let testRemoteObject = new TestRemoteObject("testObject"); 3875 data.writeRemoteObject(testRemoteObject); 3876 let proxy = data.readRemoteObject(); 3877 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3878 ``` 3879 3880### writeInterfaceToken 3881 3882writeInterfaceToken(token: string): boolean 3883 3884将接口描述符写入MessageParcel对象,远端对象可使用该信息校验本次通信。 3885 3886**系统能力**:SystemCapability.Communication.IPC.Core 3887 3888**参数:** 3889 3890 | 参数名 | 类型 | 必填 | 说明 | 3891 | ------ | ------ | ---- | ------------------ | 3892 | token | string | 是 | 字符串类型描述符。 | 3893 3894**返回值:** 3895 3896 | 类型 | 说明 | 3897 | ------- | ----------------------------------------- | 3898 | boolean | true:操作成功,false:操作失败。| 3899 3900**示例:** 3901 3902 ```ts 3903 import { hilog } from '@kit.PerformanceAnalysisKit'; 3904 3905 let data = rpc.MessageParcel.create(); 3906 let result = data.writeInterfaceToken("aaa"); 3907 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3908 ``` 3909 3910### readInterfaceToken 3911 3912readInterfaceToken(): string 3913 3914从MessageParcel中读取接口描述符,接口描述符按写入MessageParcel的顺序读取,本地对象可使用该信息检验本次通信。 3915 3916**系统能力**:SystemCapability.Communication.IPC.Core 3917 3918**返回值:** 3919 3920 | 类型 | 说明 | 3921 | ------ | ------------------------ | 3922 | string | 返回读取到的接口描述符。 | 3923 3924**示例:** 3925 3926 ```ts 3927 import { hilog } from '@kit.PerformanceAnalysisKit'; 3928 3929 class Stub extends rpc.RemoteObject { 3930 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3931 let interfaceToken = data.readInterfaceToken(); 3932 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3933 return true; 3934 } 3935 } 3936 ``` 3937 3938### getSize 3939 3940getSize(): number 3941 3942获取当前MessageParcel的数据大小。 3943 3944**系统能力**:SystemCapability.Communication.IPC.Core 3945 3946**返回值:** 3947 3948 | 类型 | 说明 | 3949 | ------ | --------------------------------------------- | 3950 | number | 获取的MessageParcel的数据大小。以字节为单位。 | 3951 3952**示例:** 3953 3954 ```ts 3955 import { hilog } from '@kit.PerformanceAnalysisKit'; 3956 3957 let data = rpc.MessageParcel.create(); 3958 let size = data.getSize(); 3959 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3960 ``` 3961 3962### getCapacity 3963 3964getCapacity(): number 3965 3966获取当前MessageParcel的容量。 3967 3968**系统能力**:SystemCapability.Communication.IPC.Core 3969 3970**返回值:** 3971 3972 | 类型 | 说明 | 3973 | ------ | --------------------------------------------- | 3974 | number | 获取的MessageParcel的容量大小。以字节为单位。 | 3975 3976**示例:** 3977 3978 ```ts 3979 import { hilog } from '@kit.PerformanceAnalysisKit'; 3980 3981 let data = rpc.MessageParcel.create(); 3982 let result = data.getCapacity(); 3983 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 3984 ``` 3985 3986### setSize 3987 3988setSize(size: number): boolean 3989 3990设置MessageParcel实例中包含的数据大小。 3991 3992**系统能力**:SystemCapability.Communication.IPC.Core 3993 3994**参数:** 3995 3996 | 参数名 | 类型 | 必填 | 说明 | 3997 | ------ | ------ | ---- | ------------------------------------------- | 3998 | size | number | 是 | MessageParcel实例的数据大小。以字节为单位。 | 3999 4000**返回值:** 4001 4002 | 类型 | 说明 | 4003 | ------- | --------------------------------- | 4004 | boolean | true:设置成功,false:设置失败。| 4005 4006**示例:** 4007 4008 ```ts 4009 import { hilog } from '@kit.PerformanceAnalysisKit'; 4010 4011 let data = rpc.MessageParcel.create(); 4012 let setSize = data.setSize(16); 4013 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 4014 ``` 4015 4016### setCapacity 4017 4018setCapacity(size: number): boolean 4019 4020设置MessageParcel实例的存储容量。 4021 4022**系统能力**:SystemCapability.Communication.IPC.Core 4023 4024**参数:** 4025 4026 | 参数名 | 类型 | 必填 | 说明 | 4027 | ------ | ------ | ---- | ------------------------------------------- | 4028 | size | number | 是 | MessageParcel实例的存储容量。以字节为单位。 | 4029 4030**返回值:** 4031 4032 | 类型 | 说明 | 4033 | ------- | --------------------------------- | 4034 | boolean | true:设置成功,false:设置失败。| 4035 4036**示例:** 4037 4038 ```ts 4039 import { hilog } from '@kit.PerformanceAnalysisKit'; 4040 4041 let data = rpc.MessageParcel.create(); 4042 let result = data.setCapacity(100); 4043 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 4044 ``` 4045 4046### getWritableBytes 4047 4048getWritableBytes(): number 4049 4050获取MessageParcel的可写字节空间。 4051 4052**系统能力**:SystemCapability.Communication.IPC.Core 4053 4054**返回值:** 4055 4056 | 类型 | 说明 | 4057 | ------ | --------------------------------------------------- | 4058 | number | 获取到的MessageParcel的可写字节空间。以字节为单位。 | 4059 4060**示例:** 4061 4062 ```ts 4063 import { hilog } from '@kit.PerformanceAnalysisKit'; 4064 4065 class Stub extends rpc.RemoteObject { 4066 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4067 let getWritableBytes = data.getWritableBytes(); 4068 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4069 return true; 4070 } 4071 } 4072 ``` 4073 4074### getReadableBytes 4075 4076getReadableBytes(): number 4077 4078获取MessageParcel的可读字节空间。 4079 4080**系统能力**:SystemCapability.Communication.IPC.Core 4081 4082**返回值:** 4083 4084 | 类型 | 说明 | 4085 | ------ | --------------------------------------------------- | 4086 | number | 获取到的MessageParcel的可读字节空间。以字节为单位。 | 4087 4088**示例:** 4089 4090 ```ts 4091 import { hilog } from '@kit.PerformanceAnalysisKit'; 4092 4093 class Stub extends rpc.RemoteObject { 4094 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4095 let result = data.getReadableBytes(); 4096 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4097 return true; 4098 } 4099 } 4100 ``` 4101 4102### getReadPosition 4103 4104getReadPosition(): number 4105 4106获取MessageParcel的读位置。 4107 4108**系统能力**:SystemCapability.Communication.IPC.Core 4109 4110**返回值:** 4111 4112 | 类型 | 说明 | 4113 | ------ | --------------------------------------- | 4114 | number | 返回MessageParcel实例中的当前读取位置。 | 4115 4116**示例:** 4117 4118 ```ts 4119 import { hilog } from '@kit.PerformanceAnalysisKit'; 4120 4121 let data = rpc.MessageParcel.create(); 4122 let readPos = data.getReadPosition(); 4123 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 4124 ``` 4125 4126### getWritePosition 4127 4128getWritePosition(): number 4129 4130获取MessageParcel的写位置。 4131 4132**系统能力**:SystemCapability.Communication.IPC.Core 4133 4134**返回值:** 4135 4136 | 类型 | 说明 | 4137 | ------ | --------------------------------------- | 4138 | number | 返回MessageParcel实例中的当前写入位置。 | 4139 4140**示例:** 4141 4142 ```ts 4143 import { hilog } from '@kit.PerformanceAnalysisKit'; 4144 4145 let data = rpc.MessageParcel.create(); 4146 data.writeInt(10); 4147 let bwPos = data.getWritePosition(); 4148 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 4149 ``` 4150 4151### rewindRead 4152 4153rewindRead(pos: number): boolean 4154 4155重新偏移读取位置到指定的位置。 4156 4157**系统能力**:SystemCapability.Communication.IPC.Core 4158 4159**参数:** 4160 4161 | 参数名 | 类型 | 必填 | 说明 | 4162 | ------ | ------ | ---- | ------------------------ | 4163 | pos | number | 是 | 开始读取数据的目标位置。 | 4164 4165**返回值:** 4166 4167 | 类型 | 说明 | 4168 | ------- | ------------------------------------------------- | 4169 | boolean | true:读取位置发生更改,false:读取位置未发生更改。| 4170 4171**示例:** 4172 4173 ```ts 4174 import { hilog } from '@kit.PerformanceAnalysisKit'; 4175 4176 let data = rpc.MessageParcel.create(); 4177 data.writeInt(12); 4178 data.writeString("parcel"); 4179 let number = data.readInt(); 4180 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 4181 data.rewindRead(0); 4182 let number2 = data.readInt(); 4183 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 4184 ``` 4185 4186### rewindWrite 4187 4188rewindWrite(pos: number): boolean 4189 4190重新偏移写位置到指定的位置。 4191 4192**系统能力**:SystemCapability.Communication.IPC.Core 4193 4194**参数:** 4195 4196 | 参数名 | 类型 | 必填 | 说明 | 4197 | ------ | ------ | ---- | ------------------------ | 4198 | pos | number | 是 | 开始写入数据的目标位置。 | 4199 4200**返回值:** 4201 4202 | 类型 | 说明 | 4203 | ------- | --------------------------------------------- | 4204 | boolean | true:写入位置发生更改,false:写入位置未发生更改。| 4205 4206**示例:** 4207 4208 ```ts 4209 import { hilog } from '@kit.PerformanceAnalysisKit'; 4210 4211 let data = rpc.MessageParcel.create(); 4212 data.writeInt(4); 4213 data.rewindWrite(0); 4214 data.writeInt(5); 4215 let number = data.readInt(); 4216 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 4217 ``` 4218 4219### writeByte 4220 4221writeByte(val: number): boolean 4222 4223将字节值写入MessageParcel实例。 4224 4225**系统能力**:SystemCapability.Communication.IPC.Core 4226 4227**参数:** 4228 4229 | 参数名 | 类型 | 必填 | 说明 | 4230 | ------ | ------ | ---- | ---------------- | 4231 | val | number | 是 | 要写入的字节值。 | 4232 4233**返回值:** 4234 4235 | 类型 | 说明 | 4236 | ------- | ----------------------------- | 4237 | boolean | true:写入成功,false:写入失败。 | 4238 4239**示例:** 4240 4241 ```ts 4242 import { hilog } from '@kit.PerformanceAnalysisKit'; 4243 4244 let data = rpc.MessageParcel.create(); 4245 let result = data.writeByte(2); 4246 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4247 ``` 4248 4249### readByte 4250 4251readByte(): number 4252 4253从MessageParcel实例读取字节值。 4254 4255**系统能力**:SystemCapability.Communication.IPC.Core 4256 4257**返回值:** 4258 4259 | 类型 | 说明 | 4260 | ------ | ------------ | 4261 | number | 返回字节值。 | 4262 4263**示例:** 4264 4265 ```ts 4266 import { hilog } from '@kit.PerformanceAnalysisKit'; 4267 4268 let data = rpc.MessageParcel.create(); 4269 let result = data.writeByte(2); 4270 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4271 let ret = data.readByte(); 4272 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 4273 ``` 4274 4275### writeShort 4276 4277writeShort(val: number): boolean 4278 4279将短整数值写入MessageParcel实例。 4280 4281**系统能力**:SystemCapability.Communication.IPC.Core 4282 4283**参数:** 4284 4285 | 参数名 | 类型 | 必填 | 说明 | 4286 | ------ | ------ | ---- | ------------------ | 4287 | val | number | 是 | 要写入的短整数值。 | 4288 4289**返回值:** 4290 4291 | 类型 | 说明 | 4292 | ------- | ----------------------------- | 4293 | boolean | true:写入成功,false:写入失败。| 4294 4295**示例:** 4296 4297 ```ts 4298 import { hilog } from '@kit.PerformanceAnalysisKit'; 4299 4300 let data = rpc.MessageParcel.create(); 4301 let result = data.writeShort(8); 4302 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4303 ``` 4304 4305### readShort 4306 4307readShort(): number 4308 4309从MessageParcel实例读取短整数值。 4310 4311**系统能力**:SystemCapability.Communication.IPC.Core 4312 4313**返回值:** 4314 4315 | 类型 | 说明 | 4316 | ------ | -------------- | 4317 | number | 返回短整数值。 | 4318 4319**示例:** 4320 4321 ```ts 4322 import { hilog } from '@kit.PerformanceAnalysisKit'; 4323 4324 let data = rpc.MessageParcel.create(); 4325 let result = data.writeShort(8); 4326 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4327 let ret = data.readShort(); 4328 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4329 ``` 4330 4331### writeInt 4332 4333writeInt(val: number): boolean 4334 4335将整数值写入MessageParcel实例。 4336 4337**系统能力**:SystemCapability.Communication.IPC.Core 4338 4339**参数:** 4340 4341 | 参数名 | 类型 | 必填 | 说明 | 4342 | ------ | ------ | ---- | ---------------- | 4343 | val | number | 是 | 要写入的整数值。 | 4344 4345**返回值:** 4346 4347 | 类型 | 说明 | 4348 | ------- | ----------------------------- | 4349 | boolean | true:写入成功,false:写入失败。 | 4350 4351**示例:** 4352 4353 ```ts 4354 import { hilog } from '@kit.PerformanceAnalysisKit'; 4355 4356 let data = rpc.MessageParcel.create(); 4357 let result = data.writeInt(10); 4358 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4359 ``` 4360 4361### readInt 4362 4363readInt(): number 4364 4365从MessageParcel实例读取整数值。 4366 4367**系统能力**:SystemCapability.Communication.IPC.Core 4368 4369**返回值:** 4370 4371 | 类型 | 说明 | 4372 | ------ | ------------ | 4373 | number | 返回整数值。 | 4374 4375**示例:** 4376 4377 ```ts 4378 import { hilog } from '@kit.PerformanceAnalysisKit'; 4379 4380 let data = rpc.MessageParcel.create(); 4381 let result = data.writeInt(10); 4382 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4383 let ret = data.readInt(); 4384 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4385 ``` 4386 4387### writeLong 4388 4389writeLong(val: number): boolean 4390 4391将长整数值写入MessageParcel实例。 4392 4393**系统能力**:SystemCapability.Communication.IPC.Core 4394 4395**参数:** 4396 4397 | 参数名 | 类型 | 必填 | 说明 | 4398 | ------ | ------ | ---- | ---------------- | 4399 | val | number | 是 | 要写入的长整数值 | 4400 4401**返回值:** 4402 4403 | 类型 | 说明 | 4404 | ------- | --------------------------------- | 4405 | boolean | true:写入成功,false:写入失败。| 4406 4407**示例:** 4408 4409 ```ts 4410 import { hilog } from '@kit.PerformanceAnalysisKit'; 4411 4412 let data = rpc.MessageParcel.create(); 4413 let result = data.writeLong(10000); 4414 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4415 ``` 4416 4417### readLong 4418 4419readLong(): number 4420 4421从MessageParcel实例中读取长整数值。 4422 4423**系统能力**:SystemCapability.Communication.IPC.Core 4424 4425**返回值:** 4426 4427 | 类型 | 说明 | 4428 | ------ | -------------- | 4429 | number | 返回长整数值。 | 4430 4431**示例:** 4432 4433 ```ts 4434 import { hilog } from '@kit.PerformanceAnalysisKit'; 4435 4436 let data = rpc.MessageParcel.create(); 4437 let result = data.writeLong(10000); 4438 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4439 let ret = data.readLong(); 4440 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4441 ``` 4442 4443### writeFloat 4444 4445writeFloat(val: number): boolean 4446 4447将浮点值写入MessageParcel实例。 4448 4449**系统能力**:SystemCapability.Communication.IPC.Core 4450 4451**参数:** 4452 4453 | 参数名 | 类型 | 必填 | 说明 | 4454 | ------ | ------ | ---- | ---------------- | 4455 | val | number | 是 | 要写入的浮点值。 | 4456 4457**返回值:** 4458 4459 | 类型 | 说明 | 4460 | ------- | --------------------------------- | 4461 | boolean | true:写入成功,false:写入失败。| 4462 4463**示例:** 4464 4465 ```ts 4466 import { hilog } from '@kit.PerformanceAnalysisKit'; 4467 4468 let data = rpc.MessageParcel.create(); 4469 let result = data.writeFloat(1.2); 4470 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4471 ``` 4472 4473### readFloat 4474 4475readFloat(): number 4476 4477从MessageParcel实例中读取浮点值。 4478 4479**系统能力**:SystemCapability.Communication.IPC.Core 4480 4481**返回值:** 4482 4483 | 类型 | 说明 | 4484 | ------ | ------------ | 4485 | number | 返回浮点值。 | 4486 4487**示例:** 4488 4489 ```ts 4490 import { hilog } from '@kit.PerformanceAnalysisKit'; 4491 4492 let data = rpc.MessageParcel.create(); 4493 let result = data.writeFloat(1.2); 4494 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4495 let ret = data.readFloat(); 4496 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4497 ``` 4498 4499### writeDouble 4500 4501writeDouble(val: number): boolean 4502 4503将双精度浮点值写入MessageParcel实例。 4504 4505**系统能力**:SystemCapability.Communication.IPC.Core 4506 4507**参数:** 4508 4509 | 参数名 | 类型 | 必填 | 说明 | 4510 | ------ | ------ | ---- | ---------------------- | 4511 | val | number | 是 | 要写入的双精度浮点值。 | 4512 4513**返回值:** 4514 4515 | 类型 | 说明 | 4516 | ------- | --------------------------------- | 4517 | boolean | true:写入成功,false:写入失败。| 4518 4519**示例:** 4520 4521 ```ts 4522 import { hilog } from '@kit.PerformanceAnalysisKit'; 4523 4524 let data = rpc.MessageParcel.create(); 4525 let result = data.writeDouble(10.2); 4526 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4527 ``` 4528 4529### readDouble 4530 4531readDouble(): number 4532 4533从MessageParcel实例读取双精度浮点值。 4534 4535**系统能力**:SystemCapability.Communication.IPC.Core 4536 4537**返回值:** 4538 4539 | 类型 | 说明 | 4540 | ------ | ------------------ | 4541 | number | 返回双精度浮点值。 | 4542 4543**示例:** 4544 4545 ```ts 4546 import { hilog } from '@kit.PerformanceAnalysisKit'; 4547 4548 let data = rpc.MessageParcel.create(); 4549 let result = data.writeDouble(10.2); 4550 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4551 let ret = data.readDouble(); 4552 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4553 ``` 4554 4555### writeBoolean 4556 4557writeBoolean(val: boolean): boolean 4558 4559将布尔值写入MessageParcel实例。 4560 4561**系统能力**:SystemCapability.Communication.IPC.Core 4562 4563**参数:** 4564 4565 | 参数名 | 类型 | 必填 | 说明 | 4566 | ------ | ------- | ---- | ---------------- | 4567 | val | boolean | 是 | 要写入的布尔值。 | 4568 4569**返回值:** 4570 4571 | 类型 | 说明 | 4572 | ------- | --------------------------------- | 4573 | boolean | true:写入成功,false:写入失败。| 4574 4575**示例:** 4576 4577 ```ts 4578 import { hilog } from '@kit.PerformanceAnalysisKit'; 4579 4580 let data = rpc.MessageParcel.create(); 4581 let result = data.writeBoolean(false); 4582 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4583 ``` 4584 4585### readBoolean 4586 4587readBoolean(): boolean 4588 4589从MessageParcel实例读取布尔值。 4590 4591**系统能力**:SystemCapability.Communication.IPC.Core 4592 4593**返回值:** 4594 4595 | 类型 | 说明 | 4596 | ------- | -------------------- | 4597 | boolean | 返回读取到的布尔值。 | 4598 4599**示例:** 4600 4601 ```ts 4602 import { hilog } from '@kit.PerformanceAnalysisKit'; 4603 4604 let data = rpc.MessageParcel.create(); 4605 let result = data.writeBoolean(false); 4606 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4607 let ret = data.readBoolean(); 4608 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4609 ``` 4610 4611### writeChar 4612 4613writeChar(val: number): boolean 4614 4615将单个字符值写入MessageParcel实例。 4616 4617**系统能力**:SystemCapability.Communication.IPC.Core 4618 4619**参数:** 4620 4621 | 参数名 | 类型 | 必填 | 说明 | 4622 | ------ | ------ | ---- | -------------------- | 4623 | val | number | 是 | 要写入的单个字符值。 | 4624 4625**返回值:** 4626 4627 | 类型 | 说明 | 4628 | ------- | ----------------------------- | 4629 | boolean | true:写入成功,false:写入失败。| 4630 4631**示例:** 4632 4633 ```ts 4634 import { hilog } from '@kit.PerformanceAnalysisKit'; 4635 4636 let data = rpc.MessageParcel.create(); 4637 let result = data.writeChar(97); 4638 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4639 ``` 4640 4641### readChar 4642 4643readChar(): number 4644 4645从MessageParcel实例中读取单个字符值。 4646 4647**系统能力**:SystemCapability.Communication.IPC.Core 4648 4649**返回值:** 4650 4651 | 类型 | 说明 | 4652 | ------ | ---------------- | 4653 | number | 返回单个字符值。 | 4654 4655**示例:** 4656 4657 ```ts 4658 import { hilog } from '@kit.PerformanceAnalysisKit'; 4659 4660 let data = rpc.MessageParcel.create(); 4661 let result = data.writeChar(97); 4662 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4663 let ret = data.readChar(); 4664 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4665 ``` 4666 4667### writeString 4668 4669writeString(val: string): boolean 4670 4671将字符串值写入MessageParcel实例。 4672 4673**系统能力**:SystemCapability.Communication.IPC.Core 4674 4675**参数:** 4676 4677 | 参数名 | 类型 | 必填 | 说明 | 4678 | ------ | ------ | ---- | ----------------------------------------- | 4679 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 4680 4681**返回值:** 4682 4683 | 类型 | 说明 | 4684 | ------- | --------------------------------- | 4685 | boolean | true:写入成功,false:写入失败。| 4686 4687**示例:** 4688 4689 ```ts 4690 import { hilog } from '@kit.PerformanceAnalysisKit'; 4691 4692 let data = rpc.MessageParcel.create(); 4693 let result = data.writeString('abc'); 4694 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4695 ``` 4696 4697### readString 4698 4699readString(): string 4700 4701从MessageParcel实例读取字符串值。 4702 4703**系统能力**:SystemCapability.Communication.IPC.Core 4704 4705**返回值:** 4706 4707 | 类型 | 说明 | 4708 | ------ | -------------- | 4709 | string | 返回字符串值。 | 4710 4711**示例:** 4712 4713 ```ts 4714 import { hilog } from '@kit.PerformanceAnalysisKit'; 4715 4716 let data = rpc.MessageParcel.create(); 4717 let result = data.writeString('abc'); 4718 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4719 let ret = data.readString(); 4720 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4721 ``` 4722 4723### writeSequenceable 4724 4725writeSequenceable(val: Sequenceable): boolean 4726 4727将自定义序列化对象写入MessageParcel实例。 4728 4729**系统能力**:SystemCapability.Communication.IPC.Core 4730 4731**参数:** 4732 4733 | 参数名 | 类型 | 必填 | 说明 | 4734 | ------ | ----------------------------- | ---- | -------------------- | 4735 | val | [Sequenceable](#sequenceabledeprecated) | 是 | 要写入的可序列对象。 | 4736 4737**返回值:** 4738 4739 | 类型 | 说明 | 4740 | ------- | -------------------------------- | 4741 | boolean | true:写入成功,false:写入失败。| 4742 4743**示例:** 4744 4745 ```ts 4746 import { hilog } from '@kit.PerformanceAnalysisKit'; 4747 4748 class MySequenceable implements rpc.Sequenceable { 4749 num: number = 0; 4750 str: string = ''; 4751 constructor(num: number, str: string) { 4752 this.num = num; 4753 this.str = str; 4754 } 4755 marshalling(messageParcel: rpc.MessageParcel): boolean { 4756 messageParcel.writeInt(this.num); 4757 messageParcel.writeString(this.str); 4758 return true; 4759 } 4760 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4761 this.num = messageParcel.readInt(); 4762 this.str = messageParcel.readString(); 4763 return true; 4764 } 4765 } 4766 let sequenceable = new MySequenceable(1, "aaa"); 4767 let data = rpc.MessageParcel.create(); 4768 let result = data.writeSequenceable(sequenceable); 4769 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4770 ``` 4771 4772### readSequenceable 4773 4774readSequenceable(dataIn: Sequenceable): boolean 4775 4776从MessageParcel实例中读取成员变量到指定的对象(dataIn)。 4777 4778**系统能力**:SystemCapability.Communication.IPC.Core 4779 4780**参数:** 4781 4782 | 参数名 | 类型 | 必填 | 说明 | 4783 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4784 | dataIn | [Sequenceable](#sequenceabledeprecated) | 是 | 需要从MessageParcel读取成员变量的对象。 | 4785 4786**返回值:** 4787 4788 | 类型 | 说明 | 4789 | ------- | ---------------------------------------- | 4790 | boolean | true:反序列化成功,false:反序列化失败。| 4791 4792**示例:** 4793 4794 ```ts 4795 import { hilog } from '@kit.PerformanceAnalysisKit'; 4796 4797 class MySequenceable implements rpc.Sequenceable { 4798 num: number = 0; 4799 str: string = ''; 4800 constructor(num: number, str: string) { 4801 this.num = num; 4802 this.str = str; 4803 } 4804 marshalling(messageParcel: rpc.MessageParcel): boolean { 4805 messageParcel.writeInt(this.num); 4806 messageParcel.writeString(this.str); 4807 return true; 4808 } 4809 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4810 this.num = messageParcel.readInt(); 4811 this.str = messageParcel.readString(); 4812 return true; 4813 } 4814 } 4815 let sequenceable = new MySequenceable(1, "aaa"); 4816 let data = rpc.MessageParcel.create(); 4817 let result = data.writeSequenceable(sequenceable); 4818 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4819 let ret = new MySequenceable(0, ""); 4820 let result2 = data.readSequenceable(ret); 4821 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4822 ``` 4823 4824### writeByteArray 4825 4826writeByteArray(byteArray: number[]): boolean 4827 4828将字节数组写入MessageParcel实例。 4829 4830**系统能力**:SystemCapability.Communication.IPC.Core 4831 4832**参数:** 4833 4834 | 参数名 | 类型 | 必填 | 说明 | 4835 | --------- | -------- | ---- | ------------------ | 4836 | byteArray | number[] | 是 | 要写入的字节数组。 | 4837 4838**返回值:** 4839 4840 | 类型 | 说明 | 4841 | ------- | -------------------------------- | 4842 | boolean | true:写入成功,false:写入失败。| 4843 4844**示例:** 4845 4846 ```ts 4847 import { hilog } from '@kit.PerformanceAnalysisKit'; 4848 4849 let data = rpc.MessageParcel.create(); 4850 let ByteArrayVar = [1, 2, 3, 4, 5]; 4851 let result = data.writeByteArray(ByteArrayVar); 4852 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4853 ``` 4854 4855### readByteArray 4856 4857readByteArray(dataIn: number[]): void 4858 4859从MessageParcel实例读取字节数组。 4860 4861**系统能力**:SystemCapability.Communication.IPC.Core 4862 4863**参数:** 4864 4865 | 参数名 | 类型 | 必填 | 说明 | 4866 | ------ | -------- | ---- | ------------------ | 4867 | dataIn | number[] | 是 | 要读取的字节数组。 | 4868 4869**示例:** 4870 4871 ```ts 4872 import { hilog } from '@kit.PerformanceAnalysisKit'; 4873 4874 let data = rpc.MessageParcel.create(); 4875 let ByteArrayVar = [1, 2, 3, 4, 5]; 4876 let result = data.writeByteArray(ByteArrayVar); 4877 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4878 let array: Array<number> = new Array(5); 4879 data.readByteArray(array); 4880 ``` 4881 4882### readByteArray 4883 4884readByteArray(): number[] 4885 4886从MessageParcel实例中读取字节数组。 4887 4888**系统能力**:SystemCapability.Communication.IPC.Core 4889 4890**返回值:** 4891 4892 | 类型 | 说明 | 4893 | -------- | -------------- | 4894 | number[] | 返回字节数组。 | 4895 4896**示例:** 4897 4898 ```ts 4899 import { hilog } from '@kit.PerformanceAnalysisKit'; 4900 4901 let data = rpc.MessageParcel.create(); 4902 let ByteArrayVar = [1, 2, 3, 4, 5]; 4903 let result = data.writeByteArray(ByteArrayVar); 4904 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4905 let array = data.readByteArray(); 4906 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4907 ``` 4908 4909### writeShortArray 4910 4911writeShortArray(shortArray: number[]): boolean 4912 4913将短整数数组写入MessageParcel实例。 4914 4915**系统能力**:SystemCapability.Communication.IPC.Core 4916 4917**参数:** 4918 4919 | 参数名 | 类型 | 必填 | 说明 | 4920 | ---------- | -------- | ---- | -------------------- | 4921 | shortArray | number[] | 是 | 要写入的短整数数组。 | 4922 4923**返回值:** 4924 4925 | 类型 | 说明 | 4926 | ------- | -------------------------------- | 4927 | boolean | true:写入成功,false:写入失败。| 4928 4929**示例:** 4930 4931 ```ts 4932 import { hilog } from '@kit.PerformanceAnalysisKit'; 4933 4934 let data = rpc.MessageParcel.create(); 4935 let result = data.writeShortArray([11, 12, 13]); 4936 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4937 ``` 4938 4939### readShortArray 4940 4941readShortArray(dataIn: number[]): void 4942 4943从MessageParcel实例中读取短整数数组。 4944 4945**系统能力**:SystemCapability.Communication.IPC.Core 4946 4947**参数:** 4948 4949 | 参数名 | 类型 | 必填 | 说明 | 4950 | ------ | -------- | ---- | -------------------- | 4951 | dataIn | number[] | 是 | 要读取的短整数数组。 | 4952 4953**示例:** 4954 4955 ```ts 4956 import { hilog } from '@kit.PerformanceAnalysisKit'; 4957 4958 let data = rpc.MessageParcel.create(); 4959 let result = data.writeShortArray([11, 12, 13]); 4960 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4961 let array: Array<number> = new Array(3); 4962 data.readShortArray(array); 4963 ``` 4964 4965### readShortArray 4966 4967readShortArray(): number[] 4968 4969从MessageParcel实例中读取短整数数组。 4970 4971**系统能力**:SystemCapability.Communication.IPC.Core 4972 4973**返回值:** 4974 4975 | 类型 | 说明 | 4976 | -------- | ---------------- | 4977 | number[] | 返回短整数数组。 | 4978 4979**示例:** 4980 4981 ```ts 4982 import { hilog } from '@kit.PerformanceAnalysisKit'; 4983 4984 let data = rpc.MessageParcel.create(); 4985 let result = data.writeShortArray([11, 12, 13]); 4986 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4987 let array = data.readShortArray(); 4988 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 4989 ``` 4990 4991### writeIntArray 4992 4993writeIntArray(intArray: number[]): boolean 4994 4995将整数数组写入MessageParcel实例。 4996 4997**系统能力**:SystemCapability.Communication.IPC.Core 4998 4999**参数:** 5000 5001 | 参数名 | 类型 | 必填 | 说明 | 5002 | -------- | -------- | ---- | ------------------ | 5003 | intArray | number[] | 是 | 要写入的整数数组。 | 5004 5005**返回值:** 5006 5007 | 类型 | 说明 | 5008 | ------- | -------------------------------- | 5009 | boolean | true:写入成功,false:写入失败。| 5010 5011**示例:** 5012 5013 ```ts 5014 import { hilog } from '@kit.PerformanceAnalysisKit'; 5015 5016 let data = rpc.MessageParcel.create(); 5017 let result = data.writeIntArray([100, 111, 112]); 5018 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5019 ``` 5020 5021### readIntArray 5022 5023readIntArray(dataIn: number[]): void 5024 5025从MessageParcel实例中读取整数数组。 5026 5027**系统能力**:SystemCapability.Communication.IPC.Core 5028 5029**参数:** 5030 5031 | 参数名 | 类型 | 必填 | 说明 | 5032 | ------ | -------- | ---- | ------------------ | 5033 | dataIn | number[] | 是 | 要读取的整数数组。 | 5034 5035**示例:** 5036 5037 ```ts 5038 import { hilog } from '@kit.PerformanceAnalysisKit'; 5039 5040 let data = rpc.MessageParcel.create(); 5041 let result = data.writeIntArray([100, 111, 112]); 5042 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5043 let array: Array<number> = new Array(3); 5044 data.readIntArray(array); 5045 ``` 5046 5047### readIntArray 5048 5049readIntArray(): number[] 5050 5051从MessageParcel实例中读取整数数组。 5052 5053**系统能力**:SystemCapability.Communication.IPC.Core 5054 5055**返回值:** 5056 5057 | 类型 | 说明 | 5058 | -------- | -------------- | 5059 | number[] | 返回整数数组。 | 5060 5061**示例:** 5062 5063 ```ts 5064 import { hilog } from '@kit.PerformanceAnalysisKit'; 5065 5066 let data = rpc.MessageParcel.create(); 5067 let result = data.writeIntArray([100, 111, 112]); 5068 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5069 let array = data.readIntArray(); 5070 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 5071 ``` 5072 5073### writeLongArray 5074 5075writeLongArray(longArray: number[]): boolean 5076 5077将长整数数组写入MessageParcel实例。 5078 5079**系统能力**:SystemCapability.Communication.IPC.Core 5080 5081**参数:** 5082 5083 | 参数名 | 类型 | 必填 | 说明 | 5084 | --------- | -------- | ---- | -------------------- | 5085 | longArray | number[] | 是 | 要写入的长整数数组。 | 5086 5087**返回值:** 5088 5089 | 类型 | 说明 | 5090 | ------- | ----------------------------- | 5091 | boolean | true:写入成功,false:写入失败。| 5092 5093**示例:** 5094 5095 ```ts 5096 import { hilog } from '@kit.PerformanceAnalysisKit'; 5097 5098 let data = rpc.MessageParcel.create(); 5099 let result = data.writeLongArray([1111, 1112, 1113]); 5100 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5101 ``` 5102 5103### readLongArray 5104 5105readLongArray(dataIn: number[]): void 5106 5107从MessageParcel实例读取长整数数组。 5108 5109**系统能力**:SystemCapability.Communication.IPC.Core 5110 5111**参数:** 5112 5113 | 参数名 | 类型 | 必填 | 说明 | 5114 | ------ | -------- | ---- | -------------------- | 5115 | dataIn | number[] | 是 | 要读取的长整数数组。 | 5116 5117**示例:** 5118 5119 ```ts 5120 import { hilog } from '@kit.PerformanceAnalysisKit'; 5121 5122 let data = rpc.MessageParcel.create(); 5123 let result = data.writeLongArray([1111, 1112, 1113]); 5124 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5125 let array: Array<number> = new Array(3); 5126 data.readLongArray(array); 5127 ``` 5128 5129### readLongArray 5130 5131readLongArray(): number[] 5132 5133从MessageParcel实例中读取长整数数组。 5134 5135**系统能力**:SystemCapability.Communication.IPC.Core 5136 5137**返回值:** 5138 5139 | 类型 | 说明 | 5140 | -------- | ---------------- | 5141 | number[] | 返回长整数数组。 | 5142 5143**示例:** 5144 5145 ```ts 5146 import { hilog } from '@kit.PerformanceAnalysisKit'; 5147 5148 let data = rpc.MessageParcel.create(); 5149 let result = data.writeLongArray([1111, 1112, 1113]); 5150 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5151 let array = data.readLongArray(); 5152 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 5153 ``` 5154 5155### writeFloatArray 5156 5157writeFloatArray(floatArray: number[]): boolean 5158 5159将浮点数组写入MessageParcel实例。 5160 5161**系统能力**:SystemCapability.Communication.IPC.Core 5162 5163**参数:** 5164 5165 | 参数名 | 类型 | 必填 | 说明 | 5166 | ---------- | -------- | ---- | --- | 5167 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5168 5169**返回值:** 5170 5171 | 类型 | 说明 | 5172 | ------- | -------------------------------- | 5173 | boolean | true:写入成功,false:写入失败。| 5174 5175**示例:** 5176 5177 ```ts 5178 import { hilog } from '@kit.PerformanceAnalysisKit'; 5179 5180 let data = rpc.MessageParcel.create(); 5181 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5182 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5183 ``` 5184 5185### readFloatArray 5186 5187readFloatArray(dataIn: number[]): void 5188 5189从MessageParcel实例中读取浮点数组。 5190 5191**系统能力**:SystemCapability.Communication.IPC.Core 5192 5193**参数:** 5194 5195 | 参数名 | 类型 | 必填 | 说明 | 5196 | ------ | -------- | ---- | ------ | 5197 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5198 5199**示例:** 5200 5201 ```ts 5202 import { hilog } from '@kit.PerformanceAnalysisKit'; 5203 5204 let data = rpc.MessageParcel.create(); 5205 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5206 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5207 let array: Array<number> = new Array(3); 5208 data.readFloatArray(array); 5209 ``` 5210 5211### readFloatArray 5212 5213readFloatArray(): number[] 5214 5215从MessageParcel实例中读取浮点数组。 5216 5217**系统能力**:SystemCapability.Communication.IPC.Core 5218 5219**返回值:** 5220 5221 | 类型 | 说明 | 5222 | -------- | -------------- | 5223 | number[] | 返回浮点数组。 | 5224 5225**示例:** 5226 5227 ```ts 5228 import { hilog } from '@kit.PerformanceAnalysisKit'; 5229 5230 let data = rpc.MessageParcel.create(); 5231 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5232 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5233 let array = data.readFloatArray(); 5234 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 5235 ``` 5236 5237### writeDoubleArray 5238 5239writeDoubleArray(doubleArray: number[]): boolean 5240 5241将双精度浮点数组写入MessageParcel实例。 5242 5243**系统能力**:SystemCapability.Communication.IPC.Core 5244 5245**参数:** 5246 5247 | 参数名 | 类型 | 必填 | 说明 | 5248 | ----------- | -------- | ---- | ------------------------ | 5249 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 5250 5251**返回值:** 5252 5253 | 类型 | 说明 | 5254 | ------- | -------------------------------- | 5255 | boolean | true:写入成功,false:写入失败。| 5256 5257**示例:** 5258 5259 ```ts 5260 import { hilog } from '@kit.PerformanceAnalysisKit'; 5261 5262 let data = rpc.MessageParcel.create(); 5263 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5264 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5265 ``` 5266 5267### readDoubleArray 5268 5269readDoubleArray(dataIn: number[]): void 5270 5271从MessageParcel实例中读取双精度浮点数组。 5272 5273**系统能力**:SystemCapability.Communication.IPC.Core 5274 5275**参数:** 5276 5277 | 参数名 | 类型 | 必填 | 说明 | 5278 | ------ | -------- | ---- | ------------------------ | 5279 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 5280 5281**示例:** 5282 5283 ```ts 5284 import { hilog } from '@kit.PerformanceAnalysisKit'; 5285 5286 let data = rpc.MessageParcel.create(); 5287 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5288 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5289 let array: Array<number> = new Array(3); 5290 data.readDoubleArray(array); 5291 ``` 5292 5293### readDoubleArray 5294 5295readDoubleArray(): number[] 5296 5297从MessageParcel实例读取双精度浮点数组。 5298 5299**系统能力**:SystemCapability.Communication.IPC.Core 5300 5301**返回值:** 5302 5303 | 类型 | 说明 | 5304 | -------- | -------------------- | 5305 | number[] | 返回双精度浮点数组。 | 5306 5307**示例:** 5308 5309 ```ts 5310 import { hilog } from '@kit.PerformanceAnalysisKit'; 5311 5312 let data = rpc.MessageParcel.create(); 5313 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5314 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5315 let array = data.readDoubleArray(); 5316 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5317 ``` 5318 5319### writeBooleanArray 5320 5321writeBooleanArray(booleanArray: boolean[]): boolean 5322 5323将布尔数组写入MessageParcel实例。 5324 5325**系统能力**:SystemCapability.Communication.IPC.Core 5326 5327**参数:** 5328 5329 | 参数名 | 类型 | 必填 | 说明 | 5330 | ------------ | --------- | ---- | ------------------ | 5331 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 5332 5333**返回值:** 5334 5335 | 类型 | 说明 | 5336 | ------- | -------------------------------- | 5337 | boolean | true:写入成功,false:写入失败。| 5338 5339**示例:** 5340 5341 ```ts 5342 import { hilog } from '@kit.PerformanceAnalysisKit'; 5343 5344 let data = rpc.MessageParcel.create(); 5345 let result = data.writeBooleanArray([false, true, false]); 5346 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5347 ``` 5348 5349### readBooleanArray 5350 5351readBooleanArray(dataIn: boolean[]): void 5352 5353从MessageParcel实例中读取布尔数组。 5354 5355**系统能力**:SystemCapability.Communication.IPC.Core 5356 5357**参数:** 5358 5359 | 参数名 | 类型 | 必填 | 说明 | 5360 | ------ | --------- | ---- | ------------------ | 5361 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 5362 5363**示例:** 5364 5365 ```ts 5366 import { hilog } from '@kit.PerformanceAnalysisKit'; 5367 5368 let data = rpc.MessageParcel.create(); 5369 let result = data.writeBooleanArray([false, true, false]); 5370 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5371 let array: Array<boolean> = new Array(3); 5372 data.readBooleanArray(array); 5373 ``` 5374 5375### readBooleanArray 5376 5377readBooleanArray(): boolean[] 5378 5379从MessageParcel实例中读取布尔数组。 5380 5381**系统能力**:SystemCapability.Communication.IPC.Core 5382 5383**返回值:** 5384 5385 | 类型 | 说明 | 5386 | --------- | -------------- | 5387 | boolean[] | 返回布尔数组。 | 5388 5389**示例:** 5390 5391 ```ts 5392 import { hilog } from '@kit.PerformanceAnalysisKit'; 5393 5394 let data = rpc.MessageParcel.create(); 5395 let result = data.writeBooleanArray([false, true, false]); 5396 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5397 let array = data.readBooleanArray(); 5398 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5399 ``` 5400 5401### writeCharArray 5402 5403writeCharArray(charArray: number[]): boolean 5404 5405将单个字符数组写入MessageParcel实例。 5406 5407**系统能力**:SystemCapability.Communication.IPC.Core 5408 5409**参数:** 5410 5411 | 参数名 | 类型 | 必填 | 说明 | 5412 | --------- | -------- | ---- | ---------------------- | 5413 | charArray | number[] | 是 | 要写入的单个字符数组。 | 5414 5415**返回值:** 5416 5417 | 类型 | 说明 | 5418 | ------- | -------------------------------- | 5419 | boolean | true:写入成功,false:写入失败。| 5420 5421**示例:** 5422 5423 ```ts 5424 import { hilog } from '@kit.PerformanceAnalysisKit'; 5425 5426 let data = rpc.MessageParcel.create(); 5427 let result = data.writeCharArray([97, 98, 88]); 5428 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5429 ``` 5430 5431### readCharArray 5432 5433readCharArray(dataIn: number[]): void 5434 5435从MessageParcel实例中读取单个字符数组。 5436 5437**系统能力**:SystemCapability.Communication.IPC.Core 5438 5439**参数:** 5440 5441 | 参数名 | 类型 | 必填 | 说明 | 5442 | ------ | -------- | ---- | ---------------------- | 5443 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 5444 5445**示例:** 5446 5447 ```ts 5448 import { hilog } from '@kit.PerformanceAnalysisKit'; 5449 5450 let data = rpc.MessageParcel.create(); 5451 let result = data.writeCharArray([97, 98, 99]); 5452 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5453 let array: Array<number> = new Array(3); 5454 data.readCharArray(array); 5455 ``` 5456 5457### readCharArray 5458 5459readCharArray(): number[] 5460 5461从MessageParcel实例读取单个字符数组。 5462 5463**系统能力**:SystemCapability.Communication.IPC.Core 5464 5465**返回值:** 5466 5467 | 类型 | 说明 | 5468 | -------- | ------------------ | 5469 | number[] | 返回单个字符数组。 | 5470 5471**示例:** 5472 5473 ```ts 5474 import { hilog } from '@kit.PerformanceAnalysisKit'; 5475 5476 let data = rpc.MessageParcel.create(); 5477 let result = data.writeCharArray([97, 98, 99]); 5478 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5479 let array = data.readCharArray(); 5480 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5481 ``` 5482 5483### writeStringArray 5484 5485writeStringArray(stringArray: string[]): boolean 5486 5487将字符串数组写入MessageParcel实例。 5488 5489**系统能力**:SystemCapability.Communication.IPC.Core 5490 5491**参数:** 5492 5493 | 参数名 | 类型 | 必填 | 说明 | 5494 | ----------- | -------- | ---- | ---------------- | 5495 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 5496 5497**返回值:** 5498 5499 | 类型 | 说明 | 5500 | ------- | -------------------------------- | 5501 | boolean | true:写入成功,false:写入失败。| 5502 5503**示例:** 5504 5505 ```ts 5506 import { hilog } from '@kit.PerformanceAnalysisKit'; 5507 5508 let data = rpc.MessageParcel.create(); 5509 let result = data.writeStringArray(["abc", "def"]); 5510 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5511 ``` 5512 5513### readStringArray 5514 5515readStringArray(dataIn: string[]): void 5516 5517从MessageParcel实例读取字符串数组。 5518 5519**系统能力**:SystemCapability.Communication.IPC.Core 5520 5521**参数:** 5522 5523 | 参数名 | 类型 | 必填 | 说明 | 5524 | ------ | -------- | ---- | -------------------- | 5525 | dataIn | string[] | 是 | 要读取的字符串数组。 | 5526 5527**示例:** 5528 5529 ```ts 5530 import { hilog } from '@kit.PerformanceAnalysisKit'; 5531 5532 let data = rpc.MessageParcel.create(); 5533 let result = data.writeStringArray(["abc", "def"]); 5534 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5535 let array: Array<string> = new Array(2); 5536 data.readStringArray(array); 5537 ``` 5538 5539### readStringArray 5540 5541readStringArray(): string[] 5542 5543从MessageParcel实例读取字符串数组。 5544 5545**系统能力**:SystemCapability.Communication.IPC.Core 5546 5547**返回值:** 5548 5549 | 类型 | 说明 | 5550 | -------- | ---------------- | 5551 | string[] | 返回字符串数组。 | 5552 5553**示例:** 5554 5555 ```ts 5556 import { hilog } from '@kit.PerformanceAnalysisKit'; 5557 5558 let data = rpc.MessageParcel.create(); 5559 let result = data.writeStringArray(["abc", "def"]); 5560 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5561 let array = data.readStringArray(); 5562 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5563 ``` 5564 5565### writeNoException<sup>8+</sup> 5566 5567writeNoException(): void 5568 5569向MessageParcel写入“指示未发生异常”的信息。 5570 5571**系统能力**:SystemCapability.Communication.IPC.Core 5572 5573**示例:** 5574 5575 ```ts 5576 import { hilog } from '@kit.PerformanceAnalysisKit'; 5577 5578 class MyDeathRecipient implements rpc.DeathRecipient { 5579 onRemoteDied() { 5580 hilog.info(0x0000, 'testTag', 'server died'); 5581 } 5582 } 5583 class TestRemoteObject extends rpc.RemoteObject { 5584 constructor(descriptor: string) { 5585 super(descriptor); 5586 } 5587 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5588 return true; 5589 } 5590 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5591 return true; 5592 } 5593 isObjectDead(): boolean { 5594 return false; 5595 } 5596 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5597 if (code === 1) { 5598 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5599 reply.writeNoException(); 5600 return true; 5601 } else { 5602 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5603 return false; 5604 } 5605 } 5606 } 5607 ``` 5608 5609### readException<sup>8+</sup> 5610 5611readException(): void 5612 5613从MessageParcel中读取异常。 5614 5615**系统能力**:SystemCapability.Communication.IPC.Core 5616 5617**示例:** 5618 5619 ```ts 5620 // FA模型需要从@kit.AbilityKit导入featureAbility 5621 // import { featureAbility } from '@kit.AbilityKit'; 5622 import { Want, common } from '@kit.AbilityKit'; 5623 import { hilog } from '@kit.PerformanceAnalysisKit'; 5624 5625 let proxy: rpc.IRemoteObject | undefined; 5626 let connect: common.ConnectOptions = { 5627 onConnect: (elementName, remoteProxy) => { 5628 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5629 proxy = remoteProxy; 5630 }, 5631 onDisconnect: (elementName) => { 5632 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5633 }, 5634 onFailed: () => { 5635 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5636 } 5637 }; 5638 let want: Want = { 5639 bundleName: "com.ohos.server", 5640 abilityName: "com.ohos.server.EntryAbility", 5641 }; 5642 5643 // FA模型使用此方法连接服务 5644 // FA.connectAbility(want,connect); 5645 5646 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5647 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 5648 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5649 let connectionId = context.connectServiceExtensionAbility(want, connect); 5650 ``` 5651 5652 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 5653 5654 ```ts 5655 import { hilog } from '@kit.PerformanceAnalysisKit'; 5656 5657 let option = new rpc.MessageOption(); 5658 let data = rpc.MessageParcel.create(); 5659 let reply = rpc.MessageParcel.create(); 5660 data.writeNoException(); 5661 data.writeString('hello'); 5662 if (proxy != undefined) { 5663 let a = proxy.sendRequest(1, data, reply, option) as Object; 5664 let b = a as Promise<rpc.SendRequestResult>; 5665 b.then((result: rpc.SendRequestResult) => { 5666 if (result.errCode === 0) { 5667 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5668 result.reply.readException(); 5669 let msg = result.reply.readString(); 5670 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5671 } else { 5672 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5673 } 5674 }).catch((e: Error) => { 5675 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5676 }).finally (() => { 5677 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5678 data.reclaim(); 5679 reply.reclaim(); 5680 }); 5681 } 5682 ``` 5683 5684### writeSequenceableArray 5685 5686writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5687 5688将可序列化对象数组写入MessageParcel实例。 5689 5690**系统能力**:SystemCapability.Communication.IPC.Core 5691 5692**参数:** 5693 5694| 参数名 | 类型 | 必填 | 说明 | 5695| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5696| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要写入的可序列化对象数组。 | 5697 5698**返回值:** 5699 5700 | 类型 | 说明 | 5701 | ------- | -------------------------------- | 5702 | boolean | true:写入成功,false:写入失败。| 5703 5704**示例:** 5705 5706 ```ts 5707 import { hilog } from '@kit.PerformanceAnalysisKit'; 5708 5709 class MySequenceable implements rpc.Sequenceable { 5710 num: number = 0; 5711 str: string = ''; 5712 constructor(num: number, str: string) { 5713 this.num = num; 5714 this.str = str; 5715 } 5716 marshalling(messageParcel: rpc.MessageParcel): boolean { 5717 messageParcel.writeInt(this.num); 5718 messageParcel.writeString(this.str); 5719 return true; 5720 } 5721 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5722 this.num = messageParcel.readInt(); 5723 this.str = messageParcel.readString(); 5724 return true; 5725 } 5726 } 5727 let sequenceable = new MySequenceable(1, "aaa"); 5728 let sequenceable2 = new MySequenceable(2, "bbb"); 5729 let sequenceable3 = new MySequenceable(3, "ccc"); 5730 let a = [sequenceable, sequenceable2, sequenceable3]; 5731 let data = rpc.MessageParcel.create(); 5732 let result = data.writeSequenceableArray(a); 5733 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5734 ``` 5735 5736### readSequenceableArray<sup>8+</sup> 5737 5738readSequenceableArray(sequenceableArray: Sequenceable[]): void 5739 5740从MessageParcel实例读取可序列化对象数组。 5741 5742**系统能力**:SystemCapability.Communication.IPC.Core 5743 5744**参数:** 5745 5746| 参数名 | 类型 | 必填 | 说明 | 5747| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5748| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要读取的可序列化对象数组。 | 5749 5750**示例:** 5751 5752 ```ts 5753 import { hilog } from '@kit.PerformanceAnalysisKit'; 5754 5755 class MySequenceable implements rpc.Sequenceable { 5756 num: number = 0; 5757 str: string = ''; 5758 constructor(num: number, str: string) { 5759 this.num = num; 5760 this.str = str; 5761 } 5762 marshalling(messageParcel: rpc.MessageParcel): boolean { 5763 messageParcel.writeInt(this.num); 5764 messageParcel.writeString(this.str); 5765 return true; 5766 } 5767 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5768 this.num = messageParcel.readInt(); 5769 this.str = messageParcel.readString(); 5770 return true; 5771 } 5772 } 5773 let sequenceable = new MySequenceable(1, "aaa"); 5774 let sequenceable2 = new MySequenceable(2, "bbb"); 5775 let sequenceable3 = new MySequenceable(3, "ccc"); 5776 let a = [sequenceable, sequenceable2, sequenceable3]; 5777 let data = rpc.MessageParcel.create(); 5778 let result = data.writeSequenceableArray(a); 5779 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5780 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5781 data.readSequenceableArray(b); 5782 ``` 5783 5784### writeRemoteObjectArray<sup>8+</sup> 5785 5786writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5787 5788将IRemoteObject对象数组写入MessageParcel。 5789 5790**系统能力**:SystemCapability.Communication.IPC.Core 5791 5792**参数:** 5793 5794 | 参数名 | 类型 | 必填 | 说明 | 5795 | ----------- | --------------- | ---- | ----- | 5796 | objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageParcel的IRemoteObject对象数组。 | 5797 5798**返回值:** 5799 5800 | 类型 | 说明 | 5801 | ------- | -------------------------------- | 5802 | boolean | true:写入成功,false:写入失败。| 5803 5804**示例:** 5805 5806 ```ts 5807 import { hilog } from '@kit.PerformanceAnalysisKit'; 5808 5809 class MyDeathRecipient implements rpc.DeathRecipient { 5810 onRemoteDied() { 5811 hilog.info(0x0000, 'testTag', 'server died'); 5812 } 5813 } 5814 class TestRemoteObject extends rpc.RemoteObject { 5815 constructor(descriptor: string) { 5816 super(descriptor); 5817 this.attachLocalInterface(this, descriptor); 5818 } 5819 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5820 return true; 5821 } 5822 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5823 return true; 5824 } 5825 isObjectDead(): boolean { 5826 return false; 5827 } 5828 asObject(): rpc.IRemoteObject { 5829 return this; 5830 } 5831 } 5832 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5833 let data = rpc.MessageParcel.create(); 5834 let result = data.writeRemoteObjectArray(a); 5835 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5836 ``` 5837 5838### readRemoteObjectArray<sup>8+</sup> 5839 5840readRemoteObjectArray(objects: IRemoteObject[]): void 5841 5842从MessageParcel读取IRemoteObject对象数组。 5843 5844**系统能力**:SystemCapability.Communication.IPC.Core 5845 5846**参数:** 5847 5848 | 参数名 | 类型 | 必填 | 说明 | 5849 | ------- | --------------- | ---- | --------- | 5850 | objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageParcel读取的IRemoteObject对象数组。 | 5851 5852**示例:** 5853 5854 ```ts 5855 import { hilog } from '@kit.PerformanceAnalysisKit'; 5856 5857 class MyDeathRecipient implements rpc.DeathRecipient { 5858 onRemoteDied() { 5859 hilog.info(0x0000, 'testTag', 'server died'); 5860 } 5861 } 5862 class TestRemoteObject extends rpc.RemoteObject { 5863 constructor(descriptor: string) { 5864 super(descriptor); 5865 this.attachLocalInterface(this, descriptor); 5866 } 5867 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5868 return true; 5869 } 5870 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5871 return true; 5872 } 5873 isObjectDead(): boolean { 5874 return false; 5875 } 5876 asObject(): rpc.IRemoteObject { 5877 return this; 5878 } 5879 } 5880 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5881 let data = rpc.MessageParcel.create(); 5882 data.writeRemoteObjectArray(a); 5883 let b: Array<rpc.IRemoteObject> = new Array(3); 5884 data.readRemoteObjectArray(b); 5885 ``` 5886 5887### readRemoteObjectArray<sup>8+</sup> 5888 5889readRemoteObjectArray(): IRemoteObject[] 5890 5891从MessageParcel读取IRemoteObject对象数组。 5892 5893**系统能力**:SystemCapability.Communication.IPC.Core 5894 5895**返回值:** 5896 5897 | 类型 | 说明 | 5898 | --------------- | --------------------------- | 5899 | [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 5900 5901**示例:** 5902 5903 ```ts 5904 import { hilog } from '@kit.PerformanceAnalysisKit'; 5905 5906 class MyDeathRecipient implements rpc.DeathRecipient { 5907 onRemoteDied() { 5908 hilog.info(0x0000, 'testTag', 'server died'); 5909 } 5910 } 5911 class TestRemoteObject extends rpc.RemoteObject { 5912 constructor(descriptor: string) { 5913 super(descriptor); 5914 this.attachLocalInterface(this, descriptor); 5915 } 5916 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5917 return true; 5918 } 5919 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5920 return true; 5921 } 5922 isObjectDead(): boolean { 5923 return false; 5924 } 5925 asObject(): rpc.IRemoteObject { 5926 return this; 5927 } 5928 } 5929 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5930 let data = rpc.MessageParcel.create(); 5931 let result = data.writeRemoteObjectArray(a); 5932 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5933 let b = data.readRemoteObjectArray(); 5934 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5935 ``` 5936 5937### closeFileDescriptor<sup>8+</sup> 5938 5939static closeFileDescriptor(fd: number): void 5940 5941静态方法,关闭给定的文件描述符。 5942 5943**系统能力**:SystemCapability.Communication.IPC.Core 5944 5945**参数:** 5946 5947 | 参数名 | 类型 | 必填 | 说明 | 5948 | ------ | ------ | ---- | -------------------- | 5949 | fd | number | 是 | 要关闭的文件描述符。 | 5950 5951**示例:** 5952 5953 ```ts 5954 import { fileIo } from '@kit.CoreFileKit'; 5955 5956 let filePath = "path/to/file"; 5957 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5958 rpc.MessageParcel.closeFileDescriptor(file.fd); 5959 ``` 5960 5961### dupFileDescriptor<sup>8+</sup> 5962 5963static dupFileDescriptor(fd: number) :number 5964 5965静态方法,复制给定的文件描述符。 5966 5967**系统能力**:SystemCapability.Communication.IPC.Core 5968 5969**参数:** 5970 5971 | 参数名 | 类型 | 必填 | 说明 | 5972 | ------ | ------ | ---- | ------------------------ | 5973 | fd | number | 是 | 表示已存在的文件描述符。 | 5974 5975**返回值:** 5976 5977 | 类型 | 说明 | 5978 | ------ | -------------------- | 5979 | number | 返回新的文件描述符。 | 5980 5981**示例:** 5982 5983 ```ts 5984 import { fileIo } from '@kit.CoreFileKit'; 5985 5986 let filePath = "path/to/file"; 5987 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5988 rpc.MessageParcel.dupFileDescriptor(file.fd); 5989 ``` 5990 5991### containFileDescriptors<sup>8+</sup> 5992 5993containFileDescriptors(): boolean 5994 5995检查此MessageParcel对象是否包含文件描述符。 5996 5997**系统能力**:SystemCapability.Communication.IPC.Core 5998 5999**返回值:** 6000 6001 | 类型 | 说明 | 6002 | ------- | --------------------------------------------- | 6003 | boolean |true:包含文件描述符,false:未包含文件描述符。| 6004 6005**示例:** 6006 6007 ```ts 6008 import { fileIo } from '@kit.CoreFileKit'; 6009 import { hilog } from '@kit.PerformanceAnalysisKit'; 6010 6011 let parcel = new rpc.MessageParcel(); 6012 let filePath = "path/to/file"; 6013 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6014 let writeResult = parcel.writeFileDescriptor(file.fd); 6015 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6016 let containFD = parcel.containFileDescriptors(); 6017 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 6018 ``` 6019 6020### writeFileDescriptor<sup>8+</sup> 6021 6022writeFileDescriptor(fd: number): boolean 6023 6024写入文件描述符到MessageParcel。 6025 6026**系统能力**:SystemCapability.Communication.IPC.Core 6027 6028**参数:** 6029 6030 | 参数名 | 类型 | 必填 | 说明 | 6031 | ------ | ------ | ---- | ------------ | 6032 | fd | number | 是 | 文件描述符。 | 6033 6034**返回值:** 6035 6036 | 类型 | 说明 | 6037 | ------- | -------------------------------- | 6038 | boolean | true:操作成功,false:操作失败。| 6039 6040**示例:** 6041 6042 ```ts 6043 import { fileIo } from '@kit.CoreFileKit'; 6044 import { hilog } from '@kit.PerformanceAnalysisKit'; 6045 6046 let parcel = new rpc.MessageParcel(); 6047 let filePath = "path/to/file"; 6048 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6049 let writeResult = parcel.writeFileDescriptor(file.fd); 6050 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6051 ``` 6052 6053### readFileDescriptor<sup>8+</sup> 6054 6055readFileDescriptor(): number 6056 6057从MessageParcel中读取文件描述符。 6058 6059**系统能力**:SystemCapability.Communication.IPC.Core 6060 6061**返回值:** 6062 6063 | 类型 | 说明 | 6064 | ------ | ---------------- | 6065 | number | 返回文件描述符。 | 6066 6067**示例:** 6068 6069 ```ts 6070 import { fileIo } from '@kit.CoreFileKit'; 6071 import { hilog } from '@kit.PerformanceAnalysisKit'; 6072 6073 let parcel = new rpc.MessageParcel(); 6074 let filePath = "path/to/file"; 6075 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6076 parcel.writeFileDescriptor(file.fd); 6077 let readFD = parcel.readFileDescriptor(); 6078 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 6079 ``` 6080 6081### writeAshmem<sup>8+</sup> 6082 6083writeAshmem(ashmem: Ashmem): boolean 6084 6085将指定的匿名共享对象写入此MessageParcel。 6086 6087**系统能力**:SystemCapability.Communication.IPC.Core 6088 6089**参数:** 6090 6091| 参数名 | 类型 | 必填 | 说明 | 6092| ------ | ------ | ---- | ----------------------------------- | 6093| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageParcel的匿名共享对象。 | 6094 6095**返回值:** 6096 6097 | 类型 | 说明 | 6098 | ------- | -------------------------------- | 6099 | boolean | true:写入成功,false:写入失败。| 6100 6101**示例:** 6102 6103 ```ts 6104 import { hilog } from '@kit.PerformanceAnalysisKit'; 6105 6106 let parcel = new rpc.MessageParcel(); 6107 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6108 let isWriteSuccess = parcel.writeAshmem(ashmem); 6109 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6110 ``` 6111 6112### readAshmem<sup>8+</sup> 6113 6114readAshmem(): Ashmem 6115 6116从MessageParcel读取匿名共享对象。 6117 6118**系统能力**:SystemCapability.Communication.IPC.Core 6119 6120**返回值:** 6121 6122| 类型 | 说明 | 6123| ------ | ------------------ | 6124| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 6125 6126**示例:** 6127 6128 ```ts 6129 import { hilog } from '@kit.PerformanceAnalysisKit'; 6130 6131 let parcel = new rpc.MessageParcel(); 6132 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6133 let isWriteSuccess = parcel.writeAshmem(ashmem); 6134 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6135 let readAshmem = parcel.readAshmem(); 6136 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 6137 ``` 6138 6139### getRawDataCapacity<sup>8+</sup> 6140 6141getRawDataCapacity(): number 6142 6143获取MessageParcel可以容纳的最大原始数据量。 6144 6145**系统能力**:SystemCapability.Communication.IPC.Core 6146 6147**返回值:** 6148 6149 | 类型 | 说明 | 6150 | ------ | ---------------------------------------------------------- | 6151 | number | 返回MessageParcel可以容纳的最大原始数据量,即128MB。 | 6152 6153**示例:** 6154 6155 ```ts 6156 import { hilog } from '@kit.PerformanceAnalysisKit'; 6157 6158 let parcel = new rpc.MessageParcel(); 6159 let result = parcel.getRawDataCapacity(); 6160 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 6161 ``` 6162 6163### writeRawData<sup>8+</sup> 6164 6165writeRawData(rawData: number[], size: number): boolean 6166 6167将原始数据写入MessageParcel对象。 6168 6169**系统能力**:SystemCapability.Communication.IPC.Core 6170 6171**参数:** 6172 6173 | 参数名 | 类型 | 必填 | 说明 | 6174 | ------- | -------- | ---- | ---------------------------------- | 6175 | rawData | number[] | 是 | 要写入的原始数据。 | 6176 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 6177 6178**返回值:** 6179 6180 | 类型 | 说明 | 6181 | ------- | -------------------------------- | 6182 | boolean | true:写入成功,false:写入失败。| 6183 6184**示例:** 6185 6186 ```ts 6187 import { hilog } from '@kit.PerformanceAnalysisKit'; 6188 6189 let parcel = new rpc.MessageParcel(); 6190 let arr = [1, 2, 3, 4, 5]; 6191 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6192 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6193 ``` 6194 6195### readRawData<sup>8+</sup> 6196 6197readRawData(size: number): number[] 6198 6199从MessageParcel读取原始数据。 6200 6201**系统能力**:SystemCapability.Communication.IPC.Core 6202 6203**参数:** 6204 6205 | 参数名 | 类型 | 必填 | 说明 | 6206 | ------ | ------ | ---- | ------------------------ | 6207 | size | number | 是 | 要读取的原始数据的大小。 | 6208 6209**返回值:** 6210 6211 | 类型 | 说明 | 6212 | -------- | ------------------------------ | 6213 | number[] | 返回原始数据(以字节为单位)。 | 6214 6215**示例:** 6216 6217 ```ts 6218 import { hilog } from '@kit.PerformanceAnalysisKit'; 6219 6220 let parcel = new rpc.MessageParcel(); 6221 let arr = [1, 2, 3, 4, 5]; 6222 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6223 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6224 let result = parcel.readRawData(5); 6225 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 6226 ``` 6227 6228## Parcelable<sup>9+</sup> 6229 6230在进程间通信(IPC)期间,将类的对象写入MessageSequence并从MessageSequence中恢复它们。 6231 6232### marshalling 6233 6234marshalling(dataOut: MessageSequence): boolean 6235 6236将此可序列对象封送到MessageSequence中。 6237 6238**系统能力**:SystemCapability.Communication.IPC.Core 6239 6240**参数:** 6241 6242| 参数名 | 类型 | 必填 | 说明 | 6243| ------- | --------------- | ---- | ------------------------------------------- | 6244| dataOut |[MessageSequence](#messagesequence9)| 是 | 可序列对象将被封送到的MessageSequence对象。 | 6245 6246**返回值:** 6247 6248 | 类型 | 说明 | 6249 | ------- | -------------------------------- | 6250 | boolean | true:封送成功,false:封送失败。| 6251 6252**示例:** 6253 6254 ```ts 6255 import { hilog } from '@kit.PerformanceAnalysisKit'; 6256 6257 class MyParcelable implements rpc.Parcelable { 6258 num: number = 0; 6259 str: string = ''; 6260 constructor(num: number, str: string) { 6261 this.num = num; 6262 this.str = str; 6263 } 6264 marshalling(messageSequence: rpc.MessageSequence): boolean { 6265 messageSequence.writeInt(this.num); 6266 messageSequence.writeString(this.str); 6267 return true; 6268 } 6269 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6270 this.num = messageSequence.readInt(); 6271 this.str = messageSequence.readString(); 6272 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6273 return true; 6274 } 6275 } 6276 let parcelable = new MyParcelable(1, "aaa"); 6277 let data = rpc.MessageSequence.create(); 6278 data.writeParcelable(parcelable); 6279 let ret = new MyParcelable(0, ""); 6280 data.readParcelable(ret); 6281 ``` 6282 6283### unmarshalling 6284 6285unmarshalling(dataIn: MessageSequence): boolean 6286 6287从MessageSequence中解封此可序列对象。 6288 6289**系统能力**:SystemCapability.Communication.IPC.Core 6290 6291**参数:** 6292 6293| 参数名 | 类型 | 必填 | 说明 | 6294| ------ | --------------- | ---- | ----------------------------------------------- | 6295| dataIn | [MessageSequence](#messagesequence9) | 是 | 已将可序列对象封送到其中的MessageSequence对象。 | 6296 6297**返回值:** 6298 6299 | 类型 | 说明 | 6300 | ------- | ---------------------------------------- | 6301 | boolean | true:反序列化成功,false:反序列化失败。| 6302 6303**示例:** 6304 6305 ```ts 6306 import { hilog } from '@kit.PerformanceAnalysisKit'; 6307 6308 class MyParcelable implements rpc.Parcelable { 6309 num: number = 0; 6310 str: string = ''; 6311 constructor(num: number, str: string) { 6312 this.num = num; 6313 this.str = str; 6314 } 6315 marshalling(messageSequence: rpc.MessageSequence): boolean { 6316 messageSequence.writeInt(this.num); 6317 messageSequence.writeString(this.str); 6318 return true; 6319 } 6320 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6321 this.num = messageSequence.readInt(); 6322 this.str = messageSequence.readString(); 6323 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6324 return true; 6325 } 6326 } 6327 let parcelable = new MyParcelable(1, "aaa"); 6328 let data = rpc.MessageSequence.create(); 6329 data.writeParcelable(parcelable); 6330 let ret = new MyParcelable(0, ""); 6331 data.readParcelable(ret); 6332 ``` 6333 6334## Sequenceable<sup>(deprecated)</sup> 6335 6336>从API version 9 开始废弃,建议使用[Parcelable](#parcelable9)替代。 6337 6338在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。 6339 6340### marshalling 6341 6342marshalling(dataOut: MessageParcel): boolean 6343 6344将此可序列对象封送到MessageParcel中。 6345 6346**系统能力**:SystemCapability.Communication.IPC.Core 6347 6348**参数:** 6349 6350 | 参数名 | 类型 | 必填 | 说明 | 6351 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6352 | dataOut | [MessageParcel](#messageparceldeprecated) | 是 | 可序列对象将被封送到的MessageParcel对象。 | 6353 6354**返回值:** 6355 6356 | 类型 | 说明 | 6357 | ------- | -------------------------------- | 6358 | boolean | true:封送成功,false:封送失败。 | 6359 6360**示例:** 6361 6362 ```ts 6363 import { hilog } from '@kit.PerformanceAnalysisKit'; 6364 6365 class MySequenceable implements rpc.Sequenceable { 6366 num: number = 0; 6367 str: string = ''; 6368 constructor(num: number, str: string) { 6369 this.num = num; 6370 this.str = str; 6371 } 6372 marshalling(messageParcel: rpc.MessageParcel): boolean { 6373 messageParcel.writeInt(this.num); 6374 messageParcel.writeString(this.str); 6375 return true; 6376 } 6377 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6378 this.num = messageParcel.readInt(); 6379 this.str = messageParcel.readString(); 6380 return true; 6381 } 6382 } 6383 let sequenceable = new MySequenceable(1, "aaa"); 6384 let data = rpc.MessageParcel.create(); 6385 let result = data.writeSequenceable(sequenceable); 6386 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6387 let ret = new MySequenceable(0, ""); 6388 let result2 = data.readSequenceable(ret); 6389 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6390 ``` 6391 6392### unmarshalling 6393 6394unmarshalling(dataIn: MessageParcel): boolean 6395 6396从MessageParcel中解封此可序列对象。 6397 6398**系统能力**:SystemCapability.Communication.IPC.Core 6399 6400**参数:** 6401 6402 | 参数名 | 类型 | 必填 | 说明 | 6403 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6404 | dataIn | [MessageParcel](#messageparceldeprecated) | 是 | 已将可序列对象封送到其中的MessageParcel对象。 | 6405 6406**返回值:** 6407 6408 | 类型 | 说明 | 6409 | ------- | ---------------------------------------- | 6410 | boolean | true:反序列化成功,false:反序列化失败。| 6411 6412**示例:** 6413 6414 ```ts 6415 import { hilog } from '@kit.PerformanceAnalysisKit'; 6416 6417 class MySequenceable implements rpc.Sequenceable { 6418 num: number = 0; 6419 str: string = ''; 6420 constructor(num: number, str: string) { 6421 this.num = num; 6422 this.str = str; 6423 } 6424 marshalling(messageParcel: rpc.MessageParcel): boolean { 6425 messageParcel.writeInt(this.num); 6426 messageParcel.writeString(this.str); 6427 return true; 6428 } 6429 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6430 this.num = messageParcel.readInt(); 6431 this.str = messageParcel.readString(); 6432 return true; 6433 } 6434 } 6435 let sequenceable = new MySequenceable(1, "aaa"); 6436 let data = rpc.MessageParcel.create(); 6437 let result = data.writeSequenceable(sequenceable); 6438 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6439 let ret = new MySequenceable(0, ""); 6440 let result2 = data.readSequenceable(ret); 6441 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6442 ``` 6443 6444## IRemoteBroker 6445 6446远端对象的代理持有者。用于获取代理对象。 6447 6448### asObject 6449 6450asObject(): IRemoteObject 6451 6452需派生类实现,获取代理或远端对象。 6453 6454**系统能力**:SystemCapability.Communication.IPC.Core 6455 6456**返回值:** 6457 6458 | 类型 | 说明 | 6459 | ----- | ----- | 6460 | [IRemoteObject](#iremoteobject) | 如果调用者是RemoteObject对象,则直接返回本身;如果调用者是[RemoteProxy](#remoteproxy)对象,则返回它的持有者[IRemoteObject](#iremoteobject)。 | 6461 6462**示例:** 6463 6464 ```ts 6465 class TestAbility extends rpc.RemoteObject { 6466 asObject() { 6467 return this; 6468 } 6469 } 6470 let remoteObject = new TestAbility("testObject").asObject(); 6471 ``` 6472 6473**示例:** 6474 6475 ```ts 6476 // FA模型需要从@kit.AbilityKit导入featureAbility 6477 // import { featureAbility } from '@kit.AbilityKit'; 6478 import { Want, common } from '@kit.AbilityKit'; 6479 import { hilog } from '@kit.PerformanceAnalysisKit'; 6480 6481 let proxy: rpc.IRemoteObject | undefined; 6482 let connect: common.ConnectOptions = { 6483 onConnect: (elementName, remoteProxy) => { 6484 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6485 proxy = remoteProxy; 6486 }, 6487 onDisconnect: (elementName) => { 6488 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6489 }, 6490 onFailed: () => { 6491 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6492 } 6493 }; 6494 let want: Want = { 6495 bundleName: "com.ohos.server", 6496 abilityName: "com.ohos.server.EntryAbility", 6497 }; 6498 6499 // FA模型使用此方法连接服务 6500 // FA.connectAbility(want,connect); 6501 6502 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6503 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 6504 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6505 let connectionId = context.connectServiceExtensionAbility(want, connect); 6506 ``` 6507 6508 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象 6509 6510 ```ts 6511 class TestProxy { 6512 remote: rpc.IRemoteObject; 6513 constructor(remote: rpc.IRemoteObject) { 6514 this.remote = remote; 6515 } 6516 asObject() { 6517 return this.remote; 6518 } 6519 } 6520 if (proxy != undefined) { 6521 let iRemoteObject = new TestProxy(proxy).asObject(); 6522 } 6523 ``` 6524 6525## DeathRecipient 6526 6527用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied](#onremotedied)接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。 6528 6529### onRemoteDied 6530 6531onRemoteDied(): void 6532 6533在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。 6534 6535**系统能力**:SystemCapability.Communication.IPC.Core 6536 6537**示例:** 6538 6539 ```ts 6540 import { hilog } from '@kit.PerformanceAnalysisKit'; 6541 6542 class MyDeathRecipient implements rpc.DeathRecipient { 6543 onRemoteDied() { 6544 hilog.info(0x0000, 'testTag', 'server died'); 6545 } 6546 } 6547 ``` 6548 6549## RequestResult<sup>9+</sup> 6550 6551发送请求的响应结果。 6552 6553**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6554 6555| 名称 | 类型 | 可读 | 可写 | 说明 | 6556| ------- | --------------- | ---- | ---- |-------------------------------------- | 6557| errCode | number | 是 | 否 | 错误码。 | 6558| code | number | 是 | 否 | 消息代码。 | 6559| data | [MessageSequence](#messagesequence9) | 是 | 否 | 发送给对端进程的MessageSequence对象。 | 6560| reply | [MessageSequence](#messagesequence9) | 是 | 否 | 对端进程返回的MessageSequence对象。 | 6561 6562## SendRequestResult<sup>(deprecated)</sup> 6563 6564>从API version 8 开始支持,API version 9 开始废弃,建议使用[RequestResult](#requestresult9)替代。 6565 6566发送请求的响应结果。 6567 6568**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6569 6570 | 名称 | 类型 | 可读 | 可写 | 说明 | 6571 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6572 | errCode | number | 是 | 否 | 错误码。 | 6573 | code | number | 是 | 否 | 消息代码。 | 6574 | data | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 发送给对端进程的MessageParcel对象。 | 6575 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 对端进程返回的MessageParcel对象。 | 6576 6577## IRemoteObject 6578 6579该接口可用于查询或获取接口描述符、添加或删除死亡通知、转储对象状态到特定文件、发送消息。 6580 6581### getLocalInterface<sup>9+</sup> 6582 6583getLocalInterface(descriptor: string): IRemoteBroker 6584 6585查询接口描述符的字符串。 6586 6587**系统能力**:SystemCapability.Communication.IPC.Core 6588 6589**参数:** 6590 6591 | 参数名 | 类型 | 必填 | 说明 | 6592 | ---------- | ------ | ---- | -------------------- | 6593 | descriptor | string | 是 | 接口描述符的字符串。 | 6594 6595**返回值:** 6596 6597| 类型 | 说明 | 6598| ------------- | --------------------------------------------- | 6599| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6600 6601**错误码:** 6602 6603以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6604 6605 | 错误码ID | 错误信息 | 6606 | -------- | -------- | 6607 | 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. | 6608 6609### queryLocalInterface<sup>(deprecated)</sup> 6610 6611>从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9)替代。 6612 6613queryLocalInterface(descriptor: string): IRemoteBroker 6614 6615查询接口描述符的字符串。 6616 6617**系统能力**:SystemCapability.Communication.IPC.Core 6618 6619**参数:** 6620 6621 | 参数名 | 类型 | 必填 | 说明 | 6622 | ---------- | ------ | ---- | -------------------- | 6623 | descriptor | string | 是 | 接口描述符的字符串。 | 6624 6625**返回值:** 6626 6627| 类型 | 说明 | 6628| ------------- | --------------------------------------------- | 6629| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6630 6631### sendRequest<sup>(deprecated)</sup> 6632 6633>从API version 8开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 6634 6635sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6636 6637以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6638 6639**系统能力**:SystemCapability.Communication.IPC.Core 6640 6641**参数:** 6642 6643 | 参数名 | 类型 | 必填 | 说明 | 6644 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6645 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6646 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6647 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6648 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6649 6650**返回值:** 6651 6652 | 类型 | 说明 | 6653 | ------- | -------------------------------- | 6654 | boolean | true:发送成功,false:发送失败。| 6655 6656### sendMessageRequest<sup>9+</sup> 6657 6658sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6659 6660以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 6661 6662**系统能力**:SystemCapability.Communication.IPC.Core 6663 6664**参数:** 6665 6666 | 参数名 | 类型 | 必填 | 说明 | 6667 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6668 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6669 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 6670 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6671 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6672 6673**返回值:** 6674 6675 | 类型 | 说明 | 6676 | ---------------------------- | ----------------------------------------- | 6677 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 6678 6679**错误码:** 6680 6681以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6682 6683 | 错误码ID | 错误信息 | 6684 | -------- | -------- | 6685 | 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. | 6686 6687### sendRequest<sup>(deprecated)</sup> 6688 6689>从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 6690 6691sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6692 6693以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6694 6695**系统能力**:SystemCapability.Communication.IPC.Core 6696 6697**参数:** 6698 6699 | 参数名 | 类型 | 必填 | 说明 | 6700 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6701 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6702 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6703 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6704 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6705 6706**返回值:** 6707 6708| 类型 | 说明 | 6709| ------------------------------------------------------------ | --------------------------------------------- | 6710| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 6711 6712### sendMessageRequest<sup>9+</sup> 6713 6714sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6715 6716以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6717 6718**系统能力**:SystemCapability.Communication.IPC.Core 6719 6720**参数:** 6721 6722 | 参数名 | 类型 | 必填 | 说明 | 6723 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6724 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6725 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 6726 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6727 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6728 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 6729 6730**错误码:** 6731 6732以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6733 6734 | 错误码ID | 错误信息 | 6735 | -------- | -------- | 6736 | 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. | 6737 6738### sendRequest<sup>(deprecated)</sup> 6739 6740>从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-1)替代。 6741 6742sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6743 6744以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6745 6746**系统能力**:SystemCapability.Communication.IPC.Core 6747 6748**参数:** 6749 6750| 参数名 | 类型 | 必填 | 说明 | 6751| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6752| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6753| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6754| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6755| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6756| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 6757 6758### registerDeathRecipient<sup>9+</sup> 6759 6760registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6761 6762注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6763 6764**系统能力**:SystemCapability.Communication.IPC.Core 6765 6766**参数:** 6767 6768 | 参数名 | 类型 | 必填 | 说明 | 6769 | --------- | --------------------------------- | ---- | -------------- | 6770 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6771 | flags | number | 是 | 死亡通知标志。 | 6772 6773**错误码:** 6774 6775以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6776 6777 | 错误码ID | 错误信息 | 6778 | -------- | -------- | 6779 | 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. | 6780 | 1900008 | The proxy or remote object is invalid. | 6781 6782### addDeathrecipient<sup>(deprecated)</sup> 6783 6784>从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9)替代。 6785 6786addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6787 6788注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6789 6790**系统能力**:SystemCapability.Communication.IPC.Core 6791 6792**参数:** 6793 6794 | 参数名 | 类型 | 必填 | 说明 | 6795 | --------- | --------------------------------- | ---- | -------------- | 6796 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6797 | flags | number | 是 | 死亡通知标志。 | 6798 6799**返回值:** 6800 6801 | 类型 | 说明 | 6802 | ------- | ---------------------------------------- | 6803 | boolean | true:回调注册成功,false:回调注册失败。| 6804 6805### unregisterDeathRecipient<sup>9+</sup> 6806 6807unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6808 6809注销用于接收远程对象死亡通知的回调。 6810 6811**系统能力**:SystemCapability.Communication.IPC.Core 6812 6813**参数:** 6814 6815 | 参数名 | 类型 | 必填 | 说明 | 6816 | --------- | --------------------------------- | ---- | -------------- | 6817 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6818 | flags | number | 是 | 死亡通知标志。 | 6819 6820**错误码:** 6821 6822以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6823 6824 | 错误码ID | 错误信息 | 6825 | -------- | -------- | 6826 | 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. | 6827 | 1900008 | The proxy or remote object is invalid. | 6828 6829### removeDeathRecipient<sup>(deprecated)</sup> 6830 6831>从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)替代。 6832 6833removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6834 6835注销用于接收远程对象死亡通知的回调。 6836 6837**系统能力**:SystemCapability.Communication.IPC.Core 6838 6839**参数:** 6840 6841 | 参数名 | 类型 | 必填 | 说明 | 6842 | --------- | --------------------------------- | ---- | -------------- | 6843 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6844 | flags | number | 是 | 死亡通知标志。 | 6845 6846**返回值:** 6847 6848 | 类型 | 说明 | 6849 | ------- | -----------------------------------------| 6850 | boolean | true:回调注销成功,false:回调注销失败。| 6851 6852### getDescriptor<sup>9+</sup> 6853 6854getDescriptor(): string 6855 6856获取对象的接口描述符,接口描述符为字符串。 6857 6858**系统能力**:SystemCapability.Communication.IPC.Core 6859 6860**返回值:** 6861 6862 | 类型 | 说明 | 6863 | ------ | ---------------- | 6864 | string | 返回接口描述符。 | 6865 6866**错误码:** 6867 6868以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6869 6870 | 错误码ID | 错误信息 | 6871 | -------- | -------- | 6872 | 1900008 | The proxy or remote object is invalid. | 6873 6874### getInterfaceDescriptor<sup>(deprecated)</sup> 6875 6876>从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9)替代。 6877 6878getInterfaceDescriptor(): string 6879 6880获取对象的接口描述符,接口描述符为字符串。 6881 6882**系统能力**:SystemCapability.Communication.IPC.Core 6883 6884**返回值:** 6885 6886 | 类型 | 说明 | 6887 | ------ | ---------------- | 6888 | string | 返回接口描述符。 | 6889 6890### isObjectDead 6891 6892isObjectDead(): boolean 6893 6894检查当前对象是否死亡。 6895 6896**系统能力**:SystemCapability.Communication.IPC.Core 6897 6898**返回值:** 6899 6900 | 类型 | 说明 | 6901 | ------- | ---------------------------------- | 6902 | boolean | true:对象死亡,false:对象未死亡。| 6903 6904## RemoteProxy 6905 6906实现IRemoteObject代理对象。 6907 6908**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6909 6910| 名称 | 值 | 说明 | 6911| --------------------- | ----------------------- | --------------------------------- | 6912| PING_TRANSACTION | 1599098439 (0x5f504e47) | 内部指令码,用于测试IPC服务正常。 | 6913| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | 内部指令码,获取Binder内部状态。 | 6914| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | 内部指令码,获取对端接口描述符。 | 6915| MIN_TRANSACTION_ID | 1 (0x00000001) | 最小有效指令码。 | 6916| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | 最大有效指令码。 | 6917 6918### sendRequest<sup>(deprecated)</sup> 6919 6920>从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 6921 6922sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6923 6924以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6925 6926**系统能力**:SystemCapability.Communication.IPC.Core 6927 6928**参数:** 6929 6930 | 参数名 | 类型 | 必填 | 说明 | 6931 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6932 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6933 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6934 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6935 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6936 6937**返回值:** 6938 6939 | 类型 | 说明 | 6940 | ------- | ---------------------------------| 6941 | boolean | true:发送成功,false:发送失败。| 6942 6943**示例:** 6944 6945 ```ts 6946 // FA模型需要从@kit.AbilityKit导入featureAbility 6947 // import { featureAbility } from '@kit.AbilityKit'; 6948 import { Want, common } from '@kit.AbilityKit'; 6949 import { hilog } from '@kit.PerformanceAnalysisKit'; 6950 6951 let proxy: rpc.IRemoteObject | undefined; 6952 let connect: common.ConnectOptions = { 6953 onConnect: (elementName, remoteProxy) => { 6954 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6955 proxy = remoteProxy; 6956 }, 6957 onDisconnect: (elementName) => { 6958 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6959 }, 6960 onFailed: () => { 6961 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6962 } 6963 }; 6964 let want: Want = { 6965 bundleName: "com.ohos.server", 6966 abilityName: "com.ohos.server.EntryAbility", 6967 }; 6968 6969 // FA模型使用此方法连接服务 6970 // FA.connectAbility(want,connect); 6971 6972 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6973 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 6974 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6975 let connectionId = context.connectServiceExtensionAbility(want, connect); 6976 ``` 6977 6978 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 6979 6980 ```ts 6981 import { hilog } from '@kit.PerformanceAnalysisKit'; 6982 6983 let option = new rpc.MessageOption(); 6984 let data = rpc.MessageParcel.create(); 6985 let reply = rpc.MessageParcel.create(); 6986 data.writeInt(1); 6987 data.writeString("hello"); 6988 if (proxy != undefined) { 6989 let ret: boolean = proxy.sendRequest(1, data, reply, option); 6990 if (ret) { 6991 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 6992 let msg = reply.readString(); 6993 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6994 } else { 6995 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 6996 } 6997 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 6998 data.reclaim(); 6999 reply.reclaim(); 7000 } 7001 ``` 7002 7003### sendMessageRequest<sup>9+</sup> 7004 7005sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7006 7007以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 7008 7009**系统能力**:SystemCapability.Communication.IPC.Core 7010 7011**参数:** 7012 7013 | 参数名 | 类型 | 必填 | 说明 | 7014 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7015 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7016 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7017 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7018 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7019 7020**返回值:** 7021 7022 | 类型 | 说明 | 7023 | ---------------------------- | ----------------------------------------- | 7024 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 7025 7026 7027**错误码:** 7028 7029以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7030 7031 | 错误码ID | 错误信息 | 7032 | -------- | -------- | 7033 | 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. | 7034 7035**示例:** 7036 7037 ```ts 7038 // FA模型需要从@kit.AbilityKit导入featureAbility 7039 // import { featureAbility } from '@kit.AbilityKit'; 7040 import { Want, common } from '@kit.AbilityKit'; 7041 import { hilog } from '@kit.PerformanceAnalysisKit'; 7042 7043 let proxy: rpc.IRemoteObject | undefined; 7044 let connect: common.ConnectOptions = { 7045 onConnect: (elementName, remoteProxy) => { 7046 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7047 proxy = remoteProxy; 7048 }, 7049 onDisconnect: (elementName) => { 7050 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7051 }, 7052 onFailed: () => { 7053 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7054 } 7055 }; 7056 let want: Want = { 7057 bundleName: "com.ohos.server", 7058 abilityName: "com.ohos.server.EntryAbility", 7059 }; 7060 7061 // FA模型使用此方法连接服务 7062 // FA.connectAbility(want,connect); 7063 7064 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7065 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7066 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7067 let connectionId = context.connectServiceExtensionAbility(want, connect); 7068 ``` 7069 7070 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 7071 7072 ```ts 7073 import { hilog } from '@kit.PerformanceAnalysisKit'; 7074 7075 let option = new rpc.MessageOption(); 7076 let data = rpc.MessageSequence.create(); 7077 let reply = rpc.MessageSequence.create(); 7078 data.writeInt(1); 7079 data.writeString("hello"); 7080 if (proxy != undefined) { 7081 proxy.sendMessageRequest(1, data, reply, option) 7082 .then((result: rpc.RequestResult) => { 7083 if (result.errCode === 0) { 7084 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7085 let num = result.reply.readInt(); 7086 let msg = result.reply.readString(); 7087 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7088 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7089 } else { 7090 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7091 } 7092 }).catch((e: Error) => { 7093 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 7094 }).finally (() => { 7095 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7096 data.reclaim(); 7097 reply.reclaim(); 7098 }); 7099 } 7100 ``` 7101 7102### sendRequest<sup>(deprecated)</sup> 7103 7104>从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 7105 7106sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7107 7108以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7109 7110**系统能力**:SystemCapability.Communication.IPC.Core 7111 7112**参数:** 7113 7114 | 参数名 | 类型 | 必填 | 说明 | 7115 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7116 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7117 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7118 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7119 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7120 7121**返回值:** 7122 7123| 类型 | 说明 | 7124| ------------------------------------------------------------ | --------------------------------------------- | 7125| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 7126 7127**示例:** 7128 7129 ```ts 7130 // FA模型需要从@kit.AbilityKit导入featureAbility 7131 // import { featureAbility } from '@kit.AbilityKit'; 7132 import { Want, common } from '@kit.AbilityKit'; 7133 import { hilog } from '@kit.PerformanceAnalysisKit'; 7134 7135 let proxy: rpc.IRemoteObject | undefined; 7136 let connect: common.ConnectOptions = { 7137 onConnect: (elementName, remoteProxy) => { 7138 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7139 proxy = remoteProxy; 7140 }, 7141 onDisconnect: (elementName) => { 7142 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7143 }, 7144 onFailed: () => { 7145 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7146 } 7147 }; 7148 let want: Want = { 7149 bundleName: "com.ohos.server", 7150 abilityName: "com.ohos.server.EntryAbility", 7151 }; 7152 7153 // FA模型使用此方法连接服务 7154 // FA.connectAbility(want,connect); 7155 7156 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7157 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7158 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7159 let connectionId = context.connectServiceExtensionAbility(want, connect); 7160 ``` 7161 7162 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7163 7164 ```ts 7165 import { hilog } from '@kit.PerformanceAnalysisKit'; 7166 7167 let option = new rpc.MessageOption(); 7168 let data = rpc.MessageParcel.create(); 7169 let reply = rpc.MessageParcel.create(); 7170 data.writeInt(1); 7171 data.writeString("hello"); 7172 if (proxy != undefined) { 7173 let a = proxy.sendRequest(1, data, reply, option) as Object; 7174 let b = a as Promise<rpc.SendRequestResult>; 7175 b.then((result: rpc.SendRequestResult) => { 7176 if (result.errCode === 0) { 7177 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7178 let num = result.reply.readInt(); 7179 let msg = result.reply.readString(); 7180 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7181 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7182 } else { 7183 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7184 } 7185 }).catch((e: Error) => { 7186 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 7187 }).finally (() => { 7188 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7189 data.reclaim(); 7190 reply.reclaim(); 7191 }); 7192 } 7193 ``` 7194 7195### sendMessageRequest<sup>9+</sup> 7196 7197sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7198 7199以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回后的某个时机执行回调,回复内容在RequestResult的reply报文里。 7200 7201**系统能力**:SystemCapability.Communication.IPC.Core 7202 7203**参数:** 7204 7205 | 参数名 | 类型 | 必填 | 说明 | 7206 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7207 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7208 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7209 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7210 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7211 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 7212 7213 7214**错误码:** 7215 7216以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7217 7218 | 错误码ID | 错误信息 | 7219 | -------- | -------- | 7220 | 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. | 7221 7222**示例:** 7223 7224 ```ts 7225 // FA模型需要从@kit.AbilityKit导入featureAbility 7226 // import { featureAbility } from '@kit.AbilityKit'; 7227 import { Want, common } from '@kit.AbilityKit'; 7228 import { hilog } from '@kit.PerformanceAnalysisKit'; 7229 import { BusinessError } from '@kit.BasicServicesKit'; 7230 7231 let proxy: rpc.IRemoteObject | undefined; 7232 let connect: common.ConnectOptions = { 7233 onConnect: (elementName, remoteProxy) => { 7234 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7235 proxy = remoteProxy; 7236 }, 7237 onDisconnect: (elementName) => { 7238 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7239 }, 7240 onFailed: () => { 7241 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7242 } 7243 }; 7244 let want: Want = { 7245 bundleName: "com.ohos.server", 7246 abilityName: "com.ohos.server.EntryAbility", 7247 }; 7248 function sendMessageRequestCallback(err: BusinessError, result: rpc.RequestResult) { 7249 if (result.errCode === 0) { 7250 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7251 let num = result.reply.readInt(); 7252 let msg = result.reply.readString(); 7253 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7254 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7255 } else { 7256 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7257 } 7258 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7259 result.data.reclaim(); 7260 result.reply.reclaim(); 7261} 7262 7263 // FA模型使用此方法连接服务 7264 // FA.connectAbility(want,connect); 7265 7266 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7267 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7268 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7269 let connectionId = context.connectServiceExtensionAbility(want, connect); 7270 ``` 7271 7272 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 7273 7274 ```ts 7275 import { hilog } from '@kit.PerformanceAnalysisKit'; 7276 import { BusinessError } from '@kit.BasicServicesKit'; 7277 7278 let option = new rpc.MessageOption(); 7279 let data = rpc.MessageSequence.create(); 7280 let reply = rpc.MessageSequence.create(); 7281 data.writeInt(1); 7282 data.writeString("hello"); 7283 if (proxy != undefined) { 7284 try { 7285 proxy.sendMessageRequest(1, data, reply, option, sendMessageRequestCallback); 7286 } catch (error) { 7287 let e: BusinessError = error as BusinessError; 7288 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorCode ' + e.code); 7289 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorMessage ' + e.message); 7290 } 7291 } 7292 ``` 7293 7294### sendRequest<sup>(deprecated)</sup> 7295 7296>从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-3)替代。 7297 7298sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7299 7300以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 7301 7302**系统能力**:SystemCapability.Communication.IPC.Core 7303 7304**参数:** 7305 7306| 参数名 | 类型 | 必填 | 说明 | 7307| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7308| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7309| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7310| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7311| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7312| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 7313 7314**示例:** 7315 7316 ```ts 7317 // FA模型需要从@kit.AbilityKit导入featureAbility 7318 // import { featureAbility } from '@kit.AbilityKit'; 7319 import { Want, common } from '@kit.AbilityKit'; 7320 import { hilog } from '@kit.PerformanceAnalysisKit'; 7321 import { BusinessError } from '@kit.BasicServicesKit'; 7322 7323 let proxy: rpc.IRemoteObject | undefined; 7324 let connect: common.ConnectOptions = { 7325 onConnect: (elementName, remoteProxy) => { 7326 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7327 proxy = remoteProxy; 7328 }, 7329 onDisconnect: (elementName) => { 7330 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7331 }, 7332 onFailed: () => { 7333 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7334 } 7335 }; 7336 let want: Want = { 7337 bundleName: "com.ohos.server", 7338 abilityName: "com.ohos.server.EntryAbility", 7339 }; 7340 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 7341 if (result.errCode === 0) { 7342 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7343 let num = result.reply.readInt(); 7344 let msg = result.reply.readString(); 7345 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7346 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7347 } else { 7348 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7349 } 7350 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7351 result.data.reclaim(); 7352 result.reply.reclaim(); 7353} 7354 7355 // FA模型使用此方法连接服务 7356 // FA.connectAbility(want,connect); 7357 7358 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7359 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7360 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7361 let connectionId = context.connectServiceExtensionAbility(want, connect); 7362 ``` 7363 7364 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7365 7366 ```ts 7367 let option = new rpc.MessageOption(); 7368 let data = rpc.MessageParcel.create(); 7369 let reply = rpc.MessageParcel.create(); 7370 data.writeInt(1); 7371 data.writeString("hello"); 7372 if (proxy != undefined) { 7373 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 7374 } 7375 ``` 7376 7377### getLocalInterface<sup>9+</sup> 7378 7379getLocalInterface(interface: string): IRemoteBroker 7380 7381查询并获取当前接口描述符对应的本地接口对象。 7382 7383**系统能力**:SystemCapability.Communication.IPC.Core 7384 7385**参数:** 7386 7387 | 参数名 | 类型 | 必填 | 说明 | 7388 | --------- | ------ | ---- | ---------------------- | 7389 | interface | string | 是 | 需要查询的接口描述符。 | 7390 7391**返回值:** 7392 7393| 类型 | 说明 | 7394| ------------------------------- | ------------------------------------------ | 7395| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7396 7397**错误码:** 7398 7399以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7400 7401 | 错误码ID | 错误信息 | 7402 | -------- | -------- | 7403 | 401 | check param failed | 7404 | 1900006 | Operation allowed only for the remote object. | 7405 7406**示例:** 7407 7408 ```ts 7409 // FA模型需要从@kit.AbilityKit导入featureAbility 7410 // import { featureAbility } from '@kit.AbilityKit'; 7411 import { Want, common } from '@kit.AbilityKit'; 7412 import { hilog } from '@kit.PerformanceAnalysisKit'; 7413 7414 let proxy: rpc.IRemoteObject | undefined; 7415 let connect: common.ConnectOptions = { 7416 onConnect: (elementName, remoteProxy) => { 7417 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7418 proxy = remoteProxy; 7419 }, 7420 onDisconnect: (elementName) => { 7421 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7422 }, 7423 onFailed: () => { 7424 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7425 } 7426 }; 7427 let want: Want = { 7428 bundleName: "com.ohos.server", 7429 abilityName: "com.ohos.server.EntryAbility", 7430 }; 7431 7432 // FA模型使用此方法连接服务 7433 // FA.connectAbility(want,connect); 7434 7435 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7436 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7437 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7438 let connectionId = context.connectServiceExtensionAbility(want, connect); 7439 ``` 7440 7441 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象 7442 7443 ```ts 7444 import { hilog } from '@kit.PerformanceAnalysisKit'; 7445 import { BusinessError } from '@kit.BasicServicesKit'; 7446 7447 if (proxy != undefined) { 7448 try { 7449 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7450 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7451 } catch (error) { 7452 let e: BusinessError = error as BusinessError; 7453 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7454 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7455 } 7456 } 7457 ``` 7458 7459### queryLocalInterface<sup>(deprecated)</sup> 7460 7461>从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-1)替代。 7462 7463queryLocalInterface(interface: string): IRemoteBroker 7464 7465查询并获取当前接口描述符对应的本地接口对象。 7466 7467**系统能力**:SystemCapability.Communication.IPC.Core 7468 7469**参数:** 7470 7471 | 参数名 | 类型 | 必填 | 说明 | 7472 | --------- | ------ | ---- | ---------------------- | 7473 | interface | string | 是 | 需要查询的接口描述符。 | 7474 7475**返回值:** 7476 7477| 类型 | 说明 | 7478| ------------------------------- | ------------------------------------------ | 7479| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7480 7481**示例:** 7482 7483 ```ts 7484 // FA模型需要从@kit.AbilityKit导入featureAbility 7485 // import { featureAbility } from '@kit.AbilityKit'; 7486 import { Want, common } from '@kit.AbilityKit'; 7487 import { hilog } from '@kit.PerformanceAnalysisKit'; 7488 7489 let proxy: rpc.IRemoteObject | undefined; 7490 let connect: common.ConnectOptions = { 7491 onConnect: (elementName, remoteProxy) => { 7492 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7493 proxy = remoteProxy; 7494 }, 7495 onDisconnect: (elementName) => { 7496 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7497 }, 7498 onFailed: () => { 7499 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7500 } 7501 }; 7502 let want: Want = { 7503 bundleName: "com.ohos.server", 7504 abilityName: "com.ohos.server.EntryAbility", 7505 }; 7506 7507 // FA模型使用此方法连接服务 7508 // FA.connectAbility(want,connect); 7509 7510 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7511 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7512 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7513 let connectionId = context.connectServiceExtensionAbility(want, connect); 7514 ``` 7515 7516 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象 7517 7518 ```ts 7519 import { hilog } from '@kit.PerformanceAnalysisKit'; 7520 7521 if (proxy != undefined) { 7522 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7523 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7524 } 7525 ``` 7526 7527### registerDeathRecipient<sup>9+</sup> 7528 7529registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7530 7531注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7532 7533**系统能力**:SystemCapability.Communication.IPC.Core 7534 7535**参数:** 7536 7537 | 参数名 | 类型 | 必填 | 说明 | 7538 | --------- | --------------------------------- | ---- | -------------- | 7539 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7540 | flags | number | 是 | 死亡通知标志。 | 7541 7542**错误码:** 7543 7544以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7545 7546 | 错误码ID | 错误信息 | 7547 | -------- | -------- | 7548 | 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. | 7549 | 1900008 | The proxy or remote object is invalid. | 7550 7551**示例:** 7552 7553 ```ts 7554 // FA模型需要从@kit.AbilityKit导入featureAbility 7555 // import { featureAbility } from '@kit.AbilityKit'; 7556 import { Want, common } from '@kit.AbilityKit'; 7557 import { hilog } from '@kit.PerformanceAnalysisKit'; 7558 7559 let proxy: rpc.IRemoteObject | undefined; 7560 let connect: common.ConnectOptions = { 7561 onConnect: (elementName, remoteProxy) => { 7562 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7563 proxy = remoteProxy; 7564 }, 7565 onDisconnect: (elementName) => { 7566 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7567 }, 7568 onFailed: () => { 7569 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7570 } 7571 }; 7572 let want: Want = { 7573 bundleName: "com.ohos.server", 7574 abilityName: "com.ohos.server.EntryAbility", 7575 }; 7576 7577 // FA模型使用此方法连接服务 7578 // FA.connectAbility(want,connect); 7579 7580 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7581 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7582 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7583 let connectionId = context.connectServiceExtensionAbility(want, connect); 7584 ``` 7585 7586 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调 7587 7588 ```ts 7589 import { hilog } from '@kit.PerformanceAnalysisKit'; 7590 import { BusinessError } from '@kit.BasicServicesKit'; 7591 7592 class MyDeathRecipient implements rpc.DeathRecipient { 7593 onRemoteDied() { 7594 hilog.info(0x0000, 'testTag', 'server died'); 7595 } 7596 } 7597 let deathRecipient = new MyDeathRecipient(); 7598 if (proxy != undefined) { 7599 try { 7600 proxy.registerDeathRecipient(deathRecipient, 0); 7601 } catch (error) { 7602 let e: BusinessError = error as BusinessError; 7603 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7604 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7605 } 7606 } 7607 ``` 7608 7609### addDeathRecipient<sup>(deprecated)</sup> 7610 7611>从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9-1)类替代。 7612 7613addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7614 7615注册用于接收远程对象死亡通知的回调,增加proxy对象上的死亡通知。 7616 7617**系统能力**:SystemCapability.Communication.IPC.Core 7618 7619**参数:** 7620 7621 | 参数名 | 类型 | 必填 | 说明 | 7622 | --------- | --------------------------------- | ---- | --------------------------------- | 7623 | recipient | [DeathRecipient](#deathrecipient) | 是 | 收件人表示要注册的回调。 | 7624 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7625 7626**返回值:** 7627 7628 | 类型 | 说明 | 7629 | ------- | ---------------------------------------- | 7630 | boolean | true:回调注册成功,false:回调注册失败。| 7631 7632**示例:** 7633 7634 ```ts 7635 // FA模型需要从@kit.AbilityKit导入featureAbility 7636 // import { featureAbility } from '@kit.AbilityKit'; 7637 import { Want, common } from '@kit.AbilityKit'; 7638 import { hilog } from '@kit.PerformanceAnalysisKit'; 7639 7640 let proxy: rpc.IRemoteObject | undefined; 7641 let connect: common.ConnectOptions = { 7642 onConnect: (elementName, remoteProxy) => { 7643 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7644 proxy = remoteProxy; 7645 }, 7646 onDisconnect: (elementName) => { 7647 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7648 }, 7649 onFailed: () => { 7650 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7651 } 7652 }; 7653 let want: Want = { 7654 bundleName: "com.ohos.server", 7655 abilityName: "com.ohos.server.EntryAbility", 7656 }; 7657 7658 // FA模型使用此方法连接服务 7659 // FA.connectAbility(want,connect); 7660 7661 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7662 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7663 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7664 let connectionId = context.connectServiceExtensionAbility(want, connect); 7665 ``` 7666 7667 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecipient接口方法新增死亡回调 7668 7669 ```ts 7670 import { hilog } from '@kit.PerformanceAnalysisKit'; 7671 7672 class MyDeathRecipient implements rpc.DeathRecipient { 7673 onRemoteDied() { 7674 hilog.info(0x0000, 'testTag', 'server died'); 7675 } 7676 } 7677 let deathRecipient = new MyDeathRecipient(); 7678 if (proxy != undefined) { 7679 proxy.addDeathRecipient(deathRecipient, 0); 7680 } 7681 ``` 7682 7683### unregisterDeathRecipient<sup>9+</sup> 7684 7685unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7686 7687注销用于接收远程对象死亡通知的回调。 7688 7689**系统能力**:SystemCapability.Communication.IPC.Core 7690 7691**参数:** 7692 7693 | 参数名 | 类型 | 必填 | 说明 | 7694 | --------- | --------------------------------- | ---- | -------------- | 7695 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 7696 | flags | number | 是 | 死亡通知标志。 | 7697 7698**错误码:** 7699 7700以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7701 7702 | 错误码ID | 错误信息 | 7703 | -------- | -------- | 7704 | 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. | 7705 | 1900008 | The proxy or remote object is invalid. | 7706 7707**示例:** 7708 7709 ```ts 7710 // FA模型需要从@kit.AbilityKit导入featureAbility 7711 // import { featureAbility } from '@kit.AbilityKit'; 7712 import { Want, common } from '@kit.AbilityKit'; 7713 import { hilog } from '@kit.PerformanceAnalysisKit'; 7714 7715 let proxy: rpc.IRemoteObject | undefined; 7716 let connect: common.ConnectOptions = { 7717 onConnect: (elementName, remoteProxy) => { 7718 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7719 proxy = remoteProxy; 7720 }, 7721 onDisconnect: (elementName) => { 7722 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7723 }, 7724 onFailed: () => { 7725 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7726 } 7727 }; 7728 let want: Want = { 7729 bundleName: "com.ohos.server", 7730 abilityName: "com.ohos.server.EntryAbility", 7731 }; 7732 7733 // FA模型使用此方法连接服务 7734 // FA.connectAbility(want,connect); 7735 7736 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7737 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7738 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7739 let connectionId = context.connectServiceExtensionAbility(want, connect); 7740 ``` 7741 7742 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 7743 7744 ```ts 7745 import { hilog } from '@kit.PerformanceAnalysisKit'; 7746 import { BusinessError } from '@kit.BasicServicesKit'; 7747 7748 class MyDeathRecipient implements rpc.DeathRecipient { 7749 onRemoteDied() { 7750 hilog.info(0x0000, 'testTag', 'server died'); 7751 } 7752 } 7753 let deathRecipient = new MyDeathRecipient(); 7754 if (proxy != undefined) { 7755 try { 7756 proxy.registerDeathRecipient(deathRecipient, 0); 7757 proxy.unregisterDeathRecipient(deathRecipient, 0); 7758 } catch (error) { 7759 let e: BusinessError = error as BusinessError; 7760 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7761 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7762 } 7763 } 7764 ``` 7765 7766### removeDeathRecipient<sup>(deprecated)</sup> 7767 7768>从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9-1)替代。 7769 7770removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7771 7772注销用于接收远程对象死亡通知的回调。 7773 7774**系统能力**:SystemCapability.Communication.IPC.Core 7775 7776**参数:** 7777 7778 | 参数名 | 类型 | 必填 | 说明 | 7779 | --------- | --------------------------------- | ---- | --------------------------------- | 7780 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的死亡回调。 | 7781 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7782 7783**返回值:** 7784 7785 | 类型 | 说明 | 7786 | ------- | ---------------------------------------- | 7787 | boolean | true:回调注销成功,false:回调注销失败。| 7788 7789**示例:** 7790 7791 ```ts 7792 // FA模型需要从@kit.AbilityKit导入featureAbility 7793 // import { featureAbility } from '@kit.AbilityKit'; 7794 import { Want, common } from '@kit.AbilityKit'; 7795 import { hilog } from '@kit.PerformanceAnalysisKit'; 7796 7797 let proxy: rpc.IRemoteObject | undefined; 7798 let connect: common.ConnectOptions = { 7799 onConnect: (elementName, remoteProxy) => { 7800 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7801 proxy = remoteProxy; 7802 }, 7803 onDisconnect: (elementName) => { 7804 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7805 }, 7806 onFailed: () => { 7807 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7808 } 7809 }; 7810 let want: Want = { 7811 bundleName: "com.ohos.server", 7812 abilityName: "com.ohos.server.EntryAbility", 7813 }; 7814 7815 // FA模型使用此方法连接服务 7816 // FA.connectAbility(want,connect); 7817 7818 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7819 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7820 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7821 let connectionId = context.connectServiceExtensionAbility(want, connect); 7822 ``` 7823 7824 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调 7825 7826 ```ts 7827 import { hilog } from '@kit.PerformanceAnalysisKit'; 7828 7829 class MyDeathRecipient implements rpc.DeathRecipient { 7830 onRemoteDied() { 7831 hilog.info(0x0000, 'testTag', 'server died'); 7832 } 7833 } 7834 let deathRecipient = new MyDeathRecipient(); 7835 if (proxy != undefined) { 7836 proxy.addDeathRecipient(deathRecipient, 0); 7837 proxy.removeDeathRecipient(deathRecipient, 0); 7838 } 7839 ``` 7840 7841### getDescriptor<sup>9+</sup> 7842 7843getDescriptor(): string 7844 7845获取对象的接口描述符,接口描述符为字符串。 7846 7847**系统能力**:SystemCapability.Communication.IPC.Core 7848 7849**返回值:** 7850 7851 | 类型 | 说明 | 7852 | ------ | ---------------- | 7853 | string | 返回接口描述符。 | 7854 7855**错误码:** 7856 7857以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7858 7859 | 错误码ID | 错误信息 | 7860 | -------- | -------- | 7861 | 1900007 | communication failed. | 7862 | 1900008 | The proxy or remote object is invalid. | 7863 7864**示例:** 7865 7866 ```ts 7867 // FA模型需要从@kit.AbilityKit导入featureAbility 7868 // import { featureAbility } from '@kit.AbilityKit'; 7869 import { Want, common } from '@kit.AbilityKit'; 7870 import { hilog } from '@kit.PerformanceAnalysisKit'; 7871 7872 let proxy: rpc.IRemoteObject | undefined; 7873 let connect: common.ConnectOptions = { 7874 onConnect: (elementName, remoteProxy) => { 7875 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7876 proxy = remoteProxy; 7877 }, 7878 onDisconnect: (elementName) => { 7879 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7880 }, 7881 onFailed: () => { 7882 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7883 } 7884 }; 7885 let want: Want = { 7886 bundleName: "com.ohos.server", 7887 abilityName: "com.ohos.server.EntryAbility", 7888 }; 7889 7890 // FA模型使用此方法连接服务 7891 // FA.connectAbility(want,connect); 7892 7893 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7894 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7895 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7896 let connectionId = context.connectServiceExtensionAbility(want, connect); 7897 ``` 7898 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符 7899 7900 ```ts 7901 import { hilog } from '@kit.PerformanceAnalysisKit'; 7902 import { BusinessError } from '@kit.BasicServicesKit'; 7903 7904 if (proxy != undefined) { 7905 try { 7906 let descriptor: string = proxy.getDescriptor(); 7907 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7908 } catch (error) { 7909 let e: BusinessError = error as BusinessError; 7910 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7911 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7912 } 7913 } 7914 ``` 7915 7916### getInterfaceDescriptor<sup>(deprecated)</sup> 7917 7918>从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-1)替代。 7919 7920getInterfaceDescriptor(): string 7921 7922查询当前代理对象接口的描述符。 7923 7924**系统能力**:SystemCapability.Communication.IPC.Core 7925 7926**返回值:** 7927 7928 | 类型 | 说明 | 7929 | ------ | ------------------ | 7930 | string | 当前的接口描述符。 | 7931 7932**示例:** 7933 7934 ```ts 7935 // FA模型需要从@kit.AbilityKit导入featureAbility 7936 // import { featureAbility } from '@kit.AbilityKit'; 7937 import { Want, common } from '@kit.AbilityKit'; 7938 import { hilog } from '@kit.PerformanceAnalysisKit'; 7939 7940 let proxy: rpc.IRemoteObject | undefined; 7941 let connect: common.ConnectOptions = { 7942 onConnect: (elementName, remoteProxy) => { 7943 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7944 proxy = remoteProxy; 7945 }, 7946 onDisconnect: (elementName) => { 7947 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7948 }, 7949 onFailed: () => { 7950 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7951 } 7952 }; 7953 let want: Want = { 7954 bundleName: "com.ohos.server", 7955 abilityName: "com.ohos.server.EntryAbility", 7956 }; 7957 7958 // FA模型使用此方法连接服务 7959 // FA.connectAbility(want,connect); 7960 7961 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7962 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7963 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7964 let connectionId = context.connectServiceExtensionAbility(want, connect); 7965 ``` 7966 7967 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符 7968 7969 ```ts 7970 import { hilog } from '@kit.PerformanceAnalysisKit'; 7971 7972 if (proxy != undefined) { 7973 let descriptor: string = proxy.getInterfaceDescriptor(); 7974 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7975 } 7976 ``` 7977 7978### isObjectDead 7979 7980isObjectDead(): boolean 7981 7982指示对应的RemoteObject是否死亡。 7983 7984**系统能力**:SystemCapability.Communication.IPC.Core 7985 7986**返回值:** 7987 7988 | 类型 | 说明 | 7989 | ------- | ------------------------------------------------- | 7990 | boolean | true:对应的对象已经死亡,false:对应的对象未死亡。 | 7991 7992**示例:** 7993 7994 ```ts 7995 // FA模型需要从@kit.AbilityKit导入featureAbility 7996 // import { featureAbility } from '@kit.AbilityKit'; 7997 import { Want, common } from '@kit.AbilityKit'; 7998 import { hilog } from '@kit.PerformanceAnalysisKit'; 7999 8000 let proxy: rpc.IRemoteObject | undefined; 8001 let connect: common.ConnectOptions = { 8002 onConnect: (elementName, remoteProxy) => { 8003 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8004 proxy = remoteProxy; 8005 }, 8006 onDisconnect: (elementName) => { 8007 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8008 }, 8009 onFailed: () => { 8010 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8011 } 8012 }; 8013 let want: Want = { 8014 bundleName: "com.ohos.server", 8015 abilityName: "com.ohos.server.EntryAbility", 8016 }; 8017 8018 // FA模型使用此方法连接服务 8019 // FA.connectAbility(want,connect); 8020 8021 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8022 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 8023 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8024 let connectionId = context.connectServiceExtensionAbility(want, connect); 8025 ``` 8026 8027 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡 8028 8029 ```ts 8030 import { hilog } from '@kit.PerformanceAnalysisKit'; 8031 8032 if (proxy != undefined) { 8033 let isDead: boolean = proxy.isObjectDead(); 8034 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 8035 } 8036 ``` 8037 8038## MessageOption 8039 8040公共消息选项,使用指定的标志类型,构造指定的MessageOption对象。 8041 8042**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 8043 8044 | 名称 | 值 | 说明 | 8045 | ------------- | --------- | ----------------------------------------------------------- | 8046 | TF_SYNC | 0 (0x00) | 同步调用标识。 | 8047 | TF_ASYNC | 1 (0x01) | 异步调用标识。 | 8048 | TF_ACCEPT_FDS | 16 (0x10) | 指示sendMessageRequest<sup>9+</sup>接口可以返回文件描述符。 | 8049 | TF_WAIT_TIME | 8 (0x8) | RPC等待时间(单位/秒),不用于IPC的情况。 | 8050 8051### constructor<sup>9+</sup> 8052 8053constructor(async?: boolean) 8054 8055MessageOption构造函数。 8056 8057**系统能力**:SystemCapability.Communication.IPC.Core 8058 8059**参数:** 8060 8061| 参数名 | 类型 | 必填 | 说明 | 8062| ------ | ------- | ---- | ------------------------------------------------------------ | 8063| async | boolean | 否 | true:表示异步调用标志,false:表示同步调用标志。默认同步调用。 | 8064 8065**示例:** 8066 8067 ```ts 8068 class TestRemoteObject extends rpc.MessageOption { 8069 constructor(async: boolean) { 8070 super(async); 8071 } 8072 } 8073 ``` 8074 8075### constructor 8076 8077constructor(syncFlags?: number, waitTime?: number) 8078 8079MessageOption构造函数。 8080 8081**系统能力**:SystemCapability.Communication.IPC.Core 8082 8083**参数:** 8084 8085 | 参数名 | 类型 | 必填 | 说明 | 8086 | --------- | ------ | ---- | --------------------------------------------- | 8087 | syncFlags | number | 否 | 同步调用或异步调用标志。默认同步调用。 | 8088 | waitTime | number | 否 | 调用rpc最长等待时间。默认TF_WAIT_TIME。 | 8089 8090**示例:** 8091 8092 ```ts 8093 class TestRemoteObject extends rpc.MessageOption { 8094 constructor(syncFlags?: number,waitTime?: number) { 8095 super(syncFlags,waitTime); 8096 } 8097 } 8098 ``` 8099### isAsync<sup>9+</sup> 8100 8101isAsync(): boolean 8102 8103获取SendMessageRequest调用中确定同步或是异步的标志。 8104 8105**系统能力**:SystemCapability.Communication.IPC.Core 8106 8107**返回值:** 8108 8109 | 类型 | 说明 | 8110 | ------- | ---------------------------------------- | 8111 | boolean | true:异步调用成功,false:同步调用成功。| 8112 8113**示例:** 8114 8115 ```ts 8116 let option = new rpc.MessageOption(); 8117 option.isAsync(); 8118 ``` 8119 8120### setAsync<sup>9+</sup> 8121 8122setAsync(async: boolean): void 8123 8124设置SendMessageRequest调用中确定同步或是异步的标志。 8125 8126**系统能力**:SystemCapability.Communication.IPC.Core 8127 8128**参数:** 8129 8130| 参数名 | 类型 | 必填 | 说明 | 8131| ------ | ------- | ---- | ------------------------------------------------- | 8132| async | boolean | 是 | true:表示异步调用标志,false:表示同步调用标志。 | 8133 8134**示例:** 8135 8136 ```ts 8137 import { hilog } from '@kit.PerformanceAnalysisKit'; 8138 8139 let option = new rpc.MessageOption(); 8140 option.setAsync(true); 8141 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 8142 ``` 8143 8144### getFlags 8145 8146getFlags(): number 8147 8148获取同步调用或异步调用标志。 8149 8150**系统能力**:SystemCapability.Communication.IPC.Core 8151 8152**返回值:** 8153 8154 | 类型 | 说明 | 8155 | ------ | ------------------------------------ | 8156 | number | 调用成功返回同步调用或异步调用标志。 | 8157 8158**示例:** 8159 8160 ```ts 8161 import { hilog } from '@kit.PerformanceAnalysisKit'; 8162 8163 try { 8164 let option = new rpc.MessageOption(); 8165 hilog.info(0x0000, 'testTag', 'create object successfully'); 8166 let flog = option.getFlags(); 8167 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8168 option.setFlags(1) 8169 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8170 let flog2 = option.getFlags(); 8171 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 8172 } catch (error) { 8173 hilog.error(0x0000, 'testTag', 'error ' + error); 8174 } 8175 ``` 8176 8177### setFlags 8178 8179setFlags(flags: number): void 8180 8181设置同步调用或异步调用标志。 8182 8183**系统能力**:SystemCapability.Communication.IPC.Core 8184 8185**参数:** 8186 8187 | 参数名 | 类型 | 必填 | 说明 | 8188 | ------ | ------ | ---- | ------------------------ | 8189 | flags | number | 是 | 同步调用或异步调用标志。 | 8190 8191**示例:** 8192 8193 ```ts 8194 import { hilog } from '@kit.PerformanceAnalysisKit'; 8195 8196 try { 8197 let option = new rpc.MessageOption(); 8198 option.setFlags(1) 8199 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8200 let flog = option.getFlags(); 8201 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8202 } catch (error) { 8203 hilog.error(0x0000, 'testTag', 'error ' + error); 8204 } 8205 ``` 8206 8207### getWaitTime 8208 8209getWaitTime(): number 8210 8211获取rpc调用的最长等待时间。 8212 8213**系统能力**:SystemCapability.Communication.IPC.Core 8214 8215**返回值:** 8216 8217 | 类型 | 说明 | 8218 | ------ | ----------------- | 8219 | number | rpc最长等待时间。 | 8220 8221**示例:** 8222 8223 ```ts 8224 import { hilog } from '@kit.PerformanceAnalysisKit'; 8225 8226 try { 8227 let option = new rpc.MessageOption(); 8228 let time = option.getWaitTime(); 8229 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8230 option.setWaitTime(16); 8231 let time2 = option.getWaitTime(); 8232 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8233 } catch (error) { 8234 hilog.error(0x0000, 'testTag', 'error ' + error); 8235 } 8236 ``` 8237 8238### setWaitTime 8239 8240setWaitTime(waitTime: number): void 8241 8242设置rpc调用最长等待时间。 8243 8244**系统能力**:SystemCapability.Communication.IPC.Core 8245 8246**参数:** 8247 8248 | 参数名 | 类型 | 必填 | 说明 | 8249 | -------- | ------ | ---- | --------------------- | 8250 | waitTime | number | 是 | rpc调用最长等待时间,上限为3000秒。 | 8251 8252**示例:** 8253 8254 ```ts 8255 import { hilog } from '@kit.PerformanceAnalysisKit'; 8256 8257 try { 8258 let option = new rpc.MessageOption(); 8259 option.setWaitTime(16); 8260 let time = option.getWaitTime(); 8261 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8262 } catch (error) { 8263 hilog.error(0x0000, 'testTag', 'error ' + error); 8264 } 8265 ``` 8266 8267## IPCSkeleton 8268 8269用于获取IPC上下文信息,包括获取UID和PID、获取本端和对端设备ID、检查接口调用是否在同一设备上。 8270 8271### getContextObject 8272 8273static getContextObject(): IRemoteObject 8274 8275静态方法,获取系统能力的管理者。 8276 8277**系统能力**:SystemCapability.Communication.IPC.Core 8278 8279**返回值:** 8280 8281 | 类型 | 说明 | 8282 | ------------------------------- | -------------------- | 8283 | [IRemoteObject](#iremoteobject) | 返回系统能力管理者。 | 8284 8285**示例:** 8286 8287 ```ts 8288 import { hilog } from '@kit.PerformanceAnalysisKit'; 8289 8290 let samgr = rpc.IPCSkeleton.getContextObject(); 8291 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8292 ``` 8293 8294### getCallingPid 8295 8296static getCallingPid(): number 8297 8298静态方法,获取调用者的PID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的PID。 8299 8300**系统能力**:SystemCapability.Communication.IPC.Core 8301 8302**返回值:** 8303 8304 | 类型 | 说明 | 8305 | ------ | ----------------- | 8306 | number | 返回调用者的PID。 | 8307 8308**示例:** 8309 8310 ```ts 8311 import { hilog } from '@kit.PerformanceAnalysisKit'; 8312 8313 class Stub extends rpc.RemoteObject { 8314 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8315 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8316 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8317 return true; 8318 } 8319 } 8320 ``` 8321 8322### getCallingUid 8323 8324static getCallingUid(): number 8325 8326静态方法,获取调用者的UID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的UID。 8327 8328**系统能力**:SystemCapability.Communication.IPC.Core 8329 8330**返回值:** 8331 8332 | 类型 | 说明 | 8333 | ------ | ----------------- | 8334 | number | 返回调用者的UID。 | 8335 8336**示例:** 8337 8338 ```ts 8339 import { hilog } from '@kit.PerformanceAnalysisKit'; 8340 8341 class Stub extends rpc.RemoteObject { 8342 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8343 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8344 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8345 return true; 8346 } 8347 } 8348 ``` 8349 8350### getCallingTokenId<sup>8+</sup> 8351 8352static getCallingTokenId(): number 8353 8354静态方法,获取调用者的TokenId,用于被调用方对调用方的身份校验。 8355 8356**系统能力**:SystemCapability.Communication.IPC.Core 8357 8358**返回值:** 8359 8360 | 类型 | 说明 | 8361 | ------ | --------------------- | 8362 | number | 返回调用者的TokenId。 | 8363 8364**示例:** 8365 8366 ```ts 8367 import { hilog } from '@kit.PerformanceAnalysisKit'; 8368 8369 class Stub extends rpc.RemoteObject { 8370 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8371 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8372 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8373 return true; 8374 } 8375 } 8376 ``` 8377 8378### getCallingDeviceID 8379 8380static getCallingDeviceID(): string 8381 8382静态方法,获取调用者进程所在的设备ID。 8383 8384**系统能力**:SystemCapability.Communication.IPC.Core 8385 8386**返回值:** 8387 8388 | 类型 | 说明 | 8389 | ------ | ---------------------------- | 8390 | string | 返回调用者进程所在的设备ID。 | 8391 8392**示例:** 8393 8394 ```ts 8395 import { hilog } from '@kit.PerformanceAnalysisKit'; 8396 8397 class Stub extends rpc.RemoteObject { 8398 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8399 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8400 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8401 return true; 8402 } 8403 } 8404 ``` 8405 8406### getLocalDeviceID 8407 8408static getLocalDeviceID(): string 8409 8410静态方法,获取本端设备ID。 8411 8412**系统能力**:SystemCapability.Communication.IPC.Core 8413 8414**返回值:** 8415 8416 | 类型 | 说明 | 8417 | ------ | ------------------ | 8418 | string | 返回本地设备的ID。 | 8419 8420**示例:** 8421 8422 ```ts 8423 import { hilog } from '@kit.PerformanceAnalysisKit'; 8424 8425 class Stub extends rpc.RemoteObject { 8426 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8427 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8428 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8429 return true; 8430 } 8431 } 8432 ``` 8433 8434### isLocalCalling 8435 8436static isLocalCalling(): boolean 8437 8438静态方法,检查当前通信对端是否是本设备的进程。 8439 8440**系统能力**:SystemCapability.Communication.IPC.Core 8441 8442**返回值:** 8443 8444 | 类型 | 说明 | 8445 | ------- | -------------------------------------------------- | 8446 | boolean | true:调用在同一台设备,false:调用未在同一台设备。| 8447 8448**示例:** 8449 8450 ```ts 8451 import { hilog } from '@kit.PerformanceAnalysisKit'; 8452 8453 class Stub extends rpc.RemoteObject { 8454 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8455 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8456 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8457 return true; 8458 } 8459 } 8460 ``` 8461 8462### flushCmdBuffer<sup>9+</sup> 8463 8464static flushCmdBuffer(object: IRemoteObject): void 8465 8466静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8467 8468**系统能力**:SystemCapability.Communication.IPC.Core 8469 8470**参数:** 8471 8472 | 参数名 | 类型 | 必填 | 说明 | 8473 | ------ | ------------------------------- | ---- | ------------------- | 8474 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8475 8476**错误码:** 8477 8478以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8479 8480 | 错误码ID | 错误信息 | 8481 | -------- | -------- | 8482 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 8483 8484**示例:** 8485 8486 ```ts 8487 import { hilog } from '@kit.PerformanceAnalysisKit'; 8488 import { BusinessError } from '@kit.BasicServicesKit'; 8489 8490 class TestRemoteObject extends rpc.RemoteObject { 8491 constructor(descriptor: string) { 8492 super(descriptor); 8493 } 8494 } 8495 let remoteObject = new TestRemoteObject("aaa"); 8496 try { 8497 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8498 } catch (error) { 8499 let e: BusinessError = error as BusinessError; 8500 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8501 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8502 } 8503 ``` 8504 8505### flushCommands<sup>(deprecated)</sup> 8506 8507>从API version 9 开始废弃,建议使用[flushCmdBuffer](#flushcmdbuffer9)替代。 8508 8509static flushCommands(object: IRemoteObject): number 8510 8511静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8512 8513**系统能力**:SystemCapability.Communication.IPC.Core 8514 8515**参数:** 8516 8517 | 参数名 | 类型 | 必填 | 说明 | 8518 | ------ | ------------------------------- | ---- | ------------------- | 8519 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8520 8521**返回值:** 8522 8523 | 类型 | 说明 | 8524 | ------ | --------------------------------------------------------------------------------- | 8525 | number | 如果操作成功,返回0;如果输入对象为空或RemoteObject,或者操作失败,返回错误代码。 | 8526 8527**示例:** 8528 8529 ```ts 8530 import { hilog } from '@kit.PerformanceAnalysisKit'; 8531 8532 class MyDeathRecipient implements rpc.DeathRecipient { 8533 onRemoteDied() { 8534 hilog.info(0x0000, 'testTag', 'server died'); 8535 } 8536 } 8537 class TestRemoteObject extends rpc.RemoteObject { 8538 constructor(descriptor: string) { 8539 super(descriptor); 8540 } 8541 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8542 return true; 8543 } 8544 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8545 return true; 8546 } 8547 isObjectDead(): boolean { 8548 return false; 8549 } 8550 } 8551 let remoteObject = new TestRemoteObject("aaa"); 8552 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8553 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8554 ``` 8555 8556### resetCallingIdentity 8557 8558static resetCallingIdentity(): string 8559 8560静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。 8561 8562**系统能力**:SystemCapability.Communication.IPC.Core 8563 8564**返回值:** 8565 8566 | 类型 | 说明 | 8567 | ------ | ------------------------------------ | 8568 | string | 返回包含远程用户的UID和PID的字符串。 | 8569 8570**示例:** 8571 8572 ```ts 8573 import { hilog } from '@kit.PerformanceAnalysisKit'; 8574 8575 class Stub extends rpc.RemoteObject { 8576 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8577 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8578 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8579 return true; 8580 } 8581 } 8582 ``` 8583 8584### restoreCallingIdentity<sup>9+</sup> 8585 8586static restoreCallingIdentity(identity: string): void 8587 8588静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8589 8590**系统能力**:SystemCapability.Communication.IPC.Core 8591 8592**参数:** 8593 8594 | 参数名 | 类型 | 必填 | 说明 | 8595 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8596 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8597 8598**错误码:** 8599 8600以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8601 8602 | 错误码ID | 错误信息 | 8603 | -------- | -------- | 8604 | 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. | 8605 8606**示例:** 8607 8608 ```ts 8609 import { hilog } from '@kit.PerformanceAnalysisKit'; 8610 8611 class Stub extends rpc.RemoteObject { 8612 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8613 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8614 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8615 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8616 return true; 8617 } 8618 } 8619 ``` 8620 8621### setCallingIdentity<sup>(deprecated)</sup> 8622 8623>从API version 9 开始废弃,建议使用[restoreCallingIdentity](#restorecallingidentity9)替代。 8624 8625static setCallingIdentity(identity: string): boolean 8626 8627静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8628 8629**系统能力**:SystemCapability.Communication.IPC.Core 8630 8631**参数:** 8632 8633 | 参数名 | 类型 | 必填 | 说明 | 8634 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8635 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8636 8637**返回值:** 8638 8639 | 类型 | 说明 | 8640 | ------- | ---------------------------------| 8641 | boolean | true:设置成功,false:设置失败。| 8642 8643**示例:** 8644 8645 ```ts 8646 import { hilog } from '@kit.PerformanceAnalysisKit'; 8647 8648 class Stub extends rpc.RemoteObject { 8649 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8650 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8651 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8652 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8653 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8654 return true; 8655 } 8656 } 8657 ``` 8658 8659## RemoteObject 8660 8661实现远程对象。服务提供者必须继承此类。 8662 8663### constructor 8664 8665constructor(descriptor: string) 8666 8667RemoteObject构造函数。 8668 8669**系统能力**:SystemCapability.Communication.IPC.Core 8670 8671**参数:** 8672 8673 | 参数名 | 类型 | 必填 | 说明 | 8674 | ---------- | ------ | ---- | ------------ | 8675 | descriptor | string | 是 | 接口描述符。 | 8676 8677### sendRequest<sup>(deprecated)</sup> 8678 8679>从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 8680 8681sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8682 8683以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8684 8685**系统能力**:SystemCapability.Communication.IPC.Core 8686 8687**参数:** 8688 8689 | 参数名 | 类型 | 必填 | 说明 | 8690 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8691 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8692 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8693 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8694 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8695 8696**返回值:** 8697 8698 | 类型 | 说明 | 8699 | ------- | -------------------------------- | 8700 | boolean | true:发送成功,false:发送失败。| 8701 8702**示例:** 8703 8704 ```ts 8705 import { hilog } from '@kit.PerformanceAnalysisKit'; 8706 8707 class MyDeathRecipient implements rpc.DeathRecipient { 8708 onRemoteDied() { 8709 hilog.info(0x0000, 'testTag', 'server died'); 8710 } 8711 } 8712 class TestRemoteObject extends rpc.RemoteObject { 8713 constructor(descriptor: string) { 8714 super(descriptor); 8715 } 8716 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8717 return true; 8718 } 8719 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8720 return true; 8721 } 8722 isObjectDead(): boolean { 8723 return false; 8724 } 8725 } 8726 let testRemoteObject = new TestRemoteObject("testObject"); 8727 let option = new rpc.MessageOption(); 8728 let data = rpc.MessageParcel.create(); 8729 let reply = rpc.MessageParcel.create(); 8730 data.writeInt(1); 8731 data.writeString("hello"); 8732 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8733 if (ret) { 8734 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8735 let msg = reply.readString(); 8736 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8737 } else { 8738 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8739 } 8740 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8741 data.reclaim(); 8742 reply.reclaim(); 8743 ``` 8744 8745### sendMessageRequest<sup>9+</sup> 8746 8747sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8748 8749以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 8750 8751**系统能力**:SystemCapability.Communication.IPC.Core 8752 8753**参数:** 8754 8755 | 参数名 | 类型 | 必填 | 说明 | 8756 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8757 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8758 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 8759 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8760 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8761 8762**返回值:** 8763 8764| 类型 | 说明 | 8765| ----------------------------------------------- | ----------------------------------------- | 8766| Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是RequestResult实例。 | 8767 8768 8769**错误码:** 8770 8771以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8772 8773 | 错误码ID | 错误信息 | 8774 | -------- | -------- | 8775 | 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. | 8776 8777**示例:** 8778 8779 ```ts 8780 import { hilog } from '@kit.PerformanceAnalysisKit'; 8781 8782 class TestRemoteObject extends rpc.RemoteObject { 8783 constructor(descriptor: string) { 8784 super(descriptor); 8785 } 8786 } 8787 let testRemoteObject = new TestRemoteObject("testObject"); 8788 let option = new rpc.MessageOption(); 8789 let data = rpc.MessageSequence.create(); 8790 let reply = rpc.MessageSequence.create(); 8791 data.writeInt(1); 8792 data.writeString("hello"); 8793 testRemoteObject.sendMessageRequest(1, data, reply, option) 8794 .then((result: rpc.RequestResult) => { 8795 if (result.errCode === 0) { 8796 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8797 let num = result.reply.readInt(); 8798 let msg = result.reply.readString(); 8799 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8800 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8801 } else { 8802 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8803 } 8804 }).catch((e: Error) => { 8805 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8806 }).finally (() => { 8807 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8808 data.reclaim(); 8809 reply.reclaim(); 8810 }); 8811 ``` 8812 8813### sendRequest<sup>(deprecated)</sup> 8814 8815>从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 8816 8817sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8818 8819以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8820 8821**系统能力**:SystemCapability.Communication.IPC.Core 8822 8823**参数:** 8824 8825 | 参数名 | 类型 | 必填 | 说明 | 8826 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8827 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8828 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8829 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8830 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8831 8832**返回值:** 8833 8834| 类型 | 说明 | 8835| ------------------------------------------------------------ | --------------------------------------------- | 8836| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 8837 8838**示例:** 8839 8840 ```ts 8841 import { hilog } from '@kit.PerformanceAnalysisKit'; 8842 8843 class MyDeathRecipient implements rpc.DeathRecipient { 8844 onRemoteDied() { 8845 hilog.info(0x0000, 'testTag', 'server died'); 8846 } 8847 } 8848 class TestRemoteObject extends rpc.RemoteObject { 8849 constructor(descriptor: string) { 8850 super(descriptor); 8851 } 8852 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8853 return true; 8854 } 8855 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8856 return true; 8857 } 8858 isObjectDead(): boolean { 8859 return false; 8860 } 8861 } 8862 let testRemoteObject = new TestRemoteObject("testObject"); 8863 let option = new rpc.MessageOption(); 8864 let data = rpc.MessageParcel.create(); 8865 let reply = rpc.MessageParcel.create(); 8866 data.writeInt(1); 8867 data.writeString("hello"); 8868 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8869 let b = a as Promise<rpc.SendRequestResult>; 8870 b.then((result: rpc.SendRequestResult) => { 8871 if (result.errCode === 0) { 8872 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8873 let num = result.reply.readInt(); 8874 let msg = result.reply.readString(); 8875 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8876 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8877 } else { 8878 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8879 } 8880 }).catch((e: Error) => { 8881 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 8882 }).finally (() => { 8883 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8884 data.reclaim(); 8885 reply.reclaim(); 8886 }); 8887 ``` 8888 8889### sendMessageRequest<sup>9+</sup> 8890 8891sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8892 8893以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回时收到回调,回复内容在reply报文里。 8894 8895**系统能力**:SystemCapability.Communication.IPC.Core 8896 8897**参数:** 8898 8899| 参数名 | 类型 | 必填 | 说明 | 8900| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8901| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8902| data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 8903| reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8904| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8905| callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 8906 8907 8908**错误码:** 8909 8910以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8911 8912 | 错误码ID | 错误信息 | 8913 | -------- | -------- | 8914 | 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. | 8915 8916**示例:** 8917 8918 ```ts 8919 import { hilog } from '@kit.PerformanceAnalysisKit'; 8920 import { BusinessError } from '@kit.BasicServicesKit'; 8921 8922 class TestRemoteObject extends rpc.RemoteObject { 8923 constructor(descriptor: string) { 8924 super(descriptor); 8925 } 8926 } 8927 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 8928 if (result.errCode === 0) { 8929 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8930 let num = result.reply.readInt(); 8931 let msg = result.reply.readString(); 8932 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8933 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8934 } else { 8935 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8936 } 8937 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8938 result.data.reclaim(); 8939 result.reply.reclaim(); 8940 } 8941 let testRemoteObject = new TestRemoteObject("testObject"); 8942 let option = new rpc.MessageOption(); 8943 let data = rpc.MessageSequence.create(); 8944 let reply = rpc.MessageSequence.create(); 8945 data.writeInt(1); 8946 data.writeString("hello"); 8947 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 8948 ``` 8949 8950### sendRequest<sup>(deprecated)</sup> 8951 8952>从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-5)替代。 8953 8954sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8955 8956以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 8957 8958**系统能力**:SystemCapability.Communication.IPC.Core 8959 8960**参数:** 8961 8962| 参数名 | 类型 | 必填 | 说明 | 8963| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8964| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8965| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8966| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8967| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8968| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 8969 8970**示例:** 8971 8972 ```ts 8973 import { hilog } from '@kit.PerformanceAnalysisKit'; 8974 import { BusinessError } from '@kit.BasicServicesKit'; 8975 8976 class MyDeathRecipient implements rpc.DeathRecipient { 8977 onRemoteDied() { 8978 hilog.info(0x0000, 'testTag', 'server died'); 8979 } 8980 } 8981 class TestRemoteObject extends rpc.RemoteObject { 8982 constructor(descriptor: string) { 8983 super(descriptor); 8984 } 8985 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8986 return true; 8987 } 8988 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8989 return true; 8990 } 8991 isObjectDead(): boolean { 8992 return false; 8993 } 8994 } 8995 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 8996 if (result.errCode === 0) { 8997 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8998 let num = result.reply.readInt(); 8999 let msg = result.reply.readString(); 9000 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 9001 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 9002 } else { 9003 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 9004 } 9005 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 9006 result.data.reclaim(); 9007 result.reply.reclaim(); 9008 } 9009 let testRemoteObject = new TestRemoteObject("testObject"); 9010 let option = new rpc.MessageOption(); 9011 let data = rpc.MessageParcel.create(); 9012 let reply = rpc.MessageParcel.create(); 9013 data.writeInt(1); 9014 data.writeString("hello"); 9015 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 9016 ``` 9017 9018### onRemoteMessageRequest<sup>9+</sup> 9019 9020onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 9021 9022> **说明:** 9023> 9024>* 开发者应优先选择重载onRemoteMessageRequest方法,其中可以自由实现同步和异步的消息处理。 9025>* 开发者同时重载onRemoteRequest和onRemoteMessageRequest方法时,仅onRemoteMessageRequest方法生效。 9026 9027sendMessageRequest请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。 9028 9029**系统能力**:SystemCapability.Communication.IPC.Core 9030 9031**参数:** 9032 9033 | 参数名 | 类型 | 必填 | 说明 | 9034 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 9035 | code | number | 是 | 对端发送的服务请求码。 | 9036 | data | [MessageSequence](#messagesequence9) | 是 | 携带客户端调用参数的MessageSequence对象。 | 9037 | reply | [MessageSequence](#messagesequence9) | 是 | 写入结果的MessageSequence对象。 | 9038 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9039 9040**返回值:** 9041 9042 | 类型 | 说明 | 9043 | ----------------- | ----------------------------------------------------------------------------------------------- | 9044 | boolean | 若在onRemoteMessageRequest中同步地处理请求,则返回一个布尔值:true:操作成功,false:操作失败。 | 9045 | Promise\<boolean> | 若在onRemoteMessageRequest中异步地处理请求,则返回一个Promise对象。 | 9046 9047**重载onRemoteMessageRequest方法同步处理请求示例:** 9048 9049 ```ts 9050 import { hilog } from '@kit.PerformanceAnalysisKit'; 9051 9052 class TestRemoteObject extends rpc.RemoteObject { 9053 constructor(descriptor: string) { 9054 super(descriptor); 9055 } 9056 9057 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9058 if (code === 1) { 9059 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9060 return true; 9061 } else { 9062 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9063 return false; 9064 } 9065 } 9066 } 9067 ``` 9068 9069 **重载onRemoteMessageRequest方法异步处理请求示例:** 9070 9071 ```ts 9072 import { hilog } from '@kit.PerformanceAnalysisKit'; 9073 9074 class TestRemoteObject extends rpc.RemoteObject { 9075 constructor(descriptor: string) { 9076 super(descriptor); 9077 } 9078 9079 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9080 if (code === 1) { 9081 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9082 } else { 9083 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9084 return false; 9085 } 9086 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9087 setTimeout(resolve, 100); 9088 }) 9089 return true; 9090 } 9091 } 9092 ``` 9093 9094**同时重载onRemoteMessageRequest和onRemoteRequest方法同步处理请求示例:** 9095 9096 ```ts 9097 import { hilog } from '@kit.PerformanceAnalysisKit'; 9098 9099 class TestRemoteObject extends rpc.RemoteObject { 9100 constructor(descriptor: string) { 9101 super(descriptor); 9102 } 9103 9104 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9105 if (code === 1) { 9106 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9107 return true; 9108 } else { 9109 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9110 return false; 9111 } 9112 } 9113 // 同时调用仅会执行onRemoteMessageRequest 9114 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9115 if (code === 1) { 9116 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9117 } else { 9118 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9119 return false; 9120 } 9121 return true; 9122 } 9123 } 9124 ``` 9125 9126 **同时重载onRemoteMessageRequest和onRemoteRequest方法异步处理请求示例:** 9127 9128 ```ts 9129 import { hilog } from '@kit.PerformanceAnalysisKit'; 9130 class TestRemoteObject extends rpc.RemoteObject { 9131 constructor(descriptor: string) { 9132 super(descriptor); 9133 } 9134 9135 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9136 if (code === 1) { 9137 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9138 return true; 9139 } else { 9140 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9141 return false; 9142 } 9143 } 9144 // 同时调用仅会执行onRemoteMessageRequest 9145 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9146 if (code === 1) { 9147 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9148 } else { 9149 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9150 return false; 9151 } 9152 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9153 setTimeout(resolve, 100); 9154 }) 9155 return true; 9156 } 9157 } 9158 ``` 9159 9160### onRemoteRequest<sup>(deprecated)</sup> 9161 9162>从API version 9 开始废弃,建议使用[onRemoteMessageRequest](#onremotemessagerequest9)替代。 9163 9164onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9165 9166sendRequest请求的响应处理函数,服务端在该函数里处理请求,回复结果。 9167 9168**系统能力**:SystemCapability.Communication.IPC.Core 9169 9170**参数:** 9171 9172 | 参数名 | 类型 | 必填 | 说明 | 9173 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 9174 | code | number | 是 | 对端发送的服务请求码。 | 9175 | data | [MessageParcel](#messageparceldeprecated) | 是 | 携带客户端调用参数的MessageParcel对象。 | 9176 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 写入结果的MessageParcel对象。 | 9177 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9178 9179**返回值:** 9180 9181 | 类型 | 说明 | 9182 | ------- | -------------------------------- | 9183 | boolean | true:操作成功,false:操作失败。| 9184 9185**示例:** 9186 9187 ```ts 9188 import { hilog } from '@kit.PerformanceAnalysisKit'; 9189 9190 class MyDeathRecipient implements rpc.DeathRecipient { 9191 onRemoteDied() { 9192 hilog.info(0x0000, 'testTag', 'server died'); 9193 } 9194 } 9195 class TestRemoteObject extends rpc.RemoteObject { 9196 constructor(descriptor: string) { 9197 super(descriptor); 9198 } 9199 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9200 return true; 9201 } 9202 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9203 return true; 9204 } 9205 isObjectDead(): boolean { 9206 return false; 9207 } 9208 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9209 if (code === 1) { 9210 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9211 return true; 9212 } else { 9213 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9214 return false; 9215 } 9216 } 9217 } 9218 ``` 9219 9220### getCallingUid 9221 9222getCallingUid(): number 9223 9224获取通信对端的进程Uid。 9225 9226**系统能力**:SystemCapability.Communication.IPC.Core 9227 9228**返回值:** 9229 | 类型 | 说明 | 9230 | ------ | ----------------------- | 9231 | number | 返回通信对端的进程Uid。 | 9232 9233**示例:** 9234 9235 ```ts 9236 import { hilog } from '@kit.PerformanceAnalysisKit'; 9237 9238 class TestRemoteObject extends rpc.RemoteObject { 9239 constructor(descriptor: string) { 9240 super(descriptor); 9241 } 9242 } 9243 let testRemoteObject = new TestRemoteObject("testObject"); 9244 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9245 ``` 9246 9247### getCallingPid 9248 9249getCallingPid(): number 9250 9251获取通信对端的进程Pid。 9252 9253**系统能力**:SystemCapability.Communication.IPC.Core 9254 9255**返回值:** 9256 9257 | 类型 | 说明 | 9258 | ------ | ----------------------- | 9259 | number | 返回通信对端的进程Pid。 | 9260 9261**示例:** 9262 9263 ```ts 9264 import { hilog } from '@kit.PerformanceAnalysisKit'; 9265 9266 class TestRemoteObject extends rpc.RemoteObject { 9267 constructor(descriptor: string) { 9268 super(descriptor); 9269 } 9270 } 9271 let testRemoteObject = new TestRemoteObject("testObject"); 9272 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9273 ``` 9274 9275### getLocalInterface<sup>9+</sup> 9276 9277getLocalInterface(descriptor: string): IRemoteBroker 9278 9279查询接口描述符的字符串。 9280 9281**系统能力**:SystemCapability.Communication.IPC.Core 9282 9283**参数:** 9284 9285 | 参数名 | 类型 | 必填 | 说明 | 9286 | ---------- | ------ | ---- | -------------------- | 9287 | descriptor | string | 是 | 接口描述符的字符串。 | 9288 9289**返回值:** 9290 9291 | 类型 | 说明 | 9292 | ------------- | --------------------------------------------- | 9293 | IRemoteBroker | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 9294 9295**错误码:** 9296 9297以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9298 9299 | 错误码ID | 错误信息 | 9300 | -------- | -------- | 9301 | 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. | 9302 9303**示例:** 9304 9305 ```ts 9306 import { hilog } from '@kit.PerformanceAnalysisKit'; 9307 import { BusinessError } from '@kit.BasicServicesKit'; 9308 9309 class MyDeathRecipient implements rpc.DeathRecipient { 9310 onRemoteDied() { 9311 hilog.info(0x0000, 'testTag', 'server died'); 9312 } 9313 } 9314 class TestRemoteObject extends rpc.RemoteObject { 9315 constructor(descriptor: string) { 9316 super(descriptor); 9317 this.modifyLocalInterface(this, descriptor); 9318 } 9319 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9320 // 方法逻辑需开发者根据业务需要实现 9321 } 9322 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9323 // 方法逻辑需开发者根据业务需要实现 9324 } 9325 isObjectDead(): boolean { 9326 return false; 9327 } 9328 asObject(): rpc.IRemoteObject { 9329 return this; 9330 } 9331 } 9332 let testRemoteObject = new TestRemoteObject("testObject"); 9333 try { 9334 testRemoteObject.getLocalInterface("testObject"); 9335 } catch (error) { 9336 let e: BusinessError = error as BusinessError; 9337 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9338 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9339 } 9340 ``` 9341 9342### queryLocalInterface<sup>(deprecated)</sup> 9343 9344>从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-2)替代。 9345 9346queryLocalInterface(descriptor: string): IRemoteBroker 9347 9348查询并获取当前接口描述符对应的远端对象是否已经存在。 9349 9350**系统能力**:SystemCapability.Communication.IPC.Core 9351 9352**参数:** 9353 9354 | 参数名 | 类型 | 必填 | 说明 | 9355 | ---------- | ------ | ---- | ---------------------- | 9356 | descriptor | string | 是 | 需要查询的接口描述符。 | 9357 9358**返回值:** 9359 9360 | 类型 | 说明 | 9361 | ------------- | ------------------------------------------------------------------ | 9362 | IRemoteBroker | 如果接口描述符对应的远端对象存在,则返回该远端对象,否则返回Null。 | 9363 9364**示例:** 9365 9366 ```ts 9367 import { hilog } from '@kit.PerformanceAnalysisKit'; 9368 9369 class MyDeathRecipient implements rpc.DeathRecipient { 9370 onRemoteDied() { 9371 hilog.info(0x0000, 'testTag', 'server died'); 9372 } 9373 } 9374 class TestRemoteObject extends rpc.RemoteObject { 9375 constructor(descriptor: string) { 9376 super(descriptor); 9377 this.attachLocalInterface(this, descriptor); 9378 } 9379 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9380 return true; 9381 } 9382 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9383 return true; 9384 } 9385 isObjectDead(): boolean { 9386 return false; 9387 } 9388 asObject(): rpc.IRemoteObject { 9389 return this; 9390 } 9391 } 9392 let testRemoteObject = new TestRemoteObject("testObject"); 9393 testRemoteObject.queryLocalInterface("testObject"); 9394 ``` 9395 9396### getDescriptor<sup>9+</sup> 9397 9398getDescriptor(): string 9399 9400获取对象的接口描述符。接口描述符为字符串。 9401 9402**系统能力**:SystemCapability.Communication.IPC.Core 9403 9404**返回值:** 9405 9406 | 类型 | 说明 | 9407 | ------ | ---------------- | 9408 | string | 返回接口描述符。 | 9409 9410**错误码:** 9411 9412以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9413 9414 | 错误码ID | 错误信息 | 9415 | -------- | -------- | 9416 | 1900008 | The proxy or remote object is invalid. | 9417 9418**示例:** 9419 9420 ```ts 9421 import { hilog } from '@kit.PerformanceAnalysisKit'; 9422 import { BusinessError } from '@kit.BasicServicesKit'; 9423 9424 class MyDeathRecipient implements rpc.DeathRecipient { 9425 onRemoteDied() { 9426 hilog.info(0x0000, 'testTag', 'server died'); 9427 } 9428 } 9429 class TestRemoteObject extends rpc.RemoteObject { 9430 constructor(descriptor: string) { 9431 super(descriptor); 9432 } 9433 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9434 // 方法逻辑需开发者根据业务需要实现 9435 } 9436 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9437 // 方法逻辑需开发者根据业务需要实现 9438 } 9439 isObjectDead(): boolean { 9440 return false; 9441 } 9442 } 9443 let testRemoteObject = new TestRemoteObject("testObject"); 9444 try { 9445 let descriptor = testRemoteObject.getDescriptor(); 9446 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9447 } catch (error) { 9448 let e: BusinessError = error as BusinessError; 9449 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9450 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9451 } 9452 ``` 9453 9454### getInterfaceDescriptor<sup>(deprecated)</sup> 9455 9456>从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-2)替代。 9457 9458getInterfaceDescriptor(): string 9459 9460查询接口描述符。 9461 9462**系统能力**:SystemCapability.Communication.IPC.Core 9463 9464**返回值:** 9465 9466 | 类型 | 说明 | 9467 | ------ | ---------------- | 9468 | string | 返回接口描述符。 | 9469 9470**示例:** 9471 9472 ```ts 9473 import { hilog } from '@kit.PerformanceAnalysisKit'; 9474 9475 class MyDeathRecipient implements rpc.DeathRecipient { 9476 onRemoteDied() { 9477 hilog.info(0x0000, 'testTag', 'server died'); 9478 } 9479 } 9480 class TestRemoteObject extends rpc.RemoteObject { 9481 constructor(descriptor: string) { 9482 super(descriptor); 9483 } 9484 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9485 return true; 9486 } 9487 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9488 return true; 9489 } 9490 isObjectDead(): boolean { 9491 return false; 9492 } 9493 } 9494 let testRemoteObject = new TestRemoteObject("testObject"); 9495 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9496 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9497 ``` 9498 9499### modifyLocalInterface<sup>9+</sup> 9500 9501modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9502 9503此接口用于把接口描述符和IRemoteBroker对象绑定。 9504 9505**系统能力**:SystemCapability.Communication.IPC.Core 9506 9507**参数:** 9508 9509| 参数名 | 类型 | 必填 | 说明 | 9510| -------------- | ------------------------------- | ---- | ------------------------------------- | 9511| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9512| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9513 9514**错误码:** 9515 9516以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9517 9518 | 错误码ID | 错误信息 | 9519 | -------- | -------- | 9520 | 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. | 9521 9522**示例:** 9523 9524 ```ts 9525 import { hilog } from '@kit.PerformanceAnalysisKit'; 9526 import { BusinessError } from '@kit.BasicServicesKit'; 9527 9528 class MyDeathRecipient implements rpc.DeathRecipient { 9529 onRemoteDied() { 9530 hilog.info(0x0000, 'testTag', 'server died'); 9531 } 9532 } 9533 class TestRemoteObject extends rpc.RemoteObject { 9534 constructor(descriptor: string) { 9535 super(descriptor); 9536 try { 9537 this.modifyLocalInterface(this, descriptor); 9538 } catch (error) { 9539 let e: BusinessError = error as BusinessError; 9540 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9541 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9542 } 9543 } 9544 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9545 // 方法逻辑需开发者根据业务需要实现 9546 } 9547 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9548 // 方法逻辑需开发者根据业务需要实现 9549 } 9550 isObjectDead(): boolean { 9551 return false; 9552 } 9553 asObject(): rpc.IRemoteObject { 9554 return this; 9555 } 9556 } 9557 let testRemoteObject = new TestRemoteObject("testObject"); 9558 ``` 9559 9560### attachLocalInterface<sup>(deprecated)</sup> 9561 9562>从API version 9 开始废弃,建议使用[modifyLocalInterface](#modifylocalinterface9)替代。 9563 9564attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9565 9566此接口用于把接口描述符和IRemoteBroker对象绑定。 9567 9568**系统能力**:SystemCapability.Communication.IPC.Core 9569 9570**参数:** 9571 9572| 参数名 | 类型 | 必填 | 说明 | 9573| -------------- | ------------------------------- | ---- | ------------------------------------- | 9574| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9575| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9576 9577**示例:** 9578 9579 ```ts 9580 import { hilog } from '@kit.PerformanceAnalysisKit'; 9581 9582 class MyDeathRecipient implements rpc.DeathRecipient { 9583 onRemoteDied() { 9584 hilog.info(0x0000, 'testTag', 'server died'); 9585 } 9586 } 9587 class TestRemoteObject extends rpc.RemoteObject { 9588 constructor(descriptor: string) { 9589 super(descriptor); 9590 this.attachLocalInterface(this, descriptor); 9591 } 9592 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9593 return true; 9594 } 9595 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9596 return true; 9597 } 9598 isObjectDead(): boolean { 9599 return false; 9600 } 9601 asObject(): rpc.IRemoteObject { 9602 return this; 9603 } 9604 } 9605 let testRemoteObject = new TestRemoteObject("testObject"); 9606 ``` 9607 9608## Ashmem<sup>8+</sup> 9609 9610提供与匿名共享内存对象相关的方法,包括创建、关闭、映射和取消映射Ashmem、从Ashmem读取数据和写入数据、获取Ashmem大小、设置Ashmem保护。 9611 9612**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 9613 9614映射内存保护类型: 9615 9616 | 名称 | 值 | 说明 | 9617 | ---------- | --- | ------------------ | 9618 | PROT_EXEC | 4 | 映射的内存可执行。 | 9619 | PROT_NONE | 0 | 映射的内存不可访问。 | 9620 | PROT_READ | 1 | 映射的内存可读。 | 9621 | PROT_WRITE | 2 | 映射的内存可写。 | 9622 9623### create<sup>9+</sup> 9624 9625static create(name: string, size: number): Ashmem 9626 9627静态方法,根据指定的名称和大小创建Ashmem对象。 9628 9629**系统能力**:SystemCapability.Communication.IPC.Core 9630 9631**参数:** 9632 9633 | 参数名 | 类型 | 必填 | 说明 | 9634 | ------ | ------ | ---- | ---------------------------- | 9635 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9636 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9637 9638**返回值:** 9639 9640| 类型 | 说明 | 9641| ------------------ | ---------------------------------------------- | 9642| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9643 9644**错误码:** 9645 9646以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9647 9648 | 错误码ID | 错误信息 | 9649 | -------- | -------- | 9650 | 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. | 9651 9652**示例:** 9653 9654 ```ts 9655 import { hilog } from '@kit.PerformanceAnalysisKit'; 9656 import { BusinessError } from '@kit.BasicServicesKit'; 9657 9658 let ashmem: rpc.Ashmem | undefined = undefined; 9659 try { 9660 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9661 let size = ashmem.getAshmemSize(); 9662 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9663 } catch (error) { 9664 let e: BusinessError = error as BusinessError; 9665 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9666 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9667 } 9668 ``` 9669 9670### createAshmem<sup>(deprecated)</sup> 9671 9672>从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9)替代。 9673 9674static createAshmem(name: string, size: number): Ashmem 9675 9676静态方法,根据指定的名称和大小创建Ashmem对象。 9677 9678**系统能力**:SystemCapability.Communication.IPC.Core 9679 9680**参数:** 9681 9682 | 参数名 | 类型 | 必填 | 说明 | 9683 | ------ | ------ | ---- | ---------------------------- | 9684 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9685 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9686 9687**返回值:** 9688 9689| 类型 | 说明 | 9690| ------------------ | ---------------------------------------------- | 9691| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9692 9693**示例:** 9694 9695 ```ts 9696 import { hilog } from '@kit.PerformanceAnalysisKit'; 9697 9698 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9699 let size = ashmem.getAshmemSize(); 9700 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9701 ``` 9702 9703### create<sup>9+</sup> 9704 9705static create(ashmem: Ashmem): Ashmem 9706 9707静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9708 9709**系统能力**:SystemCapability.Communication.IPC.Core 9710 9711**参数:** 9712 9713| 参数名 | 类型 | 必填 | 说明 | 9714| ------ | ------------------ | ---- | -------------------- | 9715| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9716 9717**返回值:** 9718 9719| 类型 | 说明 | 9720| ------------------ | ---------------------- | 9721| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9722 9723**错误码:** 9724 9725以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9726 9727 | 错误码ID | 错误信息 | 9728 | -------- | -------- | 9729 | 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. | 9730 9731**示例:** 9732 9733 ```ts 9734 import { hilog } from '@kit.PerformanceAnalysisKit'; 9735 import { BusinessError } from '@kit.BasicServicesKit'; 9736 9737 try { 9738 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9739 let ashmem2 = rpc.Ashmem.create(ashmem); 9740 let size = ashmem2.getAshmemSize(); 9741 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9742 } catch (error) { 9743 let e: BusinessError = error as BusinessError; 9744 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9745 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9746 } 9747 ``` 9748 9749### createAshmemFromExisting<sup>(deprecated)</sup> 9750 9751>从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9-1)替代。 9752 9753static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9754 9755静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9756 9757**系统能力**:SystemCapability.Communication.IPC.Core 9758 9759**参数:** 9760 9761| 参数名 | 类型 | 必填 | 说明 | 9762| ------ | ------------------ | ---- | -------------------- | 9763| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9764 9765**返回值:** 9766 9767| 类型 | 说明 | 9768| ------------------ | ---------------------- | 9769| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9770 9771**示例:** 9772 9773 ```ts 9774 import { hilog } from '@kit.PerformanceAnalysisKit'; 9775 9776 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9777 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9778 let size = ashmem2.getAshmemSize(); 9779 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9780 ``` 9781 9782### closeAshmem<sup>8+</sup> 9783 9784closeAshmem(): void 9785 9786关闭这个Ashmem。 9787 9788**系统能力**:SystemCapability.Communication.IPC.Core 9789 9790**示例:** 9791 9792 ```ts 9793 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9794 ashmem.closeAshmem(); 9795 ``` 9796 9797### unmapAshmem<sup>8+</sup> 9798 9799unmapAshmem(): void 9800 9801删除该Ashmem对象的地址映射。 9802 9803**系统能力**:SystemCapability.Communication.IPC.Core 9804 9805**示例:** 9806 9807 ```ts 9808 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9809 ashmem.unmapAshmem(); 9810 ``` 9811 9812### getAshmemSize<sup>8+</sup> 9813 9814getAshmemSize(): number 9815 9816获取Ashmem对象的内存大小。 9817 9818**系统能力**:SystemCapability.Communication.IPC.Core 9819 9820**返回值:** 9821 9822 | 类型 | 说明 | 9823 | ------ | -------------------------- | 9824 | number | 返回Ashmem对象的内存大小。 | 9825 9826**示例:** 9827 9828 ```ts 9829 import { hilog } from '@kit.PerformanceAnalysisKit'; 9830 9831 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9832 let size = ashmem.getAshmemSize(); 9833 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9834 ``` 9835 9836### mapTypedAshmem<sup>9+</sup> 9837 9838mapTypedAshmem(mapType: number): void 9839 9840在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9841 9842**系统能力**:SystemCapability.Communication.IPC.Core 9843 9844**参数:** 9845 9846 | 参数名 | 类型 | 必填 | 说明 | 9847 | ------- | ------ | ---- | ------------------------------ | 9848 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9849 9850**错误码:** 9851 9852以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9853 9854 | 错误码ID | 错误信息 | 9855 | -------- | -------- | 9856 | 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. | 9857 | 1900001 | Failed to call mmap. | 9858 9859**示例:** 9860 9861 ```ts 9862 import { hilog } from '@kit.PerformanceAnalysisKit'; 9863 import { BusinessError } from '@kit.BasicServicesKit'; 9864 9865 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9866 try { 9867 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9868 } catch (error) { 9869 let e: BusinessError = error as BusinessError; 9870 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9871 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9872 } 9873 ``` 9874 9875### mapAshmem<sup>(deprecated)</sup> 9876 9877>从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapTypedAshmem](#maptypedashmem9)替代。 9878 9879mapAshmem(mapType: number): boolean 9880 9881在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9882 9883**系统能力**:SystemCapability.Communication.IPC.Core 9884 9885**参数:** 9886 9887 | 参数名 | 类型 | 必填 | 说明 | 9888 | ------- | ------ | ---- | ------------------------------ | 9889 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9890 9891**返回值:** 9892 9893 | 类型 | 说明 | 9894 | ------- | -------------------------------- | 9895 | boolean | true:映射成功,false:映射失败。| 9896 9897**示例:** 9898 9899 ```ts 9900 import { hilog } from '@kit.PerformanceAnalysisKit'; 9901 9902 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9903 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9904 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9905 ``` 9906 9907### mapReadWriteAshmem<sup>9+</sup> 9908 9909mapReadWriteAshmem(): void 9910 9911在此进程虚拟地址空间上创建可读写的共享文件映射。 9912 9913**系统能力**:SystemCapability.Communication.IPC.Core 9914 9915**错误码:** 9916 9917以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9918 9919 | 错误码ID | 错误信息 | 9920 | -------- | -------- | 9921 | 1900001 | Failed to call mmap. | 9922 9923**示例:** 9924 9925 ```ts 9926 import { hilog } from '@kit.PerformanceAnalysisKit'; 9927 import { BusinessError } from '@kit.BasicServicesKit'; 9928 9929 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9930 try { 9931 ashmem.mapReadWriteAshmem(); 9932 } catch (error) { 9933 let e: BusinessError = error as BusinessError; 9934 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9935 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9936 } 9937 ``` 9938 9939### mapReadAndWriteAshmem<sup>(deprecated)</sup> 9940 9941>从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadWriteAshmem](#mapreadwriteashmem9)替代。 9942 9943mapReadAndWriteAshmem(): boolean 9944 9945在此进程虚拟地址空间上创建可读写的共享文件映射。 9946 9947**系统能力**:SystemCapability.Communication.IPC.Core 9948 9949**返回值:** 9950 9951 | 类型 | 说明 | 9952 | ------- | -------------------------------- | 9953 | boolean | true:映射成功,false:映射失败。| 9954 9955**示例:** 9956 9957 ```ts 9958 import { hilog } from '@kit.PerformanceAnalysisKit'; 9959 9960 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9961 let mapResult = ashmem.mapReadAndWriteAshmem(); 9962 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 9963 ``` 9964 9965### mapReadonlyAshmem<sup>9+</sup> 9966 9967mapReadonlyAshmem(): void 9968 9969在此进程虚拟地址空间上创建只读的共享文件映射。 9970 9971**系统能力**:SystemCapability.Communication.IPC.Core 9972 9973**错误码:** 9974 9975以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9976 9977 | 错误码ID | 错误信息 | 9978 | -------- | -------- | 9979 | 1900001 | Failed to call mmap. | 9980 9981**示例:** 9982 9983 ```ts 9984 import { hilog } from '@kit.PerformanceAnalysisKit'; 9985 import { BusinessError } from '@kit.BasicServicesKit'; 9986 9987 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9988 try { 9989 ashmem.mapReadonlyAshmem(); 9990 } catch (error) { 9991 let e: BusinessError = error as BusinessError; 9992 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9993 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9994 } 9995 ``` 9996 9997### mapReadOnlyAshmem<sup>(deprecated)</sup> 9998 9999>从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadonlyAshmem](#mapreadonlyashmem9)替代。 10000 10001mapReadOnlyAshmem(): boolean 10002 10003在此进程虚拟地址空间上创建只读的共享文件映射。 10004 10005**系统能力**:SystemCapability.Communication.IPC.Core 10006 10007**返回值:** 10008 10009 | 类型 | 说明 | 10010 | ------- | -------------------------------- | 10011 | boolean | true:映射成功,false:映射失败。| 10012 10013**示例:** 10014 10015 ```ts 10016 import { hilog } from '@kit.PerformanceAnalysisKit'; 10017 10018 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10019 let mapResult = ashmem.mapReadOnlyAshmem(); 10020 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 10021 ``` 10022 10023### setProtectionType<sup>9+</sup> 10024 10025setProtectionType(protectionType: number): void 10026 10027设置映射内存区域的保护等级。 10028 10029**系统能力**:SystemCapability.Communication.IPC.Core 10030 10031**参数:** 10032 10033 | 参数名 | 类型 | 必填 | 说明 | 10034 | -------------- | ------ | ---- | ------------------ | 10035 | protectionType | number | 是 | 要设置的保护类型。 | 10036 10037**错误码:** 10038 10039以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10040 10041 | 错误码ID | 错误信息 | 10042 | -------- | -------- | 10043 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10044 | 1900002 | Failed to call ioctl. | 10045 10046**示例:** 10047 10048 ```ts 10049 import { hilog } from '@kit.PerformanceAnalysisKit'; 10050 import { BusinessError } from '@kit.BasicServicesKit'; 10051 10052 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10053 try { 10054 ashmem.setProtection(ashmem.PROT_READ); 10055 } catch (error) { 10056 let e: BusinessError = error as BusinessError; 10057 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10058 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10059 } 10060 ``` 10061 10062### setProtection<sup>(deprecated)</sup> 10063 10064>从API version 8 开始支持,从API version 9 开始废弃,建议使用[setProtectionType](#setprotectiontype9)替代。 10065 10066setProtection(protectionType: number): boolean 10067 10068设置映射内存区域的保护等级。 10069 10070**系统能力**:SystemCapability.Communication.IPC.Core 10071 10072**参数:** 10073 10074 | 参数名 | 类型 | 必填 | 说明 | 10075 | -------------- | ------ | ---- | ------------------ | 10076 | protectionType | number | 是 | 要设置的保护类型。 | 10077 10078**返回值:** 10079 10080 | 类型 | 说明 | 10081 | ------- | -------------------------------- | 10082 | boolean | true:设置成功,false:设置失败。| 10083 10084**示例:** 10085 10086 ```ts 10087 import { hilog } from '@kit.PerformanceAnalysisKit'; 10088 10089 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10090 let result = ashmem.setProtection(ashmem.PROT_READ); 10091 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 10092 ``` 10093 10094### writeDataToAshmem<sup>11+</sup> 10095 10096writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10097 10098将数据写入此Ashmem对象关联的共享文件。 10099 10100**系统能力**:SystemCapability.Communication.IPC.Core 10101 10102**参数:** 10103 10104 | 参数名 | 类型 | 必填 | 说明 | 10105 | ------ | -------- | ---- | -------------------------------------------------- | 10106 | buf | ArrayBuffer | 是 | 写入Ashmem对象的数据。 | 10107 | size | number | 是 | 要写入的数据大小。 | 10108 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10109 10110**错误码:** 10111 10112以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10113 10114 | 错误码ID | 错误信息 | 10115 | -------- | -------- | 10116 | 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. | 10117 | 1900003 | Failed to write data to the shared memory. | 10118 10119**示例:** 10120 10121 ```ts 10122 import { hilog } from '@kit.PerformanceAnalysisKit'; 10123 import { BusinessError } from '@kit.BasicServicesKit'; 10124 10125 let buffer = new ArrayBuffer(1024); 10126 let int32View = new Int32Array(buffer); 10127 for (let i = 0; i < int32View.length; i++) { 10128 int32View[i] = i * 2 + 1; 10129 } 10130 let size = buffer.byteLength; 10131 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10132 ashmem.mapReadWriteAshmem(); 10133 try { 10134 ashmem.writeDataToAshmem(buffer, size, 0); 10135 } catch (error) { 10136 let e: BusinessError = error as BusinessError; 10137 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10138 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10139 } 10140 ``` 10141 10142### writeAshmem<sup>(deprecated)</sup> 10143 10144>从API version 9 开始支持,从API version 11 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10145 10146writeAshmem(buf: number[], size: number, offset: number): void 10147 10148将数据写入此Ashmem对象关联的共享文件。 10149 10150**系统能力**:SystemCapability.Communication.IPC.Core 10151 10152**参数:** 10153 10154 | 参数名 | 类型 | 必填 | 说明 | 10155 | ------ | -------- | ---- | -------------------------------------------------- | 10156 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10157 | size | number | 是 | 要写入的数据大小。 | 10158 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 | 10159 10160**错误码:** 10161 10162以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10163 10164 | 错误码ID | 错误信息 | 10165 | -------- | -------- | 10166 | 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. | 10167 | 1900003 | Failed to write data to the shared memory. | 10168 10169**示例:** 10170 10171 ```ts 10172 import { hilog } from '@kit.PerformanceAnalysisKit'; 10173 import { BusinessError } from '@kit.BasicServicesKit'; 10174 10175 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10176 ashmem.mapReadWriteAshmem(); 10177 let ByteArrayVar = [1, 2, 3, 4, 5]; 10178 try { 10179 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10180 } catch (error) { 10181 let e: BusinessError = error as BusinessError; 10182 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10183 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10184 } 10185 ``` 10186 10187### writeToAshmem<sup>(deprecated)</sup> 10188 10189>从API version 8 开始支持,从API version 9 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10190 10191writeToAshmem(buf: number[], size: number, offset: number): boolean 10192 10193将数据写入此Ashmem对象关联的共享文件。 10194 10195**系统能力**:SystemCapability.Communication.IPC.Core 10196 10197**参数:** 10198 10199 | 参数名 | 类型 | 必填 | 说明 | 10200 | ------ | -------- | ---- | -------------------------------------------------- | 10201 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10202 | size | number | 是 | 要写入的数据大小。 | 10203 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10204 10205**返回值:** 10206 10207 | 类型 | 说明 | 10208 | ------- | ----------------------------------------------------------------------------- | 10209 | boolean | true:如果数据写入成功,false:在其他情况下,如数据写入越界或未获得写入权限。 | 10210 10211**示例:** 10212 10213 ```ts 10214 import { hilog } from '@kit.PerformanceAnalysisKit'; 10215 10216 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10217 let mapResult = ashmem.mapReadAndWriteAshmem(); 10218 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10219 let ByteArrayVar = [1, 2, 3, 4, 5]; 10220 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10221 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10222 ``` 10223 10224### readDataFromAshmem<sup>11+</sup> 10225 10226readDataFromAshmem(size: number, offset: number): ArrayBuffer 10227 10228从此Ashmem对象关联的共享文件中读取数据。 10229 10230**系统能力**:SystemCapability.Communication.IPC.Core 10231 10232**参数:** 10233 10234 | 参数名 | 类型 | 必填 | 说明 | 10235 | ------ | ------ | ---- | -------------------------------------------------- | 10236 | size | number | 是 | 要读取的数据的大小。 | 10237 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10238 10239**返回值:** 10240 10241 | 类型 | 说明 | 10242 | -------- | ---------------- | 10243 | ArrayBuffer | 返回读取的数据。 | 10244 10245**错误码:** 10246 10247以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10248 10249 | 错误码ID | 错误信息 | 10250 | -------- | -------- | 10251 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10252 | 1900004 | Failed to read data from the shared memory. | 10253 10254**示例:** 10255 10256 ```ts 10257 import { hilog } from '@kit.PerformanceAnalysisKit'; 10258 import { BusinessError } from '@kit.BasicServicesKit'; 10259 10260 let buffer = new ArrayBuffer(1024); 10261 let int32View = new Int32Array(buffer); 10262 for (let i = 0; i < int32View.length; i++) { 10263 int32View[i] = i * 2 + 1; 10264 } 10265 let size = buffer.byteLength; 10266 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10267 ashmem.mapReadWriteAshmem(); 10268 try { 10269 ashmem.writeDataToAshmem(buffer, size, 0); 10270 } catch (error) { 10271 let e: BusinessError = error as BusinessError; 10272 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10273 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10274 } 10275 try { 10276 let readResult = ashmem.readDataFromAshmem(size, 0); 10277 let readInt32View = new Int32Array(readResult); 10278 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readInt32View); 10279 } catch (error) { 10280 let e: BusinessError = error as BusinessError; 10281 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10282 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10283 } 10284 ``` 10285 10286### readAshmem<sup>(deprecated)</sup> 10287 10288>从API version 9 开始支持,从API version 11 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10289 10290readAshmem(size: number, offset: number): number[] 10291 10292从此Ashmem对象关联的共享文件中读取数据。 10293 10294**系统能力**:SystemCapability.Communication.IPC.Core 10295 10296**参数:** 10297 10298 | 参数名 | 类型 | 必填 | 说明 | 10299 | ------ | ------ | ---- | -------------------------------------------------- | 10300 | size | number | 是 | 要读取的数据的大小。 | 10301 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10302 10303**返回值:** 10304 10305 | 类型 | 说明 | 10306 | -------- | ---------------- | 10307 | number[] | 返回读取的数据。 | 10308 10309**错误码:** 10310 10311以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10312 10313 | 错误码ID | 错误信息 | 10314 | -------- | -------- | 10315 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10316 | 1900004 | Failed to read data from the shared memory. | 10317 10318**示例:** 10319 10320 ```ts 10321 import { hilog } from '@kit.PerformanceAnalysisKit'; 10322 import { BusinessError } from '@kit.BasicServicesKit'; 10323 10324 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10325 ashmem.mapReadWriteAshmem(); 10326 let ByteArrayVar = [1, 2, 3, 4, 5]; 10327 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10328 try { 10329 let readResult = ashmem.readAshmem(5, 0); 10330 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 10331 } catch (error) { 10332 let e: BusinessError = error as BusinessError; 10333 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10334 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10335 } 10336 ``` 10337 10338### readFromAshmem<sup>(deprecated)</sup> 10339 10340>从API version 8 开始支持,从API version 9 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10341 10342readFromAshmem(size: number, offset: number): number[] 10343 10344从此Ashmem对象关联的共享文件中读取数据。 10345 10346**系统能力**:SystemCapability.Communication.IPC.Core 10347 10348**参数:** 10349 10350 | 参数名 | 类型 | 必填 | 说明 | 10351 | ------ | ------ | ---- | -------------------------------------------------- | 10352 | size | number | 是 | 要读取的数据的大小。 | 10353 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10354 10355**返回值:** 10356 10357 | 类型 | 说明 | 10358 | -------- | ---------------- | 10359 | number[] | 返回读取的数据。 | 10360 10361**示例:** 10362 10363 ``` ts 10364 import { hilog } from '@kit.PerformanceAnalysisKit'; 10365 10366 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10367 let mapResult = ashmem.mapReadAndWriteAshmem(); 10368 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10369 let ByteArrayVar = [1, 2, 3, 4, 5]; 10370 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10371 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10372 let readResult = ashmem.readFromAshmem(5, 0); 10373 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 10374 ```