1# @ohos.rpc (RPC通信) 2 3本模块提供进程间通信能力,包括设备内的进程间通信(IPC)和设备间的进程间通信(RPC),前者基于Binder驱动,后者基于软总线驱动。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> - 本模块从API version 9开始支持异常返回功能。 10 11## 导入模块 12 13``` 14import { rpc } from '@kit.IPCKit'; 15``` 16 17## ErrorCode<sup>9+</sup> 18 19从API version 9起,IPC支持异常返回功能。错误码对应数值及含义如下。 20 21**系统能力**:SystemCapability.Communication.IPC.Core 22 23 | 名称 | 值 | 说明 | 24 | ------------------------------------- | ------- | --------------------------------------------- | 25 | CHECK_PARAM_ERROR | 401 | 检查参数失败。 | 26 | OS_MMAP_ERROR | 1900001 | 执行系统调用mmap失败。 | 27 | OS_IOCTL_ERROR | 1900002 | 在共享内存文件描述符上执行系统调用ioctl失败。 | 28 | WRITE_TO_ASHMEM_ERROR | 1900003 | 向共享内存写数据失败。 | 29 | READ_FROM_ASHMEM_ERROR | 1900004 | 从共享内存读数据失败。 | 30 | ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | 只有proxy对象允许该操作。 | 31 | ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | 只有remote对象允许该操作。 | 32 | COMMUNICATION_ERROR | 1900007 | 和远端对象进行进程间通信失败。 | 33 | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | 非法的代理对象或者远端对象。 | 34 | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | 向MessageSequence写数据失败。 | 35 | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | 读取MessageSequence数据失败。 | 36 | PARCEL_MEMORY_ALLOC_ERROR | 1900011 | 序列化过程中内存分配失败。 | 37 | CALL_JS_METHOD_ERROR | 1900012 | 执行JS回调方法失败。 | 38 | OS_DUP_ERROR | 1900013 | 执行系统调用dup失败。 | 39 40 41## TypeCode<sup>12+</sup> 42 43从API version 12起,IPC新增[writeArrayBuffer](#writearraybuffer12)和[readArrayBuffer](#readarraybuffer12)方法传递ArrayBuffer数据,传递数据时通过具体类型值来分辨业务是以哪一种TypedArray去进行数据的读写。类型码对应数值及含义如下。 44 45**系统能力**:SystemCapability.Communication.IPC.Core 46 47 | 名称 | 值 | 说明 | 48 | ---------------------------- | ------ | -------------------------------------------- | 49 | INT8_ARRAY | 0 | TypedArray类型为INT8_ARRAY。 | 50 | UINT8_ARRAY | 1 | TypedArray类型为UINT8_ARRAY。 | 51 | INT16_ARRAY | 2 | TypedArray类型为INT16_ARRAY。 | 52 | UINT16_ARRAY | 3 | TypedArray类型为UINT16_ARRAY。 | 53 | INT32_ARRAY | 4 | TypedArray类型为INT32_ARRAY。 | 54 | UINT32_ARRAY | 5 | TypedArray类型为UINT32_ARRAY。 | 55 | FLOAT32_ARRAY | 6 | TypedArray类型为FLOAT32_ARRAY。 | 56 | FLOAT64_ARRAY | 7 | TypedArray类型为FLOAT64_ARRAY。 | 57 | BIGINT64_ARRAY | 8 | TypedArray类型为BIGINT64_ARRAY。 | 58 | BIGUINT64_ARRAY | 9 | TypedArray类型为BIGUINT64_ARRAY。 | 59 60 61## MessageSequence<sup>9+</sup> 62 63 在RPC或IPC过程中,发送方可以使用MessageSequence提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageSequence提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 64 65### create 66 67 static create(): MessageSequence 68 69 静态方法,创建MessageSequence对象。 70 71**系统能力**:SystemCapability.Communication.IPC.Core 72 73**返回值:** 74 75| 类型 | 说明 | 76| --------------- | ------------------------------- | 77| [MessageSequence](#messagesequence9) | 返回创建的MessageSequence对象。 | 78 79**示例:** 80 81 ```ts 82 import { hilog } from '@kit.PerformanceAnalysisKit'; 83 84 let data = rpc.MessageSequence.create(); 85 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 86 87 // 当MessageSequence对象不再使用,由业务主动调用reclaim方法去释放资源。 88 data.reclaim(); 89 ``` 90 91### reclaim 92 93reclaim(): void 94 95释放不再使用的MessageSequence对象。 96 97**系统能力**:SystemCapability.Communication.IPC.Core 98 99**示例:** 100 101 ```ts 102 let reply = rpc.MessageSequence.create(); 103 reply.reclaim(); 104 ``` 105 106### writeRemoteObject 107 108writeRemoteObject(object: IRemoteObject): void 109 110序列化远程对象并将其写入[MessageSequence](#messagesequence9)对象。 111 112**系统能力**:SystemCapability.Communication.IPC.Core 113 114**参数:** 115 116 | 参数名 | 类型 | 必填 | 说明 | 117 | ------ | ------------------------------- | ---- | ----------------------------------------- | 118 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageSequence的远程对象。 | 119 120**错误码:** 121 122以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 123 124 | 错误码ID | 错误信息 | 125 | -------- | -------- | 126 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 127 | 1900008 | The proxy or remote object is invalid. | 128 | 1900009 | Failed to write data to the message sequence. | 129 130**示例:** 131 132 ```ts 133 import { hilog } from '@kit.PerformanceAnalysisKit'; 134 import { BusinessError } from '@kit.BasicServicesKit'; 135 136 class TestRemoteObject extends rpc.RemoteObject { 137 constructor(descriptor: string) { 138 super(descriptor); 139 } 140 } 141 let data = rpc.MessageSequence.create(); 142 let testRemoteObject = new TestRemoteObject("testObject"); 143 try { 144 data.writeRemoteObject(testRemoteObject); 145 } catch (error) { 146 let e: BusinessError = error as BusinessError; 147 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 148 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 149 } 150 ``` 151 152### readRemoteObject 153 154readRemoteObject(): IRemoteObject 155 156从MessageSequence读取远程对象。此方法用于反序列化MessageSequence对象以生成IRemoteObject。远程对象按写入MessageSequence的顺序读取。 157 158**系统能力**:SystemCapability.Communication.IPC.Core 159 160**返回值:** 161 162 | 类型 | 说明 | 163 | ------------------------------- | ------------------ | 164 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 165 166**错误码:** 167 168以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 169 170 | 错误码ID | 错误信息 | 171 | -------- | -------- | 172 | 1900008 | The proxy or remote object is invalid. | 173 | 1900010 | Failed to read data from the message sequence. | 174 175**示例:** 176 177 ```ts 178 import { hilog } from '@kit.PerformanceAnalysisKit'; 179 import { BusinessError } from '@kit.BasicServicesKit'; 180 181 class TestRemoteObject extends rpc.RemoteObject { 182 constructor(descriptor: string) { 183 super(descriptor); 184 } 185 } 186 let data = rpc.MessageSequence.create(); 187 let testRemoteObject = new TestRemoteObject("testObject"); 188 try { 189 data.writeRemoteObject(testRemoteObject); 190 let proxy = data.readRemoteObject(); 191 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 192 } catch (error) { 193 let e: BusinessError = error as BusinessError; 194 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 195 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 196 } 197 ``` 198 199### writeInterfaceToken 200 201writeInterfaceToken(token: string): void 202 203将接口描述符写入MessageSequence对象,远端对象可使用该信息校验本次通信。 204 205**系统能力**:SystemCapability.Communication.IPC.Core 206 207**参数:** 208 209 | 参数名 | 类型 | 必填 | 说明 | 210 | ------ | ------ | ---- | ------------------ | 211 | token | string | 是 | 字符串类型描述符。 | 212 213**错误码:** 214 215以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 216 217 | 错误码ID | 错误信息 | 218 | -------- | -------- | 219 | 401 | Parameter error. Possible causes:<br/> 1.The number of parameters is incorrect;<br/> 2.The parameter type does not match;<br/> 3.The string length exceeds 40960 bytes;<br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 220 | 1900009 | Failed to write data to the message sequence. | 221 222**示例:** 223 224 ```ts 225 import { hilog } from '@kit.PerformanceAnalysisKit'; 226 import { BusinessError } from '@kit.BasicServicesKit'; 227 228 let data = rpc.MessageSequence.create(); 229 try { 230 data.writeInterfaceToken("aaa"); 231 } catch (error) { 232 let e: BusinessError = error as BusinessError; 233 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 234 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 235 } 236 ``` 237 238### readInterfaceToken 239 240readInterfaceToken(): string 241 242从MessageSequence对象中读取接口描述符,接口描述符按写入MessageSequence的顺序读取,本地对象可使用该信息检验本次通信。 243 244**系统能力**:SystemCapability.Communication.IPC.Core 245 246**返回值:** 247 248 | 类型 | 说明 | 249 | ------ | ------------------------ | 250 | string | 返回读取到的接口描述符。 | 251 252**错误码:** 253 254以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 255 256 | 错误码ID | 错误信息 | 257 | -------- | -------- | 258 | 1900010 | Failed to read data from the message sequence. | 259 260**示例:** 261 262```ts 263import { hilog } from '@kit.PerformanceAnalysisKit'; 264import { BusinessError } from '@kit.BasicServicesKit'; 265 266class Stub extends rpc.RemoteObject { 267 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 268 try { 269 let interfaceToken = data.readInterfaceToken(); 270 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 271 } catch (error) { 272 let e: BusinessError = error as BusinessError; 273 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 274 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 275 } 276 return true; 277 } 278} 279``` 280 281### getSize 282 283getSize(): number 284 285获取当前创建的MessageSequence对象的数据大小。 286 287**系统能力**:SystemCapability.Communication.IPC.Core 288 289**返回值:** 290 291 | 类型 | 说明 | 292 | ------ | ----------------------------------------------- | 293 | number | 获取的MessageSequence实例的数据大小。以字节为单位。 | 294 295**示例:** 296 297 ```ts 298 import { hilog } from '@kit.PerformanceAnalysisKit'; 299 300 let data = rpc.MessageSequence.create(); 301 let size = data.getSize(); 302 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 303 ``` 304 305### getCapacity 306 307getCapacity(): number 308 309获取当前MessageSequence对象的容量大小。 310 311**系统能力**:SystemCapability.Communication.IPC.Core 312 313**返回值:** 314 315 | 类型 | 说明 | 316 | ------ | ----- | 317 | number | 获取的MessageSequence实例的容量大小。以字节为单位。 | 318 319**示例:** 320 321 ```ts 322 import { hilog } from '@kit.PerformanceAnalysisKit'; 323 324 let data = rpc.MessageSequence.create(); 325 let result = data.getCapacity(); 326 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 327 ``` 328 329### setSize 330 331setSize(size: number): void 332 333设置MessageSequence对象中包含的数据大小。 334 335**系统能力**:SystemCapability.Communication.IPC.Core 336 337**参数:** 338 339 | 参数名 | 类型 | 必填 | 说明 | 340 | ------ | ------ | ---- | ------ | 341 | size | number | 是 | MessageSequence实例的数据大小。以字节为单位。 | 342 343**错误码:** 344 345以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 346 347 | 错误码ID | 错误信息 | 348 | -------- | -------- | 349 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 350 351**示例:** 352 353 ```ts 354 import { hilog } from '@kit.PerformanceAnalysisKit'; 355 import { BusinessError } from '@kit.BasicServicesKit'; 356 357 let data = rpc.MessageSequence.create(); 358 data.writeString('Hello World'); 359 try { 360 data.setSize(16); 361 } catch (error) { 362 let e: BusinessError = error as BusinessError; 363 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 364 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 365 } 366 ``` 367 368### setCapacity 369 370setCapacity(size: number): void 371 372设置MessageSequence对象的存储容量。 373 374**系统能力**:SystemCapability.Communication.IPC.Core 375 376**参数:** 377 378 | 参数名 | 类型 | 必填 | 说明 | 379 | ------ | ------ | ---- | --------------------------------------------- | 380 | size | number | 是 | MessageSequence实例的存储容量。以字节为单位。 | 381 382**错误码:** 383 384以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 385 386 | 错误码ID | 错误信息 | 387 | -------- | -------- | 388 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 389 | 1900011 | Memory allocation failed | 390 391**示例:** 392 393 ```ts 394 import { hilog } from '@kit.PerformanceAnalysisKit'; 395 import { BusinessError } from '@kit.BasicServicesKit'; 396 397 let data = rpc.MessageSequence.create(); 398 try { 399 data.setCapacity(100); 400 } catch (error) { 401 let e: BusinessError = error as BusinessError; 402 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 403 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 404 } 405 ``` 406 407### getWritableBytes 408 409getWritableBytes(): number 410 411获取MessageSequence的可写字节空间大小。 412 413**系统能力**:SystemCapability.Communication.IPC.Core 414 415**返回值:** 416 417 | 类型 | 说明 | 418 | ------ | ------ | 419 | number | 获取到的MessageSequence实例的可写字节空间。以字节为单位。 | 420 421**示例:** 422 423```ts 424import { hilog } from '@kit.PerformanceAnalysisKit'; 425 426class Stub extends rpc.RemoteObject { 427 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 428 let getWritableBytes = data.getWritableBytes(); 429 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 430 return true; 431 } 432} 433``` 434 435### getReadableBytes 436 437getReadableBytes(): number 438 439获取MessageSequence的可读字节空间。 440 441**系统能力**:SystemCapability.Communication.IPC.Core 442 443**返回值:** 444 445 | 类型 | 说明 | 446 | ------ | ------- | 447 | number | 获取到的MessageSequence实例的可读字节空间。以字节为单位。 | 448 449**示例:** 450 451```ts 452import { hilog } from '@kit.PerformanceAnalysisKit'; 453 454class Stub extends rpc.RemoteObject { 455 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 456 let result = data.getReadableBytes(); 457 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 458 return true; 459 } 460} 461``` 462 463### getReadPosition 464 465getReadPosition(): number 466 467获取MessageSequence的读位置。 468 469**系统能力**:SystemCapability.Communication.IPC.Core 470 471**返回值:** 472 473 | 类型 | 说明 | 474 | ------ | ------ | 475 | number | 返回MessageSequence实例中的当前读取位置。 | 476 477**示例:** 478 479 ```ts 480 import { hilog } from '@kit.PerformanceAnalysisKit'; 481 482 let data = rpc.MessageSequence.create(); 483 let readPos = data.getReadPosition(); 484 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 485 ``` 486 487### getWritePosition 488 489getWritePosition(): number 490 491获取MessageSequence的写位置。 492 493**系统能力**:SystemCapability.Communication.IPC.Core 494 495**返回值:** 496 497 | 类型 | 说明 | 498 | ------ | ----- | 499 | number | 返回MessageSequence实例中的当前写入位置。 | 500 501**示例:** 502 503 ```ts 504 import { hilog } from '@kit.PerformanceAnalysisKit'; 505 506 let data = rpc.MessageSequence.create(); 507 data.writeInt(10); 508 let bwPos = data.getWritePosition(); 509 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 510 ``` 511 512### rewindRead 513 514rewindRead(pos: number): void 515 516重新偏移读取位置到指定的位置。 517 518**系统能力**:SystemCapability.Communication.IPC.Core 519 520**参数:** 521 522 | 参数名 | 类型 | 必填 | 说明 | 523 | ------ | ------ | ---- | ------- | 524 | pos | number | 是 | 开始读取数据的目标位置。 | 525 526**错误码:** 527 528以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 529 530 | 错误码ID | 错误信息 | 531 | -------- | -------- | 532 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 533 534**示例:** 535 536 ```ts 537 import { hilog } from '@kit.PerformanceAnalysisKit'; 538 import { BusinessError } from '@kit.BasicServicesKit'; 539 540 let data = rpc.MessageSequence.create(); 541 data.writeInt(12); 542 data.writeString("sequence"); 543 let number = data.readInt(); 544 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 545 try { 546 data.rewindRead(0); 547 } catch (error) { 548 let e: BusinessError = error as BusinessError; 549 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 550 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 551 } 552 let number2 = data.readInt(); 553 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 554 ``` 555 556### rewindWrite 557 558rewindWrite(pos: number): void 559 560重新偏移写位置到指定的位置。 561 562**系统能力**:SystemCapability.Communication.IPC.Core 563 564**参数:** 565 566 | 参数名 | 类型 | 必填 | 说明 | 567 | ------ | ------ | ---- | ----- | 568 | pos | number | 是 | 开始写入数据的目标位置。 | 569 570**错误码:** 571 572以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 573 574 | 错误码ID | 错误信息 | 575 | -------- | -------- | 576 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 577 578**示例:** 579 580 ```ts 581 import { hilog } from '@kit.PerformanceAnalysisKit'; 582 import { BusinessError } from '@kit.BasicServicesKit'; 583 584 let data = rpc.MessageSequence.create(); 585 data.writeInt(4); 586 try { 587 data.rewindWrite(0); 588 } catch (error) { 589 let e: BusinessError = error as BusinessError; 590 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 591 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 592 } 593 data.writeInt(5); 594 let number = data.readInt(); 595 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 596 ``` 597 598### writeByte 599 600writeByte(val: number): void 601 602将字节值写入MessageSequence实例。 603 604**系统能力**:SystemCapability.Communication.IPC.Core 605 606**参数:** 607 608 | 参数名 | 类型 | 必填 | 说明 | 609 | ------ | ------ | ---- | ----- | 610 | val | number | 是 | 要写入的字节值。 | 611 612**错误码:** 613 614以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 615 616 | 错误码ID | 错误信息 | 617 | -------- | ------- | 618 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 619 | 1900009 | Failed to write data to the message sequence. | 620 621**示例:** 622 623 ```ts 624 import { hilog } from '@kit.PerformanceAnalysisKit'; 625 import { BusinessError } from '@kit.BasicServicesKit'; 626 627 let data = rpc.MessageSequence.create(); 628 try { 629 data.writeByte(2); 630 } catch (error) { 631 let e: BusinessError = error as BusinessError; 632 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 633 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 634 } 635 ``` 636 637### readByte 638 639readByte(): number 640 641从MessageSequence实例读取字节值。 642 643**系统能力**:SystemCapability.Communication.IPC.Core 644 645**返回值:** 646 647 | 类型 | 说明 | 648 | ------ | ----- | 649 | number | 返回字节值。 | 650 651**错误码:** 652 653以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 654 655 | 错误码ID | 错误信息 | 656 | ------- | -------- | 657 | 1900010 | Failed to read data from the message sequence. | 658 659**示例:** 660 661 ```ts 662 import { hilog } from '@kit.PerformanceAnalysisKit'; 663 import { BusinessError } from '@kit.BasicServicesKit'; 664 665 let data = rpc.MessageSequence.create(); 666 try { 667 data.writeByte(2); 668 } catch (error) { 669 let e: BusinessError = error as BusinessError; 670 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 671 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 672 } 673 try { 674 let ret = data.readByte(); 675 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 676 } catch (error) { 677 let e: BusinessError = error as BusinessError; 678 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 679 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 680 } 681 ``` 682 683### writeShort 684 685writeShort(val: number): void 686 687将短整数值写入MessageSequence实例。 688 689**系统能力**:SystemCapability.Communication.IPC.Core 690 691**参数:** 692 693 | 参数名 | 类型 | 必填 | 说明 | 694 | ------ | ------ | --- | --- | 695 | val | number | 是 | 要写入的短整数值。 | 696 697**错误码:** 698 699以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 700 701 | 错误码ID | 错误信息 | 702 | -------- | -------- | 703 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 704 | 1900009 | Failed to write data to the message sequence. | 705 706**示例:** 707 708 ```ts 709 import { hilog } from '@kit.PerformanceAnalysisKit'; 710 import { BusinessError } from '@kit.BasicServicesKit'; 711 712 let data = rpc.MessageSequence.create(); 713 try { 714 data.writeShort(8); 715 } catch (error) { 716 let e: BusinessError = error as BusinessError; 717 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 718 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 719 } 720 ``` 721 722### readShort 723 724readShort(): number 725 726从MessageSequence实例读取短整数值。 727 728**系统能力**:SystemCapability.Communication.IPC.Core 729 730**返回值:** 731 732 | 类型 | 说明 | 733 | ------ | -------------- | 734 | number | 返回短整数值。 | 735 736**错误码:** 737 738以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 739 740 | 错误码ID | 错误信息 | 741 | -------- | -------- | 742 | 1900010 | Failed to read data from the message sequence. | 743 744**示例:** 745 746 ```ts 747 import { hilog } from '@kit.PerformanceAnalysisKit'; 748 import { BusinessError } from '@kit.BasicServicesKit'; 749 750 let data = rpc.MessageSequence.create(); 751 try { 752 data.writeShort(8); 753 } catch (error) { 754 let e: BusinessError = error as BusinessError; 755 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 756 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 757 } 758 try { 759 let ret = data.readShort(); 760 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 761 } catch (error) { 762 let e: BusinessError = error as BusinessError; 763 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 764 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 765 } 766 ``` 767 768### writeInt 769 770writeInt(val: number): void 771 772将整数值写入MessageSequence实例。 773 774**系统能力**:SystemCapability.Communication.IPC.Core 775 776**参数:** 777 778 | 参数名 | 类型 | 必填 | 说明 | 779 | ------ | ------ | ---- | ---------------- | 780 | val | number | 是 | 要写入的整数值。 | 781 782**错误码:** 783 784以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 785 786 | 错误码ID | 错误信息 | 787 | -------- | -------- | 788 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 789 | 1900009 | Failed to write data to the message sequence. | 790 791**示例:** 792 793 ```ts 794 import { hilog } from '@kit.PerformanceAnalysisKit'; 795 import { BusinessError } from '@kit.BasicServicesKit'; 796 797 let data = rpc.MessageSequence.create(); 798 try { 799 data.writeInt(10); 800 } catch (error) { 801 let e: BusinessError = error as BusinessError; 802 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 803 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 804 } 805 ``` 806 807### readInt 808 809readInt(): number 810 811从MessageSequence实例读取整数值。 812 813**系统能力**:SystemCapability.Communication.IPC.Core 814 815**返回值:** 816 817 | 类型 | 说明 | 818 | ------ | ------------ | 819 | number | 返回整数值。 | 820 821**错误码:** 822 823以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 824 825 | 错误码ID | 错误信息 | 826 | -------- | -------- | 827 | 1900010 | Failed to read data from the message sequence. | 828 829**示例:** 830 831 ```ts 832 import { hilog } from '@kit.PerformanceAnalysisKit'; 833 import { BusinessError } from '@kit.BasicServicesKit'; 834 835 let data = rpc.MessageSequence.create(); 836 try { 837 data.writeInt(10); 838 } catch (error) { 839 let e: BusinessError = error as BusinessError; 840 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 841 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 842 } 843 try { 844 let ret = data.readInt(); 845 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 846 } catch (error) { 847 let e: BusinessError = error as BusinessError; 848 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 849 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 850 } 851 ``` 852 853### writeLong 854 855writeLong(val: number): void 856 857将长整数值写入MessageSequence实例。 858 859**系统能力**:SystemCapability.Communication.IPC.Core 860 861**参数:** 862 863 | 参数名 | 类型 | 必填 | 说明 | 864 | ------ | ------ | ---- | ---------------- | 865 | val | number | 是 | 要写入的长整数值。 | 866 867**错误码:** 868 869以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 870 871 | 错误码ID | 错误信息 | 872 | -------- | -------- | 873 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 874 | 1900009 | Failed to write data to the message sequence. | 875 876**示例:** 877 878 ```ts 879 import { hilog } from '@kit.PerformanceAnalysisKit'; 880 import { BusinessError } from '@kit.BasicServicesKit'; 881 882 let data = rpc.MessageSequence.create(); 883 try { 884 data.writeLong(10000); 885 } catch (error) { 886 let e: BusinessError = error as BusinessError; 887 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 888 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 889 } 890 ``` 891 892### readLong 893 894readLong(): number 895 896从MessageSequence实例中读取长整数值。 897 898**系统能力**:SystemCapability.Communication.IPC.Core 899 900**返回值:** 901 902 | 类型 | 说明 | 903 | ------ | -------------- | 904 | number | 返回长整数值。 | 905 906**错误码:** 907 908以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 909 910 | 错误码ID | 错误信息 | 911 | -------- | -------- | 912 | 1900010 | Failed to read data from the message sequence. | 913 914**示例:** 915 916 ```ts 917 import { hilog } from '@kit.PerformanceAnalysisKit'; 918 import { BusinessError } from '@kit.BasicServicesKit'; 919 920 let data = rpc.MessageSequence.create(); 921 try { 922 data.writeLong(10000); 923 } catch (error) { 924 let e: BusinessError = error as BusinessError; 925 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 926 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 927 } 928 try { 929 let ret = data.readLong(); 930 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 931 } catch (error) { 932 let e: BusinessError = error as BusinessError; 933 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 934 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 935 } 936 ``` 937 938### writeFloat 939 940writeFloat(val: number): void 941 942将浮点值写入MessageSequence实例。 943 944**系统能力**:SystemCapability.Communication.IPC.Core 945 946**参数:** 947 948 | 参数名 | 类型 | 必填 | 说明 | 949 | ------ | ------ | ---- | ----- | 950 | val | number | 是 | 要写入的浮点值。 | 951 952**错误码:** 953 954以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 955 956 | 错误码ID | 错误信息 | 957 | -------- | -------- | 958 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 959 | 1900009 | Failed to write data to the message sequence. | 960 961**示例:** 962 963 ```ts 964 import { hilog } from '@kit.PerformanceAnalysisKit'; 965 import { BusinessError } from '@kit.BasicServicesKit'; 966 967 let data = rpc.MessageSequence.create(); 968 try { 969 data.writeFloat(1.2); 970 } catch (error) { 971 let e: BusinessError = error as BusinessError; 972 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 973 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 974 } 975 ``` 976 977### readFloat 978 979readFloat(): number 980 981从MessageSequence实例中读取浮点值。 982 983**系统能力**:SystemCapability.Communication.IPC.Core 984 985**返回值:** 986 987 | 类型 | 说明 | 988 | ------ | ------------ | 989 | number | 返回浮点值。 | 990 991**错误码:** 992 993以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 994 995 | 错误码ID | 错误信息 | 996 | -------- | -------- | 997 | 1900010 | Failed to read data from the message sequence. | 998 999**示例:** 1000 1001 ```ts 1002 import { hilog } from '@kit.PerformanceAnalysisKit'; 1003 import { BusinessError } from '@kit.BasicServicesKit'; 1004 1005 let data = rpc.MessageSequence.create(); 1006 try { 1007 data.writeFloat(1.2); 1008 } catch (error) { 1009 let e: BusinessError = error as BusinessError; 1010 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 1011 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 1012 } 1013 try { 1014 let ret = data.readFloat(); 1015 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 1016 } catch (error) { 1017 let e: BusinessError = error as BusinessError; 1018 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 1019 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 1020 } 1021 ``` 1022 1023### writeDouble 1024 1025writeDouble(val: number): void 1026 1027将双精度浮点值写入MessageSequence实例。 1028 1029**系统能力**:SystemCapability.Communication.IPC.Core 1030 1031**参数:** 1032 1033 | 参数名 | 类型 | 必填 | 说明 | 1034 | ------ | ------ | ---- | ---------------------- | 1035 | val | number | 是 | 要写入的双精度浮点值。 | 1036 1037**错误码:** 1038 1039以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1040 1041 | 错误码ID | 错误信息 | 1042 | -------- | -------- | 1043 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1044 | 1900009 | Failed to write data to the message sequence. | 1045 1046**示例:** 1047 1048 ```ts 1049 import { hilog } from '@kit.PerformanceAnalysisKit'; 1050 import { BusinessError } from '@kit.BasicServicesKit'; 1051 1052 let data = rpc.MessageSequence.create(); 1053 try { 1054 data.writeDouble(10.2); 1055 } catch (error) { 1056 let e: BusinessError = error as BusinessError; 1057 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1058 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1059 } 1060 ``` 1061 1062### readDouble 1063 1064readDouble(): number 1065 1066从MessageSequence实例读取双精度浮点值。 1067 1068**系统能力**:SystemCapability.Communication.IPC.Core 1069 1070**返回值:** 1071 1072 | 类型 | 说明 | 1073 | ------ | ------------------ | 1074 | number | 返回双精度浮点值。 | 1075 1076**错误码:** 1077 1078以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1079 1080 | 错误码ID | 错误信息 | 1081 | -------- | -------- | 1082 | 1900010 | Failed to read data from the message sequence. | 1083 1084**示例:** 1085 1086 ```ts 1087 import { hilog } from '@kit.PerformanceAnalysisKit'; 1088 import { BusinessError } from '@kit.BasicServicesKit'; 1089 1090 let data = rpc.MessageSequence.create(); 1091 try { 1092 data.writeDouble(10.2); 1093 } catch (error) { 1094 let e: BusinessError = error as BusinessError; 1095 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1096 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1097 } 1098 try { 1099 let ret = data.readDouble(); 1100 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1101 } catch (error) { 1102 let e: BusinessError = error as BusinessError; 1103 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1104 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1105 } 1106 ``` 1107 1108### writeBoolean 1109 1110writeBoolean(val: boolean): void 1111 1112将布尔值写入MessageSequence实例。 1113 1114**系统能力**:SystemCapability.Communication.IPC.Core 1115 1116**参数:** 1117 1118 | 参数名 | 类型 | 必填 | 说明 | 1119 | ------ | ------- | ---- | ---------------- | 1120 | val | boolean | 是 | 要写入的布尔值。 | 1121 1122**错误码:** 1123 1124以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1125 1126 | 错误码ID | 错误信息 | 1127 | -------- | -------- | 1128 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1129 | 1900009 | Failed to write data to the message sequence. | 1130 1131**示例:** 1132 1133 ```ts 1134 import { hilog } from '@kit.PerformanceAnalysisKit'; 1135 import { BusinessError } from '@kit.BasicServicesKit'; 1136 1137 let data = rpc.MessageSequence.create(); 1138 try { 1139 data.writeBoolean(false); 1140 } catch (error) { 1141 let e: BusinessError = error as BusinessError; 1142 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1143 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1144 } 1145 ``` 1146 1147### readBoolean 1148 1149readBoolean(): boolean 1150 1151从MessageSequence实例读取布尔值。 1152 1153**系统能力**:SystemCapability.Communication.IPC.Core 1154 1155**返回值:** 1156 1157 | 类型 | 说明 | 1158 | ------- | -------------------- | 1159 | boolean | 返回读取到的布尔值。 | 1160 1161**错误码:** 1162 1163以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1164 1165 | 错误码ID | 错误信息 | 1166 | -------- | -------- | 1167 | 1900010 | Failed to read data from the message sequence. | 1168 1169**示例:** 1170 1171 ```ts 1172 import { hilog } from '@kit.PerformanceAnalysisKit'; 1173 import { BusinessError } from '@kit.BasicServicesKit'; 1174 1175 let data = rpc.MessageSequence.create(); 1176 try { 1177 data.writeBoolean(false); 1178 } catch (error) { 1179 let e: BusinessError = error as BusinessError; 1180 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1181 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1182 } 1183 try { 1184 let ret = data.readBoolean(); 1185 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1186 } catch (error) { 1187 let e: BusinessError = error as BusinessError; 1188 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1189 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1190 } 1191 ``` 1192 1193### writeChar 1194 1195writeChar(val: number): void 1196 1197将单个字符值写入MessageSequence实例。 1198 1199**系统能力**:SystemCapability.Communication.IPC.Core 1200 1201**参数:** 1202 1203 | 参数名 | 类型 | 必填 | 说明 | 1204 | ------ | ------ | ---- | -------------------- | 1205 | val | number | 是 | 要写入的单个字符值。 | 1206 1207**错误码:** 1208 1209以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1210 1211 | 错误码ID | 错误信息 | 1212 | -------- | -------- | 1213 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1214 | 1900009 | Failed to write data to the message sequence. | 1215 1216**示例:** 1217 1218 ```ts 1219 import { hilog } from '@kit.PerformanceAnalysisKit'; 1220 import { BusinessError } from '@kit.BasicServicesKit'; 1221 1222 let data = rpc.MessageSequence.create(); 1223 try { 1224 data.writeChar(97); 1225 } catch (error) { 1226 let e: BusinessError = error as BusinessError; 1227 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1228 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1229 } 1230 ``` 1231 1232### readChar 1233 1234readChar(): number 1235 1236从MessageSequence实例中读取单个字符值。 1237 1238**系统能力**:SystemCapability.Communication.IPC.Core 1239 1240**返回值:** 1241 1242 | 类型 | 说明 | 1243 | ------ | ---- | 1244 | number | 返回单个字符值。 | 1245 1246**错误码:** 1247 1248以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1249 1250 | 错误码ID | 错误信息 | 1251 | -------- | -------- | 1252 | 1900010 | Failed to read data from the message sequence. | 1253 1254**示例:** 1255 1256 ```ts 1257 import { hilog } from '@kit.PerformanceAnalysisKit'; 1258 import { BusinessError } from '@kit.BasicServicesKit'; 1259 1260 let data = rpc.MessageSequence.create(); 1261 try { 1262 data.writeChar(97); 1263 } catch (error) { 1264 let e: BusinessError = error as BusinessError; 1265 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1266 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1267 } 1268 try { 1269 let ret = data.readChar(); 1270 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1271 } catch (error) { 1272 let e: BusinessError = error as BusinessError; 1273 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1274 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1275 } 1276 ``` 1277 1278### writeString 1279 1280writeString(val: string): void 1281 1282将字符串值写入MessageSequence实例。 1283 1284**系统能力**:SystemCapability.Communication.IPC.Core 1285 1286**参数:** 1287 1288 | 参数名 | 类型 | 必填 | 说明 | 1289 | ------ | ------ | ---- | ----------------------------------------- | 1290 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 1291 1292**错误码:** 1293 1294以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1295 1296 | 错误码ID | 错误信息 | 1297 | -------- | -------- | 1298 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match; <br/> 3.The string length exceeds 40960 bytes; <br/> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 1299 | 1900009 | Failed to write data to the message sequence. | 1300 1301**示例:** 1302 1303 ```ts 1304 import { hilog } from '@kit.PerformanceAnalysisKit'; 1305 import { BusinessError } from '@kit.BasicServicesKit'; 1306 1307 let data = rpc.MessageSequence.create(); 1308 try { 1309 data.writeString('abc'); 1310 } catch (error) { 1311 let e: BusinessError = error as BusinessError; 1312 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1313 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1314 } 1315 ``` 1316 1317### readString 1318 1319readString(): string 1320 1321从MessageSequence实例读取字符串值。 1322 1323**系统能力**:SystemCapability.Communication.IPC.Core 1324 1325**返回值:** 1326 1327 | 类型 | 说明 | 1328 | ------ | -------------- | 1329 | string | 返回字符串值。 | 1330 1331**错误码:** 1332 1333以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1334 1335 | 错误码ID | 错误信息 | 1336 | -------- | -------- | 1337 | 1900010 | Failed to read data from the message sequence. | 1338 1339**示例:** 1340 1341 ```ts 1342 import { hilog } from '@kit.PerformanceAnalysisKit'; 1343 import { BusinessError } from '@kit.BasicServicesKit'; 1344 1345 let data = rpc.MessageSequence.create(); 1346 try { 1347 data.writeString('abc'); 1348 } catch (error) { 1349 let e: BusinessError = error as BusinessError; 1350 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1351 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1352 } 1353 try { 1354 let ret = data.readString(); 1355 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1356 } catch (error) { 1357 let e: BusinessError = error as BusinessError; 1358 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1359 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1360 } 1361 ``` 1362 1363### writeParcelable 1364 1365writeParcelable(val: Parcelable): void 1366 1367将自定义序列化对象写入MessageSequence实例。 1368 1369**系统能力**:SystemCapability.Communication.IPC.Core 1370 1371**参数:** 1372 1373| 参数名 | 类型 | 必填 | 说明 | 1374| ------ | --------- | ---- | ------ | 1375| val | [Parcelable](#parcelable9) | 是 | 要写入的可序列对象。 | 1376 1377**错误码:** 1378 1379以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1380 1381 | 错误码ID | 错误信息 | 1382 | -------- | -------- | 1383 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1384 | 1900009 | Failed to write data to the message sequence. | 1385 1386**示例:** 1387 1388 ```ts 1389 import { hilog } from '@kit.PerformanceAnalysisKit'; 1390 import { BusinessError } from '@kit.BasicServicesKit'; 1391 1392 class MyParcelable implements rpc.Parcelable { 1393 num: number = 0; 1394 str: string = ''; 1395 constructor( num: number, str: string) { 1396 this.num = num; 1397 this.str = str; 1398 } 1399 marshalling(messageSequence: rpc.MessageSequence): boolean { 1400 messageSequence.writeInt(this.num); 1401 messageSequence.writeString(this.str); 1402 return true; 1403 } 1404 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1405 this.num = messageSequence.readInt(); 1406 this.str = messageSequence.readString(); 1407 return true; 1408 } 1409 } 1410 let parcelable = new MyParcelable(1, "aaa"); 1411 let data = rpc.MessageSequence.create(); 1412 try { 1413 data.writeParcelable(parcelable); 1414 } catch (error) { 1415 let e: BusinessError = error as BusinessError; 1416 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1417 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1418 } 1419 ``` 1420 1421### readParcelable 1422 1423readParcelable(dataIn: Parcelable): void 1424 1425从MessageSequence实例中读取成员变量到指定的对象(dataIn)。 1426 1427**系统能力**:SystemCapability.Communication.IPC.Core 1428 1429**参数:** 1430 1431| 参数名 | 类型 | 必填 | 说明 | 1432| ------ | -------------------------- | ---- | ----------------------------------------- | 1433| dataIn | [Parcelable](#parcelable9) | 是 | 需要从MessageSequence读取成员变量的对象。 | 1434 1435**错误码:** 1436 1437以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1438 1439 | 错误码ID | 错误信息 | 1440 | -------- | -------- | 1441 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect. | 1442 | 1900010 | Failed to read data from the message sequence. | 1443 | 1900012 | Failed to call the JS callback function. | 1444 1445**示例:** 1446 1447 ```ts 1448 import { hilog } from '@kit.PerformanceAnalysisKit'; 1449 import { BusinessError } from '@kit.BasicServicesKit'; 1450 1451 class MyParcelable implements rpc.Parcelable { 1452 num: number = 0; 1453 str: string = ''; 1454 constructor(num: number, str: string) { 1455 this.num = num; 1456 this.str = str; 1457 } 1458 marshalling(messageSequence: rpc.MessageSequence): boolean { 1459 messageSequence.writeInt(this.num); 1460 messageSequence.writeString(this.str); 1461 return true; 1462 } 1463 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1464 this.num = messageSequence.readInt(); 1465 this.str = messageSequence.readString(); 1466 return true; 1467 } 1468 } 1469 let parcelable = new MyParcelable(1, "aaa"); 1470 let data = rpc.MessageSequence.create(); 1471 data.writeParcelable(parcelable); 1472 let ret = new MyParcelable(0, ""); 1473 try { 1474 data.readParcelable(ret); 1475 }catch (error) { 1476 let e: BusinessError = error as BusinessError; 1477 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1478 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1479 } 1480 ``` 1481 1482### writeByteArray 1483 1484writeByteArray(byteArray: number[]): void 1485 1486将字节数组写入MessageSequence实例。 1487 1488**系统能力**:SystemCapability.Communication.IPC.Core 1489 1490**参数:** 1491 1492 | 参数名 | 类型 | 必填 | 说明 | 1493 | --------- | -------- | ---- | ------------------ | 1494 | byteArray | number[] | 是 | 要写入的字节数组。 | 1495 1496**错误码:** 1497 1498以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1499 1500 | 错误码ID | 错误信息 | 1501 | -------- | -------- | 1502 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. <br/> 5.The type of the element in the array is incorrect. | 1503 | 1900009 | Failed to write data to the message sequence. | 1504 1505**示例:** 1506 1507 ```ts 1508 import { hilog } from '@kit.PerformanceAnalysisKit'; 1509 import { BusinessError } from '@kit.BasicServicesKit'; 1510 1511 let data = rpc.MessageSequence.create(); 1512 let ByteArrayVar = [1, 2, 3, 4, 5]; 1513 try { 1514 data.writeByteArray(ByteArrayVar); 1515 } catch (error) { 1516 let e: BusinessError = error as BusinessError; 1517 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1518 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1519 } 1520 ``` 1521 1522### readByteArray 1523 1524readByteArray(dataIn: number[]): void 1525 1526从MessageSequence实例读取字节数组。 1527 1528**系统能力**:SystemCapability.Communication.IPC.Core 1529 1530**参数:** 1531 1532 | 参数名 | 类型 | 必填 | 说明 | 1533 | ------ | -------- | ---- | ------------------ | 1534 | dataIn | number[] | 是 | 要读取的字节数组。 | 1535 1536**错误码:** 1537 1538以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1539 1540 | 错误码ID | 错误信息 | 1541 | -------- | -------- | 1542 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1543 | 1900010 | Failed to read data from the message sequence. | 1544 1545**示例:** 1546 1547 ```ts 1548 import { hilog } from '@kit.PerformanceAnalysisKit'; 1549 import { BusinessError } from '@kit.BasicServicesKit'; 1550 1551 let data = rpc.MessageSequence.create(); 1552 let ByteArrayVar = [1, 2, 3, 4, 5]; 1553 try { 1554 data.writeByteArray(ByteArrayVar); 1555 } catch (error) { 1556 let e: BusinessError = error as BusinessError; 1557 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1558 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1559 } 1560 try { 1561 let array: Array<number> = new Array(5); 1562 data.readByteArray(array); 1563 } catch (error) { 1564 let e: BusinessError = error as BusinessError; 1565 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1566 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1567 } 1568 ``` 1569 1570### readByteArray 1571 1572readByteArray(): number[] 1573 1574从MessageSequence实例中读取字节数组。 1575 1576**系统能力**:SystemCapability.Communication.IPC.Core 1577 1578**返回值:** 1579 1580 | 类型 | 说明 | 1581 | -------- | -------------- | 1582 | number[] | 返回字节数组。 | 1583 1584**错误码:** 1585 1586以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1587 1588 | 错误码ID | 错误信息 | 1589 | -------- | -------- | 1590 | 401 | check param failed | 1591 | 1900010 | Failed to read data from the message sequence. | 1592 1593**示例:** 1594 1595 ```ts 1596 import { hilog } from '@kit.PerformanceAnalysisKit'; 1597 import { BusinessError } from '@kit.BasicServicesKit'; 1598 1599 let data = rpc.MessageSequence.create(); 1600 let byteArrayVar = [1, 2, 3, 4, 5]; 1601 try { 1602 data.writeByteArray(byteArrayVar); 1603 } catch (error) { 1604 let e: BusinessError = error as BusinessError; 1605 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1606 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1607 } 1608 try { 1609 let array = data.readByteArray(); 1610 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1611 } catch (error) { 1612 let e: BusinessError = error as BusinessError; 1613 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1614 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1615 } 1616 ``` 1617 1618### writeShortArray 1619 1620writeShortArray(shortArray: number[]): void 1621 1622将短整数数组写入MessageSequence实例。 1623 1624**系统能力**:SystemCapability.Communication.IPC.Core 1625 1626**参数:** 1627 1628 | 参数名 | 类型 | 必填 | 说明 | 1629 | ---------- | -------- | ---- | -------------------- | 1630 | shortArray | number[] | 是 | 要写入的短整数数组。 | 1631 1632**错误码:** 1633 1634以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1635 1636 | 错误码ID | 错误信息 | 1637 | -------- | -------- | 1638 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 1639 | 1900009 | Failed to write data to the message sequence. | 1640 1641**示例:** 1642 1643 ```ts 1644 import { hilog } from '@kit.PerformanceAnalysisKit'; 1645 import { BusinessError } from '@kit.BasicServicesKit'; 1646 1647 let data = rpc.MessageSequence.create(); 1648 try { 1649 data.writeShortArray([11, 12, 13]); 1650 } catch (error) { 1651 let e: BusinessError = error as BusinessError; 1652 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1653 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1654 } 1655 ``` 1656 1657### readShortArray 1658 1659readShortArray(dataIn: number[]): void 1660 1661从MessageSequence实例中读取短整数数组。 1662 1663**系统能力**:SystemCapability.Communication.IPC.Core 1664 1665**参数:** 1666 1667 | 参数名 | 类型 | 必填 | 说明 | 1668 | ------ | -------- | ---- | -------------------- | 1669 | dataIn | number[] | 是 | 要读取的短整数数组。 | 1670 1671**错误码:** 1672 1673以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1674 1675 | 错误码ID | 错误信息 | 1676 | -------- | -------- | 1677 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1678 | 1900010 | Failed to read data from the message sequence. | 1679 1680**示例:** 1681 1682 ```ts 1683 import { hilog } from '@kit.PerformanceAnalysisKit'; 1684 import { BusinessError } from '@kit.BasicServicesKit'; 1685 1686 let data = rpc.MessageSequence.create(); 1687 try { 1688 data.writeShortArray([11, 12, 13]); 1689 } catch (error) { 1690 let e: BusinessError = error as BusinessError; 1691 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1692 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1693 } 1694 try { 1695 let array: Array<number> = new Array(3); 1696 data.readShortArray(array); 1697 } catch (error) { 1698 let e: BusinessError = error as BusinessError; 1699 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1700 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1701 } 1702 ``` 1703 1704### readShortArray 1705 1706readShortArray(): number[] 1707 1708从MessageSequence实例中读取短整数数组。 1709 1710**系统能力**:SystemCapability.Communication.IPC.Core 1711 1712**返回值:** 1713 1714 | 类型 | 说明 | 1715 | -------- | ---------------- | 1716 | number[] | 返回短整数数组。 | 1717 1718**错误码:** 1719 1720以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1721 1722 | 错误码ID | 错误信息 | 1723 | -------- | -------- | 1724 | 1900010 | Failed to read data from the message sequence. | 1725 1726**示例:** 1727 1728 ```ts 1729 import { hilog } from '@kit.PerformanceAnalysisKit'; 1730 import { BusinessError } from '@kit.BasicServicesKit'; 1731 1732 let data = rpc.MessageSequence.create(); 1733 try { 1734 data.writeShortArray([11, 12, 13]); 1735 } catch (error) { 1736 let e: BusinessError = error as BusinessError; 1737 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1738 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1739 } 1740 try { 1741 let array = data.readShortArray(); 1742 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1743 } catch (error) { 1744 let e: BusinessError = error as BusinessError; 1745 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1746 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1747 } 1748 ``` 1749 1750### writeIntArray 1751 1752writeIntArray(intArray: number[]): void 1753 1754将整数数组写入MessageSequence实例。 1755 1756**系统能力**:SystemCapability.Communication.IPC.Core 1757 1758**参数:** 1759 1760 | 参数名 | 类型 | 必填 | 说明 | 1761 | -------- | -------- | ---- | ------------------ | 1762 | intArray | number[] | 是 | 要写入的整数数组。 | 1763 1764**错误码:** 1765 1766以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1767 1768 | 错误码ID | 错误信息 | 1769 | -------- | -------- | 1770 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 1771 | 1900009 | Failed to write data to the message sequence. | 1772 1773**示例:** 1774 1775 ```ts 1776 import { hilog } from '@kit.PerformanceAnalysisKit'; 1777 import { BusinessError } from '@kit.BasicServicesKit'; 1778 1779 let data = rpc.MessageSequence.create(); 1780 try { 1781 data.writeIntArray([100, 111, 112]); 1782 } catch (error) { 1783 let e: BusinessError = error as BusinessError; 1784 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1785 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1786 } 1787 ``` 1788 1789### readIntArray 1790 1791readIntArray(dataIn: number[]): void 1792 1793从MessageSequence实例中读取整数数组。 1794 1795**系统能力**:SystemCapability.Communication.IPC.Core 1796 1797**参数:** 1798 1799 | 参数名 | 类型 | 必填 | 说明 | 1800 | ------ | -------- | ---- | ------------------ | 1801 | dataIn | number[] | 是 | 要读取的整数数组。 | 1802 1803**错误码:** 1804 1805以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1806 1807 | 错误码ID | 错误信息 | 1808 | -------- | -------- | 1809 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1810 | 1900010 | Failed to read data from the message sequence. | 1811 1812**示例:** 1813 1814 ```ts 1815 import { hilog } from '@kit.PerformanceAnalysisKit'; 1816 import { BusinessError } from '@kit.BasicServicesKit'; 1817 1818 let data = rpc.MessageSequence.create(); 1819 try { 1820 data.writeIntArray([100, 111, 112]); 1821 } catch (error) { 1822 let e: BusinessError = error as BusinessError; 1823 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1824 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1825 } 1826 let array: Array<number> = new Array(3); 1827 try { 1828 data.readIntArray(array); 1829 } catch (error) { 1830 let e: BusinessError = error as BusinessError; 1831 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1832 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1833 } 1834 ``` 1835 1836### readIntArray 1837 1838readIntArray(): number[] 1839 1840从MessageSequence实例中读取整数数组。 1841 1842**系统能力**:SystemCapability.Communication.IPC.Core 1843 1844**返回值:** 1845 1846 | 类型 | 说明 | 1847 | -------- | -------------- | 1848 | number[] | 返回整数数组。 | 1849 1850**错误码:** 1851 1852以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1853 1854 | 错误码ID | 错误信息 | 1855 | -------- | -------- | 1856 | 1900010 | Failed to read data from the message sequence. | 1857 1858**示例:** 1859 1860 ```ts 1861 import { hilog } from '@kit.PerformanceAnalysisKit'; 1862 import { BusinessError } from '@kit.BasicServicesKit'; 1863 1864 let data = rpc.MessageSequence.create(); 1865 try { 1866 data.writeIntArray([100, 111, 112]); 1867 } catch (error) { 1868 let e: BusinessError = error as BusinessError; 1869 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1870 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1871 } 1872 try { 1873 let array = data.readIntArray(); 1874 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1875 } catch (error) { 1876 let e: BusinessError = error as BusinessError; 1877 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1878 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1879 } 1880 ``` 1881 1882### writeLongArray 1883 1884writeLongArray(longArray: number[]): void 1885 1886将长整数数组写入MessageSequence实例。 1887 1888**系统能力**:SystemCapability.Communication.IPC.Core 1889 1890**参数:** 1891 1892 | 参数名 | 类型 | 必填 | 说明 | 1893 | --------- | -------- | ---- | -------------------- | 1894 | longArray | number[] | 是 | 要写入的长整数数组。 | 1895 1896**错误码:** 1897 1898以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1899 1900 | 错误码ID | 错误信息 | 1901 | -------- | -------- | 1902 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 1903 | 1900009 | Failed to write data to the message sequence. | 1904 1905**示例:** 1906 1907 ```ts 1908 import { hilog } from '@kit.PerformanceAnalysisKit'; 1909 import { BusinessError } from '@kit.BasicServicesKit'; 1910 1911 let data = rpc.MessageSequence.create(); 1912 try { 1913 data.writeLongArray([1111, 1112, 1113]); 1914 }catch (error){ 1915 let e: BusinessError = error as BusinessError; 1916 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1917 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1918 } 1919 ``` 1920 1921### readLongArray 1922 1923readLongArray(dataIn: number[]): void 1924 1925从MessageSequence实例读取的长整数数组。 1926 1927**系统能力**:SystemCapability.Communication.IPC.Core 1928 1929**参数:** 1930 1931 | 参数名 | 类型 | 必填 | 说明 | 1932 | ------ | -------- | ---- | -------------------- | 1933 | dataIn | number[] | 是 | 要读取的长整数数组。 | 1934 1935**错误码:** 1936 1937以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1938 1939 | 错误码ID | 错误信息 | 1940 | -------- | -------- | 1941 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 1942 | 1900010 | Failed to read data from the message sequence. | 1943 1944**示例:** 1945 1946 ```ts 1947 import { hilog } from '@kit.PerformanceAnalysisKit'; 1948 import { BusinessError } from '@kit.BasicServicesKit'; 1949 1950 let data = rpc.MessageSequence.create(); 1951 try { 1952 data.writeLongArray([1111, 1112, 1113]); 1953 } catch (error) { 1954 let e: BusinessError = error as BusinessError; 1955 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1956 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1957 } 1958 let array: Array<number> = new Array(3); 1959 try { 1960 data.readLongArray(array); 1961 } catch (error) { 1962 let e: BusinessError = error as BusinessError; 1963 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1964 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1965 } 1966 ``` 1967 1968### readLongArray 1969 1970readLongArray(): number[] 1971 1972从MessageSequence实例中读取所有的长整数数组。 1973 1974**系统能力**:SystemCapability.Communication.IPC.Core 1975 1976**返回值:** 1977 1978 | 类型 | 说明 | 1979 | -------- | ---------------- | 1980 | number[] | 返回长整数数组。 | 1981 1982**错误码:** 1983 1984以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1985 1986 | 错误码ID | 错误信息 | 1987 | -------- | -------- | 1988 | 1900010 | Failed to read data from the message sequence. | 1989 1990**示例:** 1991 1992 ```ts 1993 import { hilog } from '@kit.PerformanceAnalysisKit'; 1994 import { BusinessError } from '@kit.BasicServicesKit'; 1995 1996 let data = rpc.MessageSequence.create(); 1997 try { 1998 data.writeLongArray([1111, 1112, 1113]); 1999 } catch (error) { 2000 let e: BusinessError = error as BusinessError; 2001 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 2002 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 2003 } 2004 try { 2005 let array = data.readLongArray(); 2006 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 2007 } catch (error) { 2008 let e: BusinessError = error as BusinessError; 2009 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 2010 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 2011 } 2012 ``` 2013 2014### writeFloatArray 2015 2016writeFloatArray(floatArray: number[]): void 2017 2018将浮点数组写入MessageSequence实例。 2019 2020**系统能力**:SystemCapability.Communication.IPC.Core 2021 2022**参数:** 2023 2024 | 参数名 | 类型 | 必填 | 说明 | 2025 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2026 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2027 2028**错误码:** 2029 2030以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2031 2032 | 错误码ID | 错误信息 | 2033 | -------- | -------- | 2034 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 2035 | 1900009 | Failed to write data to the message sequence. | 2036 2037**示例:** 2038 2039 ```ts 2040 import { hilog } from '@kit.PerformanceAnalysisKit'; 2041 import { BusinessError } from '@kit.BasicServicesKit'; 2042 2043 let data = rpc.MessageSequence.create(); 2044 try { 2045 data.writeFloatArray([1.2, 1.3, 1.4]); 2046 } catch (error) { 2047 let e: BusinessError = error as BusinessError; 2048 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2049 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2050 } 2051 ``` 2052 2053### readFloatArray 2054 2055readFloatArray(dataIn: number[]): void 2056 2057从MessageSequence实例中读取浮点数组。 2058 2059**系统能力**:SystemCapability.Communication.IPC.Core 2060 2061**参数:** 2062 2063 | 参数名 | 类型 | 必填 | 说明 | 2064 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2065 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2066 2067**错误码:** 2068 2069以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2070 2071 | 错误码ID | 错误信息 | 2072 | -------- | -------- | 2073 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2074 | 1900010 | Failed to read data from the message sequence. | 2075 2076**示例:** 2077 2078 ```ts 2079 import { hilog } from '@kit.PerformanceAnalysisKit'; 2080 import { BusinessError } from '@kit.BasicServicesKit'; 2081 2082 let data = rpc.MessageSequence.create(); 2083 try { 2084 data.writeFloatArray([1.2, 1.3, 1.4]); 2085 }catch (error){ 2086 let e: BusinessError = error as BusinessError; 2087 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2088 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2089 } 2090 let array: Array<number> = new Array(3); 2091 try { 2092 data.readFloatArray(array); 2093 } catch (error) { 2094 let e: BusinessError = error as BusinessError; 2095 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2096 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2097 } 2098 ``` 2099 2100### readFloatArray 2101 2102readFloatArray(): number[] 2103 2104从MessageSequence实例中读取浮点数组。 2105 2106**系统能力**:SystemCapability.Communication.IPC.Core 2107 2108**返回值:** 2109 2110 | 类型 | 说明 | 2111 | -------- | -------------- | 2112 | number[] | 返回浮点数组。 | 2113 2114**错误码:** 2115 2116以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2117 2118 | 错误码ID | 错误信息 | 2119 | -------- | -------- | 2120 | 1900010 | Failed to read data from the message sequence. | 2121 2122**示例:** 2123 2124 ```ts 2125 import { hilog } from '@kit.PerformanceAnalysisKit'; 2126 import { BusinessError } from '@kit.BasicServicesKit'; 2127 2128 let data = rpc.MessageSequence.create(); 2129 try { 2130 data.writeFloatArray([1.2, 1.3, 1.4]); 2131 } catch (error) { 2132 let e: BusinessError = error as BusinessError; 2133 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2134 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2135 } 2136 try { 2137 let array = data.readFloatArray(); 2138 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2139 } catch (error) { 2140 let e: BusinessError = error as BusinessError; 2141 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2142 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2143 } 2144 ``` 2145 2146### writeDoubleArray 2147 2148writeDoubleArray(doubleArray: number[]): void 2149 2150将双精度浮点数组写入MessageSequence实例。 2151 2152**系统能力**:SystemCapability.Communication.IPC.Core 2153 2154**参数:** 2155 2156 | 参数名 | 类型 | 必填 | 说明 | 2157 | ----------- | -------- | ---- | ------------------------ | 2158 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 2159 2160**错误码:** 2161 2162以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2163 2164 | 错误码ID | 错误信息 | 2165 | -------- | -------- | 2166 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array; <br/> 5.The type of the element in the array is incorrect. | 2167 | 1900009 | Failed to write data to the message sequence. | 2168 2169**示例:** 2170 2171 ```ts 2172 import { hilog } from '@kit.PerformanceAnalysisKit'; 2173 import { BusinessError } from '@kit.BasicServicesKit'; 2174 2175 let data = rpc.MessageSequence.create(); 2176 try { 2177 data.writeDoubleArray([11.1, 12.2, 13.3]); 2178 } catch (error) { 2179 let e: BusinessError = error as BusinessError; 2180 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2181 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2182 } 2183 ``` 2184 2185### readDoubleArray 2186 2187readDoubleArray(dataIn: number[]): void 2188 2189从MessageSequence实例中读取双精度浮点数组。 2190 2191**系统能力**:SystemCapability.Communication.IPC.Core 2192 2193**参数:** 2194 2195 | 参数名 | 类型 | 必填 | 说明 | 2196 | ------ | -------- | ---- | ------------------------ | 2197 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 2198 2199**错误码:** 2200 2201以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2202 2203 | 错误码ID | 错误信息 | 2204 | -------- | -------- | 2205 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2206 | 1900010 | Failed to read data from the message sequence. | 2207 2208**示例:** 2209 2210 ```ts 2211 import { hilog } from '@kit.PerformanceAnalysisKit'; 2212 import { BusinessError } from '@kit.BasicServicesKit'; 2213 2214 let data = rpc.MessageSequence.create(); 2215 try { 2216 data.writeDoubleArray([11.1, 12.2, 13.3]); 2217 } catch (error) { 2218 let e: BusinessError = error as BusinessError; 2219 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2220 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2221 } 2222 let array: Array<number> = new Array(3); 2223 try { 2224 data.readDoubleArray(array); 2225 } catch (error) { 2226 let e: BusinessError = error as BusinessError; 2227 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2228 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2229 } 2230 ``` 2231 2232### readDoubleArray 2233 2234readDoubleArray(): number[] 2235 2236从MessageSequence实例读取所有双精度浮点数组。 2237 2238**系统能力**:SystemCapability.Communication.IPC.Core 2239 2240**返回值:** 2241 2242 | 类型 | 说明 | 2243 | -------- | -------------------- | 2244 | number[] | 返回双精度浮点数组。 | 2245 2246**错误码:** 2247 2248以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2249 2250 | 错误码ID | 错误信息 | 2251 | -------- | -------- | 2252 | 1900010 | Failed to read data from the message sequence. | 2253 2254**示例:** 2255 2256 ```ts 2257 import { hilog } from '@kit.PerformanceAnalysisKit'; 2258 import { BusinessError } from '@kit.BasicServicesKit'; 2259 2260 let data = rpc.MessageSequence.create(); 2261 try { 2262 data.writeDoubleArray([11.1, 12.2, 13.3]); 2263 } catch (error) { 2264 let e: BusinessError = error as BusinessError; 2265 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2266 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2267 } 2268 try { 2269 let array = data.readDoubleArray(); 2270 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2271 } catch (error) { 2272 let e: BusinessError = error as BusinessError; 2273 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2274 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2275 } 2276 ``` 2277 2278### writeBooleanArray 2279 2280writeBooleanArray(booleanArray: boolean[]): void 2281 2282将布尔数组写入MessageSequence实例。 2283 2284**系统能力**:SystemCapability.Communication.IPC.Core 2285 2286**参数:** 2287 2288 | 参数名 | 类型 | 必填 | 说明 | 2289 | ------------ | --------- | ---- | ------------------ | 2290 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 2291 2292**错误码:** 2293 2294以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2295 2296 | 错误码ID | 错误信息 | 2297 | -------- | -------- | 2298 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. | 2299 | 1900009 | Failed to write data to the message sequence. | 2300 2301**示例:** 2302 2303 ```ts 2304 import { hilog } from '@kit.PerformanceAnalysisKit'; 2305 import { BusinessError } from '@kit.BasicServicesKit'; 2306 2307 let data = rpc.MessageSequence.create(); 2308 try { 2309 data.writeBooleanArray([false, true, false]); 2310 } catch (error) { 2311 let e: BusinessError = error as BusinessError; 2312 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2313 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2314 } 2315 ``` 2316 2317### readBooleanArray 2318 2319readBooleanArray(dataIn: boolean[]): void 2320 2321从MessageSequence实例中读取布尔数组。 2322 2323**系统能力**:SystemCapability.Communication.IPC.Core 2324 2325**参数:** 2326 2327 | 参数名 | 类型 | 必填 | 说明 | 2328 | ------ | --------- | ---- | ------------------ | 2329 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 2330 2331**错误码:** 2332 2333以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2334 2335 | 错误码ID | 错误信息 | 2336 | -------- | -------- | 2337 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2338 | 1900010 | Failed to read data from the message sequence. | 2339 2340**示例:** 2341 2342 ```ts 2343 import { hilog } from '@kit.PerformanceAnalysisKit'; 2344 import { BusinessError } from '@kit.BasicServicesKit'; 2345 2346 let data = rpc.MessageSequence.create(); 2347 try { 2348 data.writeBooleanArray([false, true, false]); 2349 } catch (error) { 2350 let e: BusinessError = error as BusinessError; 2351 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2352 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2353 } 2354 let array: Array<boolean> = new Array(3); 2355 try { 2356 data.readBooleanArray(array); 2357 } catch (error) { 2358 let e: BusinessError = error as BusinessError; 2359 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2360 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2361 } 2362 ``` 2363 2364### readBooleanArray 2365 2366readBooleanArray(): boolean[] 2367 2368从MessageSequence实例中读取所有布尔数组。 2369 2370**系统能力**:SystemCapability.Communication.IPC.Core 2371 2372**返回值:** 2373 2374 | 类型 | 说明 | 2375 | --------- | -------------- | 2376 | boolean[] | 返回布尔数组。 | 2377 2378**错误码:** 2379 2380以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2381 2382 | 错误码ID | 错误信息 | 2383 | -------- | -------- | 2384 | 1900010 | Failed to read data from the message sequence. | 2385 2386**示例:** 2387 2388 ```ts 2389 import { hilog } from '@kit.PerformanceAnalysisKit'; 2390 import { BusinessError } from '@kit.BasicServicesKit'; 2391 2392 let data = rpc.MessageSequence.create(); 2393 try { 2394 data.writeBooleanArray([false, true, false]); 2395 } catch (error) { 2396 let e: BusinessError = error as BusinessError; 2397 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2398 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2399 } 2400 try { 2401 let array = data.readBooleanArray(); 2402 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2403 } catch (error) { 2404 let e: BusinessError = error as BusinessError; 2405 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2406 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2407 } 2408 ``` 2409 2410### writeCharArray 2411 2412writeCharArray(charArray: number[]): void 2413 2414将单个字符数组写入MessageSequence实例。 2415 2416**系统能力**:SystemCapability.Communication.IPC.Core 2417 2418**参数:** 2419 2420 | 参数名 | 类型 | 必填 | 说明 | 2421 | --------- | -------- | ---- | ---------------------- | 2422 | charArray | number[] | 是 | 要写入的单个字符数组。 | 2423 2424**错误码:** 2425 2426以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2427 2428 | 错误码ID | 错误信息 | 2429 | -------- | -------- | 2430 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. | 2431 | 1900009 | Failed to write data to the message sequence. | 2432 2433**示例:** 2434 2435 ```ts 2436 import { hilog } from '@kit.PerformanceAnalysisKit'; 2437 import { BusinessError } from '@kit.BasicServicesKit'; 2438 2439 let data = rpc.MessageSequence.create(); 2440 try { 2441 data.writeCharArray([97, 98, 88]); 2442 } catch (error) { 2443 let e: BusinessError = error as BusinessError; 2444 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2445 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2446 } 2447 ``` 2448 2449### readCharArray 2450 2451readCharArray(dataIn: number[]): void 2452 2453从MessageSequence实例中读取单个字符数组。 2454 2455**系统能力**:SystemCapability.Communication.IPC.Core 2456 2457**参数:** 2458 2459 | 参数名 | 类型 | 必填 | 说明 | 2460 | ------ | -------- | ---- | ---------------------- | 2461 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 2462 2463**错误码:** 2464 2465以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2466 2467 | 错误码ID | 错误信息 | 2468 | -------- | -------- | 2469 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2470 | 1900010 | Failed to read data from the message sequence. | 2471 2472**示例:** 2473 2474 ```ts 2475 import { hilog } from '@kit.PerformanceAnalysisKit'; 2476 import { BusinessError } from '@kit.BasicServicesKit'; 2477 2478 let data = rpc.MessageSequence.create(); 2479 try { 2480 data.writeCharArray([97, 98, 88]); 2481 } catch (error) { 2482 let e: BusinessError = error as BusinessError; 2483 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2484 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2485 } 2486 let array: Array<number> = new Array(3); 2487 try { 2488 data.readCharArray(array); 2489 } catch (error) { 2490 let e: BusinessError = error as BusinessError; 2491 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2492 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2493 } 2494 ``` 2495 2496### readCharArray 2497 2498readCharArray(): number[] 2499 2500从MessageSequence实例读取单个字符数组。 2501 2502**系统能力**:SystemCapability.Communication.IPC.Core 2503 2504**返回值:** 2505 2506 | 类型 | 说明 | 2507 | -------- | ------------------ | 2508 | number[] | 返回单个字符数组。 | 2509 2510**错误码:** 2511 2512以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2513 2514 | 错误码ID | 错误信息 | 2515 | -------- | -------- | 2516 | 1900010 | Failed to read data from the message sequence. | 2517 2518**示例:** 2519 2520 ```ts 2521 import { hilog } from '@kit.PerformanceAnalysisKit'; 2522 import { BusinessError } from '@kit.BasicServicesKit'; 2523 2524 let data = rpc.MessageSequence.create(); 2525 try { 2526 data.writeCharArray([97, 98, 88]); 2527 } catch (error) { 2528 let e: BusinessError = error as BusinessError; 2529 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2530 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2531 } 2532 try { 2533 let array = data.readCharArray(); 2534 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2535 } catch (error) { 2536 let e: BusinessError = error as BusinessError; 2537 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2538 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2539 } 2540 ``` 2541 2542### writeStringArray 2543 2544writeStringArray(stringArray: string[]): void 2545 2546将字符串数组写入MessageSequence实例。 2547 2548**系统能力**:SystemCapability.Communication.IPC.Core 2549 2550**参数:** 2551 2552 | 参数名 | 类型 | 必填 | 说明 | 2553 | ----------- | -------- | ---- | ------------------------------------------------------- | 2554 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 2555 2556**错误码:** 2557 2558以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2559 2560 | 错误码ID | 错误信息 | 2561 | -------- | -------- | 2562 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The string length exceeds 40960 bytes; <br/> 5.The number of bytes copied to the buffer is different from the length of the obtained string. | 2563 | 1900009 | Failed to write data to the message sequence. | 2564 2565**示例:** 2566 2567 ```ts 2568 import { hilog } from '@kit.PerformanceAnalysisKit'; 2569 import { BusinessError } from '@kit.BasicServicesKit'; 2570 2571 let data = rpc.MessageSequence.create(); 2572 try { 2573 data.writeStringArray(["abc", "def"]); 2574 } catch (error) { 2575 let e: BusinessError = error as BusinessError; 2576 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2577 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2578 } 2579 ``` 2580 2581### readStringArray 2582 2583readStringArray(dataIn: string[]): void 2584 2585从MessageSequence实例读取字符串数组。 2586 2587**系统能力**:SystemCapability.Communication.IPC.Core 2588 2589**参数:** 2590 2591 | 参数名 | 类型 | 必填 | 说明 | 2592 | ------ | -------- | ---- | -------------------- | 2593 | dataIn | string[] | 是 | 要读取的字符串数组。 | 2594 2595**错误码:** 2596 2597以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2598 2599 | 错误码ID | 错误信息 | 2600 | -------- | -------- | 2601 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match. | 2602 | 1900010 | Failed to read data from the message sequence. | 2603 2604**示例:** 2605 2606 ```ts 2607 import { hilog } from '@kit.PerformanceAnalysisKit'; 2608 import { BusinessError } from '@kit.BasicServicesKit'; 2609 2610 let data = rpc.MessageSequence.create(); 2611 try { 2612 data.writeStringArray(["abc", "def"]); 2613 } catch (error) { 2614 let e: BusinessError = error as BusinessError; 2615 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2616 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2617 } 2618 let array: Array<string> = new Array(2); 2619 try { 2620 data.readStringArray(array); 2621 } catch (error) { 2622 let e: BusinessError = error as BusinessError; 2623 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2624 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2625 } 2626 ``` 2627 2628### readStringArray 2629 2630readStringArray(): string[] 2631 2632从MessageSequence实例读取字符串数组。 2633 2634**系统能力**:SystemCapability.Communication.IPC.Core 2635 2636**返回值:** 2637 2638 | 类型 | 说明 | 2639 | -------- | ---------------- | 2640 | string[] | 返回字符串数组。 | 2641 2642**错误码:** 2643 2644以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2645 2646 | 错误码ID | 错误信息 | 2647 | -------- | -------- | 2648 | 1900010 | Failed to read data from the message sequence. | 2649 2650**示例:** 2651 2652 ```ts 2653 import { hilog } from '@kit.PerformanceAnalysisKit'; 2654 import { BusinessError } from '@kit.BasicServicesKit'; 2655 2656 let data = rpc.MessageSequence.create(); 2657 try { 2658 data.writeStringArray(["abc", "def"]); 2659 } catch (error) { 2660 let e: BusinessError = error as BusinessError; 2661 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2662 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2663 } 2664 try { 2665 let array = data.readStringArray(); 2666 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2667 } catch (error) { 2668 let e: BusinessError = error as BusinessError; 2669 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2670 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2671 } 2672 ``` 2673 2674### writeNoException 2675 2676writeNoException(): void 2677 2678向MessageSequence写入“指示未发生异常”的信息。 2679 2680**系统能力**:SystemCapability.Communication.IPC.Core 2681 2682**错误码:** 2683 2684以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2685 2686 | 错误码ID | 错误信息 | 2687 | -------- | -------- | 2688 | 1900009 | Failed to write data to the message sequence. | 2689 2690**示例:** 2691 2692 ```ts 2693 import { hilog } from '@kit.PerformanceAnalysisKit'; 2694 import { BusinessError } from '@kit.BasicServicesKit'; 2695 2696 class TestRemoteObject extends rpc.RemoteObject { 2697 constructor(descriptor: string) { 2698 super(descriptor); 2699 } 2700 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2701 if (code === 1) { 2702 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2703 try { 2704 reply.writeNoException(); 2705 } catch (error) { 2706 let e: BusinessError = error as BusinessError; 2707 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2708 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2709 } 2710 return true; 2711 } else { 2712 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2713 return false; 2714 } 2715 } 2716 } 2717 ``` 2718 2719### readException 2720 2721readException(): void 2722 2723从MessageSequence中读取异常。 2724 2725**系统能力**:SystemCapability.Communication.IPC.Core 2726 2727**错误码:** 2728 2729以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2730 2731 | 错误码ID | 错误信息 | 2732 | -------- | -------- | 2733 | 1900010 | Failed to read data from the message sequence. | 2734 2735**示例:** 2736 2737>**说明:** 2738> 2739>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 2740 2741 ```ts 2742 // FA模型需要从@kit.AbilityKit导入featureAbility 2743 // import { featureAbility } from '@kit.AbilityKit'; 2744 import { Want, common } from '@kit.AbilityKit'; 2745 import { hilog } from '@kit.PerformanceAnalysisKit'; 2746 2747 let proxy: rpc.IRemoteObject | undefined; 2748 let connect: common.ConnectOptions = { 2749 onConnect: (elementName, remoteProxy) => { 2750 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2751 proxy = remoteProxy; 2752 }, 2753 onDisconnect: (elementName) => { 2754 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2755 }, 2756 onFailed: () => { 2757 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2758 } 2759 }; 2760 let want: Want = { 2761 bundleName: "com.ohos.server", 2762 abilityName: "com.ohos.server.EntryAbility", 2763 }; 2764 2765 // FA模型使用此方法连接服务 2766 // FA.connectAbility(want,connect); 2767 2768 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2769 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 2770 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2771 let connectionId = context.connectServiceExtensionAbility(want, connect); 2772 ``` 2773 2774 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 2775 2776 ```ts 2777 import { BusinessError } from '@kit.BasicServicesKit'; 2778 import { hilog } from '@kit.PerformanceAnalysisKit'; 2779 2780 let option = new rpc.MessageOption(); 2781 let data = rpc.MessageSequence.create(); 2782 let reply = rpc.MessageSequence.create(); 2783 data.writeNoException(); 2784 data.writeInt(6); 2785 if (proxy != undefined) { 2786 proxy.sendMessageRequest(1, data, reply, option) 2787 .then((result: rpc.RequestResult) => { 2788 if (result.errCode === 0) { 2789 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2790 try { 2791 result.reply.readException(); 2792 } catch (error) { 2793 let e: BusinessError = error as BusinessError; 2794 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2795 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2796 } 2797 let num = result.reply.readInt(); 2798 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2799 } else { 2800 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2801 } 2802 }).catch((e: Error) => { 2803 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2804 }).finally (() => { 2805 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2806 data.reclaim(); 2807 reply.reclaim(); 2808 }); 2809 } 2810 ``` 2811 2812### writeParcelableArray 2813 2814writeParcelableArray(parcelableArray: Parcelable[]): void 2815 2816将可序列化对象数组写入MessageSequence实例。 2817 2818**系统能力**:SystemCapability.Communication.IPC.Core 2819 2820**参数:** 2821 2822| 参数名 | 类型 | 必填 | 说明 | 2823| --------------- | ------------ | ---- | -------------------------- | 2824| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要写入的可序列化对象数组。 | 2825 2826**错误码:** 2827 2828以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2829 2830 | 错误码ID | 错误信息 | 2831 | -------- | -------- | 2832 | 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. | 2833 | 1900009 | Failed to write data to the message sequence. | 2834 2835**示例:** 2836 2837 ```ts 2838 import { hilog } from '@kit.PerformanceAnalysisKit'; 2839 import { BusinessError } from '@kit.BasicServicesKit'; 2840 2841 class MyParcelable implements rpc.Parcelable { 2842 num: number = 0; 2843 str: string = ''; 2844 constructor(num: number, str: string) { 2845 this.num = num; 2846 this.str = str; 2847 } 2848 marshalling(messageSequence: rpc.MessageSequence): boolean { 2849 messageSequence.writeInt(this.num); 2850 messageSequence.writeString(this.str); 2851 return true; 2852 } 2853 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2854 this.num = messageSequence.readInt(); 2855 this.str = messageSequence.readString(); 2856 return true; 2857 } 2858 } 2859 let parcelable = new MyParcelable(1, "aaa"); 2860 let parcelable2 = new MyParcelable(2, "bbb"); 2861 let parcelable3 = new MyParcelable(3, "ccc"); 2862 let a = [parcelable, parcelable2, parcelable3]; 2863 let data = rpc.MessageSequence.create(); 2864 try { 2865 data.writeParcelableArray(a); 2866 } catch (error) { 2867 let e: BusinessError = error as BusinessError; 2868 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2869 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2870 } 2871 ``` 2872 2873### readParcelableArray 2874 2875readParcelableArray(parcelableArray: Parcelable[]): void 2876 2877从MessageSequence实例读取可序列化对象数组。 2878 2879**系统能力**:SystemCapability.Communication.IPC.Core 2880 2881**参数:** 2882 2883| 参数名 | 类型 | 必填 | 说明 | 2884| --------------- | ------------ | ---- | -------------------------- | 2885| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要读取的可序列化对象数组。 | 2886 2887**错误码:** 2888 2889以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2890 2891 | 错误码ID | 错误信息 | 2892 | -------- | -------- | 2893 | 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. | 2894 | 1900010 | Failed to read data from the message sequence. | 2895 | 1900012 | Failed to call the JS callback function. | 2896 2897**示例:** 2898 2899 ```ts 2900 import { hilog } from '@kit.PerformanceAnalysisKit'; 2901 import { BusinessError } from '@kit.BasicServicesKit'; 2902 2903 class MyParcelable implements rpc.Parcelable { 2904 num: number = 0; 2905 str: string = ''; 2906 constructor(num: number, str: string) { 2907 this.num = num; 2908 this.str = str; 2909 } 2910 marshalling(messageSequence: rpc.MessageSequence): boolean { 2911 messageSequence.writeInt(this.num); 2912 messageSequence.writeString(this.str); 2913 return true; 2914 } 2915 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2916 this.num = messageSequence.readInt(); 2917 this.str = messageSequence.readString(); 2918 return true; 2919 } 2920 } 2921 let parcelable = new MyParcelable(1, "aaa"); 2922 let parcelable2 = new MyParcelable(2, "bbb"); 2923 let parcelable3 = new MyParcelable(3, "ccc"); 2924 let a = [parcelable, parcelable2, parcelable3]; 2925 let data = rpc.MessageSequence.create(); 2926 data.writeParcelableArray(a); 2927 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2928 try { 2929 data.readParcelableArray(b); 2930 } catch (error) { 2931 let e: BusinessError = error as BusinessError; 2932 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2933 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2934 } 2935 ``` 2936 2937### writeRemoteObjectArray 2938 2939writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2940 2941将IRemoteObject对象数组写入MessageSequence。 2942 2943**系统能力**:SystemCapability.Communication.IPC.Core 2944 2945**参数:** 2946 2947| 参数名 | 类型 | 必填 | 说明 | 2948| ----------- | --------------- | ---- | ---------------------------------------------- | 2949| objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageSequence的IRemoteObject对象数组。 | 2950 2951**错误码:** 2952 2953以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2954 2955 | 错误码ID | 错误信息 | 2956 | -------- | -------- | 2957 | 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. | 2958 | 1900009 | Failed to write data to the message sequence. | 2959 2960**示例:** 2961 2962 ```ts 2963 import { hilog } from '@kit.PerformanceAnalysisKit'; 2964 import { BusinessError } from '@kit.BasicServicesKit'; 2965 2966 class TestRemoteObject extends rpc.RemoteObject { 2967 constructor(descriptor: string) { 2968 super(descriptor); 2969 this.modifyLocalInterface(this, descriptor); 2970 } 2971 2972 asObject(): rpc.IRemoteObject { 2973 return this; 2974 } 2975 } 2976 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2977 let data = rpc.MessageSequence.create(); 2978 try { 2979 data.writeRemoteObjectArray(a); 2980 } catch (error) { 2981 let e: BusinessError = error as BusinessError; 2982 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2983 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2984 } 2985 ``` 2986 2987### readRemoteObjectArray 2988 2989readRemoteObjectArray(objects: IRemoteObject[]): void 2990 2991从MessageSequence读取IRemoteObject对象数组。 2992 2993**系统能力**:SystemCapability.Communication.IPC.Core 2994 2995**参数:** 2996 2997| 参数名 | 类型 | 必填 | 说明 | 2998| ------- | --------------- | ---- | ---------------------------------------------- | 2999| objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageSequence读取的IRemoteObject对象数组。 | 3000 3001**错误码:** 3002 3003以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3004 3005 | 错误码ID | 错误信息 | 3006 | -------- | -------- | 3007 | 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. | 3008 | 1900010 | Failed to read data from the message sequence. | 3009 3010**示例:** 3011 3012 ```ts 3013 import { hilog } from '@kit.PerformanceAnalysisKit'; 3014 import { BusinessError } from '@kit.BasicServicesKit'; 3015 3016 class TestRemoteObject extends rpc.RemoteObject { 3017 constructor(descriptor: string) { 3018 super(descriptor); 3019 this.modifyLocalInterface(this, descriptor); 3020 } 3021 3022 asObject(): rpc.IRemoteObject { 3023 return this; 3024 } 3025 } 3026 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3027 let data = rpc.MessageSequence.create(); 3028 data.writeRemoteObjectArray(a); 3029 let b: Array<rpc.IRemoteObject> = new Array(3); 3030 try { 3031 data.readRemoteObjectArray(b); 3032 } catch (error) { 3033 let e: BusinessError = error as BusinessError; 3034 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3035 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3036 } 3037 ``` 3038 3039### readRemoteObjectArray 3040 3041readRemoteObjectArray(): IRemoteObject[] 3042 3043从MessageSequence读取IRemoteObject对象数组。 3044 3045**系统能力**:SystemCapability.Communication.IPC.Core 3046 3047**返回值:** 3048 3049| 类型 | 说明 | 3050| --------------- | --------------------------- | 3051| [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 3052 3053**错误码:** 3054 3055以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3056 3057 | 错误码ID | 错误信息 | 3058 | -------- | -------- | 3059 | 1900010 | Failed to read data from the message sequence. | 3060 3061**示例:** 3062 3063 ```ts 3064 import { hilog } from '@kit.PerformanceAnalysisKit'; 3065 import { BusinessError } from '@kit.BasicServicesKit'; 3066 3067 class TestRemoteObject extends rpc.RemoteObject { 3068 constructor(descriptor: string) { 3069 super(descriptor); 3070 this.modifyLocalInterface(this, descriptor); 3071 } 3072 3073 asObject(): rpc.IRemoteObject { 3074 return this; 3075 } 3076 } 3077 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3078 let data = rpc.MessageSequence.create(); 3079 data.writeRemoteObjectArray(a); 3080 try { 3081 let b = data.readRemoteObjectArray(); 3082 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 3083 } catch (error) { 3084 let e: BusinessError = error as BusinessError; 3085 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3086 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3087 } 3088 ``` 3089 3090### closeFileDescriptor 3091 3092static closeFileDescriptor(fd: number): void 3093 3094静态方法,关闭给定的文件描述符。 3095 3096**系统能力**:SystemCapability.Communication.IPC.Core 3097 3098**参数:** 3099 3100 | 参数名 | 类型 | 必填 | 说明 | 3101 | ------ | ------ | ---- | -------------------- | 3102 | fd | number | 是 | 要关闭的文件描述符。 | 3103 3104**错误码:** 3105 3106以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3107 3108 | 错误码ID | 错误信息 | 3109 | -------- | -------- | 3110 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3111 3112**示例:** 3113 3114 ```ts 3115 import { fileIo } from '@kit.CoreFileKit'; 3116 import { hilog } from '@kit.PerformanceAnalysisKit'; 3117 import { BusinessError } from '@kit.BasicServicesKit'; 3118 3119 let filePath = "path/to/file"; 3120 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3121 try { 3122 rpc.MessageSequence.closeFileDescriptor(file.fd); 3123 } catch (error) { 3124 let e: BusinessError = error as BusinessError; 3125 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3126 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3127 } 3128 ``` 3129 3130### dupFileDescriptor 3131 3132static dupFileDescriptor(fd: number): number 3133 3134静态方法,复制给定的文件描述符。 3135 3136**系统能力**:SystemCapability.Communication.IPC.Core 3137 3138**参数:** 3139 3140 | 参数名 | 类型 | 必填 | 说明 | 3141 | ------ | ------ | ---- | ------------------------ | 3142 | fd | number | 是 | 表示已存在的文件描述符。 | 3143 3144**返回值:** 3145 3146 | 类型 | 说明 | 3147 | ------ | -------------------- | 3148 | number | 返回新的文件描述符。 | 3149 3150**错误码:** 3151 3152以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3153 3154 | 错误码ID | 错误信息 | 3155 | -------- | -------- | 3156 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3157 | 1900013 | Failed to call dup. | 3158 3159**示例:** 3160 3161 ```ts 3162 import { fileIo } from '@kit.CoreFileKit'; 3163 import { hilog } from '@kit.PerformanceAnalysisKit'; 3164 import { BusinessError } from '@kit.BasicServicesKit'; 3165 3166 let filePath = "path/to/file"; 3167 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3168 try { 3169 rpc.MessageSequence.dupFileDescriptor(file.fd); 3170 } catch (error) { 3171 let e: BusinessError = error as BusinessError; 3172 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3173 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3174 } 3175 ``` 3176 3177### containFileDescriptors 3178 3179containFileDescriptors(): boolean 3180 3181检查此MessageSequence对象是否包含文件描述符。 3182 3183**系统能力**:SystemCapability.Communication.IPC.Core 3184 3185**返回值:** 3186 3187 | 类型 | 说明 | 3188 | ------- | -------------------------------------------------------------------- | 3189 | boolean | true:包含文件描述符,false:不包含文件描述符。| 3190 3191**示例:** 3192 3193 ```ts 3194 import { fileIo } from '@kit.CoreFileKit'; 3195 import { hilog } from '@kit.PerformanceAnalysisKit'; 3196 import { BusinessError } from '@kit.BasicServicesKit'; 3197 3198 let sequence = new rpc.MessageSequence(); 3199 let filePath = "path/to/file"; 3200 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3201 try { 3202 sequence.writeFileDescriptor(file.fd); 3203 } catch (error) { 3204 let e: BusinessError = error as BusinessError; 3205 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3206 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3207 } 3208 try { 3209 let containFD = sequence.containFileDescriptors(); 3210 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3211 } catch (error) { 3212 let e: BusinessError = error as BusinessError; 3213 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3214 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3215 } 3216 ``` 3217 3218### writeFileDescriptor 3219 3220writeFileDescriptor(fd: number): void 3221 3222写入文件描述符到MessageSequence。 3223 3224**系统能力**:SystemCapability.Communication.IPC.Core 3225 3226**参数:** 3227 3228 | 参数名 | 类型 | 必填 | 说明 | 3229 | ------ | ------ | ---- | ------------ | 3230 | fd | number | 是 | 文件描述符。 | 3231 3232**错误码:** 3233 3234以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3235 3236 | 错误码ID | 错误信息 | 3237 | -------- | -------- | 3238 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3239 | 1900009 | Failed to write data to the message sequence. | 3240 3241**示例:** 3242 3243 ```ts 3244 import { fileIo } from '@kit.CoreFileKit'; 3245 import { hilog } from '@kit.PerformanceAnalysisKit'; 3246 import { BusinessError } from '@kit.BasicServicesKit'; 3247 3248 let sequence = new rpc.MessageSequence(); 3249 let filePath = "path/to/file"; 3250 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3251 try { 3252 sequence.writeFileDescriptor(file.fd); 3253 } catch (error) { 3254 let e: BusinessError = error as BusinessError; 3255 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3256 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3257 } 3258 ``` 3259 3260### readFileDescriptor 3261 3262readFileDescriptor(): number 3263 3264从MessageSequence中读取文件描述符。 3265 3266**系统能力**:SystemCapability.Communication.IPC.Core 3267 3268**返回值:** 3269 3270 | 类型 | 说明 | 3271 | ------ | ---------------- | 3272 | number | 返回文件描述符。 | 3273 3274**错误码:** 3275 3276以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3277 3278 | 错误码ID | 错误信息 | 3279 | -------- | -------- | 3280 | 1900010 | Failed to read data from the message sequence. | 3281 3282**示例:** 3283 3284 ```ts 3285 import { fileIo } from '@kit.CoreFileKit'; 3286 import { hilog } from '@kit.PerformanceAnalysisKit'; 3287 import { BusinessError } from '@kit.BasicServicesKit'; 3288 3289 let sequence = new rpc.MessageSequence(); 3290 let filePath = "path/to/file"; 3291 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3292 try { 3293 sequence.writeFileDescriptor(file.fd); 3294 } catch (error) { 3295 let e: BusinessError = error as BusinessError; 3296 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3297 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3298 } 3299 try { 3300 let readFD = sequence.readFileDescriptor(); 3301 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3302 } catch (error) { 3303 let e: BusinessError = error as BusinessError; 3304 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3305 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3306 } 3307 ``` 3308 3309### writeAshmem 3310 3311writeAshmem(ashmem: Ashmem): void 3312 3313将指定的匿名共享对象写入此MessageSequence。 3314 3315**系统能力**:SystemCapability.Communication.IPC.Core 3316 3317**参数:** 3318 3319| 参数名 | 类型 | 必填 | 说明 | 3320| ------ | ------ | ---- | ------------------------------------- | 3321| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageSequence的匿名共享对象。 | 3322 3323**错误码:** 3324 3325以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3326 3327 | 错误码ID | 错误信息 | 3328 | -------- | ------- | 3329 | 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. | 3330 | 1900003 | Failed to write data to the shared memory. | 3331 3332**示例:** 3333 3334 ```ts 3335 import { hilog } from '@kit.PerformanceAnalysisKit'; 3336 import { BusinessError } from '@kit.BasicServicesKit'; 3337 3338 let sequence = new rpc.MessageSequence(); 3339 let ashmem: rpc.Ashmem | undefined = undefined; 3340 try { 3341 ashmem = rpc.Ashmem.create("ashmem", 1024); 3342 try { 3343 sequence.writeAshmem(ashmem); 3344 } catch (error) { 3345 let e: BusinessError = error as BusinessError; 3346 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3347 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3348 } 3349 } catch (error) { 3350 let e: BusinessError = error as BusinessError; 3351 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3352 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3353 } 3354 ``` 3355 3356### readAshmem 3357 3358readAshmem(): Ashmem 3359 3360从MessageSequence读取匿名共享对象。 3361 3362**系统能力**:SystemCapability.Communication.IPC.Core 3363 3364**返回值:** 3365 3366| 类型 | 说明 | 3367| ------ | ------------------ | 3368| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 3369 3370**错误码:** 3371 3372以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3373 3374 | 错误码ID | 错误信息 | 3375 | -------- | -------- | 3376 | 401 | check param failed | 3377 | 1900004 | Failed to read data from the shared memory. | 3378 3379**示例:** 3380 3381 ```ts 3382 import { hilog } from '@kit.PerformanceAnalysisKit'; 3383 import { BusinessError } from '@kit.BasicServicesKit'; 3384 3385 let sequence = new rpc.MessageSequence(); 3386 let ashmem: rpc.Ashmem | undefined = undefined; 3387 try { 3388 ashmem = rpc.Ashmem.create("ashmem", 1024); 3389 try { 3390 sequence.writeAshmem(ashmem); 3391 } catch (error) { 3392 let e: BusinessError = error as BusinessError; 3393 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3394 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3395 } 3396 } catch (error) { 3397 let e: BusinessError = error as BusinessError; 3398 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3399 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3400 } 3401 try { 3402 sequence.readAshmem(); 3403 } catch (error) { 3404 let e: BusinessError = error as BusinessError; 3405 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3406 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3407 } 3408 ``` 3409 3410### getRawDataCapacity 3411 3412getRawDataCapacity(): number 3413 3414获取MessageSequence可以容纳的最大原始数据量。 3415 3416**系统能力**:SystemCapability.Communication.IPC.Core 3417 3418**返回值:** 3419 3420 | 类型 | 说明 | 3421 | ------ | ------------------------------------------------------------ | 3422 | number | 返回MessageSequence可以容纳的最大原始数据量,即128MB。 | 3423 3424**示例:** 3425 3426 ```ts 3427 import { hilog } from '@kit.PerformanceAnalysisKit'; 3428 3429 let sequence = new rpc.MessageSequence(); 3430 let result = sequence.getRawDataCapacity(); 3431 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3432 ``` 3433 3434### writeRawData<sup>(deprecated)</sup> 3435 3436writeRawData(rawData: number[], size: number): void 3437 3438将原始数据写入MessageSequence对象。 3439 3440> **说明:** 3441> 3442> 从API version 11 开始废弃,建议使用[writeRawDataBuffer](#writerawdatabuffer11)替代。 3443> 3444> 该接口是一次性接口,不允许在一次parcel通信中多次调用该接口。 3445> 该接口在传输数据时,当数据量较大时(超过32KB),会使用共享内存传输数据,此时需注意selinux配置。 3446 3447**系统能力**:SystemCapability.Communication.IPC.Core 3448 3449**参数:** 3450 3451 | 参数名 | 类型 | 必填 | 说明 | 3452 | ------- | -------- | ---- | ---------------------------------- | 3453 | rawData | number[] | 是 | 要写入的原始数据,大小不能超过128MB。 | 3454 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3455 3456**错误码:** 3457 3458以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3459 3460 | 错误码ID | 错误信息 | 3461 | -------- | -------- | 3462 | 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. | 3463 | 1900009 | Failed to write data to the message sequence. | 3464 3465**示例:** 3466 3467 ```ts 3468 import { hilog } from '@kit.PerformanceAnalysisKit'; 3469 import { BusinessError } from '@kit.BasicServicesKit'; 3470 3471 let sequence = new rpc.MessageSequence(); 3472 let arr = [1, 2, 3, 4, 5]; 3473 try { 3474 sequence.writeRawData(arr, arr.length); 3475 } catch (error) { 3476 let e: BusinessError = error as BusinessError; 3477 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3478 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3479 } 3480 ``` 3481 3482### writeRawDataBuffer<sup>11+</sup> 3483 3484writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3485 3486将原始数据写入MessageSequence对象。 3487 3488> **说明:** 3489> 3490> 该接口是一次性接口,不允许在一次parcel通信中多次调用该接口。 3491> 该接口在传输数据时,当数据量较大时(超过32KB),会使用共享内存传输数据,此时需注意selinux配置。 3492 3493**系统能力**:SystemCapability.Communication.IPC.Core 3494 3495**参数:** 3496 3497 | 参数名 | 类型 | 必填 | 说明 | 3498 | ------- | ----------- | ---- | ------------------------------------ | 3499 | rawData | ArrayBuffer | 是 | 要写入的原始数据,大小不能超过128MB。 | 3500 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3501 3502**错误码:** 3503 3504以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3505 3506 | 错误码ID | 错误信息 | 3507 | -------- | -------- | 3508 | 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. | 3509 | 1900009 | Failed to write data to the message sequence. | 3510 3511**示例:** 3512 3513 ```ts 3514 import { hilog } from '@kit.PerformanceAnalysisKit'; 3515 import { BusinessError } from '@kit.BasicServicesKit'; 3516 3517 let buffer = new ArrayBuffer(64 * 1024); 3518 let int32View = new Int32Array(buffer); 3519 for (let i = 0; i < int32View.length; i++) { 3520 int32View[i] = i * 2 + 1; 3521 } 3522 let size = buffer.byteLength; 3523 let sequence = new rpc.MessageSequence(); 3524 try { 3525 sequence.writeRawDataBuffer(buffer, size); 3526 } catch (error) { 3527 let e: BusinessError = error as BusinessError; 3528 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3529 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3530 } 3531 ``` 3532 3533### readRawData<sup>(deprecated)</sup> 3534 3535readRawData(size: number): number[] 3536 3537从MessageSequence读取原始数据。 3538 3539> **说明:** 3540> 3541> 从API version 11 开始废弃,建议使用[readRawDataBuffer](#readrawdatabuffer11)替代。 3542 3543 3544**系统能力**:SystemCapability.Communication.IPC.Core 3545 3546**参数:** 3547 3548 | 参数名 | 类型 | 必填 | 说明 | 3549 | ------ | ------ | ---- | ------------------------ | 3550 | size | number | 是 | 要读取的原始数据的大小。 | 3551 3552**返回值:** 3553 3554 | 类型 | 说明 | 3555 | -------- | ------------------------------ | 3556 | number[] | 返回原始数据(以字节为单位)。 | 3557 3558**错误码:** 3559 3560以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3561 3562 | 错误码ID | 错误信息 | 3563 | -------- | -------- | 3564 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3565 | 1900010 | Failed to read data from the message sequence. | 3566 3567**示例:** 3568 3569 ```ts 3570 import { hilog } from '@kit.PerformanceAnalysisKit'; 3571 import { BusinessError } from '@kit.BasicServicesKit'; 3572 3573 let sequence = new rpc.MessageSequence(); 3574 let arr = [1, 2, 3, 4, 5]; 3575 try { 3576 sequence.writeRawData(arr, arr.length); 3577 } catch (error) { 3578 let e: BusinessError = error as BusinessError; 3579 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3580 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3581 } 3582 try { 3583 let result = sequence.readRawData(5); 3584 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3585 } catch (error) { 3586 let e: BusinessError = error as BusinessError; 3587 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3588 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3589 } 3590 ``` 3591 3592### readRawDataBuffer<sup>11+</sup> 3593 3594readRawDataBuffer(size: number): ArrayBuffer 3595 3596从MessageSequence读取原始数据。 3597 3598**系统能力**:SystemCapability.Communication.IPC.Core 3599 3600**参数:** 3601 3602 | 参数名 | 类型 | 必填 | 说明 | 3603 | ------ | ------ | ---- | ------------------------ | 3604 | size | number | 是 | 要读取的原始数据的大小。 | 3605 3606**返回值:** 3607 3608 | 类型 | 说明 | 3609 | -------- | ------------------------------ | 3610 | ArrayBuffer | 返回原始数据(以字节为单位)。 | 3611 3612**错误码:** 3613 3614以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3615 3616 | 错误码ID | 错误信息 | 3617 | -------- | -------- | 3618 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3619 | 1900010 | Failed to read data from the message sequence. | 3620 3621**示例:** 3622 3623 ```ts 3624 import { hilog } from '@kit.PerformanceAnalysisKit'; 3625 import { BusinessError } from '@kit.BasicServicesKit'; 3626 3627 let buffer = new ArrayBuffer(64 * 1024); 3628 let int32View = new Int32Array(buffer); 3629 for (let i = 0; i < int32View.length; i++) { 3630 int32View[i] = i * 2 + 1; 3631 } 3632 let size = buffer.byteLength; 3633 let sequence = new rpc.MessageSequence(); 3634 try { 3635 sequence.writeRawDataBuffer(buffer, size); 3636 } catch (error) { 3637 let e: BusinessError = error as BusinessError; 3638 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3639 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3640 } 3641 try { 3642 let result = sequence.readRawDataBuffer(size); 3643 let readInt32View = new Int32Array(result); 3644 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + readInt32View); 3645 } catch (error) { 3646 let e: BusinessError = error as BusinessError; 3647 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3648 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3649 } 3650 ``` 3651 3652### writeArrayBuffer<sup>12+</sup> 3653 3654writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3655 3656将ArrayBuffer类型数据写入MessageSequence对象。 3657 3658**系统能力**:SystemCapability.Communication.IPC.Core 3659 3660**参数:** 3661 3662 | 参数名 | 类型 | 必填 | 说明 | 3663 | --------- | ------------------------- | ---- | --------------------------- | 3664 | buf | ArrayBuffer | 是 | 要写入的ArrayBuffer数据。 | 3665 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的写入方式,需要业务正确传递枚举值。) | 3666 3667**错误码:** 3668 3669以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3670 3671 | 错误码ID | 错误信息 | 3672 | -------- | -------- | 3673 | 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. | 3674 | 1900009 | Failed to write data to the message sequence. | 3675 3676**示例:** 3677 3678 ```ts 3679 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3680 import { hilog } from '@kit.PerformanceAnalysisKit'; 3681 import { BusinessError } from '@kit.BasicServicesKit'; 3682 3683 const data = rpc.MessageSequence.create(); 3684 3685 let buffer = new ArrayBuffer(10); 3686 let int16View = new Int16Array(buffer); 3687 for (let i = 0; i < int16View.length; i++) { 3688 int16View[i] = i * 2 + 1; 3689 } 3690 3691 try { 3692 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3693 } catch (error) { 3694 let e: BusinessError = error as BusinessError; 3695 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorCode ' + e.code); 3696 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorMessage ' + e.message); 3697 } 3698 ``` 3699 3700### readArrayBuffer<sup>12+</sup> 3701 3702readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3703 3704从MessageSequence读取ArrayBuffer类型数据。 3705 3706**系统能力**:SystemCapability.Communication.IPC.Core 3707 3708**参数:** 3709 3710 | 参数名 | 类型 | 必填 | 说明 | 3711 | -------- | ----------------------- | ---- | ------------------------| 3712 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的读取方式,需要业务正确传递枚举值,读写枚举值不匹配会导致数据异常。) | 3713 3714**返回值:** 3715 3716 | 类型 | 说明 | 3717 | -------- | -------------------------------------------- | 3718 | ArrayBuffer | 返回ArrayBuffer类型数据(以字节为单位)。 | 3719 3720**错误码:** 3721 3722以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3723 3724 | 错误码ID | 错误信息 | 3725 | -------- | -------- | 3726 | 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; | 3727 | 1900010 | Failed to read data from the message sequence. | 3728 3729**示例:** 3730 3731 ```ts 3732 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3733 import { hilog } from '@kit.PerformanceAnalysisKit'; 3734 import { BusinessError } from '@kit.BasicServicesKit'; 3735 3736 const data = rpc.MessageSequence.create(); 3737 3738 let buffer = new ArrayBuffer(10); 3739 let int16View = new Int16Array(buffer); 3740 for (let i = 0; i < int16View.length; i++) { 3741 int16View[i] = i * 2 + 1; 3742 } 3743 3744 try { 3745 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3746 } catch (error) { 3747 let e: BusinessError = error as BusinessError; 3748 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorCode ' + e.code); 3749 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorMessage ' + e.message); 3750 } 3751 try { 3752 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3753 let readInt16View = new Int16Array(result); 3754 hilog.info(0x0000, 'testTag', 'RpcTest: read ArrayBuffer result is ' + readInt16View); 3755 } catch (error) { 3756 let e: BusinessError = error as BusinessError; 3757 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorCode ' + e.code); 3758 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorMessage ' + e.message); 3759 } 3760 ``` 3761 3762## MessageParcel<sup>(deprecated)</sup> 3763 3764在RPC过程中,发送方可以使用MessageParcel提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageParcel提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 3765 3766> **说明:** 3767> 3768> 从API version 9 开始废弃,建议使用[MessageSequence](#messagesequence9)替代。 3769 3770### create 3771 3772static create(): MessageParcel 3773 3774静态方法,创建MessageParcel对象。 3775 3776**系统能力**:SystemCapability.Communication.IPC.Core 3777 3778**返回值:** 3779 3780 | 类型 | 说明 | 3781 | ------------- | ----------------------------- | 3782 | [MessageParcel](#messageparceldeprecated) | 返回创建的MessageParcel对象。 | 3783 3784**示例:** 3785 3786 ```ts 3787 import { hilog } from '@kit.PerformanceAnalysisKit'; 3788 3789 let data = rpc.MessageParcel.create(); 3790 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3791 3792 // 当MessageParcel对象不再使用,由业务主动调用reclaim方法去释放资源。 3793 data.reclaim(); 3794 ``` 3795 3796### reclaim 3797 3798reclaim(): void 3799 3800释放不再使用的MessageParcel对象。 3801 3802**系统能力**:SystemCapability.Communication.IPC.Core 3803 3804**示例:** 3805 3806 ```ts 3807 let reply = rpc.MessageParcel.create(); 3808 reply.reclaim(); 3809 ``` 3810 3811### writeRemoteObject 3812 3813writeRemoteObject(object: IRemoteObject): boolean 3814 3815序列化远程对象并将其写入MessageParcel对象。 3816 3817**系统能力**:SystemCapability.Communication.IPC.Core 3818 3819**参数:** 3820 3821 | 参数名 | 类型 | 必填 | 说明 | 3822 | ------ | ------------------------------- | ---- | --------------------------------------- | 3823 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageParcel的远程对象。 | 3824 3825**返回值:** 3826 3827 | 类型 | 说明 | 3828 | ------- | ----------------------------------------- | 3829 | boolean | true:操作成功,false:操作失败。| 3830 3831**示例:** 3832 3833 ```ts 3834 import { hilog } from '@kit.PerformanceAnalysisKit'; 3835 3836 class MyDeathRecipient implements rpc.DeathRecipient { 3837 onRemoteDied() { 3838 hilog.info(0x0000, 'testTag', 'server died'); 3839 } 3840 } 3841 class TestRemoteObject extends rpc.RemoteObject { 3842 constructor(descriptor: string) { 3843 super(descriptor); 3844 } 3845 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3846 return true; 3847 } 3848 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3849 return true; 3850 } 3851 isObjectDead(): boolean { 3852 return false; 3853 } 3854 } 3855 let data = rpc.MessageParcel.create(); 3856 let testRemoteObject = new TestRemoteObject("testObject"); 3857 data.writeRemoteObject(testRemoteObject); 3858 ``` 3859 3860### readRemoteObject 3861 3862readRemoteObject(): IRemoteObject 3863 3864从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。 3865 3866**系统能力**:SystemCapability.Communication.IPC.Core 3867 3868**返回值:** 3869 3870 | 类型 | 说明 | 3871 | ------------------------------- | ------------------ | 3872 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 3873 3874**示例:** 3875 3876 ```ts 3877 import { hilog } from '@kit.PerformanceAnalysisKit'; 3878 3879 class MyDeathRecipient implements rpc.DeathRecipient { 3880 onRemoteDied() { 3881 hilog.info(0x0000, 'testTag', 'server died'); 3882 } 3883 } 3884 class TestRemoteObject extends rpc.RemoteObject { 3885 constructor(descriptor: string) { 3886 super(descriptor); 3887 } 3888 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3889 return true; 3890 } 3891 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3892 return true; 3893 } 3894 isObjectDead(): boolean { 3895 return false; 3896 } 3897 } 3898 let data = rpc.MessageParcel.create(); 3899 let testRemoteObject = new TestRemoteObject("testObject"); 3900 data.writeRemoteObject(testRemoteObject); 3901 let proxy = data.readRemoteObject(); 3902 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3903 ``` 3904 3905### writeInterfaceToken 3906 3907writeInterfaceToken(token: string): boolean 3908 3909将接口描述符写入MessageParcel对象,远端对象可使用该信息校验本次通信。 3910 3911**系统能力**:SystemCapability.Communication.IPC.Core 3912 3913**参数:** 3914 3915 | 参数名 | 类型 | 必填 | 说明 | 3916 | ------ | ------ | ---- | ------------------ | 3917 | token | string | 是 | 字符串类型描述符。 | 3918 3919**返回值:** 3920 3921 | 类型 | 说明 | 3922 | ------- | ----------------------------------------- | 3923 | boolean | true:操作成功,false:操作失败。| 3924 3925**示例:** 3926 3927 ```ts 3928 import { hilog } from '@kit.PerformanceAnalysisKit'; 3929 3930 let data = rpc.MessageParcel.create(); 3931 let result = data.writeInterfaceToken("aaa"); 3932 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3933 ``` 3934 3935### readInterfaceToken 3936 3937readInterfaceToken(): string 3938 3939从MessageParcel中读取接口描述符,接口描述符按写入MessageParcel的顺序读取,本地对象可使用该信息检验本次通信。 3940 3941**系统能力**:SystemCapability.Communication.IPC.Core 3942 3943**返回值:** 3944 3945 | 类型 | 说明 | 3946 | ------ | ------------------------ | 3947 | string | 返回读取到的接口描述符。 | 3948 3949**示例:** 3950 3951 ```ts 3952 import { hilog } from '@kit.PerformanceAnalysisKit'; 3953 3954 class Stub extends rpc.RemoteObject { 3955 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3956 let interfaceToken = data.readInterfaceToken(); 3957 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3958 return true; 3959 } 3960 } 3961 ``` 3962 3963### getSize 3964 3965getSize(): number 3966 3967获取当前MessageParcel的数据大小。 3968 3969**系统能力**:SystemCapability.Communication.IPC.Core 3970 3971**返回值:** 3972 3973 | 类型 | 说明 | 3974 | ------ | --------------------------------------------- | 3975 | number | 获取的MessageParcel的数据大小。以字节为单位。 | 3976 3977**示例:** 3978 3979 ```ts 3980 import { hilog } from '@kit.PerformanceAnalysisKit'; 3981 3982 let data = rpc.MessageParcel.create(); 3983 let size = data.getSize(); 3984 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3985 ``` 3986 3987### getCapacity 3988 3989getCapacity(): number 3990 3991获取当前MessageParcel的容量。 3992 3993**系统能力**:SystemCapability.Communication.IPC.Core 3994 3995**返回值:** 3996 3997 | 类型 | 说明 | 3998 | ------ | --------------------------------------------- | 3999 | number | 获取的MessageParcel的容量大小。以字节为单位。 | 4000 4001**示例:** 4002 4003 ```ts 4004 import { hilog } from '@kit.PerformanceAnalysisKit'; 4005 4006 let data = rpc.MessageParcel.create(); 4007 let result = data.getCapacity(); 4008 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 4009 ``` 4010 4011### setSize 4012 4013setSize(size: number): boolean 4014 4015设置MessageParcel实例中包含的数据大小。 4016 4017**系统能力**:SystemCapability.Communication.IPC.Core 4018 4019**参数:** 4020 4021 | 参数名 | 类型 | 必填 | 说明 | 4022 | ------ | ------ | ---- | ------------------------------------------- | 4023 | size | number | 是 | MessageParcel实例的数据大小。以字节为单位。 | 4024 4025**返回值:** 4026 4027 | 类型 | 说明 | 4028 | ------- | --------------------------------- | 4029 | boolean | true:设置成功,false:设置失败。| 4030 4031**示例:** 4032 4033 ```ts 4034 import { hilog } from '@kit.PerformanceAnalysisKit'; 4035 4036 let data = rpc.MessageParcel.create(); 4037 let setSize = data.setSize(16); 4038 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 4039 ``` 4040 4041### setCapacity 4042 4043setCapacity(size: number): boolean 4044 4045设置MessageParcel实例的存储容量。 4046 4047**系统能力**:SystemCapability.Communication.IPC.Core 4048 4049**参数:** 4050 4051 | 参数名 | 类型 | 必填 | 说明 | 4052 | ------ | ------ | ---- | ------------------------------------------- | 4053 | size | number | 是 | MessageParcel实例的存储容量。以字节为单位。 | 4054 4055**返回值:** 4056 4057 | 类型 | 说明 | 4058 | ------- | --------------------------------- | 4059 | boolean | true:设置成功,false:设置失败。| 4060 4061**示例:** 4062 4063 ```ts 4064 import { hilog } from '@kit.PerformanceAnalysisKit'; 4065 4066 let data = rpc.MessageParcel.create(); 4067 let result = data.setCapacity(100); 4068 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 4069 ``` 4070 4071### getWritableBytes 4072 4073getWritableBytes(): number 4074 4075获取MessageParcel的可写字节空间。 4076 4077**系统能力**:SystemCapability.Communication.IPC.Core 4078 4079**返回值:** 4080 4081 | 类型 | 说明 | 4082 | ------ | --------------------------------------------------- | 4083 | number | 获取到的MessageParcel的可写字节空间。以字节为单位。 | 4084 4085**示例:** 4086 4087 ```ts 4088 import { hilog } from '@kit.PerformanceAnalysisKit'; 4089 4090 class Stub extends rpc.RemoteObject { 4091 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4092 let getWritableBytes = data.getWritableBytes(); 4093 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4094 return true; 4095 } 4096 } 4097 ``` 4098 4099### getReadableBytes 4100 4101getReadableBytes(): number 4102 4103获取MessageParcel的可读字节空间。 4104 4105**系统能力**:SystemCapability.Communication.IPC.Core 4106 4107**返回值:** 4108 4109 | 类型 | 说明 | 4110 | ------ | --------------------------------------------------- | 4111 | number | 获取到的MessageParcel的可读字节空间。以字节为单位。 | 4112 4113**示例:** 4114 4115 ```ts 4116 import { hilog } from '@kit.PerformanceAnalysisKit'; 4117 4118 class Stub extends rpc.RemoteObject { 4119 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4120 let result = data.getReadableBytes(); 4121 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4122 return true; 4123 } 4124 } 4125 ``` 4126 4127### getReadPosition 4128 4129getReadPosition(): number 4130 4131获取MessageParcel的读位置。 4132 4133**系统能力**:SystemCapability.Communication.IPC.Core 4134 4135**返回值:** 4136 4137 | 类型 | 说明 | 4138 | ------ | --------------------------------------- | 4139 | number | 返回MessageParcel实例中的当前读取位置。 | 4140 4141**示例:** 4142 4143 ```ts 4144 import { hilog } from '@kit.PerformanceAnalysisKit'; 4145 4146 let data = rpc.MessageParcel.create(); 4147 let readPos = data.getReadPosition(); 4148 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 4149 ``` 4150 4151### getWritePosition 4152 4153getWritePosition(): number 4154 4155获取MessageParcel的写位置。 4156 4157**系统能力**:SystemCapability.Communication.IPC.Core 4158 4159**返回值:** 4160 4161 | 类型 | 说明 | 4162 | ------ | --------------------------------------- | 4163 | number | 返回MessageParcel实例中的当前写入位置。 | 4164 4165**示例:** 4166 4167 ```ts 4168 import { hilog } from '@kit.PerformanceAnalysisKit'; 4169 4170 let data = rpc.MessageParcel.create(); 4171 data.writeInt(10); 4172 let bwPos = data.getWritePosition(); 4173 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 4174 ``` 4175 4176### rewindRead 4177 4178rewindRead(pos: number): boolean 4179 4180重新偏移读取位置到指定的位置。 4181 4182**系统能力**:SystemCapability.Communication.IPC.Core 4183 4184**参数:** 4185 4186 | 参数名 | 类型 | 必填 | 说明 | 4187 | ------ | ------ | ---- | ------------------------ | 4188 | pos | number | 是 | 开始读取数据的目标位置。 | 4189 4190**返回值:** 4191 4192 | 类型 | 说明 | 4193 | ------- | ------------------------------------------------- | 4194 | boolean | true:读取位置发生更改,false:读取位置未发生更改。| 4195 4196**示例:** 4197 4198 ```ts 4199 import { hilog } from '@kit.PerformanceAnalysisKit'; 4200 4201 let data = rpc.MessageParcel.create(); 4202 data.writeInt(12); 4203 data.writeString("parcel"); 4204 let number = data.readInt(); 4205 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 4206 data.rewindRead(0); 4207 let number2 = data.readInt(); 4208 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 4209 ``` 4210 4211### rewindWrite 4212 4213rewindWrite(pos: number): boolean 4214 4215重新偏移写位置到指定的位置。 4216 4217**系统能力**:SystemCapability.Communication.IPC.Core 4218 4219**参数:** 4220 4221 | 参数名 | 类型 | 必填 | 说明 | 4222 | ------ | ------ | ---- | ------------------------ | 4223 | pos | number | 是 | 开始写入数据的目标位置。 | 4224 4225**返回值:** 4226 4227 | 类型 | 说明 | 4228 | ------- | --------------------------------------------- | 4229 | boolean | true:写入位置发生更改,false:写入位置未发生更改。| 4230 4231**示例:** 4232 4233 ```ts 4234 import { hilog } from '@kit.PerformanceAnalysisKit'; 4235 4236 let data = rpc.MessageParcel.create(); 4237 data.writeInt(4); 4238 data.rewindWrite(0); 4239 data.writeInt(5); 4240 let number = data.readInt(); 4241 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 4242 ``` 4243 4244### writeByte 4245 4246writeByte(val: number): boolean 4247 4248将字节值写入MessageParcel实例。 4249 4250**系统能力**:SystemCapability.Communication.IPC.Core 4251 4252**参数:** 4253 4254 | 参数名 | 类型 | 必填 | 说明 | 4255 | ------ | ------ | ---- | ---------------- | 4256 | val | number | 是 | 要写入的字节值。 | 4257 4258**返回值:** 4259 4260 | 类型 | 说明 | 4261 | ------- | ----------------------------- | 4262 | boolean | true:写入成功,false:写入失败。 | 4263 4264**示例:** 4265 4266 ```ts 4267 import { hilog } from '@kit.PerformanceAnalysisKit'; 4268 4269 let data = rpc.MessageParcel.create(); 4270 let result = data.writeByte(2); 4271 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4272 ``` 4273 4274### readByte 4275 4276readByte(): number 4277 4278从MessageParcel实例读取字节值。 4279 4280**系统能力**:SystemCapability.Communication.IPC.Core 4281 4282**返回值:** 4283 4284 | 类型 | 说明 | 4285 | ------ | ------------ | 4286 | number | 返回字节值。 | 4287 4288**示例:** 4289 4290 ```ts 4291 import { hilog } from '@kit.PerformanceAnalysisKit'; 4292 4293 let data = rpc.MessageParcel.create(); 4294 let result = data.writeByte(2); 4295 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4296 let ret = data.readByte(); 4297 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 4298 ``` 4299 4300### writeShort 4301 4302writeShort(val: number): boolean 4303 4304将短整数值写入MessageParcel实例。 4305 4306**系统能力**:SystemCapability.Communication.IPC.Core 4307 4308**参数:** 4309 4310 | 参数名 | 类型 | 必填 | 说明 | 4311 | ------ | ------ | ---- | ------------------ | 4312 | val | number | 是 | 要写入的短整数值。 | 4313 4314**返回值:** 4315 4316 | 类型 | 说明 | 4317 | ------- | ----------------------------- | 4318 | boolean | true:写入成功,false:写入失败。| 4319 4320**示例:** 4321 4322 ```ts 4323 import { hilog } from '@kit.PerformanceAnalysisKit'; 4324 4325 let data = rpc.MessageParcel.create(); 4326 let result = data.writeShort(8); 4327 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4328 ``` 4329 4330### readShort 4331 4332readShort(): number 4333 4334从MessageParcel实例读取短整数值。 4335 4336**系统能力**:SystemCapability.Communication.IPC.Core 4337 4338**返回值:** 4339 4340 | 类型 | 说明 | 4341 | ------ | -------------- | 4342 | number | 返回短整数值。 | 4343 4344**示例:** 4345 4346 ```ts 4347 import { hilog } from '@kit.PerformanceAnalysisKit'; 4348 4349 let data = rpc.MessageParcel.create(); 4350 let result = data.writeShort(8); 4351 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4352 let ret = data.readShort(); 4353 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4354 ``` 4355 4356### writeInt 4357 4358writeInt(val: number): boolean 4359 4360将整数值写入MessageParcel实例。 4361 4362**系统能力**:SystemCapability.Communication.IPC.Core 4363 4364**参数:** 4365 4366 | 参数名 | 类型 | 必填 | 说明 | 4367 | ------ | ------ | ---- | ---------------- | 4368 | val | number | 是 | 要写入的整数值。 | 4369 4370**返回值:** 4371 4372 | 类型 | 说明 | 4373 | ------- | ----------------------------- | 4374 | boolean | true:写入成功,false:写入失败。 | 4375 4376**示例:** 4377 4378 ```ts 4379 import { hilog } from '@kit.PerformanceAnalysisKit'; 4380 4381 let data = rpc.MessageParcel.create(); 4382 let result = data.writeInt(10); 4383 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4384 ``` 4385 4386### readInt 4387 4388readInt(): number 4389 4390从MessageParcel实例读取整数值。 4391 4392**系统能力**:SystemCapability.Communication.IPC.Core 4393 4394**返回值:** 4395 4396 | 类型 | 说明 | 4397 | ------ | ------------ | 4398 | number | 返回整数值。 | 4399 4400**示例:** 4401 4402 ```ts 4403 import { hilog } from '@kit.PerformanceAnalysisKit'; 4404 4405 let data = rpc.MessageParcel.create(); 4406 let result = data.writeInt(10); 4407 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4408 let ret = data.readInt(); 4409 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4410 ``` 4411 4412### writeLong 4413 4414writeLong(val: number): boolean 4415 4416将长整数值写入MessageParcel实例。 4417 4418**系统能力**:SystemCapability.Communication.IPC.Core 4419 4420**参数:** 4421 4422 | 参数名 | 类型 | 必填 | 说明 | 4423 | ------ | ------ | ---- | ---------------- | 4424 | val | number | 是 | 要写入的长整数值 | 4425 4426**返回值:** 4427 4428 | 类型 | 说明 | 4429 | ------- | --------------------------------- | 4430 | boolean | true:写入成功,false:写入失败。| 4431 4432**示例:** 4433 4434 ```ts 4435 import { hilog } from '@kit.PerformanceAnalysisKit'; 4436 4437 let data = rpc.MessageParcel.create(); 4438 let result = data.writeLong(10000); 4439 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4440 ``` 4441 4442### readLong 4443 4444readLong(): number 4445 4446从MessageParcel实例中读取长整数值。 4447 4448**系统能力**:SystemCapability.Communication.IPC.Core 4449 4450**返回值:** 4451 4452 | 类型 | 说明 | 4453 | ------ | -------------- | 4454 | number | 返回长整数值。 | 4455 4456**示例:** 4457 4458 ```ts 4459 import { hilog } from '@kit.PerformanceAnalysisKit'; 4460 4461 let data = rpc.MessageParcel.create(); 4462 let result = data.writeLong(10000); 4463 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4464 let ret = data.readLong(); 4465 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4466 ``` 4467 4468### writeFloat 4469 4470writeFloat(val: number): boolean 4471 4472将浮点值写入MessageParcel实例。 4473 4474**系统能力**:SystemCapability.Communication.IPC.Core 4475 4476**参数:** 4477 4478 | 参数名 | 类型 | 必填 | 说明 | 4479 | ------ | ------ | ---- | ---------------- | 4480 | val | number | 是 | 要写入的浮点值。 | 4481 4482**返回值:** 4483 4484 | 类型 | 说明 | 4485 | ------- | --------------------------------- | 4486 | boolean | true:写入成功,false:写入失败。| 4487 4488**示例:** 4489 4490 ```ts 4491 import { hilog } from '@kit.PerformanceAnalysisKit'; 4492 4493 let data = rpc.MessageParcel.create(); 4494 let result = data.writeFloat(1.2); 4495 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4496 ``` 4497 4498### readFloat 4499 4500readFloat(): number 4501 4502从MessageParcel实例中读取浮点值。 4503 4504**系统能力**:SystemCapability.Communication.IPC.Core 4505 4506**返回值:** 4507 4508 | 类型 | 说明 | 4509 | ------ | ------------ | 4510 | number | 返回浮点值。 | 4511 4512**示例:** 4513 4514 ```ts 4515 import { hilog } from '@kit.PerformanceAnalysisKit'; 4516 4517 let data = rpc.MessageParcel.create(); 4518 let result = data.writeFloat(1.2); 4519 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4520 let ret = data.readFloat(); 4521 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4522 ``` 4523 4524### writeDouble 4525 4526writeDouble(val: number): boolean 4527 4528将双精度浮点值写入MessageParcel实例。 4529 4530**系统能力**:SystemCapability.Communication.IPC.Core 4531 4532**参数:** 4533 4534 | 参数名 | 类型 | 必填 | 说明 | 4535 | ------ | ------ | ---- | ---------------------- | 4536 | val | number | 是 | 要写入的双精度浮点值。 | 4537 4538**返回值:** 4539 4540 | 类型 | 说明 | 4541 | ------- | --------------------------------- | 4542 | boolean | true:写入成功,false:写入失败。| 4543 4544**示例:** 4545 4546 ```ts 4547 import { hilog } from '@kit.PerformanceAnalysisKit'; 4548 4549 let data = rpc.MessageParcel.create(); 4550 let result = data.writeDouble(10.2); 4551 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4552 ``` 4553 4554### readDouble 4555 4556readDouble(): number 4557 4558从MessageParcel实例读取双精度浮点值。 4559 4560**系统能力**:SystemCapability.Communication.IPC.Core 4561 4562**返回值:** 4563 4564 | 类型 | 说明 | 4565 | ------ | ------------------ | 4566 | number | 返回双精度浮点值。 | 4567 4568**示例:** 4569 4570 ```ts 4571 import { hilog } from '@kit.PerformanceAnalysisKit'; 4572 4573 let data = rpc.MessageParcel.create(); 4574 let result = data.writeDouble(10.2); 4575 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4576 let ret = data.readDouble(); 4577 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4578 ``` 4579 4580### writeBoolean 4581 4582writeBoolean(val: boolean): boolean 4583 4584将布尔值写入MessageParcel实例。 4585 4586**系统能力**:SystemCapability.Communication.IPC.Core 4587 4588**参数:** 4589 4590 | 参数名 | 类型 | 必填 | 说明 | 4591 | ------ | ------- | ---- | ---------------- | 4592 | val | boolean | 是 | 要写入的布尔值。 | 4593 4594**返回值:** 4595 4596 | 类型 | 说明 | 4597 | ------- | --------------------------------- | 4598 | boolean | true:写入成功,false:写入失败。| 4599 4600**示例:** 4601 4602 ```ts 4603 import { hilog } from '@kit.PerformanceAnalysisKit'; 4604 4605 let data = rpc.MessageParcel.create(); 4606 let result = data.writeBoolean(false); 4607 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4608 ``` 4609 4610### readBoolean 4611 4612readBoolean(): boolean 4613 4614从MessageParcel实例读取布尔值。 4615 4616**系统能力**:SystemCapability.Communication.IPC.Core 4617 4618**返回值:** 4619 4620 | 类型 | 说明 | 4621 | ------- | -------------------- | 4622 | boolean | 返回读取到的布尔值。 | 4623 4624**示例:** 4625 4626 ```ts 4627 import { hilog } from '@kit.PerformanceAnalysisKit'; 4628 4629 let data = rpc.MessageParcel.create(); 4630 let result = data.writeBoolean(false); 4631 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4632 let ret = data.readBoolean(); 4633 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4634 ``` 4635 4636### writeChar 4637 4638writeChar(val: number): boolean 4639 4640将单个字符值写入MessageParcel实例。 4641 4642**系统能力**:SystemCapability.Communication.IPC.Core 4643 4644**参数:** 4645 4646 | 参数名 | 类型 | 必填 | 说明 | 4647 | ------ | ------ | ---- | -------------------- | 4648 | val | number | 是 | 要写入的单个字符值。 | 4649 4650**返回值:** 4651 4652 | 类型 | 说明 | 4653 | ------- | ----------------------------- | 4654 | boolean | true:写入成功,false:写入失败。| 4655 4656**示例:** 4657 4658 ```ts 4659 import { hilog } from '@kit.PerformanceAnalysisKit'; 4660 4661 let data = rpc.MessageParcel.create(); 4662 let result = data.writeChar(97); 4663 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4664 ``` 4665 4666### readChar 4667 4668readChar(): number 4669 4670从MessageParcel实例中读取单个字符值。 4671 4672**系统能力**:SystemCapability.Communication.IPC.Core 4673 4674**返回值:** 4675 4676 | 类型 | 说明 | 4677 | ------ | ---------------- | 4678 | number | 返回单个字符值。 | 4679 4680**示例:** 4681 4682 ```ts 4683 import { hilog } from '@kit.PerformanceAnalysisKit'; 4684 4685 let data = rpc.MessageParcel.create(); 4686 let result = data.writeChar(97); 4687 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4688 let ret = data.readChar(); 4689 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4690 ``` 4691 4692### writeString 4693 4694writeString(val: string): boolean 4695 4696将字符串值写入MessageParcel实例。 4697 4698**系统能力**:SystemCapability.Communication.IPC.Core 4699 4700**参数:** 4701 4702 | 参数名 | 类型 | 必填 | 说明 | 4703 | ------ | ------ | ---- | ----------------------------------------- | 4704 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 4705 4706**返回值:** 4707 4708 | 类型 | 说明 | 4709 | ------- | --------------------------------- | 4710 | boolean | true:写入成功,false:写入失败。| 4711 4712**示例:** 4713 4714 ```ts 4715 import { hilog } from '@kit.PerformanceAnalysisKit'; 4716 4717 let data = rpc.MessageParcel.create(); 4718 let result = data.writeString('abc'); 4719 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4720 ``` 4721 4722### readString 4723 4724readString(): string 4725 4726从MessageParcel实例读取字符串值。 4727 4728**系统能力**:SystemCapability.Communication.IPC.Core 4729 4730**返回值:** 4731 4732 | 类型 | 说明 | 4733 | ------ | -------------- | 4734 | string | 返回字符串值。 | 4735 4736**示例:** 4737 4738 ```ts 4739 import { hilog } from '@kit.PerformanceAnalysisKit'; 4740 4741 let data = rpc.MessageParcel.create(); 4742 let result = data.writeString('abc'); 4743 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4744 let ret = data.readString(); 4745 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4746 ``` 4747 4748### writeSequenceable 4749 4750writeSequenceable(val: Sequenceable): boolean 4751 4752将自定义序列化对象写入MessageParcel实例。 4753 4754**系统能力**:SystemCapability.Communication.IPC.Core 4755 4756**参数:** 4757 4758 | 参数名 | 类型 | 必填 | 说明 | 4759 | ------ | ----------------------------- | ---- | -------------------- | 4760 | val | [Sequenceable](#sequenceabledeprecated) | 是 | 要写入的可序列对象。 | 4761 4762**返回值:** 4763 4764 | 类型 | 说明 | 4765 | ------- | -------------------------------- | 4766 | boolean | true:写入成功,false:写入失败。| 4767 4768**示例:** 4769 4770 ```ts 4771 import { hilog } from '@kit.PerformanceAnalysisKit'; 4772 4773 class MySequenceable implements rpc.Sequenceable { 4774 num: number = 0; 4775 str: string = ''; 4776 constructor(num: number, str: string) { 4777 this.num = num; 4778 this.str = str; 4779 } 4780 marshalling(messageParcel: rpc.MessageParcel): boolean { 4781 messageParcel.writeInt(this.num); 4782 messageParcel.writeString(this.str); 4783 return true; 4784 } 4785 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4786 this.num = messageParcel.readInt(); 4787 this.str = messageParcel.readString(); 4788 return true; 4789 } 4790 } 4791 let sequenceable = new MySequenceable(1, "aaa"); 4792 let data = rpc.MessageParcel.create(); 4793 let result = data.writeSequenceable(sequenceable); 4794 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4795 ``` 4796 4797### readSequenceable 4798 4799readSequenceable(dataIn: Sequenceable): boolean 4800 4801从MessageParcel实例中读取成员变量到指定的对象(dataIn)。 4802 4803**系统能力**:SystemCapability.Communication.IPC.Core 4804 4805**参数:** 4806 4807 | 参数名 | 类型 | 必填 | 说明 | 4808 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4809 | dataIn | [Sequenceable](#sequenceabledeprecated) | 是 | 需要从MessageParcel读取成员变量的对象。 | 4810 4811**返回值:** 4812 4813 | 类型 | 说明 | 4814 | ------- | ---------------------------------------- | 4815 | boolean | true:反序列化成功,false:反序列化失败。| 4816 4817**示例:** 4818 4819 ```ts 4820 import { hilog } from '@kit.PerformanceAnalysisKit'; 4821 4822 class MySequenceable implements rpc.Sequenceable { 4823 num: number = 0; 4824 str: string = ''; 4825 constructor(num: number, str: string) { 4826 this.num = num; 4827 this.str = str; 4828 } 4829 marshalling(messageParcel: rpc.MessageParcel): boolean { 4830 messageParcel.writeInt(this.num); 4831 messageParcel.writeString(this.str); 4832 return true; 4833 } 4834 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4835 this.num = messageParcel.readInt(); 4836 this.str = messageParcel.readString(); 4837 return true; 4838 } 4839 } 4840 let sequenceable = new MySequenceable(1, "aaa"); 4841 let data = rpc.MessageParcel.create(); 4842 let result = data.writeSequenceable(sequenceable); 4843 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4844 let ret = new MySequenceable(0, ""); 4845 let result2 = data.readSequenceable(ret); 4846 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4847 ``` 4848 4849### writeByteArray 4850 4851writeByteArray(byteArray: number[]): boolean 4852 4853将字节数组写入MessageParcel实例。 4854 4855**系统能力**:SystemCapability.Communication.IPC.Core 4856 4857**参数:** 4858 4859 | 参数名 | 类型 | 必填 | 说明 | 4860 | --------- | -------- | ---- | ------------------ | 4861 | byteArray | number[] | 是 | 要写入的字节数组。 | 4862 4863**返回值:** 4864 4865 | 类型 | 说明 | 4866 | ------- | -------------------------------- | 4867 | boolean | true:写入成功,false:写入失败。| 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 ``` 4879 4880### readByteArray 4881 4882readByteArray(dataIn: number[]): void 4883 4884从MessageParcel实例读取字节数组。 4885 4886**系统能力**:SystemCapability.Communication.IPC.Core 4887 4888**参数:** 4889 4890 | 参数名 | 类型 | 必填 | 说明 | 4891 | ------ | -------- | ---- | ------------------ | 4892 | dataIn | number[] | 是 | 要读取的字节数组。 | 4893 4894**示例:** 4895 4896 ```ts 4897 import { hilog } from '@kit.PerformanceAnalysisKit'; 4898 4899 let data = rpc.MessageParcel.create(); 4900 let ByteArrayVar = [1, 2, 3, 4, 5]; 4901 let result = data.writeByteArray(ByteArrayVar); 4902 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4903 let array: Array<number> = new Array(5); 4904 data.readByteArray(array); 4905 ``` 4906 4907### readByteArray 4908 4909readByteArray(): number[] 4910 4911从MessageParcel实例中读取字节数组。 4912 4913**系统能力**:SystemCapability.Communication.IPC.Core 4914 4915**返回值:** 4916 4917 | 类型 | 说明 | 4918 | -------- | -------------- | 4919 | number[] | 返回字节数组。 | 4920 4921**示例:** 4922 4923 ```ts 4924 import { hilog } from '@kit.PerformanceAnalysisKit'; 4925 4926 let data = rpc.MessageParcel.create(); 4927 let ByteArrayVar = [1, 2, 3, 4, 5]; 4928 let result = data.writeByteArray(ByteArrayVar); 4929 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4930 let array = data.readByteArray(); 4931 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4932 ``` 4933 4934### writeShortArray 4935 4936writeShortArray(shortArray: number[]): boolean 4937 4938将短整数数组写入MessageParcel实例。 4939 4940**系统能力**:SystemCapability.Communication.IPC.Core 4941 4942**参数:** 4943 4944 | 参数名 | 类型 | 必填 | 说明 | 4945 | ---------- | -------- | ---- | -------------------- | 4946 | shortArray | number[] | 是 | 要写入的短整数数组。 | 4947 4948**返回值:** 4949 4950 | 类型 | 说明 | 4951 | ------- | -------------------------------- | 4952 | boolean | true:写入成功,false:写入失败。| 4953 4954**示例:** 4955 4956 ```ts 4957 import { hilog } from '@kit.PerformanceAnalysisKit'; 4958 4959 let data = rpc.MessageParcel.create(); 4960 let result = data.writeShortArray([11, 12, 13]); 4961 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4962 ``` 4963 4964### readShortArray 4965 4966readShortArray(dataIn: number[]): void 4967 4968从MessageParcel实例中读取短整数数组。 4969 4970**系统能力**:SystemCapability.Communication.IPC.Core 4971 4972**参数:** 4973 4974 | 参数名 | 类型 | 必填 | 说明 | 4975 | ------ | -------- | ---- | -------------------- | 4976 | dataIn | number[] | 是 | 要读取的短整数数组。 | 4977 4978**示例:** 4979 4980 ```ts 4981 import { hilog } from '@kit.PerformanceAnalysisKit'; 4982 4983 let data = rpc.MessageParcel.create(); 4984 let result = data.writeShortArray([11, 12, 13]); 4985 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4986 let array: Array<number> = new Array(3); 4987 data.readShortArray(array); 4988 ``` 4989 4990### readShortArray 4991 4992readShortArray(): number[] 4993 4994从MessageParcel实例中读取短整数数组。 4995 4996**系统能力**:SystemCapability.Communication.IPC.Core 4997 4998**返回值:** 4999 5000 | 类型 | 说明 | 5001 | -------- | ---------------- | 5002 | number[] | 返回短整数数组。 | 5003 5004**示例:** 5005 5006 ```ts 5007 import { hilog } from '@kit.PerformanceAnalysisKit'; 5008 5009 let data = rpc.MessageParcel.create(); 5010 let result = data.writeShortArray([11, 12, 13]); 5011 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 5012 let array = data.readShortArray(); 5013 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 5014 ``` 5015 5016### writeIntArray 5017 5018writeIntArray(intArray: number[]): boolean 5019 5020将整数数组写入MessageParcel实例。 5021 5022**系统能力**:SystemCapability.Communication.IPC.Core 5023 5024**参数:** 5025 5026 | 参数名 | 类型 | 必填 | 说明 | 5027 | -------- | -------- | ---- | ------------------ | 5028 | intArray | number[] | 是 | 要写入的整数数组。 | 5029 5030**返回值:** 5031 5032 | 类型 | 说明 | 5033 | ------- | -------------------------------- | 5034 | boolean | true:写入成功,false:写入失败。| 5035 5036**示例:** 5037 5038 ```ts 5039 import { hilog } from '@kit.PerformanceAnalysisKit'; 5040 5041 let data = rpc.MessageParcel.create(); 5042 let result = data.writeIntArray([100, 111, 112]); 5043 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5044 ``` 5045 5046### readIntArray 5047 5048readIntArray(dataIn: number[]): void 5049 5050从MessageParcel实例中读取整数数组。 5051 5052**系统能力**:SystemCapability.Communication.IPC.Core 5053 5054**参数:** 5055 5056 | 参数名 | 类型 | 必填 | 说明 | 5057 | ------ | -------- | ---- | ------------------ | 5058 | dataIn | number[] | 是 | 要读取的整数数组。 | 5059 5060**示例:** 5061 5062 ```ts 5063 import { hilog } from '@kit.PerformanceAnalysisKit'; 5064 5065 let data = rpc.MessageParcel.create(); 5066 let result = data.writeIntArray([100, 111, 112]); 5067 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5068 let array: Array<number> = new Array(3); 5069 data.readIntArray(array); 5070 ``` 5071 5072### readIntArray 5073 5074readIntArray(): number[] 5075 5076从MessageParcel实例中读取整数数组。 5077 5078**系统能力**:SystemCapability.Communication.IPC.Core 5079 5080**返回值:** 5081 5082 | 类型 | 说明 | 5083 | -------- | -------------- | 5084 | number[] | 返回整数数组。 | 5085 5086**示例:** 5087 5088 ```ts 5089 import { hilog } from '@kit.PerformanceAnalysisKit'; 5090 5091 let data = rpc.MessageParcel.create(); 5092 let result = data.writeIntArray([100, 111, 112]); 5093 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5094 let array = data.readIntArray(); 5095 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 5096 ``` 5097 5098### writeLongArray 5099 5100writeLongArray(longArray: number[]): boolean 5101 5102将长整数数组写入MessageParcel实例。 5103 5104**系统能力**:SystemCapability.Communication.IPC.Core 5105 5106**参数:** 5107 5108 | 参数名 | 类型 | 必填 | 说明 | 5109 | --------- | -------- | ---- | -------------------- | 5110 | longArray | number[] | 是 | 要写入的长整数数组。 | 5111 5112**返回值:** 5113 5114 | 类型 | 说明 | 5115 | ------- | ----------------------------- | 5116 | boolean | true:写入成功,false:写入失败。| 5117 5118**示例:** 5119 5120 ```ts 5121 import { hilog } from '@kit.PerformanceAnalysisKit'; 5122 5123 let data = rpc.MessageParcel.create(); 5124 let result = data.writeLongArray([1111, 1112, 1113]); 5125 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5126 ``` 5127 5128### readLongArray 5129 5130readLongArray(dataIn: number[]): void 5131 5132从MessageParcel实例读取长整数数组。 5133 5134**系统能力**:SystemCapability.Communication.IPC.Core 5135 5136**参数:** 5137 5138 | 参数名 | 类型 | 必填 | 说明 | 5139 | ------ | -------- | ---- | -------------------- | 5140 | dataIn | number[] | 是 | 要读取的长整数数组。 | 5141 5142**示例:** 5143 5144 ```ts 5145 import { hilog } from '@kit.PerformanceAnalysisKit'; 5146 5147 let data = rpc.MessageParcel.create(); 5148 let result = data.writeLongArray([1111, 1112, 1113]); 5149 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5150 let array: Array<number> = new Array(3); 5151 data.readLongArray(array); 5152 ``` 5153 5154### readLongArray 5155 5156readLongArray(): number[] 5157 5158从MessageParcel实例中读取长整数数组。 5159 5160**系统能力**:SystemCapability.Communication.IPC.Core 5161 5162**返回值:** 5163 5164 | 类型 | 说明 | 5165 | -------- | ---------------- | 5166 | number[] | 返回长整数数组。 | 5167 5168**示例:** 5169 5170 ```ts 5171 import { hilog } from '@kit.PerformanceAnalysisKit'; 5172 5173 let data = rpc.MessageParcel.create(); 5174 let result = data.writeLongArray([1111, 1112, 1113]); 5175 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5176 let array = data.readLongArray(); 5177 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 5178 ``` 5179 5180### writeFloatArray 5181 5182writeFloatArray(floatArray: number[]): boolean 5183 5184将浮点数组写入MessageParcel实例。 5185 5186**系统能力**:SystemCapability.Communication.IPC.Core 5187 5188**参数:** 5189 5190 | 参数名 | 类型 | 必填 | 说明 | 5191 | ---------- | -------- | ---- | --- | 5192 | floatArray | number[] | 是 | 要写入的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5193 5194**返回值:** 5195 5196 | 类型 | 说明 | 5197 | ------- | -------------------------------- | 5198 | boolean | true:写入成功,false:写入失败。| 5199 5200**示例:** 5201 5202 ```ts 5203 import { hilog } from '@kit.PerformanceAnalysisKit'; 5204 5205 let data = rpc.MessageParcel.create(); 5206 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5207 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5208 ``` 5209 5210### readFloatArray 5211 5212readFloatArray(dataIn: number[]): void 5213 5214从MessageParcel实例中读取浮点数组。 5215 5216**系统能力**:SystemCapability.Communication.IPC.Core 5217 5218**参数:** 5219 5220 | 参数名 | 类型 | 必填 | 说明 | 5221 | ------ | -------- | ---- | ------ | 5222 | dataIn | number[] | 是 | 要读取的浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5223 5224**示例:** 5225 5226 ```ts 5227 import { hilog } from '@kit.PerformanceAnalysisKit'; 5228 5229 let data = rpc.MessageParcel.create(); 5230 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5231 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5232 let array: Array<number> = new Array(3); 5233 data.readFloatArray(array); 5234 ``` 5235 5236### readFloatArray 5237 5238readFloatArray(): number[] 5239 5240从MessageParcel实例中读取浮点数组。 5241 5242**系统能力**:SystemCapability.Communication.IPC.Core 5243 5244**返回值:** 5245 5246 | 类型 | 说明 | 5247 | -------- | -------------- | 5248 | number[] | 返回浮点数组。 | 5249 5250**示例:** 5251 5252 ```ts 5253 import { hilog } from '@kit.PerformanceAnalysisKit'; 5254 5255 let data = rpc.MessageParcel.create(); 5256 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5257 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5258 let array = data.readFloatArray(); 5259 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 5260 ``` 5261 5262### writeDoubleArray 5263 5264writeDoubleArray(doubleArray: number[]): boolean 5265 5266将双精度浮点数组写入MessageParcel实例。 5267 5268**系统能力**:SystemCapability.Communication.IPC.Core 5269 5270**参数:** 5271 5272 | 参数名 | 类型 | 必填 | 说明 | 5273 | ----------- | -------- | ---- | ------------------------ | 5274 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 5275 5276**返回值:** 5277 5278 | 类型 | 说明 | 5279 | ------- | -------------------------------- | 5280 | boolean | true:写入成功,false:写入失败。| 5281 5282**示例:** 5283 5284 ```ts 5285 import { hilog } from '@kit.PerformanceAnalysisKit'; 5286 5287 let data = rpc.MessageParcel.create(); 5288 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5289 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5290 ``` 5291 5292### readDoubleArray 5293 5294readDoubleArray(dataIn: number[]): void 5295 5296从MessageParcel实例中读取双精度浮点数组。 5297 5298**系统能力**:SystemCapability.Communication.IPC.Core 5299 5300**参数:** 5301 5302 | 参数名 | 类型 | 必填 | 说明 | 5303 | ------ | -------- | ---- | ------------------------ | 5304 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 5305 5306**示例:** 5307 5308 ```ts 5309 import { hilog } from '@kit.PerformanceAnalysisKit'; 5310 5311 let data = rpc.MessageParcel.create(); 5312 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5313 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5314 let array: Array<number> = new Array(3); 5315 data.readDoubleArray(array); 5316 ``` 5317 5318### readDoubleArray 5319 5320readDoubleArray(): number[] 5321 5322从MessageParcel实例读取双精度浮点数组。 5323 5324**系统能力**:SystemCapability.Communication.IPC.Core 5325 5326**返回值:** 5327 5328 | 类型 | 说明 | 5329 | -------- | -------------------- | 5330 | number[] | 返回双精度浮点数组。 | 5331 5332**示例:** 5333 5334 ```ts 5335 import { hilog } from '@kit.PerformanceAnalysisKit'; 5336 5337 let data = rpc.MessageParcel.create(); 5338 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5339 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5340 let array = data.readDoubleArray(); 5341 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5342 ``` 5343 5344### writeBooleanArray 5345 5346writeBooleanArray(booleanArray: boolean[]): boolean 5347 5348将布尔数组写入MessageParcel实例。 5349 5350**系统能力**:SystemCapability.Communication.IPC.Core 5351 5352**参数:** 5353 5354 | 参数名 | 类型 | 必填 | 说明 | 5355 | ------------ | --------- | ---- | ------------------ | 5356 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 5357 5358**返回值:** 5359 5360 | 类型 | 说明 | 5361 | ------- | -------------------------------- | 5362 | boolean | true:写入成功,false:写入失败。| 5363 5364**示例:** 5365 5366 ```ts 5367 import { hilog } from '@kit.PerformanceAnalysisKit'; 5368 5369 let data = rpc.MessageParcel.create(); 5370 let result = data.writeBooleanArray([false, true, false]); 5371 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5372 ``` 5373 5374### readBooleanArray 5375 5376readBooleanArray(dataIn: boolean[]): void 5377 5378从MessageParcel实例中读取布尔数组。 5379 5380**系统能力**:SystemCapability.Communication.IPC.Core 5381 5382**参数:** 5383 5384 | 参数名 | 类型 | 必填 | 说明 | 5385 | ------ | --------- | ---- | ------------------ | 5386 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 5387 5388**示例:** 5389 5390 ```ts 5391 import { hilog } from '@kit.PerformanceAnalysisKit'; 5392 5393 let data = rpc.MessageParcel.create(); 5394 let result = data.writeBooleanArray([false, true, false]); 5395 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5396 let array: Array<boolean> = new Array(3); 5397 data.readBooleanArray(array); 5398 ``` 5399 5400### readBooleanArray 5401 5402readBooleanArray(): boolean[] 5403 5404从MessageParcel实例中读取布尔数组。 5405 5406**系统能力**:SystemCapability.Communication.IPC.Core 5407 5408**返回值:** 5409 5410 | 类型 | 说明 | 5411 | --------- | -------------- | 5412 | boolean[] | 返回布尔数组。 | 5413 5414**示例:** 5415 5416 ```ts 5417 import { hilog } from '@kit.PerformanceAnalysisKit'; 5418 5419 let data = rpc.MessageParcel.create(); 5420 let result = data.writeBooleanArray([false, true, false]); 5421 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5422 let array = data.readBooleanArray(); 5423 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5424 ``` 5425 5426### writeCharArray 5427 5428writeCharArray(charArray: number[]): boolean 5429 5430将单个字符数组写入MessageParcel实例。 5431 5432**系统能力**:SystemCapability.Communication.IPC.Core 5433 5434**参数:** 5435 5436 | 参数名 | 类型 | 必填 | 说明 | 5437 | --------- | -------- | ---- | ---------------------- | 5438 | charArray | number[] | 是 | 要写入的单个字符数组。 | 5439 5440**返回值:** 5441 5442 | 类型 | 说明 | 5443 | ------- | -------------------------------- | 5444 | boolean | true:写入成功,false:写入失败。| 5445 5446**示例:** 5447 5448 ```ts 5449 import { hilog } from '@kit.PerformanceAnalysisKit'; 5450 5451 let data = rpc.MessageParcel.create(); 5452 let result = data.writeCharArray([97, 98, 88]); 5453 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5454 ``` 5455 5456### readCharArray 5457 5458readCharArray(dataIn: number[]): void 5459 5460从MessageParcel实例中读取单个字符数组。 5461 5462**系统能力**:SystemCapability.Communication.IPC.Core 5463 5464**参数:** 5465 5466 | 参数名 | 类型 | 必填 | 说明 | 5467 | ------ | -------- | ---- | ---------------------- | 5468 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 5469 5470**示例:** 5471 5472 ```ts 5473 import { hilog } from '@kit.PerformanceAnalysisKit'; 5474 5475 let data = rpc.MessageParcel.create(); 5476 let result = data.writeCharArray([97, 98, 99]); 5477 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5478 let array: Array<number> = new Array(3); 5479 data.readCharArray(array); 5480 ``` 5481 5482### readCharArray 5483 5484readCharArray(): number[] 5485 5486从MessageParcel实例读取单个字符数组。 5487 5488**系统能力**:SystemCapability.Communication.IPC.Core 5489 5490**返回值:** 5491 5492 | 类型 | 说明 | 5493 | -------- | ------------------ | 5494 | number[] | 返回单个字符数组。 | 5495 5496**示例:** 5497 5498 ```ts 5499 import { hilog } from '@kit.PerformanceAnalysisKit'; 5500 5501 let data = rpc.MessageParcel.create(); 5502 let result = data.writeCharArray([97, 98, 99]); 5503 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5504 let array = data.readCharArray(); 5505 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5506 ``` 5507 5508### writeStringArray 5509 5510writeStringArray(stringArray: string[]): boolean 5511 5512将字符串数组写入MessageParcel实例。 5513 5514**系统能力**:SystemCapability.Communication.IPC.Core 5515 5516**参数:** 5517 5518 | 参数名 | 类型 | 必填 | 说明 | 5519 | ----------- | -------- | ---- | ---------------- | 5520 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 5521 5522**返回值:** 5523 5524 | 类型 | 说明 | 5525 | ------- | -------------------------------- | 5526 | boolean | true:写入成功,false:写入失败。| 5527 5528**示例:** 5529 5530 ```ts 5531 import { hilog } from '@kit.PerformanceAnalysisKit'; 5532 5533 let data = rpc.MessageParcel.create(); 5534 let result = data.writeStringArray(["abc", "def"]); 5535 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5536 ``` 5537 5538### readStringArray 5539 5540readStringArray(dataIn: string[]): void 5541 5542从MessageParcel实例读取字符串数组。 5543 5544**系统能力**:SystemCapability.Communication.IPC.Core 5545 5546**参数:** 5547 5548 | 参数名 | 类型 | 必填 | 说明 | 5549 | ------ | -------- | ---- | -------------------- | 5550 | dataIn | string[] | 是 | 要读取的字符串数组。 | 5551 5552**示例:** 5553 5554 ```ts 5555 import { hilog } from '@kit.PerformanceAnalysisKit'; 5556 5557 let data = rpc.MessageParcel.create(); 5558 let result = data.writeStringArray(["abc", "def"]); 5559 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5560 let array: Array<string> = new Array(2); 5561 data.readStringArray(array); 5562 ``` 5563 5564### readStringArray 5565 5566readStringArray(): string[] 5567 5568从MessageParcel实例读取字符串数组。 5569 5570**系统能力**:SystemCapability.Communication.IPC.Core 5571 5572**返回值:** 5573 5574 | 类型 | 说明 | 5575 | -------- | ---------------- | 5576 | string[] | 返回字符串数组。 | 5577 5578**示例:** 5579 5580 ```ts 5581 import { hilog } from '@kit.PerformanceAnalysisKit'; 5582 5583 let data = rpc.MessageParcel.create(); 5584 let result = data.writeStringArray(["abc", "def"]); 5585 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5586 let array = data.readStringArray(); 5587 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5588 ``` 5589 5590### writeNoException<sup>8+</sup> 5591 5592writeNoException(): void 5593 5594向MessageParcel写入“指示未发生异常”的信息。 5595 5596**系统能力**:SystemCapability.Communication.IPC.Core 5597 5598**示例:** 5599 5600 ```ts 5601 import { hilog } from '@kit.PerformanceAnalysisKit'; 5602 5603 class MyDeathRecipient implements rpc.DeathRecipient { 5604 onRemoteDied() { 5605 hilog.info(0x0000, 'testTag', 'server died'); 5606 } 5607 } 5608 class TestRemoteObject extends rpc.RemoteObject { 5609 constructor(descriptor: string) { 5610 super(descriptor); 5611 } 5612 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5613 return true; 5614 } 5615 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5616 return true; 5617 } 5618 isObjectDead(): boolean { 5619 return false; 5620 } 5621 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5622 if (code === 1) { 5623 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5624 reply.writeNoException(); 5625 return true; 5626 } else { 5627 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5628 return false; 5629 } 5630 } 5631 } 5632 ``` 5633 5634### readException<sup>8+</sup> 5635 5636readException(): void 5637 5638从MessageParcel中读取异常。 5639 5640**系统能力**:SystemCapability.Communication.IPC.Core 5641 5642**示例:** 5643 5644>**说明:** 5645> 5646>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 5647 5648 ```ts 5649 // FA模型需要从@kit.AbilityKit导入featureAbility 5650 // import { featureAbility } from '@kit.AbilityKit'; 5651 import { Want, common } from '@kit.AbilityKit'; 5652 import { hilog } from '@kit.PerformanceAnalysisKit'; 5653 5654 let proxy: rpc.IRemoteObject | undefined; 5655 let connect: common.ConnectOptions = { 5656 onConnect: (elementName, remoteProxy) => { 5657 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5658 proxy = remoteProxy; 5659 }, 5660 onDisconnect: (elementName) => { 5661 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5662 }, 5663 onFailed: () => { 5664 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5665 } 5666 }; 5667 let want: Want = { 5668 bundleName: "com.ohos.server", 5669 abilityName: "com.ohos.server.EntryAbility", 5670 }; 5671 5672 // FA模型使用此方法连接服务 5673 // FA.connectAbility(want,connect); 5674 5675 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5676 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 5677 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5678 let connectionId = context.connectServiceExtensionAbility(want, connect); 5679 ``` 5680 5681 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 5682 5683 ```ts 5684 import { hilog } from '@kit.PerformanceAnalysisKit'; 5685 5686 let option = new rpc.MessageOption(); 5687 let data = rpc.MessageParcel.create(); 5688 let reply = rpc.MessageParcel.create(); 5689 data.writeNoException(); 5690 data.writeString('hello'); 5691 if (proxy != undefined) { 5692 let a = proxy.sendRequest(1, data, reply, option) as Object; 5693 let b = a as Promise<rpc.SendRequestResult>; 5694 b.then((result: rpc.SendRequestResult) => { 5695 if (result.errCode === 0) { 5696 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5697 result.reply.readException(); 5698 let msg = result.reply.readString(); 5699 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5700 } else { 5701 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5702 } 5703 }).catch((e: Error) => { 5704 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5705 }).finally (() => { 5706 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5707 data.reclaim(); 5708 reply.reclaim(); 5709 }); 5710 } 5711 ``` 5712 5713### writeSequenceableArray 5714 5715writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5716 5717将可序列化对象数组写入MessageParcel实例。 5718 5719**系统能力**:SystemCapability.Communication.IPC.Core 5720 5721**参数:** 5722 5723| 参数名 | 类型 | 必填 | 说明 | 5724| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5725| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要写入的可序列化对象数组。 | 5726 5727**返回值:** 5728 5729 | 类型 | 说明 | 5730 | ------- | -------------------------------- | 5731 | boolean | true:写入成功,false:写入失败。| 5732 5733**示例:** 5734 5735 ```ts 5736 import { hilog } from '@kit.PerformanceAnalysisKit'; 5737 5738 class MySequenceable implements rpc.Sequenceable { 5739 num: number = 0; 5740 str: string = ''; 5741 constructor(num: number, str: string) { 5742 this.num = num; 5743 this.str = str; 5744 } 5745 marshalling(messageParcel: rpc.MessageParcel): boolean { 5746 messageParcel.writeInt(this.num); 5747 messageParcel.writeString(this.str); 5748 return true; 5749 } 5750 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5751 this.num = messageParcel.readInt(); 5752 this.str = messageParcel.readString(); 5753 return true; 5754 } 5755 } 5756 let sequenceable = new MySequenceable(1, "aaa"); 5757 let sequenceable2 = new MySequenceable(2, "bbb"); 5758 let sequenceable3 = new MySequenceable(3, "ccc"); 5759 let a = [sequenceable, sequenceable2, sequenceable3]; 5760 let data = rpc.MessageParcel.create(); 5761 let result = data.writeSequenceableArray(a); 5762 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5763 ``` 5764 5765### readSequenceableArray<sup>8+</sup> 5766 5767readSequenceableArray(sequenceableArray: Sequenceable[]): void 5768 5769从MessageParcel实例读取可序列化对象数组。 5770 5771**系统能力**:SystemCapability.Communication.IPC.Core 5772 5773**参数:** 5774 5775| 参数名 | 类型 | 必填 | 说明 | 5776| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5777| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要读取的可序列化对象数组。 | 5778 5779**示例:** 5780 5781 ```ts 5782 import { hilog } from '@kit.PerformanceAnalysisKit'; 5783 5784 class MySequenceable implements rpc.Sequenceable { 5785 num: number = 0; 5786 str: string = ''; 5787 constructor(num: number, str: string) { 5788 this.num = num; 5789 this.str = str; 5790 } 5791 marshalling(messageParcel: rpc.MessageParcel): boolean { 5792 messageParcel.writeInt(this.num); 5793 messageParcel.writeString(this.str); 5794 return true; 5795 } 5796 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5797 this.num = messageParcel.readInt(); 5798 this.str = messageParcel.readString(); 5799 return true; 5800 } 5801 } 5802 let sequenceable = new MySequenceable(1, "aaa"); 5803 let sequenceable2 = new MySequenceable(2, "bbb"); 5804 let sequenceable3 = new MySequenceable(3, "ccc"); 5805 let a = [sequenceable, sequenceable2, sequenceable3]; 5806 let data = rpc.MessageParcel.create(); 5807 let result = data.writeSequenceableArray(a); 5808 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5809 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5810 data.readSequenceableArray(b); 5811 ``` 5812 5813### writeRemoteObjectArray<sup>8+</sup> 5814 5815writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5816 5817将IRemoteObject对象数组写入MessageParcel。 5818 5819**系统能力**:SystemCapability.Communication.IPC.Core 5820 5821**参数:** 5822 5823 | 参数名 | 类型 | 必填 | 说明 | 5824 | ----------- | --------------- | ---- | ----- | 5825 | objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageParcel的IRemoteObject对象数组。 | 5826 5827**返回值:** 5828 5829 | 类型 | 说明 | 5830 | ------- | -------------------------------- | 5831 | boolean | true:写入成功,false:写入失败。| 5832 5833**示例:** 5834 5835 ```ts 5836 import { hilog } from '@kit.PerformanceAnalysisKit'; 5837 5838 class MyDeathRecipient implements rpc.DeathRecipient { 5839 onRemoteDied() { 5840 hilog.info(0x0000, 'testTag', 'server died'); 5841 } 5842 } 5843 class TestRemoteObject extends rpc.RemoteObject { 5844 constructor(descriptor: string) { 5845 super(descriptor); 5846 this.attachLocalInterface(this, descriptor); 5847 } 5848 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5849 return true; 5850 } 5851 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5852 return true; 5853 } 5854 isObjectDead(): boolean { 5855 return false; 5856 } 5857 asObject(): rpc.IRemoteObject { 5858 return this; 5859 } 5860 } 5861 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5862 let data = rpc.MessageParcel.create(); 5863 let result = data.writeRemoteObjectArray(a); 5864 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5865 ``` 5866 5867### readRemoteObjectArray<sup>8+</sup> 5868 5869readRemoteObjectArray(objects: IRemoteObject[]): void 5870 5871从MessageParcel读取IRemoteObject对象数组。 5872 5873**系统能力**:SystemCapability.Communication.IPC.Core 5874 5875**参数:** 5876 5877 | 参数名 | 类型 | 必填 | 说明 | 5878 | ------- | --------------- | ---- | --------- | 5879 | objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageParcel读取的IRemoteObject对象数组。 | 5880 5881**示例:** 5882 5883 ```ts 5884 import { hilog } from '@kit.PerformanceAnalysisKit'; 5885 5886 class MyDeathRecipient implements rpc.DeathRecipient { 5887 onRemoteDied() { 5888 hilog.info(0x0000, 'testTag', 'server died'); 5889 } 5890 } 5891 class TestRemoteObject extends rpc.RemoteObject { 5892 constructor(descriptor: string) { 5893 super(descriptor); 5894 this.attachLocalInterface(this, descriptor); 5895 } 5896 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5897 return true; 5898 } 5899 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5900 return true; 5901 } 5902 isObjectDead(): boolean { 5903 return false; 5904 } 5905 asObject(): rpc.IRemoteObject { 5906 return this; 5907 } 5908 } 5909 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5910 let data = rpc.MessageParcel.create(); 5911 data.writeRemoteObjectArray(a); 5912 let b: Array<rpc.IRemoteObject> = new Array(3); 5913 data.readRemoteObjectArray(b); 5914 ``` 5915 5916### readRemoteObjectArray<sup>8+</sup> 5917 5918readRemoteObjectArray(): IRemoteObject[] 5919 5920从MessageParcel读取IRemoteObject对象数组。 5921 5922**系统能力**:SystemCapability.Communication.IPC.Core 5923 5924**返回值:** 5925 5926 | 类型 | 说明 | 5927 | --------------- | --------------------------- | 5928 | [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 5929 5930**示例:** 5931 5932 ```ts 5933 import { hilog } from '@kit.PerformanceAnalysisKit'; 5934 5935 class MyDeathRecipient implements rpc.DeathRecipient { 5936 onRemoteDied() { 5937 hilog.info(0x0000, 'testTag', 'server died'); 5938 } 5939 } 5940 class TestRemoteObject extends rpc.RemoteObject { 5941 constructor(descriptor: string) { 5942 super(descriptor); 5943 this.attachLocalInterface(this, descriptor); 5944 } 5945 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5946 return true; 5947 } 5948 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5949 return true; 5950 } 5951 isObjectDead(): boolean { 5952 return false; 5953 } 5954 asObject(): rpc.IRemoteObject { 5955 return this; 5956 } 5957 } 5958 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5959 let data = rpc.MessageParcel.create(); 5960 let result = data.writeRemoteObjectArray(a); 5961 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5962 let b = data.readRemoteObjectArray(); 5963 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5964 ``` 5965 5966### closeFileDescriptor<sup>8+</sup> 5967 5968static closeFileDescriptor(fd: number): void 5969 5970静态方法,关闭给定的文件描述符。 5971 5972**系统能力**:SystemCapability.Communication.IPC.Core 5973 5974**参数:** 5975 5976 | 参数名 | 类型 | 必填 | 说明 | 5977 | ------ | ------ | ---- | -------------------- | 5978 | fd | number | 是 | 要关闭的文件描述符。 | 5979 5980**示例:** 5981 5982 ```ts 5983 import { fileIo } from '@kit.CoreFileKit'; 5984 5985 let filePath = "path/to/file"; 5986 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5987 rpc.MessageParcel.closeFileDescriptor(file.fd); 5988 ``` 5989 5990### dupFileDescriptor<sup>8+</sup> 5991 5992static dupFileDescriptor(fd: number) :number 5993 5994静态方法,复制给定的文件描述符。 5995 5996**系统能力**:SystemCapability.Communication.IPC.Core 5997 5998**参数:** 5999 6000 | 参数名 | 类型 | 必填 | 说明 | 6001 | ------ | ------ | ---- | ------------------------ | 6002 | fd | number | 是 | 表示已存在的文件描述符。 | 6003 6004**返回值:** 6005 6006 | 类型 | 说明 | 6007 | ------ | -------------------- | 6008 | number | 返回新的文件描述符。 | 6009 6010**示例:** 6011 6012 ```ts 6013 import { fileIo } from '@kit.CoreFileKit'; 6014 6015 let filePath = "path/to/file"; 6016 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6017 rpc.MessageParcel.dupFileDescriptor(file.fd); 6018 ``` 6019 6020### containFileDescriptors<sup>8+</sup> 6021 6022containFileDescriptors(): boolean 6023 6024检查此MessageParcel对象是否包含文件描述符。 6025 6026**系统能力**:SystemCapability.Communication.IPC.Core 6027 6028**返回值:** 6029 6030 | 类型 | 说明 | 6031 | ------- | --------------------------------------------- | 6032 | boolean |true:包含文件描述符,false:未包含文件描述符。| 6033 6034**示例:** 6035 6036 ```ts 6037 import { fileIo } from '@kit.CoreFileKit'; 6038 import { hilog } from '@kit.PerformanceAnalysisKit'; 6039 6040 let parcel = new rpc.MessageParcel(); 6041 let filePath = "path/to/file"; 6042 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6043 let writeResult = parcel.writeFileDescriptor(file.fd); 6044 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6045 let containFD = parcel.containFileDescriptors(); 6046 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 6047 ``` 6048 6049### writeFileDescriptor<sup>8+</sup> 6050 6051writeFileDescriptor(fd: number): boolean 6052 6053写入文件描述符到MessageParcel。 6054 6055**系统能力**:SystemCapability.Communication.IPC.Core 6056 6057**参数:** 6058 6059 | 参数名 | 类型 | 必填 | 说明 | 6060 | ------ | ------ | ---- | ------------ | 6061 | fd | number | 是 | 文件描述符。 | 6062 6063**返回值:** 6064 6065 | 类型 | 说明 | 6066 | ------- | -------------------------------- | 6067 | boolean | true:操作成功,false:操作失败。| 6068 6069**示例:** 6070 6071 ```ts 6072 import { fileIo } from '@kit.CoreFileKit'; 6073 import { hilog } from '@kit.PerformanceAnalysisKit'; 6074 6075 let parcel = new rpc.MessageParcel(); 6076 let filePath = "path/to/file"; 6077 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6078 let writeResult = parcel.writeFileDescriptor(file.fd); 6079 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6080 ``` 6081 6082### readFileDescriptor<sup>8+</sup> 6083 6084readFileDescriptor(): number 6085 6086从MessageParcel中读取文件描述符。 6087 6088**系统能力**:SystemCapability.Communication.IPC.Core 6089 6090**返回值:** 6091 6092 | 类型 | 说明 | 6093 | ------ | ---------------- | 6094 | number | 返回文件描述符。 | 6095 6096**示例:** 6097 6098 ```ts 6099 import { fileIo } from '@kit.CoreFileKit'; 6100 import { hilog } from '@kit.PerformanceAnalysisKit'; 6101 6102 let parcel = new rpc.MessageParcel(); 6103 let filePath = "path/to/file"; 6104 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6105 parcel.writeFileDescriptor(file.fd); 6106 let readFD = parcel.readFileDescriptor(); 6107 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 6108 ``` 6109 6110### writeAshmem<sup>8+</sup> 6111 6112writeAshmem(ashmem: Ashmem): boolean 6113 6114将指定的匿名共享对象写入此MessageParcel。 6115 6116**系统能力**:SystemCapability.Communication.IPC.Core 6117 6118**参数:** 6119 6120| 参数名 | 类型 | 必填 | 说明 | 6121| ------ | ------ | ---- | ----------------------------------- | 6122| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageParcel的匿名共享对象。 | 6123 6124**返回值:** 6125 6126 | 类型 | 说明 | 6127 | ------- | -------------------------------- | 6128 | boolean | true:写入成功,false:写入失败。| 6129 6130**示例:** 6131 6132 ```ts 6133 import { hilog } from '@kit.PerformanceAnalysisKit'; 6134 6135 let parcel = new rpc.MessageParcel(); 6136 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6137 let isWriteSuccess = parcel.writeAshmem(ashmem); 6138 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6139 ``` 6140 6141### readAshmem<sup>8+</sup> 6142 6143readAshmem(): Ashmem 6144 6145从MessageParcel读取匿名共享对象。 6146 6147**系统能力**:SystemCapability.Communication.IPC.Core 6148 6149**返回值:** 6150 6151| 类型 | 说明 | 6152| ------ | ------------------ | 6153| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 6154 6155**示例:** 6156 6157 ```ts 6158 import { hilog } from '@kit.PerformanceAnalysisKit'; 6159 6160 let parcel = new rpc.MessageParcel(); 6161 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6162 let isWriteSuccess = parcel.writeAshmem(ashmem); 6163 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6164 let readAshmem = parcel.readAshmem(); 6165 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 6166 ``` 6167 6168### getRawDataCapacity<sup>8+</sup> 6169 6170getRawDataCapacity(): number 6171 6172获取MessageParcel可以容纳的最大原始数据量。 6173 6174**系统能力**:SystemCapability.Communication.IPC.Core 6175 6176**返回值:** 6177 6178 | 类型 | 说明 | 6179 | ------ | ---------------------------------------------------------- | 6180 | number | 返回MessageParcel可以容纳的最大原始数据量,即128MB。 | 6181 6182**示例:** 6183 6184 ```ts 6185 import { hilog } from '@kit.PerformanceAnalysisKit'; 6186 6187 let parcel = new rpc.MessageParcel(); 6188 let result = parcel.getRawDataCapacity(); 6189 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 6190 ``` 6191 6192### writeRawData<sup>8+</sup> 6193 6194writeRawData(rawData: number[], size: number): boolean 6195 6196将原始数据写入MessageParcel对象。 6197 6198**系统能力**:SystemCapability.Communication.IPC.Core 6199 6200**参数:** 6201 6202 | 参数名 | 类型 | 必填 | 说明 | 6203 | ------- | -------- | ---- | ---------------------------------- | 6204 | rawData | number[] | 是 | 要写入的原始数据。 | 6205 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 6206 6207**返回值:** 6208 6209 | 类型 | 说明 | 6210 | ------- | -------------------------------- | 6211 | boolean | true:写入成功,false:写入失败。| 6212 6213**示例:** 6214 6215 ```ts 6216 import { hilog } from '@kit.PerformanceAnalysisKit'; 6217 6218 let parcel = new rpc.MessageParcel(); 6219 let arr = [1, 2, 3, 4, 5]; 6220 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6221 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6222 ``` 6223 6224### readRawData<sup>8+</sup> 6225 6226readRawData(size: number): number[] 6227 6228从MessageParcel读取原始数据。 6229 6230**系统能力**:SystemCapability.Communication.IPC.Core 6231 6232**参数:** 6233 6234 | 参数名 | 类型 | 必填 | 说明 | 6235 | ------ | ------ | ---- | ------------------------ | 6236 | size | number | 是 | 要读取的原始数据的大小。 | 6237 6238**返回值:** 6239 6240 | 类型 | 说明 | 6241 | -------- | ------------------------------ | 6242 | number[] | 返回原始数据(以字节为单位)。 | 6243 6244**示例:** 6245 6246 ```ts 6247 import { hilog } from '@kit.PerformanceAnalysisKit'; 6248 6249 let parcel = new rpc.MessageParcel(); 6250 let arr = [1, 2, 3, 4, 5]; 6251 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6252 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6253 let result = parcel.readRawData(5); 6254 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 6255 ``` 6256 6257## Parcelable<sup>9+</sup> 6258 6259在进程间通信(IPC)期间,将类的对象写入MessageSequence并从MessageSequence中恢复它们。 6260 6261### marshalling 6262 6263marshalling(dataOut: MessageSequence): boolean 6264 6265将此可序列对象封送到MessageSequence中。 6266 6267**系统能力**:SystemCapability.Communication.IPC.Core 6268 6269**参数:** 6270 6271| 参数名 | 类型 | 必填 | 说明 | 6272| ------- | --------------- | ---- | ------------------------------------------- | 6273| dataOut |[MessageSequence](#messagesequence9)| 是 | 可序列对象将被封送到的MessageSequence对象。 | 6274 6275**返回值:** 6276 6277 | 类型 | 说明 | 6278 | ------- | -------------------------------- | 6279 | boolean | true:封送成功,false:封送失败。| 6280 6281**示例:** 6282 6283 ```ts 6284 import { hilog } from '@kit.PerformanceAnalysisKit'; 6285 6286 class MyParcelable implements rpc.Parcelable { 6287 num: number = 0; 6288 str: string = ''; 6289 constructor(num: number, str: string) { 6290 this.num = num; 6291 this.str = str; 6292 } 6293 marshalling(messageSequence: rpc.MessageSequence): boolean { 6294 messageSequence.writeInt(this.num); 6295 messageSequence.writeString(this.str); 6296 return true; 6297 } 6298 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6299 this.num = messageSequence.readInt(); 6300 this.str = messageSequence.readString(); 6301 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6302 return true; 6303 } 6304 } 6305 let parcelable = new MyParcelable(1, "aaa"); 6306 let data = rpc.MessageSequence.create(); 6307 data.writeParcelable(parcelable); 6308 let ret = new MyParcelable(0, ""); 6309 data.readParcelable(ret); 6310 ``` 6311 6312### unmarshalling 6313 6314unmarshalling(dataIn: MessageSequence): boolean 6315 6316从MessageSequence中解封此可序列对象。 6317 6318**系统能力**:SystemCapability.Communication.IPC.Core 6319 6320**参数:** 6321 6322| 参数名 | 类型 | 必填 | 说明 | 6323| ------ | --------------- | ---- | ----------------------------------------------- | 6324| dataIn | [MessageSequence](#messagesequence9) | 是 | 已将可序列对象封送到其中的MessageSequence对象。 | 6325 6326**返回值:** 6327 6328 | 类型 | 说明 | 6329 | ------- | ---------------------------------------- | 6330 | boolean | true:反序列化成功,false:反序列化失败。| 6331 6332**示例:** 6333 6334 ```ts 6335 import { hilog } from '@kit.PerformanceAnalysisKit'; 6336 6337 class MyParcelable implements rpc.Parcelable { 6338 num: number = 0; 6339 str: string = ''; 6340 constructor(num: number, str: string) { 6341 this.num = num; 6342 this.str = str; 6343 } 6344 marshalling(messageSequence: rpc.MessageSequence): boolean { 6345 messageSequence.writeInt(this.num); 6346 messageSequence.writeString(this.str); 6347 return true; 6348 } 6349 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6350 this.num = messageSequence.readInt(); 6351 this.str = messageSequence.readString(); 6352 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6353 return true; 6354 } 6355 } 6356 let parcelable = new MyParcelable(1, "aaa"); 6357 let data = rpc.MessageSequence.create(); 6358 data.writeParcelable(parcelable); 6359 let ret = new MyParcelable(0, ""); 6360 data.readParcelable(ret); 6361 ``` 6362 6363## Sequenceable<sup>(deprecated)</sup> 6364 6365在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。 6366 6367> **说明:** 6368> 6369> 从API version 9 开始废弃,建议使用[Parcelable](#parcelable9)替代。 6370 6371### marshalling 6372 6373marshalling(dataOut: MessageParcel): boolean 6374 6375将此可序列对象封送到MessageParcel中。 6376 6377**系统能力**:SystemCapability.Communication.IPC.Core 6378 6379**参数:** 6380 6381 | 参数名 | 类型 | 必填 | 说明 | 6382 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6383 | dataOut | [MessageParcel](#messageparceldeprecated) | 是 | 可序列对象将被封送到的MessageParcel对象。 | 6384 6385**返回值:** 6386 6387 | 类型 | 说明 | 6388 | ------- | -------------------------------- | 6389 | boolean | true:封送成功,false:封送失败。 | 6390 6391**示例:** 6392 6393 ```ts 6394 import { hilog } from '@kit.PerformanceAnalysisKit'; 6395 6396 class MySequenceable implements rpc.Sequenceable { 6397 num: number = 0; 6398 str: string = ''; 6399 constructor(num: number, str: string) { 6400 this.num = num; 6401 this.str = str; 6402 } 6403 marshalling(messageParcel: rpc.MessageParcel): boolean { 6404 messageParcel.writeInt(this.num); 6405 messageParcel.writeString(this.str); 6406 return true; 6407 } 6408 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6409 this.num = messageParcel.readInt(); 6410 this.str = messageParcel.readString(); 6411 return true; 6412 } 6413 } 6414 let sequenceable = new MySequenceable(1, "aaa"); 6415 let data = rpc.MessageParcel.create(); 6416 let result = data.writeSequenceable(sequenceable); 6417 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6418 let ret = new MySequenceable(0, ""); 6419 let result2 = data.readSequenceable(ret); 6420 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6421 ``` 6422 6423### unmarshalling 6424 6425unmarshalling(dataIn: MessageParcel): boolean 6426 6427从MessageParcel中解封此可序列对象。 6428 6429**系统能力**:SystemCapability.Communication.IPC.Core 6430 6431**参数:** 6432 6433 | 参数名 | 类型 | 必填 | 说明 | 6434 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6435 | dataIn | [MessageParcel](#messageparceldeprecated) | 是 | 已将可序列对象封送到其中的MessageParcel对象。 | 6436 6437**返回值:** 6438 6439 | 类型 | 说明 | 6440 | ------- | ---------------------------------------- | 6441 | boolean | true:反序列化成功,false:反序列化失败。| 6442 6443**示例:** 6444 6445 ```ts 6446 import { hilog } from '@kit.PerformanceAnalysisKit'; 6447 6448 class MySequenceable implements rpc.Sequenceable { 6449 num: number = 0; 6450 str: string = ''; 6451 constructor(num: number, str: string) { 6452 this.num = num; 6453 this.str = str; 6454 } 6455 marshalling(messageParcel: rpc.MessageParcel): boolean { 6456 messageParcel.writeInt(this.num); 6457 messageParcel.writeString(this.str); 6458 return true; 6459 } 6460 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6461 this.num = messageParcel.readInt(); 6462 this.str = messageParcel.readString(); 6463 return true; 6464 } 6465 } 6466 let sequenceable = new MySequenceable(1, "aaa"); 6467 let data = rpc.MessageParcel.create(); 6468 let result = data.writeSequenceable(sequenceable); 6469 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6470 let ret = new MySequenceable(0, ""); 6471 let result2 = data.readSequenceable(ret); 6472 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6473 ``` 6474 6475## IRemoteBroker 6476 6477远端对象的代理持有者。用于获取代理对象。 6478 6479### asObject 6480 6481asObject(): IRemoteObject 6482 6483需派生类实现,获取代理或远端对象。 6484 6485**系统能力**:SystemCapability.Communication.IPC.Core 6486 6487**返回值:** 6488 6489 | 类型 | 说明 | 6490 | ----- | ----- | 6491 | [IRemoteObject](#iremoteobject) | 如果调用者是RemoteObject对象,则直接返回本身;如果调用者是[RemoteProxy](#remoteproxy)对象,则返回它的持有者[IRemoteObject](#iremoteobject)。 | 6492 6493**示例:** 6494 6495 ```ts 6496 class TestAbility extends rpc.RemoteObject { 6497 asObject() { 6498 return this; 6499 } 6500 } 6501 let remoteObject = new TestAbility("testObject").asObject(); 6502 ``` 6503 6504**示例:** 6505 6506>**说明:** 6507> 6508>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 6509 6510 ```ts 6511 // FA模型需要从@kit.AbilityKit导入featureAbility 6512 // import { featureAbility } from '@kit.AbilityKit'; 6513 import { Want, common } from '@kit.AbilityKit'; 6514 import { hilog } from '@kit.PerformanceAnalysisKit'; 6515 6516 let proxy: rpc.IRemoteObject | undefined; 6517 let connect: common.ConnectOptions = { 6518 onConnect: (elementName, remoteProxy) => { 6519 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6520 proxy = remoteProxy; 6521 }, 6522 onDisconnect: (elementName) => { 6523 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6524 }, 6525 onFailed: () => { 6526 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6527 } 6528 }; 6529 let want: Want = { 6530 bundleName: "com.ohos.server", 6531 abilityName: "com.ohos.server.EntryAbility", 6532 }; 6533 6534 // FA模型使用此方法连接服务 6535 // FA.connectAbility(want,connect); 6536 6537 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6538 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 6539 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6540 let connectionId = context.connectServiceExtensionAbility(want, connect); 6541 ``` 6542 6543 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象 6544 6545 ```ts 6546 class TestProxy { 6547 remote: rpc.IRemoteObject; 6548 constructor(remote: rpc.IRemoteObject) { 6549 this.remote = remote; 6550 } 6551 asObject() { 6552 return this.remote; 6553 } 6554 } 6555 if (proxy != undefined) { 6556 let iRemoteObject = new TestProxy(proxy).asObject(); 6557 } 6558 ``` 6559 6560## DeathRecipient 6561 6562用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied](#onremotedied)接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。 6563 6564### onRemoteDied 6565 6566onRemoteDied(): void 6567 6568在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。 6569 6570**系统能力**:SystemCapability.Communication.IPC.Core 6571 6572**示例:** 6573 6574 ```ts 6575 import { hilog } from '@kit.PerformanceAnalysisKit'; 6576 6577 class MyDeathRecipient implements rpc.DeathRecipient { 6578 onRemoteDied() { 6579 hilog.info(0x0000, 'testTag', 'server died'); 6580 } 6581 } 6582 ``` 6583 6584## RequestResult<sup>9+</sup> 6585 6586发送请求的响应结果。 6587 6588**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6589 6590| 名称 | 类型 | 可读 | 可写 | 说明 | 6591| ------- | --------------- | ---- | ---- |-------------------------------------- | 6592| errCode | number | 是 | 否 | 错误码。 | 6593| code | number | 是 | 否 | 消息代码。 | 6594| data | [MessageSequence](#messagesequence9) | 是 | 否 | 发送给对端进程的MessageSequence对象。 | 6595| reply | [MessageSequence](#messagesequence9) | 是 | 否 | 对端进程返回的MessageSequence对象。 | 6596 6597## SendRequestResult<sup>(deprecated)</sup> 6598 6599发送请求的响应结果。 6600 6601> **说明:** 6602> 6603> 从API version 8 开始支持,API version 9 开始废弃,建议使用[RequestResult](#requestresult9)替代。 6604 6605**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6606 6607 | 名称 | 类型 | 可读 | 可写 | 说明 | 6608 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6609 | errCode | number | 是 | 否 | 错误码。 | 6610 | code | number | 是 | 否 | 消息代码。 | 6611 | data | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 发送给对端进程的MessageParcel对象。 | 6612 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 对端进程返回的MessageParcel对象。 | 6613 6614## IRemoteObject 6615 6616该接口可用于查询或获取接口描述符、添加或删除死亡通知、转储对象状态到特定文件、发送消息。 6617 6618### getLocalInterface<sup>9+</sup> 6619 6620getLocalInterface(descriptor: string): IRemoteBroker 6621 6622查询接口描述符的字符串。 6623 6624**系统能力**:SystemCapability.Communication.IPC.Core 6625 6626**参数:** 6627 6628 | 参数名 | 类型 | 必填 | 说明 | 6629 | ---------- | ------ | ---- | -------------------- | 6630 | descriptor | string | 是 | 接口描述符的字符串。 | 6631 6632**返回值:** 6633 6634| 类型 | 说明 | 6635| ------------- | --------------------------------------------- | 6636| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6637 6638**错误码:** 6639 6640以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6641 6642 | 错误码ID | 错误信息 | 6643 | -------- | -------- | 6644 | 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. | 6645 6646### queryLocalInterface<sup>(deprecated)</sup> 6647 6648queryLocalInterface(descriptor: string): IRemoteBroker 6649 6650查询接口描述符的字符串。 6651 6652> **说明:** 6653> 6654> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9)替代。 6655 6656**系统能力**:SystemCapability.Communication.IPC.Core 6657 6658**参数:** 6659 6660 | 参数名 | 类型 | 必填 | 说明 | 6661 | ---------- | ------ | ---- | -------------------- | 6662 | descriptor | string | 是 | 接口描述符的字符串。 | 6663 6664**返回值:** 6665 6666| 类型 | 说明 | 6667| ------------- | --------------------------------------------- | 6668| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 6669 6670### sendRequest<sup>(deprecated)</sup> 6671 6672sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6673 6674以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6675 6676> **说明:** 6677> 6678> 从API version 9开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 6679 6680**系统能力**:SystemCapability.Communication.IPC.Core 6681 6682**参数:** 6683 6684 | 参数名 | 类型 | 必填 | 说明 | 6685 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6686 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6687 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6688 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6689 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6690 6691**返回值:** 6692 6693 | 类型 | 说明 | 6694 | ------- | -------------------------------- | 6695 | boolean | true:发送成功,false:发送失败。| 6696 6697### sendMessageRequest<sup>9+</sup> 6698 6699sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6700 6701以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 6702 6703**系统能力**:SystemCapability.Communication.IPC.Core 6704 6705**参数:** 6706 6707 | 参数名 | 类型 | 必填 | 说明 | 6708 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6709 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6710 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 6711 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6712 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6713 6714**返回值:** 6715 6716 | 类型 | 说明 | 6717 | ---------------------------- | ----------------------------------------- | 6718 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 6719 6720**错误码:** 6721 6722以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6723 6724 | 错误码ID | 错误信息 | 6725 | -------- | -------- | 6726 | 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. | 6727 6728### sendRequest<sup>(deprecated)</sup> 6729 6730sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6731 6732以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6733 6734> **说明:** 6735> 6736> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 6737 6738**系统能力**:SystemCapability.Communication.IPC.Core 6739 6740**参数:** 6741 6742 | 参数名 | 类型 | 必填 | 说明 | 6743 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6744 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6745 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6746 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6747 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6748 6749**返回值:** 6750 6751| 类型 | 说明 | 6752| ------------------------------------------------------------ | --------------------------------------------- | 6753| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 6754 6755### sendMessageRequest<sup>9+</sup> 6756 6757sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6758 6759以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6760 6761**系统能力**:SystemCapability.Communication.IPC.Core 6762 6763**参数:** 6764 6765 | 参数名 | 类型 | 必填 | 说明 | 6766 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6767 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6768 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 6769 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 6770 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6771 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 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.Failed to obtain the passed object instance. | 6780 6781### sendRequest<sup>(deprecated)</sup> 6782 6783sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6784 6785以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 6786 6787> **说明:** 6788> 6789> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-1)替代。 6790 6791**系统能力**:SystemCapability.Communication.IPC.Core 6792 6793**参数:** 6794 6795| 参数名 | 类型 | 必填 | 说明 | 6796| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6797| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6798| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6799| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6800| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6801| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 6802 6803### registerDeathRecipient<sup>9+</sup> 6804 6805registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6806 6807注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6808 6809**系统能力**:SystemCapability.Communication.IPC.Core 6810 6811**参数:** 6812 6813 | 参数名 | 类型 | 必填 | 说明 | 6814 | --------- | --------------------------------- | ---- | -------------- | 6815 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 6816 | flags | number | 是 | 死亡通知标志。 | 6817 6818**错误码:** 6819 6820以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6821 6822 | 错误码ID | 错误信息 | 6823 | -------- | -------- | 6824 | 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. | 6825 | 1900008 | The proxy or remote object is invalid. | 6826 6827### addDeathRecipient<sup>(deprecated)</sup> 6828 6829addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6830 6831注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 6832 6833> **说明:** 6834> 6835> 从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9)替代。 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### unregisterDeathRecipient<sup>9+</sup> 6853 6854unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6855 6856注销用于接收远程对象死亡通知的回调。 6857 6858**系统能力**:SystemCapability.Communication.IPC.Core 6859 6860**参数:** 6861 6862 | 参数名 | 类型 | 必填 | 说明 | 6863 | --------- | --------------------------------- | ---- | -------------- | 6864 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6865 | flags | number | 是 | 死亡通知标志。 | 6866 6867**错误码:** 6868 6869以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6870 6871 | 错误码ID | 错误信息 | 6872 | -------- | -------- | 6873 | 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. | 6874 | 1900008 | The proxy or remote object is invalid. | 6875 6876### removeDeathRecipient<sup>(deprecated)</sup> 6877 6878removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6879 6880注销用于接收远程对象死亡通知的回调。 6881 6882> **说明:** 6883> 6884> 从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)替代。 6885 6886**系统能力**:SystemCapability.Communication.IPC.Core 6887 6888**参数:** 6889 6890 | 参数名 | 类型 | 必填 | 说明 | 6891 | --------- | --------------------------------- | ---- | -------------- | 6892 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 6893 | flags | number | 是 | 死亡通知标志。 | 6894 6895**返回值:** 6896 6897 | 类型 | 说明 | 6898 | ------- | -----------------------------------------| 6899 | boolean | true:回调注销成功,false:回调注销失败。| 6900 6901### getDescriptor<sup>9+</sup> 6902 6903getDescriptor(): string 6904 6905获取对象的接口描述符,接口描述符为字符串。 6906 6907**系统能力**:SystemCapability.Communication.IPC.Core 6908 6909**返回值:** 6910 6911 | 类型 | 说明 | 6912 | ------ | ---------------- | 6913 | string | 返回接口描述符。 | 6914 6915**错误码:** 6916 6917以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 6918 6919 | 错误码ID | 错误信息 | 6920 | -------- | -------- | 6921 | 1900008 | The proxy or remote object is invalid. | 6922 6923### getInterfaceDescriptor<sup>(deprecated)</sup> 6924 6925getInterfaceDescriptor(): string 6926 6927获取对象的接口描述符,接口描述符为字符串。 6928 6929> **说明:** 6930> 6931> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9)替代。 6932 6933**系统能力**:SystemCapability.Communication.IPC.Core 6934 6935**返回值:** 6936 6937 | 类型 | 说明 | 6938 | ------ | ---------------- | 6939 | string | 返回接口描述符。 | 6940 6941### isObjectDead 6942 6943isObjectDead(): boolean 6944 6945检查当前对象是否死亡。 6946 6947**系统能力**:SystemCapability.Communication.IPC.Core 6948 6949**返回值:** 6950 6951 | 类型 | 说明 | 6952 | ------- | ---------------------------------- | 6953 | boolean | true:对象死亡,false:对象未死亡。| 6954 6955## RemoteProxy 6956 6957实现IRemoteObject代理对象。 6958 6959### 属性 6960 6961**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6962 6963 | 名称 | 类型 | 可读 | 可写 | 说明 | 6964 | --------------------- | -------| ------|------|------------------------------------------ | 6965 | PING_TRANSACTION | number | 是 | 否 | 内部指令码,用于测试IPC服务是否正常。 | 6966 | DUMP_TRANSACTION | number | 是 | 否 | 内部指令码,获取IPC服务相关的状态信息。 | 6967 | INTERFACE_TRANSACTION | number | 是 | 否 | 内部指令码,获取对端接口描述符。 | 6968 | MIN_TRANSACTION_ID | number | 是 | 否 | 最小有效指令码。 | 6969 | MAX_TRANSACTION_ID | number | 是 | 否 | 最大有效指令码。 | 6970 6971 6972### sendRequest<sup>(deprecated)</sup> 6973 6974sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6975 6976以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 6977 6978> **说明:** 6979> 6980> 从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 6981 6982**系统能力**:SystemCapability.Communication.IPC.Core 6983 6984**参数:** 6985 6986 | 参数名 | 类型 | 必填 | 说明 | 6987 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6988 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 6989 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 6990 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 6991 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 6992 6993**返回值:** 6994 6995 | 类型 | 说明 | 6996 | ------- | ---------------------------------| 6997 | boolean | true:发送成功,false:发送失败。| 6998 6999**示例:** 7000 7001>**说明:** 7002> 7003>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7004 7005 ```ts 7006 // FA模型需要从@kit.AbilityKit导入featureAbility 7007 // import { featureAbility } from '@kit.AbilityKit'; 7008 import { Want, common } from '@kit.AbilityKit'; 7009 import { hilog } from '@kit.PerformanceAnalysisKit'; 7010 7011 let proxy: rpc.IRemoteObject | undefined; 7012 let connect: common.ConnectOptions = { 7013 onConnect: (elementName, remoteProxy) => { 7014 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7015 proxy = remoteProxy; 7016 }, 7017 onDisconnect: (elementName) => { 7018 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7019 }, 7020 onFailed: () => { 7021 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7022 } 7023 }; 7024 let want: Want = { 7025 bundleName: "com.ohos.server", 7026 abilityName: "com.ohos.server.EntryAbility", 7027 }; 7028 7029 // FA模型使用此方法连接服务 7030 // FA.connectAbility(want,connect); 7031 7032 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7033 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7034 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7035 let connectionId = context.connectServiceExtensionAbility(want, connect); 7036 ``` 7037 7038 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7039 7040 ```ts 7041 import { hilog } from '@kit.PerformanceAnalysisKit'; 7042 7043 let option = new rpc.MessageOption(); 7044 let data = rpc.MessageParcel.create(); 7045 let reply = rpc.MessageParcel.create(); 7046 data.writeInt(1); 7047 data.writeString("hello"); 7048 if (proxy != undefined) { 7049 let ret: boolean = proxy.sendRequest(1, data, reply, option); 7050 if (ret) { 7051 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7052 let msg = reply.readString(); 7053 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7054 } else { 7055 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 7056 } 7057 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7058 data.reclaim(); 7059 reply.reclaim(); 7060 } 7061 ``` 7062 7063### sendMessageRequest<sup>9+</sup> 7064 7065sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7066 7067以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 7068 7069**系统能力**:SystemCapability.Communication.IPC.Core 7070 7071**参数:** 7072 7073 | 参数名 | 类型 | 必填 | 说明 | 7074 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7075 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7076 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7077 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7078 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7079 7080**返回值:** 7081 7082 | 类型 | 说明 | 7083 | ---------------------------- | ----------------------------------------- | 7084 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 7085 7086 7087**错误码:** 7088 7089以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7090 7091 | 错误码ID | 错误信息 | 7092 | -------- | -------- | 7093 | 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. | 7094 7095**示例:** 7096 7097>**说明:** 7098> 7099>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7100 7101 ```ts 7102 // FA模型需要从@kit.AbilityKit导入featureAbility 7103 // import { featureAbility } from '@kit.AbilityKit'; 7104 import { Want, common } from '@kit.AbilityKit'; 7105 import { hilog } from '@kit.PerformanceAnalysisKit'; 7106 7107 let proxy: rpc.IRemoteObject | undefined; 7108 let connect: common.ConnectOptions = { 7109 onConnect: (elementName, remoteProxy) => { 7110 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7111 proxy = remoteProxy; 7112 }, 7113 onDisconnect: (elementName) => { 7114 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7115 }, 7116 onFailed: () => { 7117 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7118 } 7119 }; 7120 let want: Want = { 7121 bundleName: "com.ohos.server", 7122 abilityName: "com.ohos.server.EntryAbility", 7123 }; 7124 7125 // FA模型使用此方法连接服务 7126 // FA.connectAbility(want,connect); 7127 7128 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7129 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7130 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7131 let connectionId = context.connectServiceExtensionAbility(want, connect); 7132 ``` 7133 7134 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 7135 7136 ```ts 7137 import { hilog } from '@kit.PerformanceAnalysisKit'; 7138 7139 let option = new rpc.MessageOption(); 7140 let data = rpc.MessageSequence.create(); 7141 let reply = rpc.MessageSequence.create(); 7142 data.writeInt(1); 7143 data.writeString("hello"); 7144 if (proxy != undefined) { 7145 proxy.sendMessageRequest(1, data, reply, option) 7146 .then((result: rpc.RequestResult) => { 7147 if (result.errCode === 0) { 7148 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7149 let num = result.reply.readInt(); 7150 let msg = result.reply.readString(); 7151 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7152 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7153 } else { 7154 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7155 } 7156 }).catch((e: Error) => { 7157 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 7158 }).finally (() => { 7159 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7160 data.reclaim(); 7161 reply.reclaim(); 7162 }); 7163 } 7164 ``` 7165 7166### sendRequest<sup>(deprecated)</sup> 7167 7168sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7169 7170以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7171 7172> **说明:** 7173> 7174> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 7175 7176**系统能力**:SystemCapability.Communication.IPC.Core 7177 7178**参数:** 7179 7180 | 参数名 | 类型 | 必填 | 说明 | 7181 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7182 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7183 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7184 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7185 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7186 7187**返回值:** 7188 7189| 类型 | 说明 | 7190| ------------------------------------------------------------ | --------------------------------------------- | 7191| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 7192 7193**示例:** 7194 7195>**说明:** 7196> 7197>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7198 7199 ```ts 7200 // FA模型需要从@kit.AbilityKit导入featureAbility 7201 // import { featureAbility } from '@kit.AbilityKit'; 7202 import { Want, common } from '@kit.AbilityKit'; 7203 import { hilog } from '@kit.PerformanceAnalysisKit'; 7204 7205 let proxy: rpc.IRemoteObject | undefined; 7206 let connect: common.ConnectOptions = { 7207 onConnect: (elementName, remoteProxy) => { 7208 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7209 proxy = remoteProxy; 7210 }, 7211 onDisconnect: (elementName) => { 7212 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7213 }, 7214 onFailed: () => { 7215 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7216 } 7217 }; 7218 let want: Want = { 7219 bundleName: "com.ohos.server", 7220 abilityName: "com.ohos.server.EntryAbility", 7221 }; 7222 7223 // FA模型使用此方法连接服务 7224 // FA.connectAbility(want,connect); 7225 7226 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7227 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7228 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7229 let connectionId = context.connectServiceExtensionAbility(want, connect); 7230 ``` 7231 7232 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7233 7234 ```ts 7235 import { hilog } from '@kit.PerformanceAnalysisKit'; 7236 7237 let option = new rpc.MessageOption(); 7238 let data = rpc.MessageParcel.create(); 7239 let reply = rpc.MessageParcel.create(); 7240 data.writeInt(1); 7241 data.writeString("hello"); 7242 if (proxy != undefined) { 7243 let a = proxy.sendRequest(1, data, reply, option) as Object; 7244 let b = a as Promise<rpc.SendRequestResult>; 7245 b.then((result: rpc.SendRequestResult) => { 7246 if (result.errCode === 0) { 7247 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7248 let num = result.reply.readInt(); 7249 let msg = result.reply.readString(); 7250 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7251 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7252 } else { 7253 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7254 } 7255 }).catch((e: Error) => { 7256 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 7257 }).finally (() => { 7258 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7259 data.reclaim(); 7260 reply.reclaim(); 7261 }); 7262 } 7263 ``` 7264 7265### sendMessageRequest<sup>9+</sup> 7266 7267sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7268 7269以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回后的某个时机执行回调,回复内容在RequestResult的reply报文里。 7270 7271**系统能力**:SystemCapability.Communication.IPC.Core 7272 7273**参数:** 7274 7275 | 参数名 | 类型 | 必填 | 说明 | 7276 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7277 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7278 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7279 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7280 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7281 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 7282 7283 7284**错误码:** 7285 7286以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7287 7288 | 错误码ID | 错误信息 | 7289 | -------- | -------- | 7290 | 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. | 7291 7292### sendRequest<sup>(deprecated)</sup> 7293 7294sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7295 7296以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 7297 7298> **说明:** 7299> 7300> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-3)替代。 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### getLocalInterface<sup>9+</sup> 7315 7316getLocalInterface(interface: string): IRemoteBroker 7317 7318查询并获取当前接口描述符对应的本地接口对象。 7319 7320**系统能力**:SystemCapability.Communication.IPC.Core 7321 7322**参数:** 7323 7324 | 参数名 | 类型 | 必填 | 说明 | 7325 | --------- | ------ | ---- | ---------------------- | 7326 | interface | string | 是 | 需要查询的接口描述符。 | 7327 7328**返回值:** 7329 7330| 类型 | 说明 | 7331| ------------------------------- | ------------------------------------------ | 7332| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7333 7334**错误码:** 7335 7336以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7337 7338 | 错误码ID | 错误信息 | 7339 | -------- | -------- | 7340 | 401 | check param failed | 7341 | 1900006 | Operation allowed only for the remote object. | 7342 7343**示例:** 7344 7345>**说明:** 7346> 7347>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7348 7349 ```ts 7350 // FA模型需要从@kit.AbilityKit导入featureAbility 7351 // import { featureAbility } from '@kit.AbilityKit'; 7352 import { Want, common } from '@kit.AbilityKit'; 7353 import { hilog } from '@kit.PerformanceAnalysisKit'; 7354 7355 let proxy: rpc.IRemoteObject | undefined; 7356 let connect: common.ConnectOptions = { 7357 onConnect: (elementName, remoteProxy) => { 7358 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7359 proxy = remoteProxy; 7360 }, 7361 onDisconnect: (elementName) => { 7362 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7363 }, 7364 onFailed: () => { 7365 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7366 } 7367 }; 7368 let want: Want = { 7369 bundleName: "com.ohos.server", 7370 abilityName: "com.ohos.server.EntryAbility", 7371 }; 7372 7373 // FA模型使用此方法连接服务 7374 // FA.connectAbility(want,connect); 7375 7376 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7377 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7378 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7379 let connectionId = context.connectServiceExtensionAbility(want, connect); 7380 ``` 7381 7382 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象 7383 7384 ```ts 7385 import { hilog } from '@kit.PerformanceAnalysisKit'; 7386 import { BusinessError } from '@kit.BasicServicesKit'; 7387 7388 if (proxy != undefined) { 7389 try { 7390 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7391 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7392 } catch (error) { 7393 let e: BusinessError = error as BusinessError; 7394 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7395 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7396 } 7397 } 7398 ``` 7399 7400### queryLocalInterface<sup>(deprecated)</sup> 7401 7402queryLocalInterface(interface: string): IRemoteBroker 7403 7404查询并获取当前接口描述符对应的本地接口对象。 7405 7406> **说明:** 7407> 7408> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-1)替代。 7409 7410**系统能力**:SystemCapability.Communication.IPC.Core 7411 7412**参数:** 7413 7414 | 参数名 | 类型 | 必填 | 说明 | 7415 | --------- | ------ | ---- | ---------------------- | 7416 | interface | string | 是 | 需要查询的接口描述符。 | 7417 7418**返回值:** 7419 7420| 类型 | 说明 | 7421| ------------------------------- | ------------------------------------------ | 7422| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7423 7424**示例:** 7425 7426>**说明:** 7427> 7428>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7429 7430 ```ts 7431 // FA模型需要从@kit.AbilityKit导入featureAbility 7432 // import { featureAbility } from '@kit.AbilityKit'; 7433 import { Want, common } from '@kit.AbilityKit'; 7434 import { hilog } from '@kit.PerformanceAnalysisKit'; 7435 7436 let proxy: rpc.IRemoteObject | undefined; 7437 let connect: common.ConnectOptions = { 7438 onConnect: (elementName, remoteProxy) => { 7439 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7440 proxy = remoteProxy; 7441 }, 7442 onDisconnect: (elementName) => { 7443 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7444 }, 7445 onFailed: () => { 7446 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7447 } 7448 }; 7449 let want: Want = { 7450 bundleName: "com.ohos.server", 7451 abilityName: "com.ohos.server.EntryAbility", 7452 }; 7453 7454 // FA模型使用此方法连接服务 7455 // FA.connectAbility(want,connect); 7456 7457 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7458 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7459 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7460 let connectionId = context.connectServiceExtensionAbility(want, connect); 7461 ``` 7462 7463 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象 7464 7465 ```ts 7466 import { hilog } from '@kit.PerformanceAnalysisKit'; 7467 7468 if (proxy != undefined) { 7469 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7470 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7471 } 7472 ``` 7473 7474### registerDeathRecipient<sup>9+</sup> 7475 7476registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7477 7478注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7479 7480**系统能力**:SystemCapability.Communication.IPC.Core 7481 7482**参数:** 7483 7484 | 参数名 | 类型 | 必填 | 说明 | 7485 | --------- | --------------------------------- | ---- | -------------- | 7486 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7487 | flags | number | 是 | 死亡通知标志。 | 7488 7489**错误码:** 7490 7491以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7492 7493 | 错误码ID | 错误信息 | 7494 | -------- | -------- | 7495 | 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. | 7496 | 1900008 | The proxy or remote object is invalid. | 7497 7498**示例:** 7499 7500>**说明:** 7501> 7502>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7503 7504 ```ts 7505 // FA模型需要从@kit.AbilityKit导入featureAbility 7506 // import { featureAbility } from '@kit.AbilityKit'; 7507 import { Want, common } from '@kit.AbilityKit'; 7508 import { hilog } from '@kit.PerformanceAnalysisKit'; 7509 7510 let proxy: rpc.IRemoteObject | undefined; 7511 let connect: common.ConnectOptions = { 7512 onConnect: (elementName, remoteProxy) => { 7513 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7514 proxy = remoteProxy; 7515 }, 7516 onDisconnect: (elementName) => { 7517 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7518 }, 7519 onFailed: () => { 7520 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7521 } 7522 }; 7523 let want: Want = { 7524 bundleName: "com.ohos.server", 7525 abilityName: "com.ohos.server.EntryAbility", 7526 }; 7527 7528 // FA模型使用此方法连接服务 7529 // FA.connectAbility(want,connect); 7530 7531 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7532 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7533 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7534 let connectionId = context.connectServiceExtensionAbility(want, connect); 7535 ``` 7536 7537 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调 7538 7539 ```ts 7540 import { hilog } from '@kit.PerformanceAnalysisKit'; 7541 import { BusinessError } from '@kit.BasicServicesKit'; 7542 7543 class MyDeathRecipient implements rpc.DeathRecipient { 7544 onRemoteDied() { 7545 hilog.info(0x0000, 'testTag', 'server died'); 7546 } 7547 } 7548 let deathRecipient = new MyDeathRecipient(); 7549 if (proxy != undefined) { 7550 try { 7551 proxy.registerDeathRecipient(deathRecipient, 0); 7552 } catch (error) { 7553 let e: BusinessError = error as BusinessError; 7554 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7555 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7556 } 7557 } 7558 ``` 7559 7560### addDeathRecipient<sup>(deprecated)</sup> 7561 7562addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7563 7564注册用于接收远程对象死亡通知的回调,增加proxy对象上的死亡通知。 7565 7566> **说明:** 7567> 7568> 从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9-1)类替代。 7569 7570**系统能力**:SystemCapability.Communication.IPC.Core 7571 7572**参数:** 7573 7574 | 参数名 | 类型 | 必填 | 说明 | 7575 | --------- | --------------------------------- | ---- | --------------------------------- | 7576 | recipient | [DeathRecipient](#deathrecipient) | 是 | 收件人表示要注册的回调。 | 7577 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7578 7579**返回值:** 7580 7581 | 类型 | 说明 | 7582 | ------- | ---------------------------------------- | 7583 | boolean | true:回调注册成功,false:回调注册失败。| 7584 7585**示例:** 7586 7587>**说明:** 7588> 7589>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7590 7591 ```ts 7592 // FA模型需要从@kit.AbilityKit导入featureAbility 7593 // import { featureAbility } from '@kit.AbilityKit'; 7594 import { Want, common } from '@kit.AbilityKit'; 7595 import { hilog } from '@kit.PerformanceAnalysisKit'; 7596 7597 let proxy: rpc.IRemoteObject | undefined; 7598 let connect: common.ConnectOptions = { 7599 onConnect: (elementName, remoteProxy) => { 7600 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7601 proxy = remoteProxy; 7602 }, 7603 onDisconnect: (elementName) => { 7604 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7605 }, 7606 onFailed: () => { 7607 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7608 } 7609 }; 7610 let want: Want = { 7611 bundleName: "com.ohos.server", 7612 abilityName: "com.ohos.server.EntryAbility", 7613 }; 7614 7615 // FA模型使用此方法连接服务 7616 // FA.connectAbility(want,connect); 7617 7618 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7619 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7620 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7621 let connectionId = context.connectServiceExtensionAbility(want, connect); 7622 ``` 7623 7624 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecipient接口方法新增死亡回调 7625 7626 ```ts 7627 import { hilog } from '@kit.PerformanceAnalysisKit'; 7628 7629 class MyDeathRecipient implements rpc.DeathRecipient { 7630 onRemoteDied() { 7631 hilog.info(0x0000, 'testTag', 'server died'); 7632 } 7633 } 7634 let deathRecipient = new MyDeathRecipient(); 7635 if (proxy != undefined) { 7636 proxy.addDeathRecipient(deathRecipient, 0); 7637 } 7638 ``` 7639 7640### unregisterDeathRecipient<sup>9+</sup> 7641 7642unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7643 7644注销用于接收远程对象死亡通知的回调。 7645 7646**系统能力**:SystemCapability.Communication.IPC.Core 7647 7648**参数:** 7649 7650 | 参数名 | 类型 | 必填 | 说明 | 7651 | --------- | --------------------------------- | ---- | -------------- | 7652 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 7653 | flags | number | 是 | 死亡通知标志。 | 7654 7655**错误码:** 7656 7657以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7658 7659 | 错误码ID | 错误信息 | 7660 | -------- | -------- | 7661 | 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. | 7662 | 1900008 | The proxy or remote object is invalid. | 7663 7664**示例:** 7665 7666>**说明:** 7667> 7668>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7669 7670 ```ts 7671 // FA模型需要从@kit.AbilityKit导入featureAbility 7672 // import { featureAbility } from '@kit.AbilityKit'; 7673 import { Want, common } from '@kit.AbilityKit'; 7674 import { hilog } from '@kit.PerformanceAnalysisKit'; 7675 7676 let proxy: rpc.IRemoteObject | undefined; 7677 let connect: common.ConnectOptions = { 7678 onConnect: (elementName, remoteProxy) => { 7679 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7680 proxy = remoteProxy; 7681 }, 7682 onDisconnect: (elementName) => { 7683 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7684 }, 7685 onFailed: () => { 7686 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7687 } 7688 }; 7689 let want: Want = { 7690 bundleName: "com.ohos.server", 7691 abilityName: "com.ohos.server.EntryAbility", 7692 }; 7693 7694 // FA模型使用此方法连接服务 7695 // FA.connectAbility(want,connect); 7696 7697 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7698 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7699 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7700 let connectionId = context.connectServiceExtensionAbility(want, connect); 7701 ``` 7702 7703 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 7704 7705 ```ts 7706 import { hilog } from '@kit.PerformanceAnalysisKit'; 7707 import { BusinessError } from '@kit.BasicServicesKit'; 7708 7709 class MyDeathRecipient implements rpc.DeathRecipient { 7710 onRemoteDied() { 7711 hilog.info(0x0000, 'testTag', 'server died'); 7712 } 7713 } 7714 let deathRecipient = new MyDeathRecipient(); 7715 if (proxy != undefined) { 7716 try { 7717 proxy.registerDeathRecipient(deathRecipient, 0); 7718 proxy.unregisterDeathRecipient(deathRecipient, 0); 7719 } catch (error) { 7720 let e: BusinessError = error as BusinessError; 7721 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7722 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7723 } 7724 } 7725 ``` 7726 7727### removeDeathRecipient<sup>(deprecated)</sup> 7728 7729removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7730 7731注销用于接收远程对象死亡通知的回调。 7732 7733> **说明:** 7734> 7735> 从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9-1)替代。 7736 7737**系统能力**:SystemCapability.Communication.IPC.Core 7738 7739**参数:** 7740 7741 | 参数名 | 类型 | 必填 | 说明 | 7742 | --------- | --------------------------------- | ---- | --------------------------------- | 7743 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的死亡回调。 | 7744 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7745 7746**返回值:** 7747 7748 | 类型 | 说明 | 7749 | ------- | ---------------------------------------- | 7750 | boolean | true:回调注销成功,false:回调注销失败。| 7751 7752**示例:** 7753 7754>**说明:** 7755> 7756>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7757 7758 ```ts 7759 // FA模型需要从@kit.AbilityKit导入featureAbility 7760 // import { featureAbility } from '@kit.AbilityKit'; 7761 import { Want, common } from '@kit.AbilityKit'; 7762 import { hilog } from '@kit.PerformanceAnalysisKit'; 7763 7764 let proxy: rpc.IRemoteObject | undefined; 7765 let connect: common.ConnectOptions = { 7766 onConnect: (elementName, remoteProxy) => { 7767 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7768 proxy = remoteProxy; 7769 }, 7770 onDisconnect: (elementName) => { 7771 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7772 }, 7773 onFailed: () => { 7774 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7775 } 7776 }; 7777 let want: Want = { 7778 bundleName: "com.ohos.server", 7779 abilityName: "com.ohos.server.EntryAbility", 7780 }; 7781 7782 // FA模型使用此方法连接服务 7783 // FA.connectAbility(want,connect); 7784 7785 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7786 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7787 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7788 let connectionId = context.connectServiceExtensionAbility(want, connect); 7789 ``` 7790 7791 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调 7792 7793 ```ts 7794 import { hilog } from '@kit.PerformanceAnalysisKit'; 7795 7796 class MyDeathRecipient implements rpc.DeathRecipient { 7797 onRemoteDied() { 7798 hilog.info(0x0000, 'testTag', 'server died'); 7799 } 7800 } 7801 let deathRecipient = new MyDeathRecipient(); 7802 if (proxy != undefined) { 7803 proxy.addDeathRecipient(deathRecipient, 0); 7804 proxy.removeDeathRecipient(deathRecipient, 0); 7805 } 7806 ``` 7807 7808### getDescriptor<sup>9+</sup> 7809 7810getDescriptor(): string 7811 7812获取对象的接口描述符,接口描述符为字符串。 7813 7814**系统能力**:SystemCapability.Communication.IPC.Core 7815 7816**返回值:** 7817 7818 | 类型 | 说明 | 7819 | ------ | ---------------- | 7820 | string | 返回接口描述符。 | 7821 7822**错误码:** 7823 7824以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7825 7826 | 错误码ID | 错误信息 | 7827 | -------- | -------- | 7828 | 1900007 | communication failed. | 7829 | 1900008 | The proxy or remote object is invalid. | 7830 7831**示例:** 7832 7833>**说明:** 7834> 7835>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7836 7837 ```ts 7838 // FA模型需要从@kit.AbilityKit导入featureAbility 7839 // import { featureAbility } from '@kit.AbilityKit'; 7840 import { Want, common } from '@kit.AbilityKit'; 7841 import { hilog } from '@kit.PerformanceAnalysisKit'; 7842 7843 let proxy: rpc.IRemoteObject | undefined; 7844 let connect: common.ConnectOptions = { 7845 onConnect: (elementName, remoteProxy) => { 7846 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7847 proxy = remoteProxy; 7848 }, 7849 onDisconnect: (elementName) => { 7850 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7851 }, 7852 onFailed: () => { 7853 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7854 } 7855 }; 7856 let want: Want = { 7857 bundleName: "com.ohos.server", 7858 abilityName: "com.ohos.server.EntryAbility", 7859 }; 7860 7861 // FA模型使用此方法连接服务 7862 // FA.connectAbility(want,connect); 7863 7864 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7865 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7866 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7867 let connectionId = context.connectServiceExtensionAbility(want, connect); 7868 ``` 7869 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符 7870 7871 ```ts 7872 import { hilog } from '@kit.PerformanceAnalysisKit'; 7873 import { BusinessError } from '@kit.BasicServicesKit'; 7874 7875 if (proxy != undefined) { 7876 try { 7877 let descriptor: string = proxy.getDescriptor(); 7878 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7879 } catch (error) { 7880 let e: BusinessError = error as BusinessError; 7881 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7882 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7883 } 7884 } 7885 ``` 7886 7887### getInterfaceDescriptor<sup>(deprecated)</sup> 7888 7889getInterfaceDescriptor(): string 7890 7891查询当前代理对象接口的描述符。 7892 7893> **说明:** 7894> 7895> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-1)替代。 7896 7897**系统能力**:SystemCapability.Communication.IPC.Core 7898 7899**返回值:** 7900 7901 | 类型 | 说明 | 7902 | ------ | ------------------ | 7903 | string | 当前的接口描述符。 | 7904 7905**示例:** 7906 7907>**说明:** 7908> 7909>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7910 7911 ```ts 7912 // FA模型需要从@kit.AbilityKit导入featureAbility 7913 // import { featureAbility } from '@kit.AbilityKit'; 7914 import { Want, common } from '@kit.AbilityKit'; 7915 import { hilog } from '@kit.PerformanceAnalysisKit'; 7916 7917 let proxy: rpc.IRemoteObject | undefined; 7918 let connect: common.ConnectOptions = { 7919 onConnect: (elementName, remoteProxy) => { 7920 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7921 proxy = remoteProxy; 7922 }, 7923 onDisconnect: (elementName) => { 7924 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7925 }, 7926 onFailed: () => { 7927 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7928 } 7929 }; 7930 let want: Want = { 7931 bundleName: "com.ohos.server", 7932 abilityName: "com.ohos.server.EntryAbility", 7933 }; 7934 7935 // FA模型使用此方法连接服务 7936 // FA.connectAbility(want,connect); 7937 7938 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7939 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7940 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7941 let connectionId = context.connectServiceExtensionAbility(want, connect); 7942 ``` 7943 7944 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符 7945 7946 ```ts 7947 import { hilog } from '@kit.PerformanceAnalysisKit'; 7948 7949 if (proxy != undefined) { 7950 let descriptor: string = proxy.getInterfaceDescriptor(); 7951 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7952 } 7953 ``` 7954 7955### isObjectDead 7956 7957isObjectDead(): boolean 7958 7959指示对应的RemoteObject是否死亡。 7960 7961**系统能力**:SystemCapability.Communication.IPC.Core 7962 7963**返回值:** 7964 7965 | 类型 | 说明 | 7966 | ------- | ------------------------------------------------- | 7967 | boolean | true:对应的对象已经死亡,false:对应的对象未死亡。 | 7968 7969**示例:** 7970 7971>**说明:** 7972> 7973>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7974 7975 ```ts 7976 // FA模型需要从@kit.AbilityKit导入featureAbility 7977 // import { featureAbility } from '@kit.AbilityKit'; 7978 import { Want, common } from '@kit.AbilityKit'; 7979 import { hilog } from '@kit.PerformanceAnalysisKit'; 7980 7981 let proxy: rpc.IRemoteObject | undefined; 7982 let connect: common.ConnectOptions = { 7983 onConnect: (elementName, remoteProxy) => { 7984 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7985 proxy = remoteProxy; 7986 }, 7987 onDisconnect: (elementName) => { 7988 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7989 }, 7990 onFailed: () => { 7991 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7992 } 7993 }; 7994 let want: Want = { 7995 bundleName: "com.ohos.server", 7996 abilityName: "com.ohos.server.EntryAbility", 7997 }; 7998 7999 // FA模型使用此方法连接服务 8000 // FA.connectAbility(want,connect); 8001 8002 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8003 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8004 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8005 let connectionId = context.connectServiceExtensionAbility(want, connect); 8006 ``` 8007 8008 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡 8009 8010 ```ts 8011 import { hilog } from '@kit.PerformanceAnalysisKit'; 8012 8013 if (proxy != undefined) { 8014 let isDead: boolean = proxy.isObjectDead(); 8015 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 8016 } 8017 ``` 8018 8019## MessageOption 8020 8021公共消息选项,使用指定的标志类型,构造指定的MessageOption对象。 8022 8023### 属性 8024 8025**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 8026 8027 | 名称 | 类型 | 可读 | 可写 | 说明 | 8028 | ------------- | ------ | ----- | ----- | ------------------------------------------------------------------------ | 8029 | TF_SYNC | number | 是 | 否 | 同步调用标识。 | 8030 | TF_ASYNC | number | 是 | 否 | 异步调用标识。 | 8031 | TF_ACCEPT_FDS | number | 是 | 否 | 指示sendMessageRequest<sup>9+</sup>接口可以传递文件描述符。 | 8032 | TF_WAIT_TIME | number | 是 | 是 | RPC等待时间(单位/秒),IPC场景下无效。默认等待为8秒(不建议修改等待时间)。 | 8033 8034### constructor<sup>9+</sup> 8035 8036constructor(async?: boolean) 8037 8038MessageOption构造函数。 8039 8040**系统能力**:SystemCapability.Communication.IPC.Core 8041 8042**参数:** 8043 8044| 参数名 | 类型 | 必填 | 说明 | 8045| ------ | ------- | ---- | ------------------------------------------------------------ | 8046| async | boolean | 否 | true:表示异步调用标志,false:表示同步调用标志。默认同步调用。 | 8047 8048**示例:** 8049 8050 ```ts 8051 class TestRemoteObject extends rpc.MessageOption { 8052 constructor(async: boolean) { 8053 super(async); 8054 } 8055 } 8056 ``` 8057 8058### constructor 8059 8060constructor(syncFlags?: number, waitTime?: number) 8061 8062MessageOption构造函数。 8063 8064**系统能力**:SystemCapability.Communication.IPC.Core 8065 8066**参数:** 8067 8068 | 参数名 | 类型 | 必填 | 说明 | 8069 | --------- | ------ | ---- | --------------------------------------------- | 8070 | syncFlags | number | 否 | 同步调用或异步调用标志。默认同步调用。 | 8071 | waitTime | number | 否 | 调用rpc最长等待时间。默认TF_WAIT_TIME。 | 8072 8073**示例:** 8074 8075 ```ts 8076 class TestRemoteObject extends rpc.MessageOption { 8077 constructor(syncFlags?: number,waitTime?: number) { 8078 super(syncFlags,waitTime); 8079 } 8080 } 8081 ``` 8082### isAsync<sup>9+</sup> 8083 8084isAsync(): boolean 8085 8086获取SendMessageRequest调用中确定同步或是异步的标志。 8087 8088**系统能力**:SystemCapability.Communication.IPC.Core 8089 8090**返回值:** 8091 8092 | 类型 | 说明 | 8093 | ------- | ---------------------------------------- | 8094 | boolean | true:异步调用成功,false:同步调用成功。| 8095 8096**示例:** 8097 8098 ```ts 8099 let option = new rpc.MessageOption(); 8100 option.isAsync(); 8101 ``` 8102 8103### setAsync<sup>9+</sup> 8104 8105setAsync(async: boolean): void 8106 8107设置SendMessageRequest调用中确定同步或是异步的标志。 8108 8109**系统能力**:SystemCapability.Communication.IPC.Core 8110 8111**参数:** 8112 8113| 参数名 | 类型 | 必填 | 说明 | 8114| ------ | ------- | ---- | ------------------------------------------------- | 8115| async | boolean | 是 | true:表示异步调用标志,false:表示同步调用标志。 | 8116 8117**示例:** 8118 8119 ```ts 8120 import { hilog } from '@kit.PerformanceAnalysisKit'; 8121 8122 let option = new rpc.MessageOption(); 8123 option.setAsync(true); 8124 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 8125 ``` 8126 8127### getFlags 8128 8129getFlags(): number 8130 8131获取同步调用或异步调用标志。 8132 8133**系统能力**:SystemCapability.Communication.IPC.Core 8134 8135**返回值:** 8136 8137 | 类型 | 说明 | 8138 | ------ | ------------------------------------ | 8139 | number | 调用成功返回同步调用或异步调用标志。 | 8140 8141**示例:** 8142 8143 ```ts 8144 import { hilog } from '@kit.PerformanceAnalysisKit'; 8145 8146 try { 8147 let option = new rpc.MessageOption(); 8148 hilog.info(0x0000, 'testTag', 'create object successfully'); 8149 let flog = option.getFlags(); 8150 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8151 option.setFlags(rpc.MessageOption.TF_ASYNC); 8152 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8153 let flog2 = option.getFlags(); 8154 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 8155 } catch (error) { 8156 hilog.error(0x0000, 'testTag', 'error ' + error); 8157 } 8158 ``` 8159 8160### setFlags 8161 8162setFlags(flags: number): void 8163 8164设置同步调用或异步调用标志。 8165 8166**系统能力**:SystemCapability.Communication.IPC.Core 8167 8168**参数:** 8169 8170 | 参数名 | 类型 | 必填 | 说明 | 8171 | ------ | ------ | ---- | ------------------------ | 8172 | flags | number | 是 | 同步调用或异步调用标志。 | 8173 8174**示例:** 8175 8176 ```ts 8177 import { hilog } from '@kit.PerformanceAnalysisKit'; 8178 8179 try { 8180 let option = new rpc.MessageOption(); 8181 option.setFlags(rpc.MessageOption.TF_ASYNC); 8182 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8183 let flog = option.getFlags(); 8184 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8185 } catch (error) { 8186 hilog.error(0x0000, 'testTag', 'error ' + error); 8187 } 8188 ``` 8189 8190### getWaitTime 8191 8192getWaitTime(): number 8193 8194获取rpc调用的最长等待时间。 8195 8196**系统能力**:SystemCapability.Communication.IPC.Core 8197 8198**返回值:** 8199 8200 | 类型 | 说明 | 8201 | ------ | ----------------- | 8202 | number | rpc最长等待时间。 | 8203 8204**示例:** 8205 8206 ```ts 8207 import { hilog } from '@kit.PerformanceAnalysisKit'; 8208 8209 try { 8210 let option = new rpc.MessageOption(); 8211 let time = option.getWaitTime(); 8212 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8213 option.setWaitTime(16); 8214 let time2 = option.getWaitTime(); 8215 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8216 } catch (error) { 8217 hilog.error(0x0000, 'testTag', 'error ' + error); 8218 } 8219 ``` 8220 8221### setWaitTime 8222 8223setWaitTime(waitTime: number): void 8224 8225设置rpc调用最长等待时间。 8226 8227**系统能力**:SystemCapability.Communication.IPC.Core 8228 8229**参数:** 8230 8231 | 参数名 | 类型 | 必填 | 说明 | 8232 | -------- | ------ | ---- | --------------------- | 8233 | waitTime | number | 是 | rpc调用最长等待时间,上限为3000秒。 | 8234 8235**示例:** 8236 8237 ```ts 8238 import { hilog } from '@kit.PerformanceAnalysisKit'; 8239 8240 try { 8241 let option = new rpc.MessageOption(); 8242 option.setWaitTime(16); 8243 let time = option.getWaitTime(); 8244 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8245 } catch (error) { 8246 hilog.error(0x0000, 'testTag', 'error ' + error); 8247 } 8248 ``` 8249 8250## IPCSkeleton 8251 8252用于获取IPC上下文信息,包括获取UID和PID、获取本端和对端设备ID、检查接口调用是否在同一设备上。 8253 8254### getContextObject 8255 8256static getContextObject(): IRemoteObject 8257 8258静态方法,获取系统能力的管理者。 8259 8260**系统能力**:SystemCapability.Communication.IPC.Core 8261 8262**返回值:** 8263 8264 | 类型 | 说明 | 8265 | ------------------------------- | -------------------- | 8266 | [IRemoteObject](#iremoteobject) | 返回系统能力管理者。 | 8267 8268**示例:** 8269 8270 ```ts 8271 import { hilog } from '@kit.PerformanceAnalysisKit'; 8272 8273 let samgr = rpc.IPCSkeleton.getContextObject(); 8274 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8275 ``` 8276 8277### getCallingPid 8278 8279static getCallingPid(): number 8280 8281静态方法,获取调用者的PID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的PID。 8282 8283**系统能力**:SystemCapability.Communication.IPC.Core 8284 8285**返回值:** 8286 8287 | 类型 | 说明 | 8288 | ------ | ----------------- | 8289 | number | 返回调用者的PID。 | 8290 8291**示例:** 8292 8293 ```ts 8294 import { hilog } from '@kit.PerformanceAnalysisKit'; 8295 8296 class Stub extends rpc.RemoteObject { 8297 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8298 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8299 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8300 return true; 8301 } 8302 } 8303 ``` 8304 8305### getCallingUid 8306 8307static getCallingUid(): number 8308 8309静态方法,获取调用者的UID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的UID。 8310 8311**系统能力**:SystemCapability.Communication.IPC.Core 8312 8313**返回值:** 8314 8315 | 类型 | 说明 | 8316 | ------ | ----------------- | 8317 | number | 返回调用者的UID。 | 8318 8319**示例:** 8320 8321 ```ts 8322 import { hilog } from '@kit.PerformanceAnalysisKit'; 8323 8324 class Stub extends rpc.RemoteObject { 8325 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8326 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8327 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8328 return true; 8329 } 8330 } 8331 ``` 8332 8333### getCallingTokenId<sup>8+</sup> 8334 8335static getCallingTokenId(): number 8336 8337静态方法,获取调用者的TokenId,用于被调用方对调用方的身份校验。 8338 8339**系统能力**:SystemCapability.Communication.IPC.Core 8340 8341**返回值:** 8342 8343 | 类型 | 说明 | 8344 | ------ | --------------------- | 8345 | number | 返回调用者的TokenId。 | 8346 8347**示例:** 8348 8349 ```ts 8350 import { hilog } from '@kit.PerformanceAnalysisKit'; 8351 8352 class Stub extends rpc.RemoteObject { 8353 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8354 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8355 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8356 return true; 8357 } 8358 } 8359 ``` 8360 8361### getCallingDeviceID 8362 8363static getCallingDeviceID(): string 8364 8365静态方法,获取调用者进程所在的设备ID。 8366 8367**系统能力**:SystemCapability.Communication.IPC.Core 8368 8369**返回值:** 8370 8371 | 类型 | 说明 | 8372 | ------ | ---------------------------- | 8373 | string | 返回调用者进程所在的设备ID。 | 8374 8375**示例:** 8376 8377 ```ts 8378 import { hilog } from '@kit.PerformanceAnalysisKit'; 8379 8380 class Stub extends rpc.RemoteObject { 8381 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8382 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8383 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8384 return true; 8385 } 8386 } 8387 ``` 8388 8389### getLocalDeviceID 8390 8391static getLocalDeviceID(): string 8392 8393静态方法,获取本端设备ID。 8394 8395**系统能力**:SystemCapability.Communication.IPC.Core 8396 8397**返回值:** 8398 8399 | 类型 | 说明 | 8400 | ------ | ------------------ | 8401 | string | 返回本地设备的ID。 | 8402 8403**示例:** 8404 8405 ```ts 8406 import { hilog } from '@kit.PerformanceAnalysisKit'; 8407 8408 class Stub extends rpc.RemoteObject { 8409 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8410 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8411 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8412 return true; 8413 } 8414 } 8415 ``` 8416 8417### isLocalCalling 8418 8419static isLocalCalling(): boolean 8420 8421静态方法,检查当前通信对端是否是本设备的进程。 8422 8423**系统能力**:SystemCapability.Communication.IPC.Core 8424 8425**返回值:** 8426 8427 | 类型 | 说明 | 8428 | ------- | -------------------------------------------------- | 8429 | boolean | true:调用在同一台设备,false:调用未在同一台设备。| 8430 8431**示例:** 8432 8433 ```ts 8434 import { hilog } from '@kit.PerformanceAnalysisKit'; 8435 8436 class Stub extends rpc.RemoteObject { 8437 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8438 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8439 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8440 return true; 8441 } 8442 } 8443 ``` 8444 8445### flushCmdBuffer<sup>9+</sup> 8446 8447static flushCmdBuffer(object: IRemoteObject): void 8448 8449静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8450 8451**系统能力**:SystemCapability.Communication.IPC.Core 8452 8453**参数:** 8454 8455 | 参数名 | 类型 | 必填 | 说明 | 8456 | ------ | ------------------------------- | ---- | ------------------- | 8457 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8458 8459**错误码:** 8460 8461以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8462 8463 | 错误码ID | 错误信息 | 8464 | -------- | -------- | 8465 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 8466 8467**示例:** 8468 8469 ```ts 8470 import { hilog } from '@kit.PerformanceAnalysisKit'; 8471 import { BusinessError } from '@kit.BasicServicesKit'; 8472 8473 class TestRemoteObject extends rpc.RemoteObject { 8474 constructor(descriptor: string) { 8475 super(descriptor); 8476 } 8477 } 8478 let remoteObject = new TestRemoteObject("aaa"); 8479 try { 8480 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8481 } catch (error) { 8482 let e: BusinessError = error as BusinessError; 8483 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8484 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8485 } 8486 ``` 8487 8488### flushCommands<sup>(deprecated)</sup> 8489 8490static flushCommands(object: IRemoteObject): number 8491 8492静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8493 8494> **说明:** 8495> 8496> 从API version 9 开始废弃,建议使用[flushCmdBuffer](#flushcmdbuffer9)替代。 8497 8498**系统能力**:SystemCapability.Communication.IPC.Core 8499 8500**参数:** 8501 8502 | 参数名 | 类型 | 必填 | 说明 | 8503 | ------ | ------------------------------- | ---- | ------------------- | 8504 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8505 8506**返回值:** 8507 8508 | 类型 | 说明 | 8509 | ------ | --------------------------------------------------------------------------------- | 8510 | number | 如果操作成功,返回0;如果输入对象为空或RemoteObject,或者操作失败,返回错误代码。 | 8511 8512**示例:** 8513 8514 ```ts 8515 import { hilog } from '@kit.PerformanceAnalysisKit'; 8516 8517 class MyDeathRecipient implements rpc.DeathRecipient { 8518 onRemoteDied() { 8519 hilog.info(0x0000, 'testTag', 'server died'); 8520 } 8521 } 8522 class TestRemoteObject extends rpc.RemoteObject { 8523 constructor(descriptor: string) { 8524 super(descriptor); 8525 } 8526 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8527 return true; 8528 } 8529 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8530 return true; 8531 } 8532 isObjectDead(): boolean { 8533 return false; 8534 } 8535 } 8536 let remoteObject = new TestRemoteObject("aaa"); 8537 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8538 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8539 ``` 8540 8541### resetCallingIdentity 8542 8543static resetCallingIdentity(): string 8544 8545静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。 8546 8547**系统能力**:SystemCapability.Communication.IPC.Core 8548 8549**返回值:** 8550 8551 | 类型 | 说明 | 8552 | ------ | ------------------------------------ | 8553 | string | 返回包含远程用户的UID和PID的字符串。 | 8554 8555**示例:** 8556 8557 ```ts 8558 import { hilog } from '@kit.PerformanceAnalysisKit'; 8559 8560 class Stub extends rpc.RemoteObject { 8561 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8562 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8563 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8564 return true; 8565 } 8566 } 8567 ``` 8568 8569### restoreCallingIdentity<sup>9+</sup> 8570 8571static restoreCallingIdentity(identity: string): void 8572 8573静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8574 8575**系统能力**:SystemCapability.Communication.IPC.Core 8576 8577**参数:** 8578 8579 | 参数名 | 类型 | 必填 | 说明 | 8580 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8581 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8582 8583**错误码:** 8584 8585以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8586 8587 | 错误码ID | 错误信息 | 8588 | -------- | -------- | 8589 | 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. | 8590 8591**示例:** 8592 8593 ```ts 8594 import { hilog } from '@kit.PerformanceAnalysisKit'; 8595 8596 class Stub extends rpc.RemoteObject { 8597 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8598 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8599 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8600 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8601 return true; 8602 } 8603 } 8604 ``` 8605 8606### setCallingIdentity<sup>(deprecated)</sup> 8607 8608static setCallingIdentity(identity: string): boolean 8609 8610静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 8611 8612> **说明:** 8613> 8614> 从API version 9 开始废弃,建议使用[restoreCallingIdentity](#restorecallingidentity9)替代。 8615 8616**系统能力**:SystemCapability.Communication.IPC.Core 8617 8618**参数:** 8619 8620 | 参数名 | 类型 | 必填 | 说明 | 8621 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8622 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 8623 8624**返回值:** 8625 8626 | 类型 | 说明 | 8627 | ------- | ---------------------------------| 8628 | boolean | true:设置成功,false:设置失败。| 8629 8630**示例:** 8631 8632 ```ts 8633 import { hilog } from '@kit.PerformanceAnalysisKit'; 8634 8635 class Stub extends rpc.RemoteObject { 8636 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8637 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8638 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8639 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8640 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8641 return true; 8642 } 8643 } 8644 ``` 8645 8646## RemoteObject 8647 8648实现远程对象。服务提供者必须继承此类。 8649 8650### constructor 8651 8652constructor(descriptor: string) 8653 8654RemoteObject构造函数。 8655 8656**系统能力**:SystemCapability.Communication.IPC.Core 8657 8658**参数:** 8659 8660 | 参数名 | 类型 | 必填 | 说明 | 8661 | ---------- | ------ | ---- | ------------ | 8662 | descriptor | string | 是 | 接口描述符。 | 8663 8664**示例:** 8665 8666 ```ts 8667 class TestRemoteObject extends rpc.RemoteObject { 8668 constructor(descriptor: string) { 8669 super(descriptor); 8670 } 8671 } 8672 ``` 8673### sendRequest<sup>(deprecated)</sup> 8674 8675sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8676 8677以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8678 8679> **说明:** 8680> 8681> 从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 8682 8683**系统能力**:SystemCapability.Communication.IPC.Core 8684 8685**参数:** 8686 8687 | 参数名 | 类型 | 必填 | 说明 | 8688 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8689 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8690 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8691 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8692 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8693 8694**返回值:** 8695 8696 | 类型 | 说明 | 8697 | ------- | -------------------------------- | 8698 | boolean | true:发送成功,false:发送失败。| 8699 8700**示例:** 8701 8702 ```ts 8703 import { hilog } from '@kit.PerformanceAnalysisKit'; 8704 8705 class MyDeathRecipient implements rpc.DeathRecipient { 8706 onRemoteDied() { 8707 hilog.info(0x0000, 'testTag', 'server died'); 8708 } 8709 } 8710 class TestRemoteObject extends rpc.RemoteObject { 8711 constructor(descriptor: string) { 8712 super(descriptor); 8713 } 8714 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8715 return true; 8716 } 8717 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8718 return true; 8719 } 8720 isObjectDead(): boolean { 8721 return false; 8722 } 8723 } 8724 let testRemoteObject = new TestRemoteObject("testObject"); 8725 let option = new rpc.MessageOption(); 8726 let data = rpc.MessageParcel.create(); 8727 let reply = rpc.MessageParcel.create(); 8728 data.writeInt(1); 8729 data.writeString("hello"); 8730 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8731 if (ret) { 8732 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8733 let msg = reply.readString(); 8734 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8735 } else { 8736 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8737 } 8738 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8739 data.reclaim(); 8740 reply.reclaim(); 8741 ``` 8742 8743### sendMessageRequest<sup>9+</sup> 8744 8745sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8746 8747以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 8748 8749**系统能力**:SystemCapability.Communication.IPC.Core 8750 8751**参数:** 8752 8753 | 参数名 | 类型 | 必填 | 说明 | 8754 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8755 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8756 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 8757 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 8758 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8759 8760**返回值:** 8761 8762| 类型 | 说明 | 8763| ----------------------------------------------- | ----------------------------------------- | 8764| Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是RequestResult实例。 | 8765 8766 8767**错误码:** 8768 8769以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8770 8771 | 错误码ID | 错误信息 | 8772 | -------- | -------- | 8773 | 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. | 8774 8775**示例:** 8776 8777 ```ts 8778 import { hilog } from '@kit.PerformanceAnalysisKit'; 8779 8780 class TestRemoteObject extends rpc.RemoteObject { 8781 constructor(descriptor: string) { 8782 super(descriptor); 8783 } 8784 } 8785 let testRemoteObject = new TestRemoteObject("testObject"); 8786 let option = new rpc.MessageOption(); 8787 let data = rpc.MessageSequence.create(); 8788 let reply = rpc.MessageSequence.create(); 8789 data.writeInt(1); 8790 data.writeString("hello"); 8791 testRemoteObject.sendMessageRequest(1, data, reply, option) 8792 .then((result: rpc.RequestResult) => { 8793 if (result.errCode === 0) { 8794 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8795 let num = result.reply.readInt(); 8796 let msg = result.reply.readString(); 8797 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8798 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8799 } else { 8800 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8801 } 8802 }).catch((e: Error) => { 8803 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8804 }).finally (() => { 8805 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8806 data.reclaim(); 8807 reply.reclaim(); 8808 }); 8809 ``` 8810 8811### sendRequest<sup>(deprecated)</sup> 8812 8813sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8814 8815以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 8816 8817> **说明:** 8818> 8819> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 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### sendRequest<sup>(deprecated)</sup> 8918 8919sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8920 8921以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 8922 8923> **说明:** 8924> 8925> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-5)替代。 8926 8927**系统能力**:SystemCapability.Communication.IPC.Core 8928 8929**参数:** 8930 8931| 参数名 | 类型 | 必填 | 说明 | 8932| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8933| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 8934| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 8935| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 8936| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 8937| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 8938 8939### onRemoteMessageRequest<sup>9+</sup> 8940 8941onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 8942 8943> **说明:** 8944> 8945>* 开发者应优先选择重载onRemoteMessageRequest方法,其中可以自由实现同步和异步的消息处理。 8946>* 开发者同时重载onRemoteRequest和onRemoteMessageRequest方法时,仅onRemoteMessageRequest方法生效。 8947 8948sendMessageRequest请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。 8949 8950**系统能力**:SystemCapability.Communication.IPC.Core 8951 8952**参数:** 8953 8954 | 参数名 | 类型 | 必填 | 说明 | 8955 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 8956 | code | number | 是 | 对端发送的服务请求码。 | 8957 | data | [MessageSequence](#messagesequence9) | 是 | 携带客户端调用参数的MessageSequence对象。 | 8958 | reply | [MessageSequence](#messagesequence9) | 是 | 写入结果的MessageSequence对象。 | 8959 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 8960 8961**返回值:** 8962 8963 | 类型 | 说明 | 8964 | ----------------- | ----------------------------------------------------------------------------------------------- | 8965 | boolean | 若在onRemoteMessageRequest中同步地处理请求,则返回一个布尔值:true:操作成功,false:操作失败。 | 8966 | Promise\<boolean> | 若在onRemoteMessageRequest中异步地处理请求,则返回一个Promise对象。 | 8967 8968**重载onRemoteMessageRequest方法同步处理请求示例:** 8969 8970 ```ts 8971 import { hilog } from '@kit.PerformanceAnalysisKit'; 8972 8973 class TestRemoteObject extends rpc.RemoteObject { 8974 constructor(descriptor: string) { 8975 super(descriptor); 8976 } 8977 8978 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8979 if (code === 1) { 8980 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 8981 return true; 8982 } else { 8983 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 8984 return false; 8985 } 8986 } 8987 } 8988 ``` 8989 8990 **重载onRemoteMessageRequest方法异步处理请求示例:** 8991 8992 ```ts 8993 import { hilog } from '@kit.PerformanceAnalysisKit'; 8994 8995 class TestRemoteObject extends rpc.RemoteObject { 8996 constructor(descriptor: string) { 8997 super(descriptor); 8998 } 8999 9000 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9001 if (code === 1) { 9002 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9003 } else { 9004 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9005 return false; 9006 } 9007 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9008 setTimeout(resolve, 100); 9009 }) 9010 return true; 9011 } 9012 } 9013 ``` 9014 9015**同时重载onRemoteMessageRequest和onRemoteRequest方法同步处理请求示例:** 9016 9017 ```ts 9018 import { hilog } from '@kit.PerformanceAnalysisKit'; 9019 9020 class TestRemoteObject extends rpc.RemoteObject { 9021 constructor(descriptor: string) { 9022 super(descriptor); 9023 } 9024 9025 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9026 if (code === 1) { 9027 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9028 return true; 9029 } else { 9030 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9031 return false; 9032 } 9033 } 9034 // 同时调用仅会执行onRemoteMessageRequest 9035 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9036 if (code === 1) { 9037 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9038 } else { 9039 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9040 return false; 9041 } 9042 return true; 9043 } 9044 } 9045 ``` 9046 9047 **同时重载onRemoteMessageRequest和onRemoteRequest方法异步处理请求示例:** 9048 9049 ```ts 9050 import { hilog } from '@kit.PerformanceAnalysisKit'; 9051 class TestRemoteObject extends rpc.RemoteObject { 9052 constructor(descriptor: string) { 9053 super(descriptor); 9054 } 9055 9056 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9057 if (code === 1) { 9058 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9059 return true; 9060 } else { 9061 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9062 return false; 9063 } 9064 } 9065 // 同时调用仅会执行onRemoteMessageRequest 9066 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9067 if (code === 1) { 9068 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9069 } else { 9070 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9071 return false; 9072 } 9073 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9074 setTimeout(resolve, 100); 9075 }) 9076 return true; 9077 } 9078 } 9079 ``` 9080 9081### onRemoteRequest<sup>(deprecated)</sup> 9082 9083onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9084 9085sendRequest请求的响应处理函数,服务端在该函数里处理请求,回复结果。 9086 9087> **说明:** 9088> 9089> 从API version 9 开始废弃,建议使用[onRemoteMessageRequest](#onremotemessagerequest9)替代。 9090 9091**系统能力**:SystemCapability.Communication.IPC.Core 9092 9093**参数:** 9094 9095 | 参数名 | 类型 | 必填 | 说明 | 9096 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 9097 | code | number | 是 | 对端发送的服务请求码。 | 9098 | data | [MessageParcel](#messageparceldeprecated) | 是 | 携带客户端调用参数的MessageParcel对象。 | 9099 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 写入结果的MessageParcel对象。 | 9100 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9101 9102**返回值:** 9103 9104 | 类型 | 说明 | 9105 | ------- | -------------------------------- | 9106 | boolean | true:操作成功,false:操作失败。| 9107 9108**示例:** 9109 9110 ```ts 9111 import { hilog } from '@kit.PerformanceAnalysisKit'; 9112 9113 class MyDeathRecipient implements rpc.DeathRecipient { 9114 onRemoteDied() { 9115 hilog.info(0x0000, 'testTag', 'server died'); 9116 } 9117 } 9118 class TestRemoteObject extends rpc.RemoteObject { 9119 constructor(descriptor: string) { 9120 super(descriptor); 9121 } 9122 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9123 return true; 9124 } 9125 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9126 return true; 9127 } 9128 isObjectDead(): boolean { 9129 return false; 9130 } 9131 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9132 if (code === 1) { 9133 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9134 return true; 9135 } else { 9136 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9137 return false; 9138 } 9139 } 9140 } 9141 ``` 9142 9143### getCallingUid 9144 9145getCallingUid(): number 9146 9147获取通信对端的进程Uid。 9148 9149**系统能力**:SystemCapability.Communication.IPC.Core 9150 9151**返回值:** 9152 | 类型 | 说明 | 9153 | ------ | ----------------------- | 9154 | number | 返回通信对端的进程Uid。 | 9155 9156**示例:** 9157 9158 ```ts 9159 import { hilog } from '@kit.PerformanceAnalysisKit'; 9160 9161 class TestRemoteObject extends rpc.RemoteObject { 9162 constructor(descriptor: string) { 9163 super(descriptor); 9164 } 9165 } 9166 let testRemoteObject = new TestRemoteObject("testObject"); 9167 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9168 ``` 9169 9170### getCallingPid 9171 9172getCallingPid(): number 9173 9174获取通信对端的进程Pid。 9175 9176**系统能力**:SystemCapability.Communication.IPC.Core 9177 9178**返回值:** 9179 9180 | 类型 | 说明 | 9181 | ------ | ----------------------- | 9182 | number | 返回通信对端的进程Pid。 | 9183 9184**示例:** 9185 9186 ```ts 9187 import { hilog } from '@kit.PerformanceAnalysisKit'; 9188 9189 class TestRemoteObject extends rpc.RemoteObject { 9190 constructor(descriptor: string) { 9191 super(descriptor); 9192 } 9193 } 9194 let testRemoteObject = new TestRemoteObject("testObject"); 9195 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9196 ``` 9197 9198### getLocalInterface<sup>9+</sup> 9199 9200getLocalInterface(descriptor: string): IRemoteBroker 9201 9202查询接口描述符的字符串。 9203 9204**系统能力**:SystemCapability.Communication.IPC.Core 9205 9206**参数:** 9207 9208 | 参数名 | 类型 | 必填 | 说明 | 9209 | ---------- | ------ | ---- | -------------------- | 9210 | descriptor | string | 是 | 接口描述符的字符串。 | 9211 9212**返回值:** 9213 9214 | 类型 | 说明 | 9215 | ------------- | --------------------------------------------- | 9216 | [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 9217 9218**错误码:** 9219 9220以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9221 9222 | 错误码ID | 错误信息 | 9223 | -------- | -------- | 9224 | 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. | 9225 9226**示例:** 9227 9228 ```ts 9229 import { hilog } from '@kit.PerformanceAnalysisKit'; 9230 import { BusinessError } from '@kit.BasicServicesKit'; 9231 9232 class MyDeathRecipient implements rpc.DeathRecipient { 9233 onRemoteDied() { 9234 hilog.info(0x0000, 'testTag', 'server died'); 9235 } 9236 } 9237 class TestRemoteObject extends rpc.RemoteObject { 9238 constructor(descriptor: string) { 9239 super(descriptor); 9240 this.modifyLocalInterface(this, descriptor); 9241 } 9242 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9243 // 方法逻辑需开发者根据业务需要实现 9244 } 9245 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9246 // 方法逻辑需开发者根据业务需要实现 9247 } 9248 isObjectDead(): boolean { 9249 return false; 9250 } 9251 asObject(): rpc.IRemoteObject { 9252 return this; 9253 } 9254 } 9255 let testRemoteObject = new TestRemoteObject("testObject"); 9256 try { 9257 testRemoteObject.getLocalInterface("testObject"); 9258 } catch (error) { 9259 let e: BusinessError = error as BusinessError; 9260 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9261 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9262 } 9263 ``` 9264 9265### queryLocalInterface<sup>(deprecated)</sup> 9266 9267queryLocalInterface(descriptor: string): IRemoteBroker 9268 9269查询并获取当前接口描述符对应的远端对象是否已经存在。 9270 9271> **说明:** 9272> 9273> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-2)替代。 9274 9275**系统能力**:SystemCapability.Communication.IPC.Core 9276 9277**参数:** 9278 9279 | 参数名 | 类型 | 必填 | 说明 | 9280 | ---------- | ------ | ---- | ---------------------- | 9281 | descriptor | string | 是 | 需要查询的接口描述符。 | 9282 9283**返回值:** 9284 9285 | 类型 | 说明 | 9286 | ------------- | ------------------------------------------------------------------ | 9287 | [IRemoteBroker](#iremotebroker) | 如果接口描述符对应的远端对象存在,则返回该远端对象,否则返回Null。 | 9288 9289**示例:** 9290 9291 ```ts 9292 import { hilog } from '@kit.PerformanceAnalysisKit'; 9293 9294 class MyDeathRecipient implements rpc.DeathRecipient { 9295 onRemoteDied() { 9296 hilog.info(0x0000, 'testTag', 'server died'); 9297 } 9298 } 9299 class TestRemoteObject extends rpc.RemoteObject { 9300 constructor(descriptor: string) { 9301 super(descriptor); 9302 this.attachLocalInterface(this, descriptor); 9303 } 9304 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9305 return true; 9306 } 9307 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9308 return true; 9309 } 9310 isObjectDead(): boolean { 9311 return false; 9312 } 9313 asObject(): rpc.IRemoteObject { 9314 return this; 9315 } 9316 } 9317 let testRemoteObject = new TestRemoteObject("testObject"); 9318 testRemoteObject.queryLocalInterface("testObject"); 9319 ``` 9320 9321### getDescriptor<sup>9+</sup> 9322 9323getDescriptor(): string 9324 9325获取对象的接口描述符。接口描述符为字符串。 9326 9327**系统能力**:SystemCapability.Communication.IPC.Core 9328 9329**返回值:** 9330 9331 | 类型 | 说明 | 9332 | ------ | ---------------- | 9333 | string | 返回接口描述符。 | 9334 9335**错误码:** 9336 9337以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9338 9339 | 错误码ID | 错误信息 | 9340 | -------- | -------- | 9341 | 1900008 | The proxy or remote object is invalid. | 9342 9343**示例:** 9344 9345 ```ts 9346 import { hilog } from '@kit.PerformanceAnalysisKit'; 9347 import { BusinessError } from '@kit.BasicServicesKit'; 9348 9349 class MyDeathRecipient implements rpc.DeathRecipient { 9350 onRemoteDied() { 9351 hilog.info(0x0000, 'testTag', 'server died'); 9352 } 9353 } 9354 class TestRemoteObject extends rpc.RemoteObject { 9355 constructor(descriptor: string) { 9356 super(descriptor); 9357 } 9358 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9359 // 方法逻辑需开发者根据业务需要实现 9360 } 9361 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9362 // 方法逻辑需开发者根据业务需要实现 9363 } 9364 isObjectDead(): boolean { 9365 return false; 9366 } 9367 } 9368 let testRemoteObject = new TestRemoteObject("testObject"); 9369 try { 9370 let descriptor = testRemoteObject.getDescriptor(); 9371 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9372 } catch (error) { 9373 let e: BusinessError = error as BusinessError; 9374 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9375 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9376 } 9377 ``` 9378 9379### getInterfaceDescriptor<sup>(deprecated)</sup> 9380 9381getInterfaceDescriptor(): string 9382 9383查询接口描述符。 9384 9385> **说明:** 9386> 9387> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-2)替代。 9388 9389**系统能力**:SystemCapability.Communication.IPC.Core 9390 9391**返回值:** 9392 9393 | 类型 | 说明 | 9394 | ------ | ---------------- | 9395 | string | 返回接口描述符。 | 9396 9397**示例:** 9398 9399 ```ts 9400 import { hilog } from '@kit.PerformanceAnalysisKit'; 9401 9402 class MyDeathRecipient implements rpc.DeathRecipient { 9403 onRemoteDied() { 9404 hilog.info(0x0000, 'testTag', 'server died'); 9405 } 9406 } 9407 class TestRemoteObject extends rpc.RemoteObject { 9408 constructor(descriptor: string) { 9409 super(descriptor); 9410 } 9411 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9412 return true; 9413 } 9414 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9415 return true; 9416 } 9417 isObjectDead(): boolean { 9418 return false; 9419 } 9420 } 9421 let testRemoteObject = new TestRemoteObject("testObject"); 9422 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9423 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9424 ``` 9425 9426### modifyLocalInterface<sup>9+</sup> 9427 9428modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9429 9430此接口用于把接口描述符和IRemoteBroker对象绑定。 9431 9432**系统能力**:SystemCapability.Communication.IPC.Core 9433 9434**参数:** 9435 9436| 参数名 | 类型 | 必填 | 说明 | 9437| -------------- | ------------------------------- | ---- | ------------------------------------- | 9438| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9439| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9440 9441**错误码:** 9442 9443以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9444 9445 | 错误码ID | 错误信息 | 9446 | -------- | -------- | 9447 | 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. | 9448 9449**示例:** 9450 9451 ```ts 9452 import { hilog } from '@kit.PerformanceAnalysisKit'; 9453 import { BusinessError } from '@kit.BasicServicesKit'; 9454 9455 class MyDeathRecipient implements rpc.DeathRecipient { 9456 onRemoteDied() { 9457 hilog.info(0x0000, 'testTag', 'server died'); 9458 } 9459 } 9460 class TestRemoteObject extends rpc.RemoteObject { 9461 constructor(descriptor: string) { 9462 super(descriptor); 9463 try { 9464 this.modifyLocalInterface(this, descriptor); 9465 } catch (error) { 9466 let e: BusinessError = error as BusinessError; 9467 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9468 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9469 } 9470 } 9471 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9472 // 方法逻辑需开发者根据业务需要实现 9473 } 9474 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9475 // 方法逻辑需开发者根据业务需要实现 9476 } 9477 isObjectDead(): boolean { 9478 return false; 9479 } 9480 asObject(): rpc.IRemoteObject { 9481 return this; 9482 } 9483 } 9484 let testRemoteObject = new TestRemoteObject("testObject"); 9485 ``` 9486 9487### attachLocalInterface<sup>(deprecated)</sup> 9488 9489attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9490 9491此接口用于把接口描述符和IRemoteBroker对象绑定。 9492 9493> **说明:** 9494> 9495> 从API version 9 开始废弃,建议使用[modifyLocalInterface](#modifylocalinterface9)替代。 9496 9497**系统能力**:SystemCapability.Communication.IPC.Core 9498 9499**参数:** 9500 9501| 参数名 | 类型 | 必填 | 说明 | 9502| -------------- | ------------------------------- | ---- | ------------------------------------- | 9503| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9504| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9505 9506**示例:** 9507 9508 ```ts 9509 import { hilog } from '@kit.PerformanceAnalysisKit'; 9510 9511 class MyDeathRecipient implements rpc.DeathRecipient { 9512 onRemoteDied() { 9513 hilog.info(0x0000, 'testTag', 'server died'); 9514 } 9515 } 9516 class TestRemoteObject extends rpc.RemoteObject { 9517 constructor(descriptor: string) { 9518 super(descriptor); 9519 this.attachLocalInterface(this, descriptor); 9520 } 9521 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9522 return true; 9523 } 9524 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9525 return true; 9526 } 9527 isObjectDead(): boolean { 9528 return false; 9529 } 9530 asObject(): rpc.IRemoteObject { 9531 return this; 9532 } 9533 } 9534 let testRemoteObject = new TestRemoteObject("testObject"); 9535 ``` 9536 9537## Ashmem<sup>8+</sup> 9538 9539提供与匿名共享内存对象相关的方法,包括创建、关闭、映射和取消映射Ashmem、从Ashmem读取数据和写入数据、获取Ashmem大小、设置Ashmem保护。 9540共享内存只适用与本设备内跨进程通信。 9541 9542### 属性 9543 9544**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 9545 9546 | 名称 | 类型 | 可读 | 可写 | 说明 | 9547 | ---------- | ------ | ----- | ----- |----------------------------------------- | 9548 | PROT_EXEC | number | 是 | 否 | 映射内存保护类型,代表映射的内存可执行。 | 9549 | PROT_NONE | number | 是 | 否 | 映射内存保护类型,代表映射的内存不可访问。| 9550 | PROT_READ | number | 是 | 否 | 映射内存保护类型,代表映射的内存可读。 | 9551 | PROT_WRITE | number | 是 | 否 | 映射内存保护类型,代表映射的内存可写。 | 9552 9553### create<sup>9+</sup> 9554 9555static create(name: string, size: number): Ashmem 9556 9557静态方法,根据指定的名称和大小创建Ashmem对象。 9558 9559**系统能力**:SystemCapability.Communication.IPC.Core 9560 9561**参数:** 9562 9563 | 参数名 | 类型 | 必填 | 说明 | 9564 | ------ | ------ | ---- | ---------------------------- | 9565 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9566 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9567 9568**返回值:** 9569 9570| 类型 | 说明 | 9571| ------------------ | ---------------------------------------------- | 9572| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9573 9574**错误码:** 9575 9576以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9577 9578 | 错误码ID | 错误信息 | 9579 | -------- | -------- | 9580 | 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. | 9581 9582**示例:** 9583 9584 ```ts 9585 import { hilog } from '@kit.PerformanceAnalysisKit'; 9586 import { BusinessError } from '@kit.BasicServicesKit'; 9587 9588 let ashmem: rpc.Ashmem | undefined = undefined; 9589 try { 9590 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9591 let size = ashmem.getAshmemSize(); 9592 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9593 } catch (error) { 9594 let e: BusinessError = error as BusinessError; 9595 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9596 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9597 } 9598 ``` 9599 9600### createAshmem<sup>(deprecated)</sup> 9601 9602static createAshmem(name: string, size: number): Ashmem 9603 9604静态方法,根据指定的名称和大小创建Ashmem对象。 9605 9606> **说明:** 9607> 9608> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9)替代。 9609 9610**系统能力**:SystemCapability.Communication.IPC.Core 9611 9612**参数:** 9613 9614 | 参数名 | 类型 | 必填 | 说明 | 9615 | ------ | ------ | ---- | ---------------------------- | 9616 | name | string | 是 | 名称,用于查询Ashmem信息。 | 9617 | size | number | 是 | Ashmem的大小,以字节为单位。 | 9618 9619**返回值:** 9620 9621| 类型 | 说明 | 9622| ------------------ | ---------------------------------------------- | 9623| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 9624 9625**示例:** 9626 9627 ```ts 9628 import { hilog } from '@kit.PerformanceAnalysisKit'; 9629 9630 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9631 let size = ashmem.getAshmemSize(); 9632 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9633 ``` 9634 9635### create<sup>9+</sup> 9636 9637static create(ashmem: Ashmem): Ashmem 9638 9639静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9640 9641**系统能力**:SystemCapability.Communication.IPC.Core 9642 9643**参数:** 9644 9645| 参数名 | 类型 | 必填 | 说明 | 9646| ------ | ------------------ | ---- | -------------------- | 9647| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9648 9649**返回值:** 9650 9651| 类型 | 说明 | 9652| ------------------ | ---------------------- | 9653| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9654 9655**错误码:** 9656 9657以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9658 9659 | 错误码ID | 错误信息 | 9660 | -------- | -------- | 9661 | 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. | 9662 9663**示例:** 9664 9665 ```ts 9666 import { hilog } from '@kit.PerformanceAnalysisKit'; 9667 import { BusinessError } from '@kit.BasicServicesKit'; 9668 9669 try { 9670 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9671 let ashmem2 = rpc.Ashmem.create(ashmem); 9672 let size = ashmem2.getAshmemSize(); 9673 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9674 } catch (error) { 9675 let e: BusinessError = error as BusinessError; 9676 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9677 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9678 } 9679 ``` 9680 9681### createAshmemFromExisting<sup>(deprecated)</sup> 9682 9683static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9684 9685静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 9686 9687> **说明:** 9688> 9689> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9-1)替代。 9690 9691**系统能力**:SystemCapability.Communication.IPC.Core 9692 9693**参数:** 9694 9695| 参数名 | 类型 | 必填 | 说明 | 9696| ------ | ------------------ | ---- | -------------------- | 9697| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 9698 9699**返回值:** 9700 9701| 类型 | 说明 | 9702| ------------------ | ---------------------- | 9703| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 9704 9705**示例:** 9706 9707 ```ts 9708 import { hilog } from '@kit.PerformanceAnalysisKit'; 9709 9710 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9711 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9712 let size = ashmem2.getAshmemSize(); 9713 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9714 ``` 9715 9716### closeAshmem<sup>8+</sup> 9717 9718closeAshmem(): void 9719 9720关闭这个Ashmem。 9721 9722> **说明:** 9723> 9724> 关闭Ashmem对象前需要先解除地址映射。 9725 9726**系统能力**:SystemCapability.Communication.IPC.Core 9727 9728**示例:** 9729 9730 ```ts 9731 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9732 ashmem.closeAshmem(); 9733 ``` 9734 9735### unmapAshmem<sup>8+</sup> 9736 9737unmapAshmem(): void 9738 9739删除该Ashmem对象的地址映射。 9740 9741**系统能力**:SystemCapability.Communication.IPC.Core 9742 9743**示例:** 9744 9745 ```ts 9746 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9747 ashmem.unmapAshmem(); 9748 ``` 9749 9750### getAshmemSize<sup>8+</sup> 9751 9752getAshmemSize(): number 9753 9754获取Ashmem对象的内存大小。 9755 9756**系统能力**:SystemCapability.Communication.IPC.Core 9757 9758**返回值:** 9759 9760 | 类型 | 说明 | 9761 | ------ | -------------------------- | 9762 | number | 返回Ashmem对象的内存大小。 | 9763 9764**示例:** 9765 9766 ```ts 9767 import { hilog } from '@kit.PerformanceAnalysisKit'; 9768 9769 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9770 let size = ashmem.getAshmemSize(); 9771 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9772 ``` 9773 9774### mapTypedAshmem<sup>9+</sup> 9775 9776mapTypedAshmem(mapType: number): void 9777 9778在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9779 9780**系统能力**:SystemCapability.Communication.IPC.Core 9781 9782**参数:** 9783 9784 | 参数名 | 类型 | 必填 | 说明 | 9785 | ------- | ------ | ---- | ------------------------------ | 9786 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9787 9788**错误码:** 9789 9790以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9791 9792 | 错误码ID | 错误信息 | 9793 | -------- | -------- | 9794 | 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. | 9795 | 1900001 | Failed to call mmap. | 9796 9797**示例:** 9798 9799 ```ts 9800 import { hilog } from '@kit.PerformanceAnalysisKit'; 9801 import { BusinessError } from '@kit.BasicServicesKit'; 9802 9803 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9804 try { 9805 ashmem.mapTypedAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE); 9806 } catch (error) { 9807 let e: BusinessError = error as BusinessError; 9808 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9809 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9810 } 9811 ``` 9812 9813### mapAshmem<sup>(deprecated)</sup> 9814 9815mapAshmem(mapType: number): boolean 9816 9817在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 9818 9819> **说明:** 9820> 9821> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapTypedAshmem](#maptypedashmem9)替代。 9822 9823**系统能力**:SystemCapability.Communication.IPC.Core 9824 9825**参数:** 9826 9827 | 参数名 | 类型 | 必填 | 说明 | 9828 | ------- | ------ | ---- | ------------------------------ | 9829 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 9830 9831**返回值:** 9832 9833 | 类型 | 说明 | 9834 | ------- | -------------------------------- | 9835 | boolean | true:映射成功,false:映射失败。| 9836 9837**示例:** 9838 9839 ```ts 9840 import { hilog } from '@kit.PerformanceAnalysisKit'; 9841 9842 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9843 let mapReadAndWrite = ashmem.mapAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE); 9844 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9845 ``` 9846 9847### mapReadWriteAshmem<sup>9+</sup> 9848 9849mapReadWriteAshmem(): void 9850 9851在此进程虚拟地址空间上创建可读写的共享文件映射。 9852 9853**系统能力**:SystemCapability.Communication.IPC.Core 9854 9855**错误码:** 9856 9857以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9858 9859 | 错误码ID | 错误信息 | 9860 | -------- | -------- | 9861 | 1900001 | Failed to call mmap. | 9862 9863**示例:** 9864 9865 ```ts 9866 import { hilog } from '@kit.PerformanceAnalysisKit'; 9867 import { BusinessError } from '@kit.BasicServicesKit'; 9868 9869 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9870 try { 9871 ashmem.mapReadWriteAshmem(); 9872 } catch (error) { 9873 let e: BusinessError = error as BusinessError; 9874 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9875 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9876 } 9877 ``` 9878 9879### mapReadAndWriteAshmem<sup>(deprecated)</sup> 9880 9881mapReadAndWriteAshmem(): boolean 9882 9883在此进程虚拟地址空间上创建可读写的共享文件映射。 9884 9885> **说明:** 9886> 9887> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadWriteAshmem](#mapreadwriteashmem9)替代。 9888 9889**系统能力**:SystemCapability.Communication.IPC.Core 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 mapResult = ashmem.mapReadAndWriteAshmem(); 9904 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 9905 ``` 9906 9907### mapReadonlyAshmem<sup>9+</sup> 9908 9909mapReadonlyAshmem(): 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.mapReadonlyAshmem(); 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### mapReadOnlyAshmem<sup>(deprecated)</sup> 9940 9941mapReadOnlyAshmem(): boolean 9942 9943在此进程虚拟地址空间上创建只读的共享文件映射。 9944 9945> **说明:** 9946> 9947> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadonlyAshmem](#mapreadonlyashmem9)替代。 9948 9949**系统能力**:SystemCapability.Communication.IPC.Core 9950 9951**返回值:** 9952 9953 | 类型 | 说明 | 9954 | ------- | -------------------------------- | 9955 | boolean | true:映射成功,false:映射失败。| 9956 9957**示例:** 9958 9959 ```ts 9960 import { hilog } from '@kit.PerformanceAnalysisKit'; 9961 9962 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9963 let mapResult = ashmem.mapReadOnlyAshmem(); 9964 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 9965 ``` 9966 9967### setProtectionType<sup>9+</sup> 9968 9969setProtectionType(protectionType: number): void 9970 9971设置映射内存区域的保护等级。 9972 9973**系统能力**:SystemCapability.Communication.IPC.Core 9974 9975**参数:** 9976 9977 | 参数名 | 类型 | 必填 | 说明 | 9978 | -------------- | ------ | ---- | ------------------ | 9979 | protectionType | number | 是 | 要设置的保护类型。 | 9980 9981**错误码:** 9982 9983以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9984 9985 | 错误码ID | 错误信息 | 9986 | -------- | -------- | 9987 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 9988 | 1900002 | Failed to call ioctl. | 9989 9990**示例:** 9991 9992 ```ts 9993 import { hilog } from '@kit.PerformanceAnalysisKit'; 9994 import { BusinessError } from '@kit.BasicServicesKit'; 9995 9996 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9997 try { 9998 ashmem.setProtectionType(rpc.Ashmem.PROT_READ); 9999 } catch (error) { 10000 let e: BusinessError = error as BusinessError; 10001 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10002 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10003 } 10004 ``` 10005 10006### setProtection<sup>(deprecated)</sup> 10007 10008setProtection(protectionType: number): boolean 10009 10010设置映射内存区域的保护等级。 10011 10012> **说明:** 10013> 10014> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[setProtectionType](#setprotectiontype9)替代。 10015 10016**系统能力**:SystemCapability.Communication.IPC.Core 10017 10018**参数:** 10019 10020 | 参数名 | 类型 | 必填 | 说明 | 10021 | -------------- | ------ | ---- | ------------------ | 10022 | protectionType | number | 是 | 要设置的保护类型。 | 10023 10024**返回值:** 10025 10026 | 类型 | 说明 | 10027 | ------- | -------------------------------- | 10028 | boolean | true:设置成功,false:设置失败。| 10029 10030**示例:** 10031 10032 ```ts 10033 import { hilog } from '@kit.PerformanceAnalysisKit'; 10034 10035 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10036 let result = ashmem.setProtection(rpc.Ashmem.PROT_READ); 10037 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 10038 ``` 10039 10040### writeDataToAshmem<sup>11+</sup> 10041 10042writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10043 10044将数据写入此Ashmem对象关联的共享文件。 10045 10046> **说明:** 10047> 10048> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10049 10050**系统能力**:SystemCapability.Communication.IPC.Core 10051 10052**参数:** 10053 10054 | 参数名 | 类型 | 必填 | 说明 | 10055 | ------ | -------- | ---- | -------------------------------------------------- | 10056 | buf | ArrayBuffer | 是 | 写入Ashmem对象的数据。 | 10057 | size | number | 是 | 要写入的数据大小。 | 10058 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10059 10060**错误码:** 10061 10062以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10063 10064 | 错误码ID | 错误信息 | 10065 | -------- | -------- | 10066 | 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. | 10067 | 1900003 | Failed to write data to the shared memory. | 10068 10069**示例:** 10070 10071 ```ts 10072 import { hilog } from '@kit.PerformanceAnalysisKit'; 10073 import { BusinessError } from '@kit.BasicServicesKit'; 10074 10075 let buffer = new ArrayBuffer(1024); 10076 let int32View = new Int32Array(buffer); 10077 for (let i = 0; i < int32View.length; i++) { 10078 int32View[i] = i * 2 + 1; 10079 } 10080 let size = buffer.byteLength; 10081 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10082 ashmem.mapReadWriteAshmem(); 10083 try { 10084 ashmem.writeDataToAshmem(buffer, size, 0); 10085 } catch (error) { 10086 let e: BusinessError = error as BusinessError; 10087 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10088 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10089 } 10090 ``` 10091 10092### writeAshmem<sup>(deprecated)</sup> 10093 10094writeAshmem(buf: number[], size: number, offset: number): void 10095 10096将数据写入此Ashmem对象关联的共享文件。 10097 10098> **说明:** 10099> 10100> 从API version 9 开始支持,从API version 11 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10101> 10102> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10103 10104**系统能力**:SystemCapability.Communication.IPC.Core 10105 10106**参数:** 10107 10108 | 参数名 | 类型 | 必填 | 说明 | 10109 | ------ | -------- | ---- | -------------------------------------------------- | 10110 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10111 | size | number | 是 | 要写入的数据大小。 | 10112 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置 | 10113 10114**错误码:** 10115 10116以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10117 10118 | 错误码ID | 错误信息 | 10119 | -------- | -------- | 10120 | 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. | 10121 | 1900003 | Failed to write data to the shared memory. | 10122 10123**示例:** 10124 10125 ```ts 10126 import { hilog } from '@kit.PerformanceAnalysisKit'; 10127 import { BusinessError } from '@kit.BasicServicesKit'; 10128 10129 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10130 ashmem.mapReadWriteAshmem(); 10131 let ByteArrayVar = [1, 2, 3, 4, 5]; 10132 try { 10133 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10134 } catch (error) { 10135 let e: BusinessError = error as BusinessError; 10136 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10137 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10138 } 10139 ``` 10140 10141### writeToAshmem<sup>(deprecated)</sup> 10142 10143writeToAshmem(buf: number[], size: number, offset: number): boolean 10144 10145将数据写入此Ashmem对象关联的共享文件。 10146 10147> **说明:** 10148> 10149> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10150> 10151> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10152 10153**系统能力**:SystemCapability.Communication.IPC.Core 10154 10155**参数:** 10156 10157 | 参数名 | 类型 | 必填 | 说明 | 10158 | ------ | -------- | ---- | -------------------------------------------------- | 10159 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10160 | size | number | 是 | 要写入的数据大小。 | 10161 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10162 10163**返回值:** 10164 10165 | 类型 | 说明 | 10166 | ------- | ----------------------------------------------------------------------------- | 10167 | boolean | true:如果数据写入成功,false:在其他情况下,如数据写入越界或未获得写入权限。 | 10168 10169**示例:** 10170 10171 ```ts 10172 import { hilog } from '@kit.PerformanceAnalysisKit'; 10173 10174 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10175 let mapResult = ashmem.mapReadAndWriteAshmem(); 10176 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10177 let ByteArrayVar = [1, 2, 3, 4, 5]; 10178 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10179 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10180 ``` 10181 10182### readDataFromAshmem<sup>11+</sup> 10183 10184readDataFromAshmem(size: number, offset: number): ArrayBuffer 10185 10186从此Ashmem对象关联的共享文件中读取数据。 10187 10188> **说明:** 10189> 10190> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10191 10192**系统能力**:SystemCapability.Communication.IPC.Core 10193 10194**参数:** 10195 10196 | 参数名 | 类型 | 必填 | 说明 | 10197 | ------ | ------ | ---- | -------------------------------------------------- | 10198 | size | number | 是 | 要读取的数据的大小。 | 10199 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10200 10201**返回值:** 10202 10203 | 类型 | 说明 | 10204 | -------- | ---------------- | 10205 | ArrayBuffer | 返回读取的数据。 | 10206 10207**错误码:** 10208 10209以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10210 10211 | 错误码ID | 错误信息 | 10212 | -------- | -------- | 10213 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10214 | 1900004 | Failed to read data from the shared memory. | 10215 10216**示例:** 10217 10218 ```ts 10219 import { hilog } from '@kit.PerformanceAnalysisKit'; 10220 import { BusinessError } from '@kit.BasicServicesKit'; 10221 10222 let buffer = new ArrayBuffer(1024); 10223 let int32View = new Int32Array(buffer); 10224 for (let i = 0; i < int32View.length; i++) { 10225 int32View[i] = i * 2 + 1; 10226 } 10227 let size = buffer.byteLength; 10228 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10229 ashmem.mapReadWriteAshmem(); 10230 try { 10231 ashmem.writeDataToAshmem(buffer, size, 0); 10232 } catch (error) { 10233 let e: BusinessError = error as BusinessError; 10234 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10235 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10236 } 10237 try { 10238 let readResult = ashmem.readDataFromAshmem(size, 0); 10239 let readInt32View = new Int32Array(readResult); 10240 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readInt32View); 10241 } catch (error) { 10242 let e: BusinessError = error as BusinessError; 10243 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10244 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10245 } 10246 ``` 10247 10248### readAshmem<sup>(deprecated)</sup> 10249 10250readAshmem(size: number, offset: number): number[] 10251 10252从此Ashmem对象关联的共享文件中读取数据。 10253 10254> **说明:** 10255> 10256> 从API version 9 开始支持,从API version 11 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10257> 10258> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10259 10260 10261**系统能力**:SystemCapability.Communication.IPC.Core 10262 10263**参数:** 10264 10265 | 参数名 | 类型 | 必填 | 说明 | 10266 | ------ | ------ | ---- | -------------------------------------------------- | 10267 | size | number | 是 | 要读取的数据的大小。 | 10268 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10269 10270**返回值:** 10271 10272 | 类型 | 说明 | 10273 | -------- | ---------------- | 10274 | number[] | 返回读取的数据。 | 10275 10276**错误码:** 10277 10278以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10279 10280 | 错误码ID | 错误信息 | 10281 | -------- | -------- | 10282 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10283 | 1900004 | Failed to read data from the shared memory. | 10284 10285**示例:** 10286 10287 ```ts 10288 import { hilog } from '@kit.PerformanceAnalysisKit'; 10289 import { BusinessError } from '@kit.BasicServicesKit'; 10290 10291 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10292 ashmem.mapReadWriteAshmem(); 10293 let ByteArrayVar = [1, 2, 3, 4, 5]; 10294 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10295 try { 10296 let readResult = ashmem.readAshmem(5, 0); 10297 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 10298 } catch (error) { 10299 let e: BusinessError = error as BusinessError; 10300 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10301 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10302 } 10303 ``` 10304 10305### readFromAshmem<sup>(deprecated)</sup> 10306 10307readFromAshmem(size: number, offset: number): number[] 10308 10309从此Ashmem对象关联的共享文件中读取数据。 10310 10311> **说明:** 10312> 10313> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10314> 10315> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10316 10317**系统能力**:SystemCapability.Communication.IPC.Core 10318 10319**参数:** 10320 10321 | 参数名 | 类型 | 必填 | 说明 | 10322 | ------ | ------ | ---- | -------------------------------------------------- | 10323 | size | number | 是 | 要读取的数据的大小。 | 10324 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10325 10326**返回值:** 10327 10328 | 类型 | 说明 | 10329 | -------- | ---------------- | 10330 | number[] | 返回读取的数据。 | 10331 10332**示例:** 10333 10334 ``` ts 10335 import { hilog } from '@kit.PerformanceAnalysisKit'; 10336 10337 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10338 let mapResult = ashmem.mapReadAndWriteAshmem(); 10339 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10340 let ByteArrayVar = [1, 2, 3, 4, 5]; 10341 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10342 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10343 let readResult = ashmem.readFromAshmem(5, 0); 10344 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 10345 ```