1# @ohos.rpc (RPC通信) 2<!--Kit: IPC Kit--> 3<!--Subsystem: Communication--> 4<!--Owner: @xdx19211@luodonghui0157--> 5<!--Designer: @zhaopeng_gitee--> 6<!--Tester: @maxiaorong--> 7<!--Adviser: @zhang_yixin13--> 8 9本模块提供进程间通信能力,包括设备内的进程间通信(IPC)和设备间的进程间通信(RPC),前者基于Binder驱动,后者基于软总线驱动。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> - 本模块从API version 9开始支持异常返回功能。 16 17## 导入模块 18 19``` 20import { rpc } from '@kit.IPCKit'; 21``` 22 23## ErrorCode<sup>9+</sup> 24 25从API version 9起,IPC支持异常返回功能。错误码对应数值及含义如下。 26 27**系统能力**:SystemCapability.Communication.IPC.Core 28 29 | 名称 | 值 | 说明 | 30 | ------------------------------------- | ------- | --------------------------------------------- | 31 | CHECK_PARAM_ERROR | 401 | 检查参数失败。 | 32 | OS_MMAP_ERROR | 1900001 | 执行系统调用mmap失败。 | 33 | OS_IOCTL_ERROR | 1900002 | 在共享内存文件描述符上执行系统调用ioctl失败。 | 34 | WRITE_TO_ASHMEM_ERROR | 1900003 | 向共享内存写数据失败。 | 35 | READ_FROM_ASHMEM_ERROR | 1900004 | 从共享内存读数据失败。 | 36 | ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | 只有proxy对象允许该操作。 | 37 | ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | 只有remote对象允许该操作。 | 38 | COMMUNICATION_ERROR | 1900007 | 和远端对象进行进程间通信失败。 | 39 | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | 非法的代理对象或者远端对象。 | 40 | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | 向MessageSequence写数据失败。 | 41 | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | 读取MessageSequence数据失败。 | 42 | PARCEL_MEMORY_ALLOC_ERROR | 1900011 | 序列化过程中内存分配失败。 | 43 | CALL_JS_METHOD_ERROR | 1900012 | 执行JS回调方法失败。 | 44 | OS_DUP_ERROR | 1900013 | 执行系统调用dup失败。 | 45 46 47## TypeCode<sup>12+</sup> 48 49从API version 12起,IPC新增[writeArrayBuffer](#writearraybuffer12)和[readArrayBuffer](#readarraybuffer12)方法传递ArrayBuffer数据,传递数据时通过具体类型值来分辨业务是以哪一种TypedArray去进行数据的读写。类型码对应数值及含义如下。 50 51**系统能力**:SystemCapability.Communication.IPC.Core 52 53 | 名称 | 值 | 说明 | 54 | ---------------------------- | ------ | -------------------------------------------- | 55 | INT8_ARRAY | 0 | TypedArray类型为INT8_ARRAY。 | 56 | UINT8_ARRAY | 1 | TypedArray类型为UINT8_ARRAY。 | 57 | INT16_ARRAY | 2 | TypedArray类型为INT16_ARRAY。 | 58 | UINT16_ARRAY | 3 | TypedArray类型为UINT16_ARRAY。 | 59 | INT32_ARRAY | 4 | TypedArray类型为INT32_ARRAY。 | 60 | UINT32_ARRAY | 5 | TypedArray类型为UINT32_ARRAY。 | 61 | FLOAT32_ARRAY | 6 | TypedArray类型为FLOAT32_ARRAY。 | 62 | FLOAT64_ARRAY | 7 | TypedArray类型为FLOAT64_ARRAY。 | 63 | BIGINT64_ARRAY | 8 | TypedArray类型为BIGINT64_ARRAY。 | 64 | BIGUINT64_ARRAY | 9 | TypedArray类型为BIGUINT64_ARRAY。 | 65 66 67## MessageSequence<sup>9+</sup> 68 69 在RPC或IPC过程中,发送方可以使用MessageSequence提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageSequence提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 70 71### create<sup>9+</sup> 72 73 static create(): MessageSequence 74 75 静态方法,创建MessageSequence对象。 76 77**系统能力**:SystemCapability.Communication.IPC.Core 78 79**返回值:** 80 81| 类型 | 说明 | 82| --------------- | ------------------------------- | 83| [MessageSequence](#messagesequence9) | 返回创建的MessageSequence对象。 | 84 85**示例:** 86 87 ```ts 88 import { rpc } from '@kit.IPCKit'; 89 import { hilog } from '@kit.PerformanceAnalysisKit'; 90 import { BusinessError } from '@kit.BasicServicesKit'; 91 92 try { 93 let data = rpc.MessageSequence.create(); 94 hilog.info(0x0000, 'testTag', 'data is ' + data); 95 96 // 当MessageSequence对象不再使用,由业务主动调用reclaim方法去释放资源。 97 data.reclaim(); 98 } catch (error) { 99 let e: BusinessError = error as BusinessError; 100 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 101 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 102 } 103 ``` 104 105### reclaim<sup>9+</sup> 106 107reclaim(): void 108 109释放不再使用的MessageSequence对象。 110 111**系统能力**:SystemCapability.Communication.IPC.Core 112 113**示例:** 114 115 ```ts 116 import { rpc } from '@kit.IPCKit'; 117 import { hilog } from '@kit.PerformanceAnalysisKit'; 118 import { BusinessError } from '@kit.BasicServicesKit'; 119 120 try { 121 let reply = rpc.MessageSequence.create(); 122 reply.reclaim(); 123 } catch (error) { 124 let e: BusinessError = error as BusinessError; 125 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 126 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 127 } 128 ``` 129 130### writeRemoteObject<sup>9+</sup> 131 132writeRemoteObject(object: IRemoteObject): void 133 134序列化远程对象并将其写入[MessageSequence](#messagesequence9)对象。 135 136**系统能力**:SystemCapability.Communication.IPC.Core 137 138**参数:** 139 140 | 参数名 | 类型 | 必填 | 说明 | 141 | ------ | ------------------------------- | ---- | ----------------------------------------- | 142 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageSequence的远程对象。 | 143 144**错误码:** 145 146以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 147 148 | 错误码ID | 错误信息 | 149 | -------- | -------- | 150 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 151 | 1900008 | The proxy or remote object is invalid. | 152 | 1900009 | Failed to write data to the message sequence. | 153 154**示例:** 155 156 ```ts 157 import { rpc } from '@kit.IPCKit'; 158 import { hilog } from '@kit.PerformanceAnalysisKit'; 159 import { BusinessError } from '@kit.BasicServicesKit'; 160 161 class TestRemoteObject extends rpc.RemoteObject { 162 constructor(descriptor: string) { 163 super(descriptor); 164 } 165 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 166 // 根据业务实际逻辑,进行相应处理 167 return true; 168 } 169 } 170 171 try { 172 let data = rpc.MessageSequence.create(); 173 let testRemoteObject = new TestRemoteObject("testObject"); 174 data.writeRemoteObject(testRemoteObject); 175 } catch (error) { 176 let e: BusinessError = error as BusinessError; 177 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 178 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 179 } 180 ``` 181 182### readRemoteObject<sup>9+</sup> 183 184readRemoteObject(): IRemoteObject 185 186从MessageSequence读取远程对象。此方法用于反序列化MessageSequence对象以生成IRemoteObject。远程对象按写入MessageSequence的顺序读取。 187 188**系统能力**:SystemCapability.Communication.IPC.Core 189 190**返回值:** 191 192 | 类型 | 说明 | 193 | ------------------------------- | ------------------ | 194 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 195 196**错误码:** 197 198以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 199 200 | 错误码ID | 错误信息 | 201 | -------- | -------- | 202 | 1900008 | The proxy or remote object is invalid. | 203 | 1900010 | Failed to read data from the message sequence. | 204 205**示例:** 206 207 ```ts 208 import { rpc } from '@kit.IPCKit'; 209 import { hilog } from '@kit.PerformanceAnalysisKit'; 210 import { BusinessError } from '@kit.BasicServicesKit'; 211 212 class TestRemoteObject extends rpc.RemoteObject { 213 constructor(descriptor: string) { 214 super(descriptor); 215 } 216 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 217 // 根据业务实际逻辑,进行相应处理 218 return true; 219 } 220 } 221 222 try { 223 let data = rpc.MessageSequence.create(); 224 let testRemoteObject = new TestRemoteObject("testObject"); 225 data.writeRemoteObject(testRemoteObject); 226 let proxy = data.readRemoteObject(); 227 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 228 } catch (error) { 229 let e: BusinessError = error as BusinessError; 230 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 231 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 232 } 233 ``` 234 235### writeInterfaceToken<sup>9+</sup> 236 237writeInterfaceToken(token: string): void 238 239将接口描述符写入MessageSequence对象,远端对象可使用该信息校验本次通信。 240 241**系统能力**:SystemCapability.Communication.IPC.Core 242 243**参数:** 244 245 | 参数名 | 类型 | 必填 | 说明 | 246 | ------ | ------ | ---- | ------------------ | 247 | token | string | 是 | 字符串类型描述符,其长度应小于40960字节。 | 248 249**错误码:** 250 251以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 252 253 | 错误码ID | 错误信息 | 254 | -------- | -------- | 255 | 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. | 256 | 1900009 | Failed to write data to the message sequence. | 257 258**示例:** 259 260 ```ts 261 import { rpc } from '@kit.IPCKit'; 262 import { hilog } from '@kit.PerformanceAnalysisKit'; 263 import { BusinessError } from '@kit.BasicServicesKit'; 264 265 try { 266 let data = rpc.MessageSequence.create(); 267 data.writeInterfaceToken("aaa"); 268 } catch (error) { 269 let e: BusinessError = error as BusinessError; 270 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 271 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 272 } 273 ``` 274 275### readInterfaceToken<sup>9+</sup> 276 277readInterfaceToken(): string 278 279从MessageSequence对象中读取接口描述符,接口描述符按写入MessageSequence的顺序读取,本地对象可使用该信息检验本次通信。 280 281**系统能力**:SystemCapability.Communication.IPC.Core 282 283**返回值:** 284 285 | 类型 | 说明 | 286 | ------ | ------------------------ | 287 | string | 返回读取到的接口描述符。 | 288 289**错误码:** 290 291以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 292 293 | 错误码ID | 错误信息 | 294 | -------- | -------- | 295 | 1900010 | Failed to read data from the message sequence. | 296 297**示例:** 298 299```ts 300 import { rpc } from '@kit.IPCKit'; 301 import { hilog } from '@kit.PerformanceAnalysisKit'; 302 import { BusinessError } from '@kit.BasicServicesKit'; 303 304 try { 305 let data = rpc.MessageSequence.create(); 306 data.writeInterfaceToken("aaa"); 307 let interfaceToken = data.readInterfaceToken(); 308 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 309 } catch (error) { 310 let e: BusinessError = error as BusinessError; 311 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 312 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 313 } 314``` 315 316### getSize<sup>9+</sup> 317 318getSize(): number 319 320获取当前创建的MessageSequence对象的数据大小。 321 322**系统能力**:SystemCapability.Communication.IPC.Core 323 324**返回值:** 325 326 | 类型 | 说明 | 327 | ------ | ----------------------------------------------- | 328 | number | 获取的MessageSequence实例的数据大小。以字节为单位。 | 329 330**示例:** 331 332 ```ts 333 import { rpc } from '@kit.IPCKit'; 334 import { hilog } from '@kit.PerformanceAnalysisKit'; 335 import { BusinessError } from '@kit.BasicServicesKit'; 336 337 try { 338 let data = rpc.MessageSequence.create(); 339 let size = data.getSize(); 340 hilog.info(0x0000, 'testTag', 'size is ' + size); 341 } catch (error) { 342 let e: BusinessError = error as BusinessError; 343 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 344 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 345 } 346 ``` 347 348### getCapacity<sup>9+</sup> 349 350getCapacity(): number 351 352获取当前MessageSequence对象的容量大小。 353 354**系统能力**:SystemCapability.Communication.IPC.Core 355 356**返回值:** 357 358 | 类型 | 说明 | 359 | ------ | ----- | 360 | number | 获取的MessageSequence实例的容量大小。以字节为单位。 | 361 362**示例:** 363 364 ```ts 365 import { rpc } from '@kit.IPCKit'; 366 import { hilog } from '@kit.PerformanceAnalysisKit'; 367 import { BusinessError } from '@kit.BasicServicesKit'; 368 369 try { 370 let data = rpc.MessageSequence.create(); 371 let result = data.getCapacity(); 372 hilog.info(0x0000, 'testTag', 'capacity is ' + result); 373 } catch (error) { 374 let e: BusinessError = error as BusinessError; 375 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 376 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 377 } 378 ``` 379 380### setSize<sup>9+</sup> 381 382setSize(size: number): void 383 384设置MessageSequence对象中包含的数据大小。 385 386**系统能力**:SystemCapability.Communication.IPC.Core 387 388**参数:** 389 390 | 参数名 | 类型 | 必填 | 说明 | 391 | ------ | ------ | ---- | ------ | 392 | size | number | 是 | MessageSequence实例的数据大小。以字节为单位。 | 393 394**错误码:** 395 396以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 397 398 | 错误码ID | 错误信息 | 399 | -------- | -------- | 400 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 401 | 1900009 | Failed to write data to the message sequence. | 402 403**示例:** 404 405 ```ts 406 import { rpc } from '@kit.IPCKit'; 407 import { hilog } from '@kit.PerformanceAnalysisKit'; 408 import { BusinessError } from '@kit.BasicServicesKit'; 409 410 try { 411 let data = rpc.MessageSequence.create(); 412 data.writeString('Hello World'); 413 data.setSize(16); 414 } catch (error) { 415 let e: BusinessError = error as BusinessError; 416 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 417 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 418 } 419 ``` 420 421### setCapacity<sup>9+</sup> 422 423setCapacity(size: number): void 424 425设置MessageSequence对象的存储容量。 426 427**系统能力**:SystemCapability.Communication.IPC.Core 428 429**参数:** 430 431 | 参数名 | 类型 | 必填 | 说明 | 432 | ------ | ------ | ---- | --------------------------------------------- | 433 | size | number | 是 | MessageSequence实例的存储容量。以字节为单位。 | 434 435**错误码:** 436 437以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 438 439 | 错误码ID | 错误信息 | 440 | -------- | -------- | 441 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 442 | 1900009 | Failed to write data to the message sequence. | 443 | 1900011 | Memory allocation failed. | 444 445**示例:** 446 447 ```ts 448 import { rpc } from '@kit.IPCKit'; 449 import { hilog } from '@kit.PerformanceAnalysisKit'; 450 import { BusinessError } from '@kit.BasicServicesKit'; 451 452 try { 453 let data = rpc.MessageSequence.create(); 454 data.setCapacity(100); 455 } catch (error) { 456 let e: BusinessError = error as BusinessError; 457 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 458 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 459 } 460 ``` 461 462### getWritableBytes<sup>9+</sup> 463 464getWritableBytes(): number 465 466获取MessageSequence的可写字节空间大小。 467 468**系统能力**:SystemCapability.Communication.IPC.Core 469 470**返回值:** 471 472 | 类型 | 说明 | 473 | ------ | ------ | 474 | number | 获取到的MessageSequence实例的可写字节空间。以字节为单位。 | 475 476**示例:** 477 478```ts 479 import { rpc } from '@kit.IPCKit'; 480 import { hilog } from '@kit.PerformanceAnalysisKit'; 481 import { BusinessError } from '@kit.BasicServicesKit'; 482 483 try { 484 let data = rpc.MessageSequence.create(); 485 data.setCapacity(100); 486 let getWritableBytes = data.getWritableBytes(); 487 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 488 } catch (error) { 489 let e: BusinessError = error as BusinessError; 490 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 491 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 492 } 493``` 494 495### getReadableBytes<sup>9+</sup> 496 497getReadableBytes(): number 498 499获取MessageSequence的可读字节空间。 500 501**系统能力**:SystemCapability.Communication.IPC.Core 502 503**返回值:** 504 505 | 类型 | 说明 | 506 | ------ | ------- | 507 | number | 获取到的MessageSequence实例的可读字节空间。以字节为单位。 | 508 509**示例:** 510 511```ts 512 import { rpc } from '@kit.IPCKit'; 513 import { hilog } from '@kit.PerformanceAnalysisKit'; 514 import { BusinessError } from '@kit.BasicServicesKit'; 515 516 try { 517 let data = rpc.MessageSequence.create(); 518 data.writeString("hello world"); 519 let result = data.getReadableBytes(); 520 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 521 } catch (error) { 522 let e: BusinessError = error as BusinessError; 523 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 524 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 525 } 526``` 527 528### getReadPosition<sup>9+</sup> 529 530getReadPosition(): number 531 532获取MessageSequence的读位置。 533 534**系统能力**:SystemCapability.Communication.IPC.Core 535 536**返回值:** 537 538 | 类型 | 说明 | 539 | ------ | ------ | 540 | number | 返回MessageSequence实例中的当前读取位置。 | 541 542**示例:** 543 544 ```ts 545 import { rpc } from '@kit.IPCKit'; 546 import { hilog } from '@kit.PerformanceAnalysisKit'; 547 import { BusinessError } from '@kit.BasicServicesKit'; 548 549 try { 550 let data = rpc.MessageSequence.create(); 551 data.writeString("hello world"); 552 let readPos = data.getReadPosition(); 553 hilog.info(0x0000, 'testTag', 'readPos is ' + readPos); 554 } catch (error) { 555 let e: BusinessError = error as BusinessError; 556 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 557 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 558 } 559 ``` 560 561### getWritePosition<sup>9+</sup> 562 563getWritePosition(): number 564 565获取MessageSequence的写位置。 566 567**系统能力**:SystemCapability.Communication.IPC.Core 568 569**返回值:** 570 571 | 类型 | 说明 | 572 | ------ | ----- | 573 | number | 返回MessageSequence实例中的当前写入位置。 | 574 575**示例:** 576 577 ```ts 578 import { rpc } from '@kit.IPCKit'; 579 import { hilog } from '@kit.PerformanceAnalysisKit'; 580 import { BusinessError } from '@kit.BasicServicesKit'; 581 582 try { 583 let data = rpc.MessageSequence.create(); 584 data.writeInt(10); 585 let bwPos = data.getWritePosition(); 586 hilog.info(0x0000, 'testTag', 'bwPos is ' + bwPos); 587 } catch (error) { 588 let e: BusinessError = error as BusinessError; 589 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 590 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 591 } 592 ``` 593 594### rewindRead<sup>9+</sup> 595 596rewindRead(pos: number): void 597 598重新偏移读取位置到指定的位置。 599 600**系统能力**:SystemCapability.Communication.IPC.Core 601 602**参数:** 603 604 | 参数名 | 类型 | 必填 | 说明 | 605 | ------ | ------ | ---- | ------- | 606 | pos | number | 是 | 开始读取数据的目标位置。 | 607 608**错误码:** 609 610以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 611 612 | 错误码ID | 错误信息 | 613 | -------- | -------- | 614 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 615 | 1900010 | Failed to read data from the message sequence. | 616 617**示例:** 618 619 ```ts 620 import { rpc } from '@kit.IPCKit'; 621 import { hilog } from '@kit.PerformanceAnalysisKit'; 622 import { BusinessError } from '@kit.BasicServicesKit'; 623 624 try { 625 let data = rpc.MessageSequence.create(); 626 data.writeInt(12); 627 data.writeString("sequence"); 628 let number = data.readInt(); 629 hilog.info(0x0000, 'testTag', 'number is ' + number); 630 data.rewindRead(0); 631 let number2 = data.readInt(); 632 hilog.info(0x0000, 'testTag', 'rewindRead is ' + number2); 633 } catch (error) { 634 let e: BusinessError = error as BusinessError; 635 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 636 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 637 } 638 ``` 639 640### rewindWrite<sup>9+</sup> 641 642rewindWrite(pos: number): void 643 644重新偏移写位置到指定的位置。 645 646**系统能力**:SystemCapability.Communication.IPC.Core 647 648**参数:** 649 650 | 参数名 | 类型 | 必填 | 说明 | 651 | ------ | ------ | ---- | ----- | 652 | pos | number | 是 | 开始写入数据的目标位置。 | 653 654**错误码:** 655 656以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 657 658 | 错误码ID | 错误信息 | 659 | -------- | -------- | 660 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 661 | 1900009 | Failed to write data to the message sequence. | 662 663**示例:** 664 665 ```ts 666 import { rpc } from '@kit.IPCKit'; 667 import { hilog } from '@kit.PerformanceAnalysisKit'; 668 import { BusinessError } from '@kit.BasicServicesKit'; 669 670 try { 671 let data = rpc.MessageSequence.create(); 672 data.writeInt(4); 673 data.rewindWrite(0); 674 data.writeInt(5); 675 let number = data.readInt(); 676 hilog.info(0x0000, 'testTag', 'rewindWrite is: ' + number); 677 } catch (error) { 678 let e: BusinessError = error as BusinessError; 679 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 680 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 681 } 682 ``` 683 684### writeByte<sup>9+</sup> 685 686writeByte(val: number): void 687 688将字节值写入MessageSequence实例。 689 690**系统能力**:SystemCapability.Communication.IPC.Core 691 692**参数:** 693 694 | 参数名 | 类型 | 必填 | 说明 | 695 | ------ | ------ | ---- | ----- | 696 | val | number | 是 | 要写入的字节值。 | 697 698**错误码:** 699 700以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 701 702 | 错误码ID | 错误信息 | 703 | -------- | ------- | 704 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 705 | 1900009 | Failed to write data to the message sequence. | 706 707**示例:** 708 709 ```ts 710 import { rpc } from '@kit.IPCKit'; 711 import { hilog } from '@kit.PerformanceAnalysisKit'; 712 import { BusinessError } from '@kit.BasicServicesKit'; 713 714 try { 715 let data = rpc.MessageSequence.create(); 716 data.writeByte(2); 717 } catch (error) { 718 let e: BusinessError = error as BusinessError; 719 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 720 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 721 } 722 ``` 723 724### readByte<sup>9+</sup> 725 726readByte(): number 727 728从MessageSequence实例读取字节值。 729 730**系统能力**:SystemCapability.Communication.IPC.Core 731 732**返回值:** 733 734 | 类型 | 说明 | 735 | ------ | ----- | 736 | number | 返回字节值。 | 737 738**错误码:** 739 740以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 741 742 | 错误码ID | 错误信息 | 743 | ------- | -------- | 744 | 1900010 | Failed to read data from the message sequence. | 745 746**示例:** 747 748 ```ts 749 import { rpc } from '@kit.IPCKit'; 750 import { hilog } from '@kit.PerformanceAnalysisKit'; 751 import { BusinessError } from '@kit.BasicServicesKit'; 752 753 try { 754 let data = rpc.MessageSequence.create(); 755 data.writeByte(2); 756 let ret = data.readByte(); 757 hilog.info(0x0000, 'testTag', 'readByte is: ' + ret); 758 } catch (error) { 759 let e: BusinessError = error as BusinessError; 760 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 761 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 762 } 763 ``` 764 765### writeShort<sup>9+</sup> 766 767writeShort(val: number): void 768 769将短整数值写入MessageSequence实例。 770 771**系统能力**:SystemCapability.Communication.IPC.Core 772 773**参数:** 774 775 | 参数名 | 类型 | 必填 | 说明 | 776 | ------ | ------ | --- | --- | 777 | val | number | 是 | 要写入的短整数值。 | 778 779**错误码:** 780 781以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 782 783 | 错误码ID | 错误信息 | 784 | -------- | -------- | 785 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 786 | 1900009 | Failed to write data to the message sequence. | 787 788**示例:** 789 790 ```ts 791 import { rpc } from '@kit.IPCKit'; 792 import { hilog } from '@kit.PerformanceAnalysisKit'; 793 import { BusinessError } from '@kit.BasicServicesKit'; 794 795 try { 796 let data = rpc.MessageSequence.create(); 797 data.writeShort(8); 798 } catch (error) { 799 let e: BusinessError = error as BusinessError; 800 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 801 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 802 } 803 ``` 804 805### readShort<sup>9+</sup> 806 807readShort(): number 808 809从MessageSequence实例读取短整数值。 810 811**系统能力**:SystemCapability.Communication.IPC.Core 812 813**返回值:** 814 815 | 类型 | 说明 | 816 | ------ | -------------- | 817 | number | 返回短整数值。 | 818 819**错误码:** 820 821以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 822 823 | 错误码ID | 错误信息 | 824 | -------- | -------- | 825 | 1900010 | Failed to read data from the message sequence. | 826 827**示例:** 828 829 ```ts 830 import { rpc } from '@kit.IPCKit'; 831 import { hilog } from '@kit.PerformanceAnalysisKit'; 832 import { BusinessError } from '@kit.BasicServicesKit'; 833 834 try { 835 let data = rpc.MessageSequence.create(); 836 data.writeShort(8); 837 let ret = data.readShort(); 838 hilog.info(0x0000, 'testTag', 'readShort is ' + ret); 839 } catch (error) { 840 let e: BusinessError = error as BusinessError; 841 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 842 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 843 } 844 ``` 845 846### writeInt<sup>9+</sup> 847 848writeInt(val: number): void 849 850将整数值写入MessageSequence实例。 851 852**系统能力**:SystemCapability.Communication.IPC.Core 853 854**参数:** 855 856 | 参数名 | 类型 | 必填 | 说明 | 857 | ------ | ------ | ---- | ---------------- | 858 | val | number | 是 | 要写入的整数值。 | 859 860**错误码:** 861 862以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 863 864 | 错误码ID | 错误信息 | 865 | -------- | -------- | 866 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 867 | 1900009 | Failed to write data to the message sequence. | 868 869**示例:** 870 871 ```ts 872 import { rpc } from '@kit.IPCKit'; 873 import { hilog } from '@kit.PerformanceAnalysisKit'; 874 import { BusinessError } from '@kit.BasicServicesKit'; 875 876 try { 877 let data = rpc.MessageSequence.create(); 878 data.writeInt(10); 879 } catch (error) { 880 let e: BusinessError = error as BusinessError; 881 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 882 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 883 } 884 ``` 885 886### readInt<sup>9+</sup> 887 888readInt(): number 889 890从MessageSequence实例读取整数值。 891 892**系统能力**:SystemCapability.Communication.IPC.Core 893 894**返回值:** 895 896 | 类型 | 说明 | 897 | ------ | ------------ | 898 | number | 返回整数值。 | 899 900**错误码:** 901 902以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 903 904 | 错误码ID | 错误信息 | 905 | -------- | -------- | 906 | 1900010 | Failed to read data from the message sequence. | 907 908**示例:** 909 910 ```ts 911 import { rpc } from '@kit.IPCKit'; 912 import { hilog } from '@kit.PerformanceAnalysisKit'; 913 import { BusinessError } from '@kit.BasicServicesKit'; 914 915 try { 916 let data = rpc.MessageSequence.create(); 917 data.writeInt(10); 918 let ret = data.readInt(); 919 hilog.info(0x0000, 'testTag', 'readInt is ' + ret); 920 } catch (error) { 921 let e: BusinessError = error as BusinessError; 922 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 923 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 924 } 925 ``` 926 927### writeLong<sup>9+</sup> 928 929writeLong(val: number): void 930 931将长整数值写入MessageSequence实例。 932 933**系统能力**:SystemCapability.Communication.IPC.Core 934 935**参数:** 936 937 | 参数名 | 类型 | 必填 | 说明 | 938 | ------ | ------ | ---- | ---------------- | 939 | val | number | 是 | 要写入的长整数值。 | 940 941**错误码:** 942 943以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 944 945 | 错误码ID | 错误信息 | 946 | -------- | -------- | 947 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 948 | 1900009 | Failed to write data to the message sequence. | 949 950**示例:** 951 952 ```ts 953 import { rpc } from '@kit.IPCKit'; 954 import { hilog } from '@kit.PerformanceAnalysisKit'; 955 import { BusinessError } from '@kit.BasicServicesKit'; 956 957 try { 958 let data = rpc.MessageSequence.create(); 959 data.writeLong(10000); 960 } catch (error) { 961 let e: BusinessError = error as BusinessError; 962 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 963 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 964 } 965 ``` 966 967### readLong<sup>9+</sup> 968 969readLong(): number 970 971从MessageSequence实例中读取长整数值。 972 973**系统能力**:SystemCapability.Communication.IPC.Core 974 975**返回值:** 976 977 | 类型 | 说明 | 978 | ------ | -------------- | 979 | number | 返回长整数值。 | 980 981**错误码:** 982 983以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 984 985 | 错误码ID | 错误信息 | 986 | -------- | -------- | 987 | 1900010 | Failed to read data from the message sequence. | 988 989**示例:** 990 991 ```ts 992 import { rpc } from '@kit.IPCKit'; 993 import { hilog } from '@kit.PerformanceAnalysisKit'; 994 import { BusinessError } from '@kit.BasicServicesKit'; 995 996 try { 997 let data = rpc.MessageSequence.create(); 998 data.writeLong(10000); 999 let ret = data.readLong(); 1000 hilog.info(0x0000, 'testTag', 'readLong is ' + ret); 1001 } catch (error) { 1002 let e: BusinessError = error as BusinessError; 1003 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1004 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1005 } 1006 ``` 1007 1008### writeFloat<sup>9+</sup> 1009 1010writeFloat(val: number): void 1011 1012将双精度浮点值写入MessageSequence实例。 1013 1014**系统能力**:SystemCapability.Communication.IPC.Core 1015 1016**参数:** 1017 1018 | 参数名 | 类型 | 必填 | 说明 | 1019 | ------ | ------ | ---- | ----- | 1020 | val | number | 是 | 要写入的双精度浮点值。 | 1021 1022**错误码:** 1023 1024以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1025 1026 | 错误码ID | 错误信息 | 1027 | -------- | -------- | 1028 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1029 | 1900009 | Failed to write data to the message sequence. | 1030 1031**示例:** 1032 1033 ```ts 1034 import { rpc } from '@kit.IPCKit'; 1035 import { hilog } from '@kit.PerformanceAnalysisKit'; 1036 import { BusinessError } from '@kit.BasicServicesKit'; 1037 1038 try { 1039 let data = rpc.MessageSequence.create(); 1040 data.writeFloat(1.2); 1041 } catch (error) { 1042 let e: BusinessError = error as BusinessError; 1043 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1044 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1045 } 1046 ``` 1047 1048### readFloat<sup>9+</sup> 1049 1050readFloat(): number 1051 1052从MessageSequence实例中读取双精度浮点值。 1053 1054**系统能力**:SystemCapability.Communication.IPC.Core 1055 1056**返回值:** 1057 1058 | 类型 | 说明 | 1059 | ------ | ------------ | 1060 | number | 返回双精度浮点值。 | 1061 1062**错误码:** 1063 1064以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1065 1066 | 错误码ID | 错误信息 | 1067 | -------- | -------- | 1068 | 1900010 | Failed to read data from the message sequence. | 1069 1070**示例:** 1071 1072 ```ts 1073 import { rpc } from '@kit.IPCKit'; 1074 import { hilog } from '@kit.PerformanceAnalysisKit'; 1075 import { BusinessError } from '@kit.BasicServicesKit'; 1076 1077 try { 1078 let data = rpc.MessageSequence.create(); 1079 data.writeFloat(1.2); 1080 let ret = data.readFloat(); 1081 hilog.info(0x0000, 'testTag', 'readFloat is ' + ret); 1082 } catch (error) { 1083 let e: BusinessError = error as BusinessError; 1084 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1085 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1086 } 1087 ``` 1088 1089### writeDouble<sup>9+</sup> 1090 1091writeDouble(val: number): void 1092 1093将双精度浮点值写入MessageSequence实例。 1094 1095**系统能力**:SystemCapability.Communication.IPC.Core 1096 1097**参数:** 1098 1099 | 参数名 | 类型 | 必填 | 说明 | 1100 | ------ | ------ | ---- | ---------------------- | 1101 | val | number | 是 | 要写入的双精度浮点值。 | 1102 1103**错误码:** 1104 1105以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1106 1107 | 错误码ID | 错误信息 | 1108 | -------- | -------- | 1109 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1110 | 1900009 | Failed to write data to the message sequence. | 1111 1112**示例:** 1113 1114 ```ts 1115 import { rpc } from '@kit.IPCKit'; 1116 import { hilog } from '@kit.PerformanceAnalysisKit'; 1117 import { BusinessError } from '@kit.BasicServicesKit'; 1118 1119 try { 1120 let data = rpc.MessageSequence.create(); 1121 data.writeDouble(10.2); 1122 } catch (error) { 1123 let e: BusinessError = error as BusinessError; 1124 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1125 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1126 } 1127 ``` 1128 1129### readDouble<sup>9+</sup> 1130 1131readDouble(): number 1132 1133从MessageSequence实例读取双精度浮点值。 1134 1135**系统能力**:SystemCapability.Communication.IPC.Core 1136 1137**返回值:** 1138 1139 | 类型 | 说明 | 1140 | ------ | ------------------ | 1141 | number | 返回双精度浮点值。 | 1142 1143**错误码:** 1144 1145以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1146 1147 | 错误码ID | 错误信息 | 1148 | -------- | -------- | 1149 | 1900010 | Failed to read data from the message sequence. | 1150 1151**示例:** 1152 1153 ```ts 1154 import { rpc } from '@kit.IPCKit'; 1155 import { hilog } from '@kit.PerformanceAnalysisKit'; 1156 import { BusinessError } from '@kit.BasicServicesKit'; 1157 1158 try { 1159 let data = rpc.MessageSequence.create(); 1160 data.writeDouble(10.2); 1161 let ret = data.readDouble(); 1162 hilog.info(0x0000, 'testTag', 'readDouble is ' + ret); 1163 } catch (error) { 1164 let e: BusinessError = error as BusinessError; 1165 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1166 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1167 } 1168 ``` 1169 1170### writeBoolean<sup>9+</sup> 1171 1172writeBoolean(val: boolean): void 1173 1174将布尔值写入MessageSequence实例。 1175 1176**系统能力**:SystemCapability.Communication.IPC.Core 1177 1178**参数:** 1179 1180 | 参数名 | 类型 | 必填 | 说明 | 1181 | ------ | ------- | ---- | ---------------- | 1182 | val | boolean | 是 | 要写入的布尔值。 | 1183 1184**错误码:** 1185 1186以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1187 1188 | 错误码ID | 错误信息 | 1189 | -------- | -------- | 1190 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1191 | 1900009 | Failed to write data to the message sequence. | 1192 1193**示例:** 1194 1195 ```ts 1196 import { rpc } from '@kit.IPCKit'; 1197 import { hilog } from '@kit.PerformanceAnalysisKit'; 1198 import { BusinessError } from '@kit.BasicServicesKit'; 1199 1200 try { 1201 let data = rpc.MessageSequence.create(); 1202 data.writeBoolean(false); 1203 } catch (error) { 1204 let e: BusinessError = error as BusinessError; 1205 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1206 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1207 } 1208 ``` 1209 1210### readBoolean<sup>9+</sup> 1211 1212readBoolean(): boolean 1213 1214从MessageSequence实例读取布尔值。 1215 1216**系统能力**:SystemCapability.Communication.IPC.Core 1217 1218**返回值:** 1219 1220 | 类型 | 说明 | 1221 | ------- | -------------------- | 1222 | boolean | 返回读取到的布尔值。 | 1223 1224**错误码:** 1225 1226以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1227 1228 | 错误码ID | 错误信息 | 1229 | -------- | -------- | 1230 | 1900010 | Failed to read data from the message sequence. | 1231 1232**示例:** 1233 1234 ```ts 1235 import { rpc } from '@kit.IPCKit'; 1236 import { hilog } from '@kit.PerformanceAnalysisKit'; 1237 import { BusinessError } from '@kit.BasicServicesKit'; 1238 1239 try { 1240 let data = rpc.MessageSequence.create(); 1241 data.writeBoolean(false); 1242 let ret = data.readBoolean(); 1243 hilog.info(0x0000, 'testTag', 'readBoolean is ' + ret); 1244 } catch (error) { 1245 let e: BusinessError = error as BusinessError; 1246 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1247 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1248 } 1249 ``` 1250 1251### writeChar<sup>9+</sup> 1252 1253writeChar(val: number): void 1254 1255将单个字符值写入MessageSequence实例。 1256 1257**系统能力**:SystemCapability.Communication.IPC.Core 1258 1259**参数:** 1260 1261 | 参数名 | 类型 | 必填 | 说明 | 1262 | ------ | ------ | ---- | -------------------- | 1263 | val | number | 是 | 要写入的单个字符值。 | 1264 1265**错误码:** 1266 1267以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1268 1269 | 错误码ID | 错误信息 | 1270 | -------- | -------- | 1271 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1272 | 1900009 | Failed to write data to the message sequence. | 1273 1274**示例:** 1275 1276 ```ts 1277 import { rpc } from '@kit.IPCKit'; 1278 import { hilog } from '@kit.PerformanceAnalysisKit'; 1279 import { BusinessError } from '@kit.BasicServicesKit'; 1280 1281 try { 1282 let data = rpc.MessageSequence.create(); 1283 data.writeChar(97); 1284 } catch (error) { 1285 let e: BusinessError = error as BusinessError; 1286 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1287 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1288 } 1289 ``` 1290 1291### readChar<sup>9+</sup> 1292 1293readChar(): number 1294 1295从MessageSequence实例中读取单个字符值。 1296 1297**系统能力**:SystemCapability.Communication.IPC.Core 1298 1299**返回值:** 1300 1301 | 类型 | 说明 | 1302 | ------ | ---- | 1303 | number | 返回单个字符值。 | 1304 1305**错误码:** 1306 1307以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1308 1309 | 错误码ID | 错误信息 | 1310 | -------- | -------- | 1311 | 1900010 | Failed to read data from the message sequence. | 1312 1313**示例:** 1314 1315 ```ts 1316 import { rpc } from '@kit.IPCKit'; 1317 import { hilog } from '@kit.PerformanceAnalysisKit'; 1318 import { BusinessError } from '@kit.BasicServicesKit'; 1319 1320 try { 1321 let data = rpc.MessageSequence.create(); 1322 data.writeChar(97); 1323 let ret = data.readChar(); 1324 hilog.info(0x0000, 'testTag', 'readChar is ' + ret); 1325 } catch (error) { 1326 let e: BusinessError = error as BusinessError; 1327 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1328 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1329 } 1330 ``` 1331 1332### writeString<sup>9+</sup> 1333 1334writeString(val: string): void 1335 1336将字符串值写入MessageSequence实例。 1337 1338**系统能力**:SystemCapability.Communication.IPC.Core 1339 1340**参数:** 1341 1342 | 参数名 | 类型 | 必填 | 说明 | 1343 | ------ | ------ | ---- | ----------------------------------------- | 1344 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 1345 1346**错误码:** 1347 1348以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1349 1350 | 错误码ID | 错误信息 | 1351 | -------- | -------- | 1352 | 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. | 1353 | 1900009 | Failed to write data to the message sequence. | 1354 1355**示例:** 1356 1357 ```ts 1358 import { rpc } from '@kit.IPCKit'; 1359 import { hilog } from '@kit.PerformanceAnalysisKit'; 1360 import { BusinessError } from '@kit.BasicServicesKit'; 1361 1362 try { 1363 let data = rpc.MessageSequence.create(); 1364 data.writeString('abc'); 1365 } catch (error) { 1366 let e: BusinessError = error as BusinessError; 1367 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1368 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1369 } 1370 ``` 1371 1372### readString<sup>9+</sup> 1373 1374readString(): string 1375 1376从MessageSequence实例读取字符串值。 1377 1378**系统能力**:SystemCapability.Communication.IPC.Core 1379 1380**返回值:** 1381 1382 | 类型 | 说明 | 1383 | ------ | -------------- | 1384 | string | 返回字符串值。 | 1385 1386**错误码:** 1387 1388以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1389 1390 | 错误码ID | 错误信息 | 1391 | -------- | -------- | 1392 | 1900010 | Failed to read data from the message sequence. | 1393 1394**示例:** 1395 1396 ```ts 1397 import { rpc } from '@kit.IPCKit'; 1398 import { hilog } from '@kit.PerformanceAnalysisKit'; 1399 import { BusinessError } from '@kit.BasicServicesKit'; 1400 1401 try { 1402 let data = rpc.MessageSequence.create(); 1403 data.writeString('abc'); 1404 let ret = data.readString(); 1405 hilog.info(0x0000, 'testTag', 'readString is ' + ret); 1406 } catch (error) { 1407 let e: BusinessError = error as BusinessError; 1408 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1409 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1410 } 1411 ``` 1412 1413### writeParcelable<sup>9+</sup> 1414 1415writeParcelable(val: Parcelable): void 1416 1417将自定义序列化对象写入MessageSequence实例。 1418 1419**系统能力**:SystemCapability.Communication.IPC.Core 1420 1421**参数:** 1422 1423| 参数名 | 类型 | 必填 | 说明 | 1424| ------ | --------- | ---- | ------ | 1425| val | [Parcelable](#parcelable9) | 是 | 要写入的可序列对象。 | 1426 1427**错误码:** 1428 1429以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1430 1431 | 错误码ID | 错误信息 | 1432 | -------- | -------- | 1433 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 1434 | 1900009 | Failed to write data to the message sequence. | 1435 1436**示例:** 1437 1438 ```ts 1439 import { rpc } from '@kit.IPCKit'; 1440 import { hilog } from '@kit.PerformanceAnalysisKit'; 1441 import { BusinessError } from '@kit.BasicServicesKit'; 1442 1443 class MyParcelable implements rpc.Parcelable { 1444 num: number = 0; 1445 str: string = ''; 1446 constructor( num: number, str: string) { 1447 this.num = num; 1448 this.str = str; 1449 } 1450 marshalling(messageSequence: rpc.MessageSequence): boolean { 1451 messageSequence.writeInt(this.num); 1452 messageSequence.writeString(this.str); 1453 return true; 1454 } 1455 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1456 this.num = messageSequence.readInt(); 1457 this.str = messageSequence.readString(); 1458 return true; 1459 } 1460 } 1461 1462 try { 1463 let parcelable = new MyParcelable(1, "aaa"); 1464 let data = rpc.MessageSequence.create(); 1465 data.writeParcelable(parcelable); 1466 } catch (error) { 1467 let e: BusinessError = error as BusinessError; 1468 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1469 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1470 } 1471 ``` 1472 1473### readParcelable<sup>9+</sup> 1474 1475readParcelable(dataIn: Parcelable): void 1476 1477从MessageSequence实例中读取成员变量到指定的对象(dataIn)。 1478 1479**系统能力**:SystemCapability.Communication.IPC.Core 1480 1481**参数:** 1482 1483| 参数名 | 类型 | 必填 | 说明 | 1484| ------ | -------------------------- | ---- | ----------------------------------------- | 1485| dataIn | [Parcelable](#parcelable9) | 是 | 需要从MessageSequence读取成员变量的对象。 | 1486 1487**错误码:** 1488 1489以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1490 1491 | 错误码ID | 错误信息 | 1492 | -------- | -------- | 1493 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect. | 1494 | 1900010 | Failed to read data from the message sequence. | 1495 | 1900012 | Failed to call the JS callback function. | 1496 1497**示例:** 1498 1499 ```ts 1500 import { rpc } from '@kit.IPCKit'; 1501 import { hilog } from '@kit.PerformanceAnalysisKit'; 1502 import { BusinessError } from '@kit.BasicServicesKit'; 1503 1504 class MyParcelable implements rpc.Parcelable { 1505 num: number = 0; 1506 str: string = ''; 1507 constructor( num: number, str: string) { 1508 this.num = num; 1509 this.str = str; 1510 } 1511 marshalling(messageSequence: rpc.MessageSequence): boolean { 1512 messageSequence.writeInt(this.num); 1513 messageSequence.writeString(this.str); 1514 return true; 1515 } 1516 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1517 this.num = messageSequence.readInt(); 1518 this.str = messageSequence.readString(); 1519 return true; 1520 } 1521 } 1522 1523 try { 1524 let parcelable = new MyParcelable(1, "aaa"); 1525 let data = rpc.MessageSequence.create(); 1526 data.writeParcelable(parcelable); 1527 let ret = new MyParcelable(0, ""); 1528 data.readParcelable(ret); 1529 } catch (error) { 1530 let e: BusinessError = error as BusinessError; 1531 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1532 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1533 } 1534 ``` 1535 1536### writeByteArray<sup>9+</sup> 1537 1538writeByteArray(byteArray: number[]): void 1539 1540将字节数组写入MessageSequence实例。 1541 1542**系统能力**:SystemCapability.Communication.IPC.Core 1543 1544**参数:** 1545 1546 | 参数名 | 类型 | 必填 | 说明 | 1547 | --------- | -------- | ---- | ------------------ | 1548 | byteArray | number[] | 是 | 要写入的字节数组。 | 1549 1550**错误码:** 1551 1552以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1553 1554 | 错误码ID | 错误信息 | 1555 | -------- | -------- | 1556 | 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. | 1557 | 1900009 | Failed to write data to the message sequence. | 1558 1559**示例:** 1560 1561 ```ts 1562 import { rpc } from '@kit.IPCKit'; 1563 import { hilog } from '@kit.PerformanceAnalysisKit'; 1564 import { BusinessError } from '@kit.BasicServicesKit'; 1565 1566 try { 1567 let data = rpc.MessageSequence.create(); 1568 let ByteArrayVar = [1, 2, 3, 4, 5]; 1569 data.writeByteArray(ByteArrayVar); 1570 } catch (error) { 1571 let e: BusinessError = error as BusinessError; 1572 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1573 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1574 } 1575 ``` 1576 1577### readByteArray<sup>9+</sup> 1578 1579readByteArray(dataIn: number[]): void 1580 1581从MessageSequence实例读取字节数组。 1582 1583**系统能力**:SystemCapability.Communication.IPC.Core 1584 1585**参数:** 1586 1587 | 参数名 | 类型 | 必填 | 说明 | 1588 | ------ | -------- | ---- | ------------------ | 1589 | dataIn | number[] | 是 | 要读取的字节数组。 | 1590 1591**错误码:** 1592 1593以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1594 1595 | 错误码ID | 错误信息 | 1596 | -------- | -------- | 1597 | 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. | 1598 | 1900010 | Failed to read data from the message sequence. | 1599 1600**示例:** 1601 1602 ```ts 1603 import { rpc } from '@kit.IPCKit'; 1604 import { hilog } from '@kit.PerformanceAnalysisKit'; 1605 import { BusinessError } from '@kit.BasicServicesKit'; 1606 1607 try { 1608 let data = rpc.MessageSequence.create(); 1609 let ByteArrayVar = [1, 2, 3, 4, 5]; 1610 data.writeByteArray(ByteArrayVar); 1611 let array: Array<number> = new Array(5); 1612 data.readByteArray(array); 1613 hilog.info(0x0000, 'testTag', 'readByteArray is ' + array); 1614 } catch (error) { 1615 let e: BusinessError = error as BusinessError; 1616 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1617 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1618 } 1619 ``` 1620 1621### readByteArray<sup>9+</sup> 1622 1623readByteArray(): number[] 1624 1625从MessageSequence实例中读取字节数组。 1626 1627**系统能力**:SystemCapability.Communication.IPC.Core 1628 1629**返回值:** 1630 1631 | 类型 | 说明 | 1632 | -------- | -------------- | 1633 | number[] | 返回字节数组。 | 1634 1635**错误码:** 1636 1637以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1638 1639 | 错误码ID | 错误信息 | 1640 | -------- | -------- | 1641 | 1900010 | Failed to read data from the message sequence. | 1642 1643**示例:** 1644 1645 ```ts 1646 import { rpc } from '@kit.IPCKit'; 1647 import { hilog } from '@kit.PerformanceAnalysisKit'; 1648 import { BusinessError } from '@kit.BasicServicesKit'; 1649 1650 try { 1651 let data = rpc.MessageSequence.create(); 1652 let ByteArrayVar = [1, 2, 3, 4, 5]; 1653 data.writeByteArray(ByteArrayVar); 1654 let array = data.readByteArray(); 1655 hilog.info(0x0000, 'testTag', 'readByteArray is ' + array); 1656 } catch (error) { 1657 let e: BusinessError = error as BusinessError; 1658 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1659 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1660 } 1661 ``` 1662 1663### writeShortArray<sup>9+</sup> 1664 1665writeShortArray(shortArray: number[]): void 1666 1667将短整数数组写入MessageSequence实例。 1668 1669**系统能力**:SystemCapability.Communication.IPC.Core 1670 1671**参数:** 1672 1673 | 参数名 | 类型 | 必填 | 说明 | 1674 | ---------- | -------- | ---- | -------------------- | 1675 | shortArray | number[] | 是 | 要写入的短整数数组。 | 1676 1677**错误码:** 1678 1679以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1680 1681 | 错误码ID | 错误信息 | 1682 | -------- | -------- | 1683 | 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. | 1684 | 1900009 | Failed to write data to the message sequence. | 1685 1686**示例:** 1687 1688 ```ts 1689 import { rpc } from '@kit.IPCKit'; 1690 import { hilog } from '@kit.PerformanceAnalysisKit'; 1691 import { BusinessError } from '@kit.BasicServicesKit'; 1692 1693 try { 1694 let data = rpc.MessageSequence.create(); 1695 data.writeShortArray([11, 12, 13]); 1696 } catch (error) { 1697 let e: BusinessError = error as BusinessError; 1698 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1699 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1700 } 1701 ``` 1702 1703### readShortArray<sup>9+</sup> 1704 1705readShortArray(dataIn: number[]): void 1706 1707从MessageSequence实例中读取短整数数组。 1708 1709**系统能力**:SystemCapability.Communication.IPC.Core 1710 1711**参数:** 1712 1713 | 参数名 | 类型 | 必填 | 说明 | 1714 | ------ | -------- | ---- | -------------------- | 1715 | dataIn | number[] | 是 | 要读取的短整数数组。 | 1716 1717**错误码:** 1718 1719以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1720 1721 | 错误码ID | 错误信息 | 1722 | -------- | -------- | 1723 | 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. | 1724 | 1900010 | Failed to read data from the message sequence. | 1725 1726**示例:** 1727 1728 ```ts 1729 import { rpc } from '@kit.IPCKit'; 1730 import { hilog } from '@kit.PerformanceAnalysisKit'; 1731 import { BusinessError } from '@kit.BasicServicesKit'; 1732 1733 try { 1734 let data = rpc.MessageSequence.create(); 1735 data.writeShortArray([11, 12, 13]); 1736 let array: Array<number> = new Array(3); 1737 data.readShortArray(array); 1738 hilog.info(0x0000, 'testTag', 'readShortArray is ' + array); 1739 } catch (error) { 1740 let e: BusinessError = error as BusinessError; 1741 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1742 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1743 } 1744 ``` 1745 1746### readShortArray<sup>9+</sup> 1747 1748readShortArray(): number[] 1749 1750从MessageSequence实例中读取短整数数组。 1751 1752**系统能力**:SystemCapability.Communication.IPC.Core 1753 1754**返回值:** 1755 1756 | 类型 | 说明 | 1757 | -------- | ---------------- | 1758 | number[] | 返回短整数数组。 | 1759 1760**错误码:** 1761 1762以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1763 1764 | 错误码ID | 错误信息 | 1765 | -------- | -------- | 1766 | 1900010 | Failed to read data from the message sequence. | 1767 1768**示例:** 1769 1770 ```ts 1771 import { rpc } from '@kit.IPCKit'; 1772 import { hilog } from '@kit.PerformanceAnalysisKit'; 1773 import { BusinessError } from '@kit.BasicServicesKit'; 1774 1775 try { 1776 let data = rpc.MessageSequence.create(); 1777 data.writeShortArray([11, 12, 13]); 1778 let array = data.readShortArray(); 1779 hilog.info(0x0000, 'testTag', 'readShortArray is ' + array); 1780 } catch (error) { 1781 let e: BusinessError = error as BusinessError; 1782 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1783 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1784 } 1785 ``` 1786 1787### writeIntArray<sup>9+</sup> 1788 1789writeIntArray(intArray: number[]): void 1790 1791将整数数组写入MessageSequence实例。 1792 1793**系统能力**:SystemCapability.Communication.IPC.Core 1794 1795**参数:** 1796 1797 | 参数名 | 类型 | 必填 | 说明 | 1798 | -------- | -------- | ---- | ------------------ | 1799 | intArray | number[] | 是 | 要写入的整数数组。 | 1800 1801**错误码:** 1802 1803以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1804 1805 | 错误码ID | 错误信息 | 1806 | -------- | -------- | 1807 | 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. | 1808 | 1900009 | Failed to write data to the message sequence. | 1809 1810**示例:** 1811 1812 ```ts 1813 import { rpc } from '@kit.IPCKit'; 1814 import { hilog } from '@kit.PerformanceAnalysisKit'; 1815 import { BusinessError } from '@kit.BasicServicesKit'; 1816 1817 try { 1818 let data = rpc.MessageSequence.create(); 1819 data.writeIntArray([100, 111, 112]); 1820 } catch (error) { 1821 let e: BusinessError = error as BusinessError; 1822 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1823 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1824 } 1825 ``` 1826 1827### readIntArray<sup>9+</sup> 1828 1829readIntArray(dataIn: number[]): void 1830 1831从MessageSequence实例中读取整数数组。 1832 1833**系统能力**:SystemCapability.Communication.IPC.Core 1834 1835**参数:** 1836 1837 | 参数名 | 类型 | 必填 | 说明 | 1838 | ------ | -------- | ---- | ------------------ | 1839 | dataIn | number[] | 是 | 要读取的整数数组。 | 1840 1841**错误码:** 1842 1843以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1844 1845 | 错误码ID | 错误信息 | 1846 | -------- | -------- | 1847 | 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. | 1848 | 1900010 | Failed to read data from the message sequence. | 1849 1850**示例:** 1851 1852 ```ts 1853 import { rpc } from '@kit.IPCKit'; 1854 import { hilog } from '@kit.PerformanceAnalysisKit'; 1855 import { BusinessError } from '@kit.BasicServicesKit'; 1856 1857 try { 1858 let data = rpc.MessageSequence.create(); 1859 data.writeIntArray([100, 111, 112]); 1860 let array: Array<number> = new Array(3); 1861 data.readIntArray(array); 1862 hilog.info(0x0000, 'testTag', 'readIntArray is ' + array); 1863 } catch (error) { 1864 let e: BusinessError = error as BusinessError; 1865 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1866 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1867 } 1868 ``` 1869 1870### readIntArray<sup>9+</sup> 1871 1872readIntArray(): number[] 1873 1874从MessageSequence实例中读取整数数组。 1875 1876**系统能力**:SystemCapability.Communication.IPC.Core 1877 1878**返回值:** 1879 1880 | 类型 | 说明 | 1881 | -------- | -------------- | 1882 | number[] | 返回整数数组。 | 1883 1884**错误码:** 1885 1886以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1887 1888 | 错误码ID | 错误信息 | 1889 | -------- | -------- | 1890 | 1900010 | Failed to read data from the message sequence. | 1891 1892**示例:** 1893 1894 ```ts 1895 import { rpc } from '@kit.IPCKit'; 1896 import { hilog } from '@kit.PerformanceAnalysisKit'; 1897 import { BusinessError } from '@kit.BasicServicesKit'; 1898 1899 try { 1900 let data = rpc.MessageSequence.create(); 1901 data.writeIntArray([100, 111, 112]); 1902 let array = data.readIntArray(); 1903 hilog.info(0x0000, 'testTag', 'readIntArray is ' + array); 1904 } catch (error) { 1905 let e: BusinessError = error as BusinessError; 1906 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1907 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1908 } 1909 ``` 1910 1911### writeLongArray<sup>9+</sup> 1912 1913writeLongArray(longArray: number[]): void 1914 1915将长整数数组写入MessageSequence实例。 1916 1917**系统能力**:SystemCapability.Communication.IPC.Core 1918 1919**参数:** 1920 1921 | 参数名 | 类型 | 必填 | 说明 | 1922 | --------- | -------- | ---- | -------------------- | 1923 | longArray | number[] | 是 | 要写入的长整数数组。 | 1924 1925**错误码:** 1926 1927以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1928 1929 | 错误码ID | 错误信息 | 1930 | -------- | -------- | 1931 | 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. | 1932 | 1900009 | Failed to write data to the message sequence. | 1933 1934**示例:** 1935 1936 ```ts 1937 import { rpc } from '@kit.IPCKit'; 1938 import { hilog } from '@kit.PerformanceAnalysisKit'; 1939 import { BusinessError } from '@kit.BasicServicesKit'; 1940 1941 try { 1942 let data = rpc.MessageSequence.create(); 1943 data.writeLongArray([1111, 1112, 1113]); 1944 } catch (error) { 1945 let e: BusinessError = error as BusinessError; 1946 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1947 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1948 } 1949 ``` 1950 1951### readLongArray<sup>9+</sup> 1952 1953readLongArray(dataIn: number[]): void 1954 1955从MessageSequence实例读取的长整数数组。 1956 1957**系统能力**:SystemCapability.Communication.IPC.Core 1958 1959**参数:** 1960 1961 | 参数名 | 类型 | 必填 | 说明 | 1962 | ------ | -------- | ---- | -------------------- | 1963 | dataIn | number[] | 是 | 要读取的长整数数组。 | 1964 1965**错误码:** 1966 1967以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 1968 1969 | 错误码ID | 错误信息 | 1970 | -------- | -------- | 1971 | 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. | 1972 | 1900010 | Failed to read data from the message sequence. | 1973 1974**示例:** 1975 1976 ```ts 1977 import { rpc } from '@kit.IPCKit'; 1978 import { hilog } from '@kit.PerformanceAnalysisKit'; 1979 import { BusinessError } from '@kit.BasicServicesKit'; 1980 1981 try { 1982 let data = rpc.MessageSequence.create(); 1983 data.writeLongArray([1111, 1112, 1113]); 1984 let array: Array<number> = new Array(3); 1985 data.readLongArray(array); 1986 hilog.info(0x0000, 'testTag', 'readLongArray is ' + array); 1987 } catch (error) { 1988 let e: BusinessError = error as BusinessError; 1989 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 1990 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 1991 } 1992 ``` 1993 1994### readLongArray<sup>9+</sup> 1995 1996readLongArray(): number[] 1997 1998从MessageSequence实例中读取所有的长整数数组。 1999 2000**系统能力**:SystemCapability.Communication.IPC.Core 2001 2002**返回值:** 2003 2004 | 类型 | 说明 | 2005 | -------- | ---------------- | 2006 | number[] | 返回长整数数组。 | 2007 2008**错误码:** 2009 2010以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2011 2012 | 错误码ID | 错误信息 | 2013 | -------- | -------- | 2014 | 1900010 | Failed to read data from the message sequence. | 2015 2016**示例:** 2017 2018 ```ts 2019 import { rpc } from '@kit.IPCKit'; 2020 import { hilog } from '@kit.PerformanceAnalysisKit'; 2021 import { BusinessError } from '@kit.BasicServicesKit'; 2022 2023 try { 2024 let data = rpc.MessageSequence.create(); 2025 data.writeLongArray([1111, 1112, 1113]); 2026 let array = data.readLongArray(); 2027 hilog.info(0x0000, 'testTag', 'readLongArray is ' + array); 2028 } catch (error) { 2029 let e: BusinessError = error as BusinessError; 2030 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2031 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2032 } 2033 ``` 2034 2035### writeFloatArray<sup>9+</sup> 2036 2037writeFloatArray(floatArray: number[]): void 2038 2039将双精度浮点数组写入MessageSequence实例。 2040 2041**系统能力**:SystemCapability.Communication.IPC.Core 2042 2043**参数:** 2044 2045 | 参数名 | 类型 | 必填 | 说明 | 2046 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2047 | floatArray | number[] | 是 | 要写入的双精度浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2048 2049**错误码:** 2050 2051以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2052 2053 | 错误码ID | 错误信息 | 2054 | -------- | -------- | 2055 | 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. | 2056 | 1900009 | Failed to write data to the message sequence. | 2057 2058**示例:** 2059 2060 ```ts 2061 import { rpc } from '@kit.IPCKit'; 2062 import { hilog } from '@kit.PerformanceAnalysisKit'; 2063 import { BusinessError } from '@kit.BasicServicesKit'; 2064 2065 try { 2066 let data = rpc.MessageSequence.create(); 2067 data.writeFloatArray([1.2, 1.3, 1.4]); 2068 } catch (error) { 2069 let e: BusinessError = error as BusinessError; 2070 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2071 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2072 } 2073 ``` 2074 2075### readFloatArray<sup>9+</sup> 2076 2077readFloatArray(dataIn: number[]): void 2078 2079从MessageSequence实例中读取双精度浮点数组。 2080 2081**系统能力**:SystemCapability.Communication.IPC.Core 2082 2083**参数:** 2084 2085 | 参数名 | 类型 | 必填 | 说明 | 2086 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2087 | dataIn | number[] | 是 | 要读取的双精度浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 2088 2089**错误码:** 2090 2091以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2092 2093 | 错误码ID | 错误信息 | 2094 | -------- | -------- | 2095 | 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. | 2096 | 1900010 | Failed to read data from the message sequence. | 2097 2098**示例:** 2099 2100 ```ts 2101 import { rpc } from '@kit.IPCKit'; 2102 import { hilog } from '@kit.PerformanceAnalysisKit'; 2103 import { BusinessError } from '@kit.BasicServicesKit'; 2104 2105 try { 2106 let data = rpc.MessageSequence.create(); 2107 data.writeFloatArray([1.2, 1.3, 1.4]); 2108 let array: Array<number> = new Array(3); 2109 data.readFloatArray(array); 2110 hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array); 2111 } catch (error) { 2112 let e: BusinessError = error as BusinessError; 2113 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2114 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2115 } 2116 ``` 2117 2118### readFloatArray<sup>9+</sup> 2119 2120readFloatArray(): number[] 2121 2122从MessageSequence实例中读取双精度浮点数组。 2123 2124**系统能力**:SystemCapability.Communication.IPC.Core 2125 2126**返回值:** 2127 2128 | 类型 | 说明 | 2129 | -------- | -------------- | 2130 | number[] | 返回双精度浮点数组。 | 2131 2132**错误码:** 2133 2134以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2135 2136 | 错误码ID | 错误信息 | 2137 | -------- | -------- | 2138 | 1900010 | Failed to read data from the message sequence. | 2139 2140**示例:** 2141 2142 ```ts 2143 import { rpc } from '@kit.IPCKit'; 2144 import { hilog } from '@kit.PerformanceAnalysisKit'; 2145 import { BusinessError } from '@kit.BasicServicesKit'; 2146 2147 try { 2148 let data = rpc.MessageSequence.create(); 2149 data.writeFloatArray([1.2, 1.3, 1.4]); 2150 let array = data.readFloatArray(); 2151 hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array); 2152 } catch (error) { 2153 let e: BusinessError = error as BusinessError; 2154 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2155 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2156 } 2157 ``` 2158 2159### writeDoubleArray<sup>9+</sup> 2160 2161writeDoubleArray(doubleArray: number[]): void 2162 2163将双精度浮点数组写入MessageSequence实例。 2164 2165**系统能力**:SystemCapability.Communication.IPC.Core 2166 2167**参数:** 2168 2169 | 参数名 | 类型 | 必填 | 说明 | 2170 | ----------- | -------- | ---- | ------------------------ | 2171 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 2172 2173**错误码:** 2174 2175以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2176 2177 | 错误码ID | 错误信息 | 2178 | -------- | -------- | 2179 | 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. | 2180 | 1900009 | Failed to write data to the message sequence. | 2181 2182**示例:** 2183 2184 ```ts 2185 import { rpc } from '@kit.IPCKit'; 2186 import { hilog } from '@kit.PerformanceAnalysisKit'; 2187 import { BusinessError } from '@kit.BasicServicesKit'; 2188 2189 try { 2190 let data = rpc.MessageSequence.create(); 2191 data.writeDoubleArray([11.1, 12.2, 13.3]); 2192 } catch (error) { 2193 let e: BusinessError = error as BusinessError; 2194 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2195 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2196 } 2197 ``` 2198 2199### readDoubleArray<sup>9+</sup> 2200 2201readDoubleArray(dataIn: number[]): void 2202 2203从MessageSequence实例中读取双精度浮点数组。 2204 2205**系统能力**:SystemCapability.Communication.IPC.Core 2206 2207**参数:** 2208 2209 | 参数名 | 类型 | 必填 | 说明 | 2210 | ------ | -------- | ---- | ------------------------ | 2211 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 2212 2213**错误码:** 2214 2215以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2216 2217 | 错误码ID | 错误信息 | 2218 | -------- | -------- | 2219 | 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. | 2220 | 1900010 | Failed to read data from the message sequence. | 2221 2222**示例:** 2223 2224 ```ts 2225 import { rpc } from '@kit.IPCKit'; 2226 import { hilog } from '@kit.PerformanceAnalysisKit'; 2227 import { BusinessError } from '@kit.BasicServicesKit'; 2228 2229 try { 2230 let data = rpc.MessageSequence.create(); 2231 data.writeDoubleArray([11.1, 12.2, 13.3]); 2232 let array: Array<number> = new Array(3); 2233 data.readDoubleArray(array); 2234 hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array); 2235 } catch (error) { 2236 let e: BusinessError = error as BusinessError; 2237 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2238 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2239 } 2240 ``` 2241 2242### readDoubleArray<sup>9+</sup> 2243 2244readDoubleArray(): number[] 2245 2246从MessageSequence实例读取所有双精度浮点数组。 2247 2248**系统能力**:SystemCapability.Communication.IPC.Core 2249 2250**返回值:** 2251 2252 | 类型 | 说明 | 2253 | -------- | -------------------- | 2254 | number[] | 返回双精度浮点数组。 | 2255 2256**错误码:** 2257 2258以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2259 2260 | 错误码ID | 错误信息 | 2261 | -------- | -------- | 2262 | 1900010 | Failed to read data from the message sequence. | 2263 2264**示例:** 2265 2266 ```ts 2267 import { rpc } from '@kit.IPCKit'; 2268 import { hilog } from '@kit.PerformanceAnalysisKit'; 2269 import { BusinessError } from '@kit.BasicServicesKit'; 2270 2271 try { 2272 let data = rpc.MessageSequence.create(); 2273 data.writeDoubleArray([11.1, 12.2, 13.3]); 2274 let array = data.readDoubleArray(); 2275 hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array); 2276 } catch (error) { 2277 let e: BusinessError = error as BusinessError; 2278 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2279 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2280 } 2281 ``` 2282 2283### writeBooleanArray<sup>9+</sup> 2284 2285writeBooleanArray(booleanArray: boolean[]): void 2286 2287将布尔数组写入MessageSequence实例。 2288 2289**系统能力**:SystemCapability.Communication.IPC.Core 2290 2291**参数:** 2292 2293 | 参数名 | 类型 | 必填 | 说明 | 2294 | ------------ | --------- | ---- | ------------------ | 2295 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 2296 2297**错误码:** 2298 2299以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2300 2301 | 错误码ID | 错误信息 | 2302 | -------- | -------- | 2303 | 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. | 2304 | 1900009 | Failed to write data to the message sequence. | 2305 2306**示例:** 2307 2308 ```ts 2309 import { rpc } from '@kit.IPCKit'; 2310 import { hilog } from '@kit.PerformanceAnalysisKit'; 2311 import { BusinessError } from '@kit.BasicServicesKit'; 2312 2313 try { 2314 let data = rpc.MessageSequence.create(); 2315 data.writeBooleanArray([false, true, false]); 2316 } catch (error) { 2317 let e: BusinessError = error as BusinessError; 2318 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2319 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2320 } 2321 ``` 2322 2323### readBooleanArray<sup>9+</sup> 2324 2325readBooleanArray(dataIn: boolean[]): void 2326 2327从MessageSequence实例中读取布尔数组。 2328 2329**系统能力**:SystemCapability.Communication.IPC.Core 2330 2331**参数:** 2332 2333 | 参数名 | 类型 | 必填 | 说明 | 2334 | ------ | --------- | ---- | ------------------ | 2335 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 2336 2337**错误码:** 2338 2339以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2340 2341 | 错误码ID | 错误信息 | 2342 | -------- | -------- | 2343 | 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. | 2344 | 1900010 | Failed to read data from the message sequence. | 2345 2346**示例:** 2347 2348 ```ts 2349 import { rpc } from '@kit.IPCKit'; 2350 import { hilog } from '@kit.PerformanceAnalysisKit'; 2351 import { BusinessError } from '@kit.BasicServicesKit'; 2352 2353 try { 2354 let data = rpc.MessageSequence.create(); 2355 data.writeBooleanArray([false, true, false]); 2356 let array: Array<boolean> = new Array(3); 2357 data.readBooleanArray(array); 2358 hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array); 2359 } catch (error) { 2360 let e: BusinessError = error as BusinessError; 2361 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2362 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2363 } 2364 ``` 2365 2366### readBooleanArray<sup>9+</sup> 2367 2368readBooleanArray(): boolean[] 2369 2370从MessageSequence实例中读取所有布尔数组。 2371 2372**系统能力**:SystemCapability.Communication.IPC.Core 2373 2374**返回值:** 2375 2376 | 类型 | 说明 | 2377 | --------- | -------------- | 2378 | boolean[] | 返回布尔数组。 | 2379 2380**错误码:** 2381 2382以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2383 2384 | 错误码ID | 错误信息 | 2385 | -------- | -------- | 2386 | 1900010 | Failed to read data from the message sequence. | 2387 2388**示例:** 2389 2390 ```ts 2391 import { rpc } from '@kit.IPCKit'; 2392 import { hilog } from '@kit.PerformanceAnalysisKit'; 2393 import { BusinessError } from '@kit.BasicServicesKit'; 2394 2395 try { 2396 let data = rpc.MessageSequence.create(); 2397 data.writeBooleanArray([false, true, false]); 2398 let array = data.readBooleanArray(); 2399 hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array); 2400 } catch (error) { 2401 let e: BusinessError = error as BusinessError; 2402 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2403 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2404 } 2405 ``` 2406 2407### writeCharArray<sup>9+</sup> 2408 2409writeCharArray(charArray: number[]): void 2410 2411将单个字符数组写入MessageSequence实例。 2412 2413**系统能力**:SystemCapability.Communication.IPC.Core 2414 2415**参数:** 2416 2417 | 参数名 | 类型 | 必填 | 说明 | 2418 | --------- | -------- | ---- | ---------------------- | 2419 | charArray | number[] | 是 | 要写入的单个字符数组。 | 2420 2421**错误码:** 2422 2423以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2424 2425 | 错误码ID | 错误信息 | 2426 | -------- | -------- | 2427 | 401 | Parameter error. Possible causes: <br/> 1.The parameter is an empty array; <br/> 2.The number of parameters is incorrect; <br/> 3.The parameter type does not match; <br/> 4.The element does not exist in the array. | 2428 | 1900009 | Failed to write data to the message sequence. | 2429 2430**示例:** 2431 2432 ```ts 2433 import { rpc } from '@kit.IPCKit'; 2434 import { hilog } from '@kit.PerformanceAnalysisKit'; 2435 import { BusinessError } from '@kit.BasicServicesKit'; 2436 2437 try { 2438 let data = rpc.MessageSequence.create(); 2439 data.writeCharArray([97, 98, 88]); 2440 } catch (error) { 2441 let e: BusinessError = error as BusinessError; 2442 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2443 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2444 } 2445 ``` 2446 2447### readCharArray<sup>9+</sup> 2448 2449readCharArray(dataIn: number[]): void 2450 2451从MessageSequence实例中读取单个字符数组。 2452 2453**系统能力**:SystemCapability.Communication.IPC.Core 2454 2455**参数:** 2456 2457 | 参数名 | 类型 | 必填 | 说明 | 2458 | ------ | -------- | ---- | ---------------------- | 2459 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 2460 2461**错误码:** 2462 2463以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2464 2465 | 错误码ID | 错误信息 | 2466 | -------- | -------- | 2467 | 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. | 2468 | 1900010 | Failed to read data from the message sequence. | 2469 2470**示例:** 2471 2472 ```ts 2473 import { rpc } from '@kit.IPCKit'; 2474 import { hilog } from '@kit.PerformanceAnalysisKit'; 2475 import { BusinessError } from '@kit.BasicServicesKit'; 2476 2477 try { 2478 let data = rpc.MessageSequence.create(); 2479 data.writeCharArray([97, 98, 88]); 2480 let array: Array<number> = new Array(3); 2481 data.readCharArray(array); 2482 hilog.info(0x0000, 'testTag', 'readCharArray is ' + array); 2483 } catch (error) { 2484 let e: BusinessError = error as BusinessError; 2485 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2486 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2487 } 2488 ``` 2489 2490### readCharArray<sup>9+</sup> 2491 2492readCharArray(): number[] 2493 2494从MessageSequence实例读取单个字符数组。 2495 2496**系统能力**:SystemCapability.Communication.IPC.Core 2497 2498**返回值:** 2499 2500 | 类型 | 说明 | 2501 | -------- | ------------------ | 2502 | number[] | 返回单个字符数组。 | 2503 2504**错误码:** 2505 2506以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2507 2508 | 错误码ID | 错误信息 | 2509 | -------- | -------- | 2510 | 1900010 | Failed to read data from the message sequence. | 2511 2512**示例:** 2513 2514 ```ts 2515 import { rpc } from '@kit.IPCKit'; 2516 import { hilog } from '@kit.PerformanceAnalysisKit'; 2517 import { BusinessError } from '@kit.BasicServicesKit'; 2518 2519 try { 2520 let data = rpc.MessageSequence.create(); 2521 data.writeCharArray([97, 98, 88]); 2522 let array = data.readCharArray(); 2523 hilog.info(0x0000, 'testTag', 'readCharArray is ' + array); 2524 } catch (error) { 2525 let e: BusinessError = error as BusinessError; 2526 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2527 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2528 } 2529 ``` 2530 2531### writeStringArray<sup>9+</sup> 2532 2533writeStringArray(stringArray: string[]): void 2534 2535将字符串数组写入MessageSequence实例。 2536 2537**系统能力**:SystemCapability.Communication.IPC.Core 2538 2539**参数:** 2540 2541 | 参数名 | 类型 | 必填 | 说明 | 2542 | ----------- | -------- | ---- | ------------------------------------------------------- | 2543 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 2544 2545**错误码:** 2546 2547以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2548 2549 | 错误码ID | 错误信息 | 2550 | -------- | -------- | 2551 | 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. | 2552 | 1900009 | Failed to write data to the message sequence. | 2553 2554**示例:** 2555 2556 ```ts 2557 import { rpc } from '@kit.IPCKit'; 2558 import { hilog } from '@kit.PerformanceAnalysisKit'; 2559 import { BusinessError } from '@kit.BasicServicesKit'; 2560 2561 try { 2562 let data = rpc.MessageSequence.create(); 2563 data.writeStringArray(["abc", "def"]); 2564 } catch (error) { 2565 let e: BusinessError = error as BusinessError; 2566 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2567 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2568 } 2569 ``` 2570 2571### readStringArray<sup>9+</sup> 2572 2573readStringArray(dataIn: string[]): void 2574 2575从MessageSequence实例读取字符串数组。 2576 2577**系统能力**:SystemCapability.Communication.IPC.Core 2578 2579**参数:** 2580 2581 | 参数名 | 类型 | 必填 | 说明 | 2582 | ------ | -------- | ---- | -------------------- | 2583 | dataIn | string[] | 是 | 要读取的字符串数组。 | 2584 2585**错误码:** 2586 2587以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2588 2589 | 错误码ID | 错误信息 | 2590 | -------- | -------- | 2591 | 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. | 2592 | 1900010 | Failed to read data from the message sequence. | 2593 2594**示例:** 2595 2596 ```ts 2597 import { rpc } from '@kit.IPCKit'; 2598 import { hilog } from '@kit.PerformanceAnalysisKit'; 2599 import { BusinessError } from '@kit.BasicServicesKit'; 2600 2601 try { 2602 let data = rpc.MessageSequence.create(); 2603 data.writeStringArray(["abc", "def"]); 2604 let array: Array<string> = new Array(2); 2605 data.readStringArray(array); 2606 hilog.info(0x0000, 'testTag', 'readStringArray is ' + array); 2607 } catch (error) { 2608 let e: BusinessError = error as BusinessError; 2609 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2610 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2611 } 2612 ``` 2613 2614### readStringArray<sup>9+</sup> 2615 2616readStringArray(): string[] 2617 2618从MessageSequence实例读取字符串数组。 2619 2620**系统能力**:SystemCapability.Communication.IPC.Core 2621 2622**返回值:** 2623 2624 | 类型 | 说明 | 2625 | -------- | ---------------- | 2626 | string[] | 返回字符串数组。 | 2627 2628**错误码:** 2629 2630以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2631 2632 | 错误码ID | 错误信息 | 2633 | -------- | -------- | 2634 | 1900010 | Failed to read data from the message sequence. | 2635 2636**示例:** 2637 2638 ```ts 2639 import { rpc } from '@kit.IPCKit'; 2640 import { hilog } from '@kit.PerformanceAnalysisKit'; 2641 import { BusinessError } from '@kit.BasicServicesKit'; 2642 2643 try { 2644 let data = rpc.MessageSequence.create(); 2645 data.writeStringArray(["abc", "def"]); 2646 let array = data.readStringArray(); 2647 hilog.info(0x0000, 'testTag', 'readStringArray is ' + array); 2648 } catch (error) { 2649 let e: BusinessError = error as BusinessError; 2650 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2651 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2652 } 2653 ``` 2654 2655### writeNoException<sup>9+</sup> 2656 2657writeNoException(): void 2658 2659向MessageSequence写入“指示未发生异常”的信息。 2660 2661**系统能力**:SystemCapability.Communication.IPC.Core 2662 2663**错误码:** 2664 2665以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2666 2667 | 错误码ID | 错误信息 | 2668 | -------- | -------- | 2669 | 1900009 | Failed to write data to the message sequence. | 2670 2671**示例:** 2672 2673 ```ts 2674 import { rpc } from '@kit.IPCKit'; 2675 import { hilog } from '@kit.PerformanceAnalysisKit'; 2676 import { BusinessError } from '@kit.BasicServicesKit'; 2677 2678 class TestRemoteObject extends rpc.RemoteObject { 2679 constructor(descriptor: string) { 2680 super(descriptor); 2681 } 2682 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2683 if (code === 1) { 2684 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2685 try { 2686 reply.writeNoException(); 2687 } catch (error) { 2688 let e: BusinessError = error as BusinessError; 2689 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2690 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2691 } 2692 return true; 2693 } else { 2694 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2695 return false; 2696 } 2697 } 2698 } 2699 ``` 2700 2701### readException<sup>9+</sup> 2702 2703readException(): void 2704 2705从MessageSequence中读取异常。 2706 2707**系统能力**:SystemCapability.Communication.IPC.Core 2708 2709**错误码:** 2710 2711以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2712 2713 | 错误码ID | 错误信息 | 2714 | -------- | -------- | 2715 | 1900010 | Failed to read data from the message sequence. | 2716 2717**示例:** 2718 2719>**说明:** 2720> 2721>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 2722 2723 <!--code_no_check--> 2724 ```ts 2725 // FA模型需要从@kit.AbilityKit导入featureAbility 2726 // import { featureAbility } from '@kit.AbilityKit'; 2727 import { rpc } from '@kit.IPCKit'; 2728 import { Want, common } from '@kit.AbilityKit'; 2729 import { hilog } from '@kit.PerformanceAnalysisKit'; 2730 2731 let proxy: rpc.IRemoteObject | undefined; 2732 let connect: common.ConnectOptions = { 2733 onConnect: (elementName, remoteProxy) => { 2734 hilog.info(0x0000, 'testTag', 'js onConnect called'); 2735 proxy = remoteProxy; 2736 }, 2737 onDisconnect: (elementName) => { 2738 hilog.info(0x0000, 'testTag', 'onDisconnect'); 2739 }, 2740 onFailed: () => { 2741 hilog.info(0x0000, 'testTag', 'onFailed'); 2742 } 2743 }; 2744 let want: Want = { 2745 bundleName: "com.ohos.server", 2746 abilityName: "com.ohos.server.EntryAbility", 2747 }; 2748 2749 // FA模型使用此方法连接服务 2750 // FA.connectAbility(want,connect); 2751 2752 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2753 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 2754 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 2755 let connectionId = context.connectServiceExtensionAbility(want, connect); 2756 ``` 2757 2758 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 2759 2760 ```ts 2761 import { rpc } from '@kit.IPCKit'; 2762 import { BusinessError } from '@kit.BasicServicesKit'; 2763 import { hilog } from '@kit.PerformanceAnalysisKit'; 2764 2765 try { 2766 let option = new rpc.MessageOption(); 2767 let data = rpc.MessageSequence.create(); 2768 let reply = rpc.MessageSequence.create(); 2769 data.writeNoException(); 2770 data.writeInt(6); 2771 if (proxy != undefined) { 2772 proxy.sendMessageRequest(1, data, reply, option) 2773 .then((result: rpc.RequestResult) => { 2774 if (result.errCode === 0) { 2775 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2776 result.reply.readException(); 2777 let num = result.reply.readInt(); 2778 hilog.info(0x0000, 'testTag', 'reply num: ' + num); 2779 } else { 2780 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode: ' + result.errCode); 2781 } 2782 }).catch((e: Error) => { 2783 hilog.error(0x0000, 'testTag', 'sendMessageRequest got exception: ' + e); 2784 }).finally (() => { 2785 hilog.info(0x0000, 'testTag', 'sendMessageRequest ends, reclaim parcel'); 2786 data.reclaim(); 2787 reply.reclaim(); 2788 }); 2789 } 2790 } catch (error) { 2791 let e: BusinessError = error as BusinessError; 2792 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2793 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2794 } 2795 ``` 2796 2797### writeParcelableArray<sup>9+</sup> 2798 2799writeParcelableArray(parcelableArray: Parcelable[]): void 2800 2801将可序列化对象数组写入MessageSequence实例。 2802 2803**系统能力**:SystemCapability.Communication.IPC.Core 2804 2805**参数:** 2806 2807| 参数名 | 类型 | 必填 | 说明 | 2808| --------------- | ------------ | ---- | -------------------------- | 2809| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要写入的可序列化对象数组。 | 2810 2811**错误码:** 2812 2813以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2814 2815 | 错误码ID | 错误信息 | 2816 | -------- | -------- | 2817 | 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. | 2818 | 1900009 | Failed to write data to the message sequence. | 2819 2820**示例:** 2821 2822 ```ts 2823 import { rpc } from '@kit.IPCKit'; 2824 import { hilog } from '@kit.PerformanceAnalysisKit'; 2825 import { BusinessError } from '@kit.BasicServicesKit'; 2826 2827 class MyParcelable implements rpc.Parcelable { 2828 num: number = 0; 2829 str: string = ''; 2830 constructor(num: number, str: string) { 2831 this.num = num; 2832 this.str = str; 2833 } 2834 marshalling(messageSequence: rpc.MessageSequence): boolean { 2835 messageSequence.writeInt(this.num); 2836 messageSequence.writeString(this.str); 2837 return true; 2838 } 2839 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2840 this.num = messageSequence.readInt(); 2841 this.str = messageSequence.readString(); 2842 return true; 2843 } 2844 } 2845 2846 try { 2847 let parcelable = new MyParcelable(1, "aaa"); 2848 let parcelable2 = new MyParcelable(2, "bbb"); 2849 let parcelable3 = new MyParcelable(3, "ccc"); 2850 let a = [parcelable, parcelable2, parcelable3]; 2851 let data = rpc.MessageSequence.create(); 2852 data.writeParcelableArray(a); 2853 } catch (error) { 2854 let e: BusinessError = error as BusinessError; 2855 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2856 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2857 } 2858 ``` 2859 2860### readParcelableArray<sup>9+</sup> 2861 2862readParcelableArray(parcelableArray: Parcelable[]): void 2863 2864从MessageSequence实例读取可序列化对象数组。 2865 2866**系统能力**:SystemCapability.Communication.IPC.Core 2867 2868**参数:** 2869 2870| 参数名 | 类型 | 必填 | 说明 | 2871| --------------- | ------------ | ---- | -------------------------- | 2872| parcelableArray | [Parcelable](#parcelable9)[] | 是 | 要读取的可序列化对象数组。 | 2873 2874**错误码:** 2875 2876以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2877 2878 | 错误码ID | 错误信息 | 2879 | -------- | -------- | 2880 | 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. | 2881 | 1900010 | Failed to read data from the message sequence. | 2882 | 1900012 | Failed to call the JS callback function. | 2883 2884**示例:** 2885 2886 ```ts 2887 import { rpc } from '@kit.IPCKit'; 2888 import { hilog } from '@kit.PerformanceAnalysisKit'; 2889 import { BusinessError } from '@kit.BasicServicesKit'; 2890 2891 class MyParcelable implements rpc.Parcelable { 2892 num: number = 0; 2893 str: string = ''; 2894 constructor(num: number, str: string) { 2895 this.num = num; 2896 this.str = str; 2897 } 2898 marshalling(messageSequence: rpc.MessageSequence): boolean { 2899 messageSequence.writeInt(this.num); 2900 messageSequence.writeString(this.str); 2901 return true; 2902 } 2903 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2904 this.num = messageSequence.readInt(); 2905 this.str = messageSequence.readString(); 2906 return true; 2907 } 2908 } 2909 2910 try { 2911 let parcelable = new MyParcelable(1, "aaa"); 2912 let parcelable2 = new MyParcelable(2, "bbb"); 2913 let parcelable3 = new MyParcelable(3, "ccc"); 2914 let a = [parcelable, parcelable2, parcelable3]; 2915 let data = rpc.MessageSequence.create(); 2916 data.writeParcelableArray(a); 2917 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2918 data.readParcelableArray(b); 2919 } catch (error) { 2920 let e: BusinessError = error as BusinessError; 2921 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2922 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2923 } 2924 ``` 2925 2926### writeRemoteObjectArray<sup>9+</sup> 2927 2928writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2929 2930将IRemoteObject对象数组写入MessageSequence。 2931 2932**系统能力**:SystemCapability.Communication.IPC.Core 2933 2934**参数:** 2935 2936| 参数名 | 类型 | 必填 | 说明 | 2937| ----------- | --------------- | ---- | ---------------------------------------------- | 2938| objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageSequence的IRemoteObject对象数组。 | 2939 2940**错误码:** 2941 2942以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2943 2944 | 错误码ID | 错误信息 | 2945 | -------- | -------- | 2946 | 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. | 2947 | 1900009 | Failed to write data to the message sequence. | 2948 2949**示例:** 2950 2951 ```ts 2952 import { rpc } from '@kit.IPCKit'; 2953 import { hilog } from '@kit.PerformanceAnalysisKit'; 2954 import { BusinessError } from '@kit.BasicServicesKit'; 2955 2956 class TestRemoteObject extends rpc.RemoteObject { 2957 constructor(descriptor: string) { 2958 super(descriptor); 2959 } 2960 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2961 // 根据业务实际逻辑,进行相应处理 2962 return true; 2963 } 2964 } 2965 2966 try { 2967 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2968 let data = rpc.MessageSequence.create(); 2969 data.writeRemoteObjectArray(a); 2970 } catch (error) { 2971 let e: BusinessError = error as BusinessError; 2972 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 2973 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 2974 } 2975 ``` 2976 2977### readRemoteObjectArray<sup>9+</sup> 2978 2979readRemoteObjectArray(objects: IRemoteObject[]): void 2980 2981从MessageSequence读取IRemoteObject对象数组。 2982 2983**系统能力**:SystemCapability.Communication.IPC.Core 2984 2985**参数:** 2986 2987| 参数名 | 类型 | 必填 | 说明 | 2988| ------- | --------------- | ---- | ---------------------------------------------- | 2989| objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageSequence读取的IRemoteObject对象数组。 | 2990 2991**错误码:** 2992 2993以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 2994 2995 | 错误码ID | 错误信息 | 2996 | -------- | -------- | 2997 | 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. | 2998 | 1900010 | Failed to read data from the message sequence. | 2999 3000**示例:** 3001 3002 ```ts 3003 import { rpc } from '@kit.IPCKit'; 3004 import { hilog } from '@kit.PerformanceAnalysisKit'; 3005 import { BusinessError } from '@kit.BasicServicesKit'; 3006 3007 class TestRemoteObject extends rpc.RemoteObject { 3008 constructor(descriptor: string) { 3009 super(descriptor); 3010 } 3011 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 3012 // 根据业务实际逻辑,进行相应处理 3013 return true; 3014 } 3015 } 3016 3017 try { 3018 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3019 let data = rpc.MessageSequence.create(); 3020 data.writeRemoteObjectArray(a); 3021 let b: Array<rpc.IRemoteObject> = new Array(3); 3022 data.readRemoteObjectArray(b); 3023 hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b); 3024 } catch (error) { 3025 let e: BusinessError = error as BusinessError; 3026 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3027 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3028 } 3029 ``` 3030 3031### readRemoteObjectArray<sup>9+</sup> 3032 3033readRemoteObjectArray(): IRemoteObject[] 3034 3035从MessageSequence读取IRemoteObject对象数组。 3036 3037**系统能力**:SystemCapability.Communication.IPC.Core 3038 3039**返回值:** 3040 3041| 类型 | 说明 | 3042| --------------- | --------------------------- | 3043| [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 3044 3045**错误码:** 3046 3047以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3048 3049 | 错误码ID | 错误信息 | 3050 | -------- | -------- | 3051 | 1900010 | Failed to read data from the message sequence. | 3052 3053**示例:** 3054 3055 ```ts 3056 import { rpc } from '@kit.IPCKit'; 3057 import { hilog } from '@kit.PerformanceAnalysisKit'; 3058 import { BusinessError } from '@kit.BasicServicesKit'; 3059 3060 class TestRemoteObject extends rpc.RemoteObject { 3061 constructor(descriptor: string) { 3062 super(descriptor); 3063 } 3064 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 3065 // 根据业务实际逻辑,进行相应处理 3066 return true; 3067 } 3068 } 3069 3070 try { 3071 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3072 let data = rpc.MessageSequence.create(); 3073 let b = data.readRemoteObjectArray(); 3074 hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b); 3075 } catch (error) { 3076 let e: BusinessError = error as BusinessError; 3077 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3078 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3079 } 3080 ``` 3081 3082### closeFileDescriptor<sup>9+</sup> 3083 3084static closeFileDescriptor(fd: number): void 3085 3086静态方法,关闭给定的文件描述符。 3087 3088**系统能力**:SystemCapability.Communication.IPC.Core 3089 3090**参数:** 3091 3092 | 参数名 | 类型 | 必填 | 说明 | 3093 | ------ | ------ | ---- | -------------------- | 3094 | fd | number | 是 | 要关闭的文件描述符。 | 3095 3096**错误码:** 3097 3098以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3099 3100 | 错误码ID | 错误信息 | 3101 | -------- | -------- | 3102 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3103 3104**示例:** 3105 3106 ```ts 3107 import { rpc } from '@kit.IPCKit'; 3108 import { fileIo } from '@kit.CoreFileKit'; 3109 import { hilog } from '@kit.PerformanceAnalysisKit'; 3110 import { BusinessError } from '@kit.BasicServicesKit'; 3111 3112 try { 3113 let filePath = "path/to/file"; 3114 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3115 rpc.MessageSequence.closeFileDescriptor(file.fd); 3116 } catch (error) { 3117 let e: BusinessError = error as BusinessError; 3118 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3119 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3120 } 3121 ``` 3122 3123### dupFileDescriptor<sup>9+</sup> 3124 3125static dupFileDescriptor(fd: number): number 3126 3127静态方法,复制给定的文件描述符。 3128 3129**系统能力**:SystemCapability.Communication.IPC.Core 3130 3131**参数:** 3132 3133 | 参数名 | 类型 | 必填 | 说明 | 3134 | ------ | ------ | ---- | ------------------------ | 3135 | fd | number | 是 | 表示已存在的文件描述符。 | 3136 3137**返回值:** 3138 3139 | 类型 | 说明 | 3140 | ------ | -------------------- | 3141 | number | 返回新的文件描述符。 | 3142 3143**错误码:** 3144 3145以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3146 3147 | 错误码ID | 错误信息 | 3148 | -------- | -------- | 3149 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3150 | 1900013 | Failed to call dup. | 3151 3152**示例:** 3153 3154 ```ts 3155 import { rpc } from '@kit.IPCKit'; 3156 import { fileIo } from '@kit.CoreFileKit'; 3157 import { hilog } from '@kit.PerformanceAnalysisKit'; 3158 import { BusinessError } from '@kit.BasicServicesKit'; 3159 3160 try { 3161 let filePath = "path/to/file"; 3162 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3163 rpc.MessageSequence.dupFileDescriptor(file.fd); 3164 } catch (error) { 3165 let e: BusinessError = error as BusinessError; 3166 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3167 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3168 } 3169 ``` 3170 3171### containFileDescriptors<sup>9+</sup> 3172 3173containFileDescriptors(): boolean 3174 3175检查此MessageSequence对象是否包含文件描述符。 3176 3177**系统能力**:SystemCapability.Communication.IPC.Core 3178 3179**返回值:** 3180 3181 | 类型 | 说明 | 3182 | ------- | -------------------------------------------------------------------- | 3183 | boolean | true:包含文件描述符,false:不包含文件描述符。| 3184 3185**示例:** 3186 3187 ```ts 3188 import { rpc } from '@kit.IPCKit'; 3189 import { fileIo } from '@kit.CoreFileKit'; 3190 import { hilog } from '@kit.PerformanceAnalysisKit'; 3191 import { BusinessError } from '@kit.BasicServicesKit'; 3192 3193 try { 3194 let sequence = rpc.MessageSequence.create(); 3195 let filePath = "path/to/file"; 3196 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3197 let containFD = sequence.containFileDescriptors(); 3198 hilog.info(0x0000, 'testTag', 'sequence after write fd containFd result is ' + containFD); 3199 } catch (error) { 3200 let e: BusinessError = error as BusinessError; 3201 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3202 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3203 } 3204 ``` 3205 3206### writeFileDescriptor<sup>9+</sup> 3207 3208writeFileDescriptor(fd: number): void 3209 3210写入文件描述符到MessageSequence。 3211 3212**系统能力**:SystemCapability.Communication.IPC.Core 3213 3214**参数:** 3215 3216 | 参数名 | 类型 | 必填 | 说明 | 3217 | ------ | ------ | ---- | ------------ | 3218 | fd | number | 是 | 文件描述符。 | 3219 3220**错误码:** 3221 3222以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3223 3224 | 错误码ID | 错误信息 | 3225 | -------- | -------- | 3226 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3227 | 1900009 | Failed to write data to the message sequence. | 3228 3229**示例:** 3230 3231 ```ts 3232 import { rpc } from '@kit.IPCKit'; 3233 import { fileIo } from '@kit.CoreFileKit'; 3234 import { hilog } from '@kit.PerformanceAnalysisKit'; 3235 import { BusinessError } from '@kit.BasicServicesKit'; 3236 3237 try { 3238 let sequence = rpc.MessageSequence.create(); 3239 let filePath = "path/to/file"; 3240 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3241 sequence.writeFileDescriptor(file.fd); 3242 } catch (error) { 3243 let e: BusinessError = error as BusinessError; 3244 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3245 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3246 } 3247 ``` 3248 3249### readFileDescriptor<sup>9+</sup> 3250 3251readFileDescriptor(): number 3252 3253从MessageSequence中读取文件描述符。 3254 3255**系统能力**:SystemCapability.Communication.IPC.Core 3256 3257**返回值:** 3258 3259 | 类型 | 说明 | 3260 | ------ | ---------------- | 3261 | number | 返回文件描述符。 | 3262 3263**错误码:** 3264 3265以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3266 3267 | 错误码ID | 错误信息 | 3268 | -------- | -------- | 3269 | 1900010 | Failed to read data from the message sequence. | 3270 3271**示例:** 3272 3273 ```ts 3274 import { rpc } from '@kit.IPCKit'; 3275 import { fileIo } from '@kit.CoreFileKit'; 3276 import { hilog } from '@kit.PerformanceAnalysisKit'; 3277 import { BusinessError } from '@kit.BasicServicesKit'; 3278 3279 try { 3280 let sequence = rpc.MessageSequence.create(); 3281 let filePath = "path/to/file"; 3282 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3283 sequence.writeFileDescriptor(file.fd); 3284 let readFD = sequence.readFileDescriptor(); 3285 hilog.info(0x0000, 'testTag', 'readFileDescriptor is ' + readFD); 3286 } catch (error) { 3287 let e: BusinessError = error as BusinessError; 3288 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3289 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3290 } 3291 ``` 3292 3293### writeAshmem<sup>9+</sup> 3294 3295writeAshmem(ashmem: Ashmem): void 3296 3297将指定的匿名共享对象写入此MessageSequence。 3298 3299**系统能力**:SystemCapability.Communication.IPC.Core 3300 3301**参数:** 3302 3303| 参数名 | 类型 | 必填 | 说明 | 3304| ------ | ------ | ---- | ------------------------------------- | 3305| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageSequence的匿名共享对象。 | 3306 3307**错误码:** 3308 3309以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3310 3311 | 错误码ID | 错误信息 | 3312 | -------- | ------- | 3313 | 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. | 3314 | 1900009 | Failed to write data to the message sequence. | 3315 3316**示例:** 3317 3318 ```ts 3319 import { rpc } from '@kit.IPCKit'; 3320 import { hilog } from '@kit.PerformanceAnalysisKit'; 3321 import { BusinessError } from '@kit.BasicServicesKit'; 3322 3323 try { 3324 let sequence = rpc.MessageSequence.create(); 3325 let ashmem = rpc.Ashmem.create("ashmem", 1024); 3326 // ashmem里写入数据 3327 let buffer = new ArrayBuffer(1024); 3328 let int32View = new Int32Array(buffer); 3329 for (let i = 0; i < int32View.length; i++) { 3330 int32View[i] = i * 2 + 1; 3331 } 3332 let size = buffer.byteLength; 3333 ashmem.mapReadWriteAshmem(); 3334 ashmem.writeDataToAshmem(buffer, size, 0); 3335 // 将ashmem对象写入messageSequence对象中 3336 sequence.writeAshmem(ashmem); 3337 // 将传递的数据大小写入messageSequence对象中 3338 sequence.writeInt(size); 3339 } catch (error) { 3340 let e: BusinessError = error as BusinessError; 3341 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3342 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3343 } 3344 ``` 3345 3346### readAshmem<sup>9+</sup> 3347 3348readAshmem(): Ashmem 3349 3350从MessageSequence读取匿名共享对象。 3351 3352**系统能力**:SystemCapability.Communication.IPC.Core 3353 3354**返回值:** 3355 3356| 类型 | 说明 | 3357| ------ | ------------------ | 3358| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 3359 3360**错误码:** 3361 3362以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3363 3364 | 错误码ID | 错误信息 | 3365 | -------- | -------- | 3366 | 1900010 | Failed to read data from the message sequence. | 3367 3368**示例:** 3369 3370 ```ts 3371 import { rpc } from '@kit.IPCKit'; 3372 import { hilog } from '@kit.PerformanceAnalysisKit'; 3373 import { BusinessError } from '@kit.BasicServicesKit'; 3374 3375 try { 3376 let sequence = rpc.MessageSequence.create(); 3377 let ashmem = rpc.Ashmem.create("ashmem", 1024); 3378 // ashmem里写入数据 3379 let buffer = new ArrayBuffer(1024); 3380 let int32View = new Int32Array(buffer); 3381 for (let i = 0; i < int32View.length; i++) { 3382 int32View[i] = i * 2 + 1; 3383 } 3384 let size = buffer.byteLength; 3385 ashmem.mapReadWriteAshmem(); 3386 ashmem.writeDataToAshmem(buffer, size, 0); 3387 // 将传递的数据大小写入messageSequence对象中 3388 sequence.writeInt(size); 3389 // 将ashmem对象写入messageSequence对象中 3390 sequence.writeAshmem(ashmem); 3391 3392 // 读取传递的数据大小 3393 let dataSize = sequence.readInt(); 3394 // 从messageSequence对象中读取ashmem对象 3395 let ashmem1 = sequence.readAshmem(); 3396 // 从ashmem对象中读取数据 3397 ashmem1.mapReadWriteAshmem(); 3398 let readResult = ashmem1.readDataFromAshmem(dataSize, 0); 3399 let readInt32View = new Int32Array(readResult); 3400 hilog.info(0x0000, 'testTag', 'read from Ashmem result is ' + readInt32View); 3401 } catch (error) { 3402 let e: BusinessError = error as BusinessError; 3403 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3404 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3405 } 3406 ``` 3407 3408### getRawDataCapacity<sup>9+</sup> 3409 3410getRawDataCapacity(): number 3411 3412获取MessageSequence可以容纳的最大原始数据量。 3413 3414**系统能力**:SystemCapability.Communication.IPC.Core 3415 3416**返回值:** 3417 3418 | 类型 | 说明 | 3419 | ------ | ------------------------------------------------------------ | 3420 | number | 返回MessageSequence可以容纳的最大原始数据量,即128MB。 | 3421 3422**示例:** 3423 3424 ```ts 3425 import { rpc } from '@kit.IPCKit'; 3426 import { hilog } from '@kit.PerformanceAnalysisKit'; 3427 import { BusinessError } from '@kit.BasicServicesKit'; 3428 3429 try { 3430 let sequence = rpc.MessageSequence.create(); 3431 let result = sequence.getRawDataCapacity(); 3432 hilog.info(0x0000, 'testTag', 'sequence get RawDataCapacity result is ' + result); 3433 } catch (error) { 3434 let e: BusinessError = error as BusinessError; 3435 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3436 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3437 } 3438 ``` 3439 3440### writeRawData<sup>(deprecated)</sup> 3441 3442writeRawData(rawData: number[], size: number): void 3443 3444将原始数据写入MessageSequence对象。 3445 3446> **说明:** 3447> 3448> 从API version 11 开始废弃,建议使用[writeRawDataBuffer](#writerawdatabuffer11)替代。 3449> 3450> 该接口是一次性接口,不允许在一次parcel通信中多次调用该接口。 3451> 该接口在传输数据时,当数据量较大时(超过32KB),会使用共享内存传输数据,此时需注意selinux配置。 3452 3453**系统能力**:SystemCapability.Communication.IPC.Core 3454 3455**参数:** 3456 3457 | 参数名 | 类型 | 必填 | 说明 | 3458 | ------- | -------- | ---- | ---------------------------------- | 3459 | rawData | number[] | 是 | 要写入的原始数据,大小不能超过128MB。 | 3460 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3461 3462**错误码:** 3463 3464以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3465 3466 | 错误码ID | 错误信息 | 3467 | -------- | -------- | 3468 | 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. | 3469 | 1900009 | Failed to write data to the message sequence. | 3470 3471**示例:** 3472<!--deprecated_code_no_check--> 3473 ```ts 3474 import { rpc } from '@kit.IPCKit'; 3475 import { hilog } from '@kit.PerformanceAnalysisKit'; 3476 import { BusinessError } from '@kit.BasicServicesKit'; 3477 3478 try { 3479 let sequence = rpc.MessageSequence.create(); 3480 let arr = [1, 2, 3, 4, 5]; 3481 sequence.writeRawData(arr, arr.length); 3482 } catch (error) { 3483 let e: BusinessError = error as BusinessError; 3484 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3485 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3486 } 3487 ``` 3488 3489### writeRawDataBuffer<sup>11+</sup> 3490 3491writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3492 3493将原始数据写入MessageSequence对象。 3494 3495> **说明:** 3496> 3497> 该接口是一次性接口,不允许在一次parcel通信中多次调用该接口。 3498> 该接口在传输数据时,当数据量较大时(超过32KB),会使用共享内存传输数据,此时需注意selinux配置。 3499 3500**系统能力**:SystemCapability.Communication.IPC.Core 3501 3502**参数:** 3503 3504 | 参数名 | 类型 | 必填 | 说明 | 3505 | ------- | ----------- | ---- | ------------------------------------ | 3506 | rawData | ArrayBuffer | 是 | 要写入的原始数据,大小不能超过128MB。 | 3507 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 3508 3509**错误码:** 3510 3511以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3512 3513 | 错误码ID | 错误信息 | 3514 | -------- | -------- | 3515 | 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. | 3516 | 1900009 | Failed to write data to the message sequence. | 3517 3518**示例:** 3519 3520 ```ts 3521 import { rpc } from '@kit.IPCKit'; 3522 import { hilog } from '@kit.PerformanceAnalysisKit'; 3523 import { BusinessError } from '@kit.BasicServicesKit'; 3524 3525 try { 3526 let buffer = new ArrayBuffer(64 * 1024); 3527 let int32View = new Int32Array(buffer); 3528 for (let i = 0; i < int32View.length; i++) { 3529 int32View[i] = i * 2 + 1; 3530 } 3531 let size = buffer.byteLength; 3532 let sequence = rpc.MessageSequence.create(); 3533 sequence.writeRawDataBuffer(buffer, size); 3534 } catch (error) { 3535 let e: BusinessError = error as BusinessError; 3536 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3537 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3538 } 3539 ``` 3540 3541### readRawData<sup>(deprecated)</sup> 3542 3543readRawData(size: number): number[] 3544 3545从MessageSequence读取原始数据。 3546 3547> **说明:** 3548> 3549> 从API version 11 开始废弃,建议使用[readRawDataBuffer](#readrawdatabuffer11)替代。 3550 3551**系统能力**:SystemCapability.Communication.IPC.Core 3552 3553**参数:** 3554 3555 | 参数名 | 类型 | 必填 | 说明 | 3556 | ------ | ------ | ---- | ------------------------ | 3557 | size | number | 是 | 要读取的原始数据的大小。 | 3558 3559**返回值:** 3560 3561 | 类型 | 说明 | 3562 | -------- | ------------------------------ | 3563 | number[] | 返回原始数据(以字节为单位)。 | 3564 3565**错误码:** 3566 3567以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3568 3569 | 错误码ID | 错误信息 | 3570 | -------- | -------- | 3571 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3572 | 1900010 | Failed to read data from the message sequence. | 3573 3574**示例:** 3575<!--deprecated_code_no_check--> 3576 ```ts 3577 import { rpc } from '@kit.IPCKit'; 3578 import { hilog } from '@kit.PerformanceAnalysisKit'; 3579 import { BusinessError } from '@kit.BasicServicesKit'; 3580 3581 try { 3582 let sequence = rpc.MessageSequence.create(); 3583 let arr = [1, 2, 3, 4, 5]; 3584 sequence.writeRawData(arr, arr.length); 3585 let size = arr.length; 3586 let result = sequence.readRawData(size); 3587 hilog.info(0x0000, 'testTag', 'sequence read raw data result is ' + result); 3588 } catch (error) { 3589 let e: BusinessError = error as BusinessError; 3590 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3591 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3592 } 3593 ``` 3594 3595### readRawDataBuffer<sup>11+</sup> 3596 3597readRawDataBuffer(size: number): ArrayBuffer 3598 3599从MessageSequence读取原始数据。 3600 3601**系统能力**:SystemCapability.Communication.IPC.Core 3602 3603**参数:** 3604 3605 | 参数名 | 类型 | 必填 | 说明 | 3606 | ------ | ------ | ---- | ------------------------ | 3607 | size | number | 是 | 要读取的原始数据的大小。 | 3608 3609**返回值:** 3610 3611 | 类型 | 说明 | 3612 | -------- | ------------------------------ | 3613 | ArrayBuffer | 返回原始数据(以字节为单位)。 | 3614 3615**错误码:** 3616 3617以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3618 3619 | 错误码ID | 错误信息 | 3620 | -------- | -------- | 3621 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 3622 | 1900010 | Failed to read data from the message sequence. | 3623 3624**示例:** 3625 3626 ```ts 3627 import { rpc } from '@kit.IPCKit'; 3628 import { hilog } from '@kit.PerformanceAnalysisKit'; 3629 import { BusinessError } from '@kit.BasicServicesKit'; 3630 3631 try { 3632 let buffer = new ArrayBuffer(64 * 1024); 3633 let int32View = new Int32Array(buffer); 3634 for (let i = 0; i < int32View.length; i++) { 3635 int32View[i] = i * 2 + 1; 3636 } 3637 let size = buffer.byteLength; 3638 let sequence = rpc.MessageSequence.create(); 3639 sequence.writeRawDataBuffer(buffer, size); 3640 let result = sequence.readRawDataBuffer(size); 3641 let readInt32View = new Int32Array(result); 3642 hilog.info(0x0000, 'testTag', 'sequence read raw data result is ' + readInt32View); 3643 } catch (error) { 3644 let e: BusinessError = error as BusinessError; 3645 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3646 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3647 } 3648 ``` 3649 3650### writeArrayBuffer<sup>12+</sup> 3651 3652writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3653 3654将ArrayBuffer类型数据写入MessageSequence对象。 3655 3656**系统能力**:SystemCapability.Communication.IPC.Core 3657 3658**参数:** 3659 3660 | 参数名 | 类型 | 必填 | 说明 | 3661 | --------- | ------------------------- | ---- | --------------------------- | 3662 | buf | ArrayBuffer | 是 | 要写入的ArrayBuffer数据。 | 3663 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的写入方式,需要业务正确传递枚举值。) | 3664 3665**错误码:** 3666 3667以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3668 3669 | 错误码ID | 错误信息 | 3670 | -------- | -------- | 3671 | 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. | 3672 | 1900009 | Failed to write data to the message sequence. | 3673 3674**示例:** 3675 3676 ```ts 3677 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3678 import { rpc } from '@kit.IPCKit'; 3679 import { hilog } from '@kit.PerformanceAnalysisKit'; 3680 import { BusinessError } from '@kit.BasicServicesKit'; 3681 3682 try { 3683 let data = rpc.MessageSequence.create(); 3684 let buffer = new ArrayBuffer(10); 3685 let int16View = new Int16Array(buffer); 3686 for (let i = 0; i < int16View.length; i++) { 3687 int16View[i] = i * 2 + 1; 3688 } 3689 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3690 } catch (error) { 3691 let e: BusinessError = error as BusinessError; 3692 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3693 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3694 } 3695 ``` 3696 3697### readArrayBuffer<sup>12+</sup> 3698 3699readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3700 3701从MessageSequence读取ArrayBuffer类型数据。 3702 3703**系统能力**:SystemCapability.Communication.IPC.Core 3704 3705**参数:** 3706 3707 | 参数名 | 类型 | 必填 | 说明 | 3708 | -------- | ----------------------- | ---- | ------------------------| 3709 | typeCode | [TypeCode](#typecode12) | 是 | ArrayBuffer数据具体是以哪一种TypedArray来访问和操作(会根据业务传递的类型枚举值去决定底层的读取方式,需要业务正确传递枚举值,读写枚举值不匹配会导致数据异常。) | 3710 3711**返回值:** 3712 3713 | 类型 | 说明 | 3714 | -------- | -------------------------------------------- | 3715 | ArrayBuffer | 返回ArrayBuffer类型数据(以字节为单位)。 | 3716 3717**错误码:** 3718 3719以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 3720 3721 | 错误码ID | 错误信息 | 3722 | -------- | -------- | 3723 | 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; | 3724 | 1900010 | Failed to read data from the message sequence. | 3725 3726**示例:** 3727 3728 ```ts 3729 // TypeCode 类型枚举较多,示例代码以Int16Array为例 3730 import { rpc } from '@kit.IPCKit'; 3731 import { hilog } from '@kit.PerformanceAnalysisKit'; 3732 import { BusinessError } from '@kit.BasicServicesKit'; 3733 3734 try { 3735 let data = rpc.MessageSequence.create(); 3736 let buffer = new ArrayBuffer(10); 3737 let int16View = new Int16Array(buffer); 3738 for (let i = 0; i < int16View.length; i++) { 3739 int16View[i] = i * 2 + 1; 3740 } 3741 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3742 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3743 let readInt16View = new Int16Array(result); 3744 hilog.info(0x0000, 'testTag', 'read ArrayBuffer result is ' + readInt16View); 3745 } catch (error) { 3746 let e: BusinessError = error as BusinessError; 3747 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 3748 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 3749 } 3750 ``` 3751 3752## MessageParcel<sup>(deprecated)</sup> 3753 3754在RPC过程中,发送方可以使用MessageParcel提供的写方法,将待发送的数据以特定格式写入该对象。接收方可以使用MessageParcel提供的读方法从该对象中读取特定格式的数据。数据格式包括:基础类型及数组、IPC对象、接口描述符和自定义序列化对象。 3755 3756> **说明:** 3757> 3758> 从API version 9 开始废弃,建议使用[MessageSequence](#messagesequence9)替代。 3759 3760### create 3761 3762static create(): MessageParcel 3763 3764静态方法,创建MessageParcel对象。 3765 3766**系统能力**:SystemCapability.Communication.IPC.Core 3767 3768**返回值:** 3769 3770 | 类型 | 说明 | 3771 | ------------- | ----------------------------- | 3772 | [MessageParcel](#messageparceldeprecated) | 返回创建的MessageParcel对象。 | 3773 3774**示例:** 3775<!--deprecated_code_no_check--> 3776 ```ts 3777 import { rpc } from '@kit.IPCKit'; 3778 import { hilog } from '@kit.PerformanceAnalysisKit'; 3779 3780 try { 3781 let data = rpc.MessageParcel.create(); 3782 hilog.info(0x0000, 'testTag', 'data is ' + data); 3783 3784 // 当MessageParcel对象不再使用,由业务主动调用reclaim方法去释放资源。 3785 data.reclaim(); 3786 } catch (error) { 3787 hilog.error(0x0000, 'testTag', 'error ' + error); 3788 } 3789 ``` 3790 3791### reclaim 3792 3793reclaim(): void 3794 3795释放不再使用的MessageParcel对象。 3796 3797**系统能力**:SystemCapability.Communication.IPC.Core 3798 3799**示例:** 3800<!--deprecated_code_no_check--> 3801 ```ts 3802 import { rpc } from '@kit.IPCKit'; 3803 import { hilog } from '@kit.PerformanceAnalysisKit'; 3804 3805 try { 3806 let reply = rpc.MessageParcel.create(); 3807 reply.reclaim(); 3808 } catch (error) { 3809 hilog.error(0x0000, 'testTag', 'error ' + error); 3810 } 3811 ``` 3812 3813### writeRemoteObject 3814 3815writeRemoteObject(object: IRemoteObject): boolean 3816 3817序列化远程对象并将其写入MessageParcel对象。 3818 3819**系统能力**:SystemCapability.Communication.IPC.Core 3820 3821**参数:** 3822 3823 | 参数名 | 类型 | 必填 | 说明 | 3824 | ------ | ------------------------------- | ---- | --------------------------------------- | 3825 | object | [IRemoteObject](#iremoteobject) | 是 | 要序列化并写入MessageParcel的远程对象。 | 3826 3827**返回值:** 3828 3829 | 类型 | 说明 | 3830 | ------- | ----------------------------------------- | 3831 | boolean | true:操作成功,false:操作失败。| 3832 3833**示例:** 3834<!--deprecated_code_no_check--> 3835 ```ts 3836 import { rpc } from '@kit.IPCKit'; 3837 import { hilog } from '@kit.PerformanceAnalysisKit'; 3838 3839 class TestRemoteObject extends rpc.RemoteObject { 3840 constructor(descriptor: string) { 3841 super(descriptor); 3842 } 3843 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3844 // 根据业务实际逻辑,进行相应处理 3845 return true; 3846 } 3847 } 3848 3849 try { 3850 let data = rpc.MessageParcel.create(); 3851 let testRemoteObject = new TestRemoteObject("testObject"); 3852 data.writeRemoteObject(testRemoteObject); 3853 } catch (error) { 3854 hilog.error(0x0000, 'testTag', 'error ' + error); 3855 } 3856 ``` 3857 3858### readRemoteObject 3859 3860readRemoteObject(): IRemoteObject 3861 3862从MessageParcel读取远程对象。此方法用于反序列化MessageParcel对象以生成IRemoteObject。远程对象按写入MessageParcel的顺序读取。 3863 3864**系统能力**:SystemCapability.Communication.IPC.Core 3865 3866**返回值:** 3867 3868 | 类型 | 说明 | 3869 | ------------------------------- | ------------------ | 3870 | [IRemoteObject](#iremoteobject) | 读取到的远程对象。 | 3871 3872**示例:** 3873<!--deprecated_code_no_check--> 3874 ```ts 3875 import { rpc } from '@kit.IPCKit'; 3876 import { hilog } from '@kit.PerformanceAnalysisKit'; 3877 3878 class TestRemoteObject extends rpc.RemoteObject { 3879 constructor(descriptor: string) { 3880 super(descriptor); 3881 } 3882 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3883 // 根据业务实际逻辑,进行相应处理 3884 return true; 3885 } 3886 } 3887 3888 try { 3889 let data = rpc.MessageParcel.create(); 3890 let testRemoteObject = new TestRemoteObject("testObject"); 3891 data.writeRemoteObject(testRemoteObject); 3892 let proxy = data.readRemoteObject(); 3893 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3894 } catch (error) { 3895 hilog.error(0x0000, 'testTag', 'error ' + error); 3896 } 3897 ``` 3898 3899### writeInterfaceToken 3900 3901writeInterfaceToken(token: string): boolean 3902 3903将接口描述符写入MessageParcel对象,远端对象可使用该信息校验本次通信。 3904 3905**系统能力**:SystemCapability.Communication.IPC.Core 3906 3907**参数:** 3908 3909 | 参数名 | 类型 | 必填 | 说明 | 3910 | ------ | ------ | ---- | ------------------ | 3911 | token | string | 是 | 字符串类型描述符,其长度应小于40960字节。 | 3912 3913**返回值:** 3914 3915 | 类型 | 说明 | 3916 | ------- | ----------------------------------------- | 3917 | boolean | true:操作成功,false:操作失败。| 3918 3919**示例:** 3920<!--deprecated_code_no_check--> 3921 ```ts 3922 import { rpc } from '@kit.IPCKit'; 3923 import { hilog } from '@kit.PerformanceAnalysisKit'; 3924 3925 try { 3926 let data = rpc.MessageParcel.create(); 3927 let result = data.writeInterfaceToken("aaa"); 3928 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3929 } catch (error) { 3930 hilog.error(0x0000, 'testTag', 'error ' + error); 3931 } 3932 ``` 3933 3934### readInterfaceToken 3935 3936readInterfaceToken(): string 3937 3938从MessageParcel中读取接口描述符,接口描述符按写入MessageParcel的顺序读取,本地对象可使用该信息检验本次通信。 3939 3940**系统能力**:SystemCapability.Communication.IPC.Core 3941 3942**返回值:** 3943 3944 | 类型 | 说明 | 3945 | ------ | ------------------------ | 3946 | string | 返回读取到的接口描述符。 | 3947 3948**示例:** 3949<!--deprecated_code_no_check--> 3950 ```ts 3951 import { rpc } from '@kit.IPCKit'; 3952 import { hilog } from '@kit.PerformanceAnalysisKit'; 3953 3954 try { 3955 let data = rpc.MessageParcel.create(); 3956 let result = data.writeInterfaceToken("aaa"); 3957 let interfaceToken = data.readInterfaceToken(); 3958 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3959 } catch (error) { 3960 hilog.error(0x0000, 'testTag', 'error ' + error); 3961 } 3962 ``` 3963 3964### getSize 3965 3966getSize(): number 3967 3968获取当前MessageParcel的数据大小。 3969 3970**系统能力**:SystemCapability.Communication.IPC.Core 3971 3972**返回值:** 3973 3974 | 类型 | 说明 | 3975 | ------ | --------------------------------------------- | 3976 | number | 获取的MessageParcel的数据大小。以字节为单位。 | 3977 3978**示例:** 3979<!--deprecated_code_no_check--> 3980 ```ts 3981 import { rpc } from '@kit.IPCKit'; 3982 import { hilog } from '@kit.PerformanceAnalysisKit'; 3983 3984 try { 3985 let data = rpc.MessageParcel.create(); 3986 data.writeInt(1); 3987 let size = data.getSize(); 3988 hilog.info(0x0000, 'testTag', 'size is ' + size); 3989 } catch (error) { 3990 hilog.error(0x0000, 'testTag', 'error ' + error); 3991 } 3992 ``` 3993 3994### getCapacity 3995 3996getCapacity(): number 3997 3998获取当前MessageParcel的容量。 3999 4000**系统能力**:SystemCapability.Communication.IPC.Core 4001 4002**返回值:** 4003 4004 | 类型 | 说明 | 4005 | ------ | --------------------------------------------- | 4006 | number | 获取的MessageParcel的容量大小。以字节为单位。 | 4007 4008**示例:** 4009<!--deprecated_code_no_check--> 4010 ```ts 4011 import { rpc } from '@kit.IPCKit'; 4012 import { hilog } from '@kit.PerformanceAnalysisKit'; 4013 4014 try { 4015 let data = rpc.MessageParcel.create(); 4016 let data = rpc.MessageParcel.create(); 4017 let result = data.getCapacity(); 4018 hilog.info(0x0000, 'testTag', 'capacity is ' + result); 4019 } catch (error) { 4020 hilog.error(0x0000, 'testTag', 'error ' + error); 4021 } 4022 ``` 4023 4024### setSize 4025 4026setSize(size: number): boolean 4027 4028设置MessageParcel实例中包含的数据大小。 4029 4030**系统能力**:SystemCapability.Communication.IPC.Core 4031 4032**参数:** 4033 4034 | 参数名 | 类型 | 必填 | 说明 | 4035 | ------ | ------ | ---- | ------------------------------------------- | 4036 | size | number | 是 | MessageParcel实例的数据大小。以字节为单位。 | 4037 4038**返回值:** 4039 4040 | 类型 | 说明 | 4041 | ------- | --------------------------------- | 4042 | boolean | true:设置成功,false:设置失败。| 4043 4044**示例:** 4045<!--deprecated_code_no_check--> 4046 ```ts 4047 import { rpc } from '@kit.IPCKit'; 4048 import { hilog } from '@kit.PerformanceAnalysisKit'; 4049 4050 try { 4051 let data = rpc.MessageParcel.create(); 4052 let setSize = data.setSize(16); 4053 hilog.info(0x0000, 'testTag', 'setSize is ' + setSize); 4054 } catch (error) { 4055 hilog.error(0x0000, 'testTag', 'error ' + error); 4056 } 4057 ``` 4058 4059### setCapacity 4060 4061setCapacity(size: number): boolean 4062 4063设置MessageParcel实例的存储容量。 4064 4065**系统能力**:SystemCapability.Communication.IPC.Core 4066 4067**参数:** 4068 4069 | 参数名 | 类型 | 必填 | 说明 | 4070 | ------ | ------ | ---- | ------------------------------------------- | 4071 | size | number | 是 | MessageParcel实例的存储容量。以字节为单位。 | 4072 4073**返回值:** 4074 4075 | 类型 | 说明 | 4076 | ------- | --------------------------------- | 4077 | boolean | true:设置成功,false:设置失败。| 4078 4079**示例:** 4080<!--deprecated_code_no_check--> 4081 ```ts 4082 import { rpc } from '@kit.IPCKit'; 4083 import { hilog } from '@kit.PerformanceAnalysisKit'; 4084 4085 try { 4086 let data = rpc.MessageParcel.create(); 4087 let result = data.setCapacity(100); 4088 hilog.info(0x0000, 'testTag', 'setCapacity is ' + result); 4089 } catch (error) { 4090 hilog.error(0x0000, 'testTag', 'error ' + error); 4091 } 4092 ``` 4093 4094### getWritableBytes 4095 4096getWritableBytes(): number 4097 4098获取MessageParcel的可写字节空间。 4099 4100**系统能力**:SystemCapability.Communication.IPC.Core 4101 4102**返回值:** 4103 4104 | 类型 | 说明 | 4105 | ------ | --------------------------------------------------- | 4106 | number | 获取到的MessageParcel的可写字节空间。以字节为单位。 | 4107 4108**示例:** 4109<!--deprecated_code_no_check--> 4110 ```ts 4111 import { rpc } from '@kit.IPCKit'; 4112 import { hilog } from '@kit.PerformanceAnalysisKit'; 4113 4114 try { 4115 let data = rpc.MessageParcel.create(); 4116 data.writeInt(1); 4117 let getWritableBytes = data.getWritableBytes(); 4118 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4119 } catch (error) { 4120 hilog.error(0x0000, 'testTag', 'error ' + error); 4121 } 4122 ``` 4123 4124### getReadableBytes 4125 4126getReadableBytes(): number 4127 4128获取MessageParcel的可读字节空间。 4129 4130**系统能力**:SystemCapability.Communication.IPC.Core 4131 4132**返回值:** 4133 4134 | 类型 | 说明 | 4135 | ------ | --------------------------------------------------- | 4136 | number | 获取到的MessageParcel的可读字节空间。以字节为单位。 | 4137 4138**示例:** 4139<!--deprecated_code_no_check--> 4140 ```ts 4141 import { rpc } from '@kit.IPCKit'; 4142 import { hilog } from '@kit.PerformanceAnalysisKit'; 4143 4144 try { 4145 let data = rpc.MessageParcel.create(); 4146 data.writeInt(1); 4147 let result = data.getReadableBytes(); 4148 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4149 } catch (error) { 4150 hilog.error(0x0000, 'testTag', 'error ' + error); 4151 } 4152 ``` 4153 4154### getReadPosition 4155 4156getReadPosition(): number 4157 4158获取MessageParcel的读位置。 4159 4160**系统能力**:SystemCapability.Communication.IPC.Core 4161 4162**返回值:** 4163 4164 | 类型 | 说明 | 4165 | ------ | --------------------------------------- | 4166 | number | 返回MessageParcel实例中的当前读取位置。 | 4167 4168**示例:** 4169<!--deprecated_code_no_check--> 4170 ```ts 4171 import { rpc } from '@kit.IPCKit'; 4172 import { hilog } from '@kit.PerformanceAnalysisKit'; 4173 4174 try { 4175 let data = rpc.MessageParcel.create(); 4176 let readPos = data.getReadPosition(); 4177 hilog.info(0x0000, 'testTag', 'readPos is ' + readPos); 4178 } catch (error) { 4179 hilog.error(0x0000, 'testTag', 'error ' + error); 4180 } 4181 ``` 4182 4183### getWritePosition 4184 4185getWritePosition(): number 4186 4187获取MessageParcel的写位置。 4188 4189**系统能力**:SystemCapability.Communication.IPC.Core 4190 4191**返回值:** 4192 4193 | 类型 | 说明 | 4194 | ------ | --------------------------------------- | 4195 | number | 返回MessageParcel实例中的当前写入位置。 | 4196 4197**示例:** 4198<!--deprecated_code_no_check--> 4199 ```ts 4200 import { rpc } from '@kit.IPCKit'; 4201 import { hilog } from '@kit.PerformanceAnalysisKit'; 4202 4203 try { 4204 let data = rpc.MessageParcel.create(); 4205 data.writeInt(10); 4206 let bwPos = data.getWritePosition(); 4207 hilog.info(0x0000, 'testTag', 'bwPos is ' + bwPos); 4208 } catch (error) { 4209 hilog.error(0x0000, 'testTag', 'error ' + error); 4210 } 4211 ``` 4212 4213### rewindRead 4214 4215rewindRead(pos: number): boolean 4216 4217重新偏移读取位置到指定的位置。 4218 4219**系统能力**:SystemCapability.Communication.IPC.Core 4220 4221**参数:** 4222 4223 | 参数名 | 类型 | 必填 | 说明 | 4224 | ------ | ------ | ---- | ------------------------ | 4225 | pos | number | 是 | 开始读取数据的目标位置。 | 4226 4227**返回值:** 4228 4229 | 类型 | 说明 | 4230 | ------- | ------------------------------------------------- | 4231 | boolean | true:读取位置发生更改,false:读取位置未发生更改。| 4232 4233**示例:** 4234<!--deprecated_code_no_check--> 4235 ```ts 4236 import { rpc } from '@kit.IPCKit'; 4237 import { hilog } from '@kit.PerformanceAnalysisKit'; 4238 4239 try { 4240 let data = rpc.MessageParcel.create(); 4241 data.writeInt(12); 4242 data.writeString("parcel"); 4243 let number = data.readInt(); 4244 hilog.info(0x0000, 'testTag', 'number is ' + number); 4245 data.rewindRead(0); 4246 let number2 = data.readInt(); 4247 hilog.info(0x0000, 'testTag', 'rewindRead is ' + number2); 4248 } catch (error) { 4249 hilog.error(0x0000, 'testTag', 'error ' + error); 4250 } 4251 ``` 4252 4253### rewindWrite 4254 4255rewindWrite(pos: number): boolean 4256 4257重新偏移写位置到指定的位置。 4258 4259**系统能力**:SystemCapability.Communication.IPC.Core 4260 4261**参数:** 4262 4263 | 参数名 | 类型 | 必填 | 说明 | 4264 | ------ | ------ | ---- | ------------------------ | 4265 | pos | number | 是 | 开始写入数据的目标位置。 | 4266 4267**返回值:** 4268 4269 | 类型 | 说明 | 4270 | ------- | --------------------------------------------- | 4271 | boolean | true:写入位置发生更改,false:写入位置未发生更改。| 4272 4273**示例:** 4274<!--deprecated_code_no_check--> 4275 ```ts 4276 import { rpc } from '@kit.IPCKit'; 4277 import { hilog } from '@kit.PerformanceAnalysisKit'; 4278 4279 try { 4280 let data = rpc.MessageParcel.create(); 4281 data.writeInt(4); 4282 data.rewindWrite(0); 4283 data.writeInt(5); 4284 let number = data.readInt(); 4285 hilog.info(0x0000, 'testTag', 'rewindWrite is ' + number); 4286 } catch (error) { 4287 hilog.error(0x0000, 'testTag', 'error ' + error); 4288 } 4289 ``` 4290 4291### writeByte 4292 4293writeByte(val: number): boolean 4294 4295将字节值写入MessageParcel实例。 4296 4297**系统能力**:SystemCapability.Communication.IPC.Core 4298 4299**参数:** 4300 4301 | 参数名 | 类型 | 必填 | 说明 | 4302 | ------ | ------ | ---- | ---------------- | 4303 | val | number | 是 | 要写入的字节值。 | 4304 4305**返回值:** 4306 4307 | 类型 | 说明 | 4308 | ------- | ----------------------------- | 4309 | boolean | true:写入成功,false:写入失败。 | 4310 4311**示例:** 4312<!--deprecated_code_no_check--> 4313 ```ts 4314 import { rpc } from '@kit.IPCKit'; 4315 import { hilog } from '@kit.PerformanceAnalysisKit'; 4316 4317 try { 4318 let data = rpc.MessageParcel.create(); 4319 let result = data.writeByte(2); 4320 hilog.info(0x0000, 'testTag', 'writeByte is ' + result); 4321 } catch (error) { 4322 hilog.error(0x0000, 'testTag', 'error ' + error); 4323 } 4324 ``` 4325 4326### readByte 4327 4328readByte(): number 4329 4330从MessageParcel实例读取字节值。 4331 4332**系统能力**:SystemCapability.Communication.IPC.Core 4333 4334**返回值:** 4335 4336 | 类型 | 说明 | 4337 | ------ | ------------ | 4338 | number | 返回字节值。 | 4339 4340**示例:** 4341<!--deprecated_code_no_check--> 4342 ```ts 4343 import { rpc } from '@kit.IPCKit'; 4344 import { hilog } from '@kit.PerformanceAnalysisKit'; 4345 4346 try { 4347 let data = rpc.MessageParcel.create(); 4348 let result = data.writeByte(2); 4349 hilog.info(0x0000, 'testTag', 'writeByte is ' + result); 4350 let ret = data.readByte(); 4351 hilog.info(0x0000, 'testTag', 'readByte is ' + ret); 4352 } catch (error) { 4353 hilog.error(0x0000, 'testTag', 'error ' + error); 4354 } 4355 ``` 4356 4357### writeShort 4358 4359writeShort(val: number): boolean 4360 4361将短整数值写入MessageParcel实例。 4362 4363**系统能力**:SystemCapability.Communication.IPC.Core 4364 4365**参数:** 4366 4367 | 参数名 | 类型 | 必填 | 说明 | 4368 | ------ | ------ | ---- | ------------------ | 4369 | val | number | 是 | 要写入的短整数值。 | 4370 4371**返回值:** 4372 4373 | 类型 | 说明 | 4374 | ------- | ----------------------------- | 4375 | boolean | true:写入成功,false:写入失败。| 4376 4377**示例:** 4378<!--deprecated_code_no_check--> 4379 ```ts 4380 import { rpc } from '@kit.IPCKit'; 4381 import { hilog } from '@kit.PerformanceAnalysisKit'; 4382 4383 try { 4384 let data = rpc.MessageParcel.create(); 4385 let result = data.writeShort(8); 4386 hilog.info(0x0000, 'testTag', 'writeShort is ' + result); 4387 } catch (error) { 4388 hilog.error(0x0000, 'testTag', 'error ' + error); 4389 } 4390 ``` 4391 4392### readShort 4393 4394readShort(): number 4395 4396从MessageParcel实例读取短整数值。 4397 4398**系统能力**:SystemCapability.Communication.IPC.Core 4399 4400**返回值:** 4401 4402 | 类型 | 说明 | 4403 | ------ | -------------- | 4404 | number | 返回短整数值。 | 4405 4406**示例:** 4407<!--deprecated_code_no_check--> 4408 ```ts 4409 import { rpc } from '@kit.IPCKit'; 4410 import { hilog } from '@kit.PerformanceAnalysisKit'; 4411 4412 try { 4413 let data = rpc.MessageParcel.create(); 4414 let result = data.writeShort(8); 4415 hilog.info(0x0000, 'testTag', 'writeShort is ' + result); 4416 let ret = data.readShort(); 4417 hilog.info(0x0000, 'testTag', 'readShort is ' + ret); 4418 } catch (error) { 4419 hilog.error(0x0000, 'testTag', 'error ' + error); 4420 } 4421 ``` 4422 4423### writeInt 4424 4425writeInt(val: number): boolean 4426 4427将整数值写入MessageParcel实例。 4428 4429**系统能力**:SystemCapability.Communication.IPC.Core 4430 4431**参数:** 4432 4433 | 参数名 | 类型 | 必填 | 说明 | 4434 | ------ | ------ | ---- | ---------------- | 4435 | val | number | 是 | 要写入的整数值。 | 4436 4437**返回值:** 4438 4439 | 类型 | 说明 | 4440 | ------- | ----------------------------- | 4441 | boolean | true:写入成功,false:写入失败。 | 4442 4443**示例:** 4444<!--deprecated_code_no_check--> 4445 ```ts 4446 import { rpc } from '@kit.IPCKit'; 4447 import { hilog } from '@kit.PerformanceAnalysisKit'; 4448 4449 try { 4450 let data = rpc.MessageParcel.create(); 4451 let result = data.writeInt(10); 4452 hilog.info(0x0000, 'testTag', 'writeInt is ' + result); 4453 } catch (error) { 4454 hilog.error(0x0000, 'testTag', 'error ' + error); 4455 } 4456 ``` 4457 4458### readInt 4459 4460readInt(): number 4461 4462从MessageParcel实例读取整数值。 4463 4464**系统能力**:SystemCapability.Communication.IPC.Core 4465 4466**返回值:** 4467 4468 | 类型 | 说明 | 4469 | ------ | ------------ | 4470 | number | 返回整数值。 | 4471 4472**示例:** 4473<!--deprecated_code_no_check--> 4474 ```ts 4475 import { rpc } from '@kit.IPCKit'; 4476 import { hilog } from '@kit.PerformanceAnalysisKit'; 4477 4478 try { 4479 let data = rpc.MessageParcel.create(); 4480 let result = data.writeInt(10); 4481 hilog.info(0x0000, 'testTag', 'writeInt is ' + result); 4482 let ret = data.readInt(); 4483 hilog.info(0x0000, 'testTag', 'readInt is ' + ret); 4484 } catch (error) { 4485 hilog.error(0x0000, 'testTag', 'error ' + error); 4486 } 4487 ``` 4488 4489### writeLong 4490 4491writeLong(val: number): boolean 4492 4493将长整数值写入MessageParcel实例。 4494 4495**系统能力**:SystemCapability.Communication.IPC.Core 4496 4497**参数:** 4498 4499 | 参数名 | 类型 | 必填 | 说明 | 4500 | ------ | ------ | ---- | ---------------- | 4501 | val | number | 是 | 要写入的长整数值 | 4502 4503**返回值:** 4504 4505 | 类型 | 说明 | 4506 | ------- | --------------------------------- | 4507 | boolean | true:写入成功,false:写入失败。| 4508 4509**示例:** 4510<!--deprecated_code_no_check--> 4511 ```ts 4512 import { rpc } from '@kit.IPCKit'; 4513 import { hilog } from '@kit.PerformanceAnalysisKit'; 4514 4515 try { 4516 let data = rpc.MessageParcel.create(); 4517 let result = data.writeLong(10000); 4518 hilog.info(0x0000, 'testTag', 'writeLong is ' + result); 4519 } catch (error) { 4520 hilog.error(0x0000, 'testTag', 'error ' + error); 4521 } 4522 ``` 4523 4524### readLong 4525 4526readLong(): number 4527 4528从MessageParcel实例中读取长整数值。 4529 4530**系统能力**:SystemCapability.Communication.IPC.Core 4531 4532**返回值:** 4533 4534 | 类型 | 说明 | 4535 | ------ | -------------- | 4536 | number | 返回长整数值。 | 4537 4538**示例:** 4539<!--deprecated_code_no_check--> 4540 ```ts 4541 import { rpc } from '@kit.IPCKit'; 4542 import { hilog } from '@kit.PerformanceAnalysisKit'; 4543 4544 try { 4545 let data = rpc.MessageParcel.create(); 4546 let result = data.writeLong(10000); 4547 hilog.info(0x0000, 'testTag', 'writeLong is ' + result); 4548 let ret = data.readLong(); 4549 hilog.info(0x0000, 'testTag', 'readLong is ' + ret); 4550 } catch (error) { 4551 hilog.error(0x0000, 'testTag', 'error ' + error); 4552 } 4553 ``` 4554 4555### writeFloat 4556 4557writeFloat(val: number): boolean 4558 4559将双精度浮点值写入MessageParcel实例。 4560 4561**系统能力**:SystemCapability.Communication.IPC.Core 4562 4563**参数:** 4564 4565 | 参数名 | 类型 | 必填 | 说明 | 4566 | ------ | ------ | ---- | ---------------- | 4567 | val | number | 是 | 要写入的双精度浮点值。 | 4568 4569**返回值:** 4570 4571 | 类型 | 说明 | 4572 | ------- | --------------------------------- | 4573 | boolean | true:写入成功,false:写入失败。| 4574 4575**示例:** 4576<!--deprecated_code_no_check--> 4577 ```ts 4578 import { rpc } from '@kit.IPCKit'; 4579 import { hilog } from '@kit.PerformanceAnalysisKit'; 4580 4581 try { 4582 let data = rpc.MessageParcel.create(); 4583 let result = data.writeFloat(1.2); 4584 hilog.info(0x0000, 'testTag', 'writeFloat is ' + result); 4585 } catch (error) { 4586 hilog.error(0x0000, 'testTag', 'error ' + error); 4587 } 4588 ``` 4589 4590### readFloat 4591 4592readFloat(): number 4593 4594从MessageParcel实例中读取双精度浮点值。 4595 4596**系统能力**:SystemCapability.Communication.IPC.Core 4597 4598**返回值:** 4599 4600 | 类型 | 说明 | 4601 | ------ | ------------ | 4602 | number | 返回双精度浮点值。 | 4603 4604**示例:** 4605<!--deprecated_code_no_check--> 4606 ```ts 4607 import { rpc } from '@kit.IPCKit'; 4608 import { hilog } from '@kit.PerformanceAnalysisKit'; 4609 4610 try { 4611 let data = rpc.MessageParcel.create(); 4612 let result = data.writeFloat(1.2); 4613 hilog.info(0x0000, 'testTag', 'writeFloat is ' + result); 4614 let ret = data.readFloat(); 4615 hilog.info(0x0000, 'testTag', 'readFloat is ' + ret); 4616 } catch (error) { 4617 hilog.error(0x0000, 'testTag', 'error ' + error); 4618 } 4619 ``` 4620 4621### writeDouble 4622 4623writeDouble(val: number): boolean 4624 4625将双精度浮点值写入MessageParcel实例。 4626 4627**系统能力**:SystemCapability.Communication.IPC.Core 4628 4629**参数:** 4630 4631 | 参数名 | 类型 | 必填 | 说明 | 4632 | ------ | ------ | ---- | ---------------------- | 4633 | val | number | 是 | 要写入的双精度浮点值。 | 4634 4635**返回值:** 4636 4637 | 类型 | 说明 | 4638 | ------- | --------------------------------- | 4639 | boolean | true:写入成功,false:写入失败。| 4640 4641**示例:** 4642<!--deprecated_code_no_check--> 4643 ```ts 4644 import { rpc } from '@kit.IPCKit'; 4645 import { hilog } from '@kit.PerformanceAnalysisKit'; 4646 4647 try { 4648 let data = rpc.MessageParcel.create(); 4649 let result = data.writeDouble(10.2); 4650 hilog.info(0x0000, 'testTag', 'writeDouble is ' + result); 4651 } catch (error) { 4652 hilog.error(0x0000, 'testTag', 'error ' + error); 4653 } 4654 ``` 4655 4656### readDouble 4657 4658readDouble(): number 4659 4660从MessageParcel实例读取双精度浮点值。 4661 4662**系统能力**:SystemCapability.Communication.IPC.Core 4663 4664**返回值:** 4665 4666 | 类型 | 说明 | 4667 | ------ | ------------------ | 4668 | number | 返回双精度浮点值。 | 4669 4670**示例:** 4671<!--deprecated_code_no_check--> 4672 ```ts 4673 import { rpc } from '@kit.IPCKit'; 4674 import { hilog } from '@kit.PerformanceAnalysisKit'; 4675 4676 try { 4677 let data = rpc.MessageParcel.create(); 4678 let result = data.writeDouble(10.2); 4679 hilog.info(0x0000, 'testTag', 'writeDouble is ' + result); 4680 let ret = data.readDouble(); 4681 hilog.info(0x0000, 'testTag', 'readDouble is ' + ret); 4682 } catch (error) { 4683 hilog.error(0x0000, 'testTag', 'error ' + error); 4684 } 4685 ``` 4686 4687### writeBoolean 4688 4689writeBoolean(val: boolean): boolean 4690 4691将布尔值写入MessageParcel实例。 4692 4693**系统能力**:SystemCapability.Communication.IPC.Core 4694 4695**参数:** 4696 4697 | 参数名 | 类型 | 必填 | 说明 | 4698 | ------ | ------- | ---- | ---------------- | 4699 | val | boolean | 是 | 要写入的布尔值。 | 4700 4701**返回值:** 4702 4703 | 类型 | 说明 | 4704 | ------- | --------------------------------- | 4705 | boolean | true:写入成功,false:写入失败。| 4706 4707**示例:** 4708<!--deprecated_code_no_check--> 4709 ```ts 4710 import { rpc } from '@kit.IPCKit'; 4711 import { hilog } from '@kit.PerformanceAnalysisKit'; 4712 4713 try { 4714 let data = rpc.MessageParcel.create(); 4715 let result = data.writeBoolean(false); 4716 hilog.info(0x0000, 'testTag', 'writeBoolean is ' + result); 4717 } catch (error) { 4718 hilog.error(0x0000, 'testTag', 'error ' + error); 4719 } 4720 ``` 4721 4722### readBoolean 4723 4724readBoolean(): boolean 4725 4726从MessageParcel实例读取布尔值。 4727 4728**系统能力**:SystemCapability.Communication.IPC.Core 4729 4730**返回值:** 4731 4732 | 类型 | 说明 | 4733 | ------- | -------------------- | 4734 | boolean | 返回读取到的布尔值。 | 4735 4736**示例:** 4737<!--deprecated_code_no_check--> 4738 ```ts 4739 import { rpc } from '@kit.IPCKit'; 4740 import { hilog } from '@kit.PerformanceAnalysisKit'; 4741 4742 try { 4743 let data = rpc.MessageParcel.create(); 4744 let result = data.writeBoolean(false); 4745 hilog.info(0x0000, 'testTag', 'writeBoolean is ' + result); 4746 let ret = data.readBoolean(); 4747 hilog.info(0x0000, 'testTag', 'readBoolean is ' + ret); 4748 } catch (error) { 4749 hilog.error(0x0000, 'testTag', 'error ' + error); 4750 } 4751 ``` 4752 4753### writeChar 4754 4755writeChar(val: number): boolean 4756 4757将单个字符值写入MessageParcel实例。 4758 4759**系统能力**:SystemCapability.Communication.IPC.Core 4760 4761**参数:** 4762 4763 | 参数名 | 类型 | 必填 | 说明 | 4764 | ------ | ------ | ---- | -------------------- | 4765 | val | number | 是 | 要写入的单个字符值。 | 4766 4767**返回值:** 4768 4769 | 类型 | 说明 | 4770 | ------- | ----------------------------- | 4771 | boolean | true:写入成功,false:写入失败。| 4772 4773**示例:** 4774<!--deprecated_code_no_check--> 4775 ```ts 4776 import { rpc } from '@kit.IPCKit'; 4777 import { hilog } from '@kit.PerformanceAnalysisKit'; 4778 4779 try { 4780 let data = rpc.MessageParcel.create(); 4781 let result = data.writeChar(97); 4782 hilog.info(0x0000, 'testTag', 'writeChar is ' + result); 4783 } catch (error) { 4784 hilog.error(0x0000, 'testTag', 'error ' + error); 4785 } 4786 ``` 4787 4788### readChar 4789 4790readChar(): number 4791 4792从MessageParcel实例中读取单个字符值。 4793 4794**系统能力**:SystemCapability.Communication.IPC.Core 4795 4796**返回值:** 4797 4798 | 类型 | 说明 | 4799 | ------ | ---------------- | 4800 | number | 返回单个字符值。 | 4801 4802**示例:** 4803<!--deprecated_code_no_check--> 4804 ```ts 4805 import { rpc } from '@kit.IPCKit'; 4806 import { hilog } from '@kit.PerformanceAnalysisKit'; 4807 4808 try { 4809 let data = rpc.MessageParcel.create(); 4810 let result = data.writeChar(97); 4811 hilog.info(0x0000, 'testTag', 'writeChar is ' + result); 4812 let ret = data.readChar(); 4813 hilog.info(0x0000, 'testTag', 'readChar is ' + ret); 4814 } catch (error) { 4815 hilog.error(0x0000, 'testTag', 'error ' + error); 4816 } 4817 ``` 4818 4819### writeString 4820 4821writeString(val: string): boolean 4822 4823将字符串值写入MessageParcel实例。 4824 4825**系统能力**:SystemCapability.Communication.IPC.Core 4826 4827**参数:** 4828 4829 | 参数名 | 类型 | 必填 | 说明 | 4830 | ------ | ------ | ---- | ----------------------------------------- | 4831 | val | string | 是 | 要写入的字符串值,其长度应小于40960字节。 | 4832 4833**返回值:** 4834 4835 | 类型 | 说明 | 4836 | ------- | --------------------------------- | 4837 | boolean | true:写入成功,false:写入失败。| 4838 4839**示例:** 4840<!--deprecated_code_no_check--> 4841 ```ts 4842 import { rpc } from '@kit.IPCKit'; 4843 import { hilog } from '@kit.PerformanceAnalysisKit'; 4844 4845 try { 4846 let data = rpc.MessageParcel.create(); 4847 let result = data.writeString('abc'); 4848 hilog.info(0x0000, 'testTag', 'writeString is ' + result); 4849 } catch (error) { 4850 hilog.error(0x0000, 'testTag', 'error ' + error); 4851 } 4852 ``` 4853 4854### readString 4855 4856readString(): string 4857 4858从MessageParcel实例读取字符串值。 4859 4860**系统能力**:SystemCapability.Communication.IPC.Core 4861 4862**返回值:** 4863 4864 | 类型 | 说明 | 4865 | ------ | -------------- | 4866 | string | 返回字符串值。 | 4867 4868**示例:** 4869<!--deprecated_code_no_check--> 4870 ```ts 4871 import { rpc } from '@kit.IPCKit'; 4872 import { hilog } from '@kit.PerformanceAnalysisKit'; 4873 4874 try { 4875 let data = rpc.MessageParcel.create(); 4876 let result = data.writeString('abc'); 4877 hilog.info(0x0000, 'testTag', 'writeString is ' + result); 4878 let ret = data.readString(); 4879 hilog.info(0x0000, 'testTag', 'readString is ' + ret); 4880 } catch (error) { 4881 hilog.error(0x0000, 'testTag', 'error ' + error); 4882 } 4883 ``` 4884 4885### writeSequenceable 4886 4887writeSequenceable(val: Sequenceable): boolean 4888 4889将自定义序列化对象写入MessageParcel实例。 4890 4891**系统能力**:SystemCapability.Communication.IPC.Core 4892 4893**参数:** 4894 4895 | 参数名 | 类型 | 必填 | 说明 | 4896 | ------ | ----------------------------- | ---- | -------------------- | 4897 | val | [Sequenceable](#sequenceabledeprecated) | 是 | 要写入的可序列对象。 | 4898 4899**返回值:** 4900 4901 | 类型 | 说明 | 4902 | ------- | -------------------------------- | 4903 | boolean | true:写入成功,false:写入失败。| 4904 4905**示例:** 4906<!--deprecated_code_no_check--> 4907 ```ts 4908 import { rpc } from '@kit.IPCKit'; 4909 import { hilog } from '@kit.PerformanceAnalysisKit'; 4910 4911 class MySequenceable implements rpc.Sequenceable { 4912 num: number = 0; 4913 str: string = ''; 4914 constructor(num: number, str: string) { 4915 this.num = num; 4916 this.str = str; 4917 } 4918 marshalling(messageParcel: rpc.MessageParcel): boolean { 4919 messageParcel.writeInt(this.num); 4920 messageParcel.writeString(this.str); 4921 return true; 4922 } 4923 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4924 this.num = messageParcel.readInt(); 4925 this.str = messageParcel.readString(); 4926 return true; 4927 } 4928 } 4929 4930 try { 4931 let sequenceable = new MySequenceable(1, "aaa"); 4932 let data = rpc.MessageParcel.create(); 4933 let result = data.writeSequenceable(sequenceable); 4934 hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result); 4935 } catch (error) { 4936 hilog.error(0x0000, 'testTag', 'error ' + error); 4937 } 4938 ``` 4939 4940### readSequenceable 4941 4942readSequenceable(dataIn: Sequenceable): boolean 4943 4944从MessageParcel实例中读取成员变量到指定的对象(dataIn)。 4945 4946**系统能力**:SystemCapability.Communication.IPC.Core 4947 4948**参数:** 4949 4950 | 参数名 | 类型 | 必填 | 说明 | 4951 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4952 | dataIn | [Sequenceable](#sequenceabledeprecated) | 是 | 需要从MessageParcel读取成员变量的对象。 | 4953 4954**返回值:** 4955 4956 | 类型 | 说明 | 4957 | ------- | ---------------------------------------- | 4958 | boolean | true:反序列化成功,false:反序列化失败。| 4959 4960**示例:** 4961<!--deprecated_code_no_check--> 4962 ```ts 4963 import { rpc } from '@kit.IPCKit'; 4964 import { hilog } from '@kit.PerformanceAnalysisKit'; 4965 4966 class MySequenceable implements rpc.Sequenceable { 4967 num: number = 0; 4968 str: string = ''; 4969 constructor(num: number, str: string) { 4970 this.num = num; 4971 this.str = str; 4972 } 4973 marshalling(messageParcel: rpc.MessageParcel): boolean { 4974 messageParcel.writeInt(this.num); 4975 messageParcel.writeString(this.str); 4976 return true; 4977 } 4978 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4979 this.num = messageParcel.readInt(); 4980 this.str = messageParcel.readString(); 4981 return true; 4982 } 4983 } 4984 4985 try { 4986 let sequenceable = new MySequenceable(1, "aaa"); 4987 let data = rpc.MessageParcel.create(); 4988 let result = data.writeSequenceable(sequenceable); 4989 hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result); 4990 let ret = new MySequenceable(0, ""); 4991 let result2 = data.readSequenceable(ret); 4992 hilog.info(0x0000, 'testTag', 'readSequenceable is ' + result2); 4993 } catch (error) { 4994 hilog.error(0x0000, 'testTag', 'error ' + error); 4995 } 4996 ``` 4997 4998### writeByteArray 4999 5000writeByteArray(byteArray: number[]): boolean 5001 5002将字节数组写入MessageParcel实例。 5003 5004**系统能力**:SystemCapability.Communication.IPC.Core 5005 5006**参数:** 5007 5008 | 参数名 | 类型 | 必填 | 说明 | 5009 | --------- | -------- | ---- | ------------------ | 5010 | byteArray | number[] | 是 | 要写入的字节数组。 | 5011 5012**返回值:** 5013 5014 | 类型 | 说明 | 5015 | ------- | -------------------------------- | 5016 | boolean | true:写入成功,false:写入失败。| 5017 5018**示例:** 5019<!--deprecated_code_no_check--> 5020 ```ts 5021 import { rpc } from '@kit.IPCKit'; 5022 import { hilog } from '@kit.PerformanceAnalysisKit'; 5023 5024 try { 5025 let data = rpc.MessageParcel.create(); 5026 let ByteArrayVar = [1, 2, 3, 4, 5]; 5027 let result = data.writeByteArray(ByteArrayVar); 5028 hilog.info(0x0000, 'testTag', 'writeByteArray is ' + result); 5029 } catch (error) { 5030 hilog.error(0x0000, 'testTag', 'error ' + error); 5031 } 5032 ``` 5033 5034### readByteArray 5035 5036readByteArray(dataIn: number[]): void 5037 5038从MessageParcel实例读取字节数组。 5039 5040**系统能力**:SystemCapability.Communication.IPC.Core 5041 5042**参数:** 5043 5044 | 参数名 | 类型 | 必填 | 说明 | 5045 | ------ | -------- | ---- | ------------------ | 5046 | dataIn | number[] | 是 | 要读取的字节数组。 | 5047 5048**示例:** 5049<!--deprecated_code_no_check--> 5050 ```ts 5051 import { rpc } from '@kit.IPCKit'; 5052 import { hilog } from '@kit.PerformanceAnalysisKit'; 5053 5054 try { 5055 let data = rpc.MessageParcel.create(); 5056 let ByteArrayVar = [1, 2, 3, 4, 5]; 5057 let result = data.writeByteArray(ByteArrayVar); 5058 let array: Array<number> = new Array(5); 5059 data.readByteArray(array); 5060 hilog.info(0x0000, 'testTag', 'readByteArray is ' + array); 5061 } catch (error) { 5062 hilog.error(0x0000, 'testTag', 'error ' + error); 5063 } 5064 ``` 5065 5066### readByteArray 5067 5068readByteArray(): number[] 5069 5070从MessageParcel实例中读取字节数组。 5071 5072**系统能力**:SystemCapability.Communication.IPC.Core 5073 5074**返回值:** 5075 5076 | 类型 | 说明 | 5077 | -------- | -------------- | 5078 | number[] | 返回字节数组。 | 5079 5080**示例:** 5081<!--deprecated_code_no_check--> 5082 ```ts 5083 import { rpc } from '@kit.IPCKit'; 5084 import { hilog } from '@kit.PerformanceAnalysisKit'; 5085 5086 try { 5087 let data = rpc.MessageParcel.create(); 5088 let ByteArrayVar = [1, 2, 3, 4, 5]; 5089 let result = data.writeByteArray(ByteArrayVar); 5090 hilog.info(0x0000, 'testTag', 'writeByteArray is ' + result); 5091 let array = data.readByteArray(); 5092 hilog.info(0x0000, 'testTag', 'readByteArray is ' + array); 5093 } catch (error) { 5094 hilog.error(0x0000, 'testTag', 'error ' + error); 5095 } 5096 ``` 5097 5098### writeShortArray 5099 5100writeShortArray(shortArray: number[]): boolean 5101 5102将短整数数组写入MessageParcel实例。 5103 5104**系统能力**:SystemCapability.Communication.IPC.Core 5105 5106**参数:** 5107 5108 | 参数名 | 类型 | 必填 | 说明 | 5109 | ---------- | -------- | ---- | -------------------- | 5110 | shortArray | number[] | 是 | 要写入的短整数数组。 | 5111 5112**返回值:** 5113 5114 | 类型 | 说明 | 5115 | ------- | -------------------------------- | 5116 | boolean | true:写入成功,false:写入失败。| 5117 5118**示例:** 5119<!--deprecated_code_no_check--> 5120 ```ts 5121 import { rpc } from '@kit.IPCKit'; 5122 import { hilog } from '@kit.PerformanceAnalysisKit'; 5123 5124 try { 5125 let data = rpc.MessageParcel.create(); 5126 let result = data.writeShortArray([11, 12, 13]); 5127 hilog.info(0x0000, 'testTag', 'writeShortArray is ' + result); 5128 } catch (error) { 5129 hilog.error(0x0000, 'testTag', 'error ' + error); 5130 } 5131 ``` 5132 5133### readShortArray 5134 5135readShortArray(dataIn: number[]): void 5136 5137从MessageParcel实例中读取短整数数组。 5138 5139**系统能力**:SystemCapability.Communication.IPC.Core 5140 5141**参数:** 5142 5143 | 参数名 | 类型 | 必填 | 说明 | 5144 | ------ | -------- | ---- | -------------------- | 5145 | dataIn | number[] | 是 | 要读取的短整数数组。 | 5146 5147**示例:** 5148<!--deprecated_code_no_check--> 5149 ```ts 5150 import { rpc } from '@kit.IPCKit'; 5151 import { hilog } from '@kit.PerformanceAnalysisKit'; 5152 5153 try { 5154 let data = rpc.MessageParcel.create(); 5155 let result = data.writeShortArray([11, 12, 13]); 5156 hilog.info(0x0000, 'testTag', 'writeShortArray is ' + result); 5157 let array: Array<number> = new Array(3); 5158 data.readShortArray(array); 5159 hilog.info(0x0000, 'testTag', 'readShortArray is ' + array); 5160 } catch (error) { 5161 hilog.error(0x0000, 'testTag', 'error ' + error); 5162 } 5163 ``` 5164 5165### readShortArray 5166 5167readShortArray(): number[] 5168 5169从MessageParcel实例中读取短整数数组。 5170 5171**系统能力**:SystemCapability.Communication.IPC.Core 5172 5173**返回值:** 5174 5175 | 类型 | 说明 | 5176 | -------- | ---------------- | 5177 | number[] | 返回短整数数组。 | 5178 5179**示例:** 5180<!--deprecated_code_no_check--> 5181 ```ts 5182 import { rpc } from '@kit.IPCKit'; 5183 import { hilog } from '@kit.PerformanceAnalysisKit'; 5184 5185 try { 5186 let data = rpc.MessageParcel.create(); 5187 let result = data.writeShortArray([11, 12, 13]); 5188 hilog.info(0x0000, 'testTag', 'writeShortArray is ' + result); 5189 let array = data.readShortArray(); 5190 hilog.info(0x0000, 'testTag', 'readShortArray is ' + array); 5191 } catch (error) { 5192 hilog.error(0x0000, 'testTag', 'error ' + error); 5193 } 5194 ``` 5195 5196### writeIntArray 5197 5198writeIntArray(intArray: number[]): boolean 5199 5200将整数数组写入MessageParcel实例。 5201 5202**系统能力**:SystemCapability.Communication.IPC.Core 5203 5204**参数:** 5205 5206 | 参数名 | 类型 | 必填 | 说明 | 5207 | -------- | -------- | ---- | ------------------ | 5208 | intArray | number[] | 是 | 要写入的整数数组。 | 5209 5210**返回值:** 5211 5212 | 类型 | 说明 | 5213 | ------- | -------------------------------- | 5214 | boolean | true:写入成功,false:写入失败。| 5215 5216**示例:** 5217<!--deprecated_code_no_check--> 5218 ```ts 5219 import { rpc } from '@kit.IPCKit'; 5220 import { hilog } from '@kit.PerformanceAnalysisKit'; 5221 5222 try { 5223 let data = rpc.MessageParcel.create(); 5224 let result = data.writeIntArray([100, 111, 112]); 5225 hilog.info(0x0000, 'testTag', 'writeIntArray is ' + result); 5226 } catch (error) { 5227 hilog.error(0x0000, 'testTag', 'error ' + error); 5228 } 5229 ``` 5230 5231### readIntArray 5232 5233readIntArray(dataIn: number[]): void 5234 5235从MessageParcel实例中读取整数数组。 5236 5237**系统能力**:SystemCapability.Communication.IPC.Core 5238 5239**参数:** 5240 5241 | 参数名 | 类型 | 必填 | 说明 | 5242 | ------ | -------- | ---- | ------------------ | 5243 | dataIn | number[] | 是 | 要读取的整数数组。 | 5244 5245**示例:** 5246<!--deprecated_code_no_check--> 5247 ```ts 5248 import { rpc } from '@kit.IPCKit'; 5249 import { hilog } from '@kit.PerformanceAnalysisKit'; 5250 5251 try { 5252 let data = rpc.MessageParcel.create(); 5253 let result = data.writeIntArray([100, 111, 112]); 5254 hilog.info(0x0000, 'testTag', 'writeIntArray is ' + result); 5255 let array: Array<number> = new Array(3); 5256 data.readIntArray(array); 5257 hilog.info(0x0000, 'testTag', 'readIntArray is ' + array); 5258 } catch (error) { 5259 hilog.error(0x0000, 'testTag', 'error ' + error); 5260 } 5261 ``` 5262 5263### readIntArray 5264 5265readIntArray(): number[] 5266 5267从MessageParcel实例中读取整数数组。 5268 5269**系统能力**:SystemCapability.Communication.IPC.Core 5270 5271**返回值:** 5272 5273 | 类型 | 说明 | 5274 | -------- | -------------- | 5275 | number[] | 返回整数数组。 | 5276 5277**示例:** 5278<!--deprecated_code_no_check--> 5279 ```ts 5280 import { rpc } from '@kit.IPCKit'; 5281 import { hilog } from '@kit.PerformanceAnalysisKit'; 5282 5283 try { 5284 let data = rpc.MessageParcel.create(); 5285 let result = data.writeIntArray([100, 111, 112]); 5286 hilog.info(0x0000, 'testTag', 'writeIntArray is ' + result); 5287 let array = data.readIntArray(); 5288 hilog.info(0x0000, 'testTag', 'readIntArray is ' + array); 5289 } catch (error) { 5290 hilog.error(0x0000, 'testTag', 'error ' + error); 5291 } 5292 ``` 5293 5294### writeLongArray 5295 5296writeLongArray(longArray: number[]): boolean 5297 5298将长整数数组写入MessageParcel实例。 5299 5300**系统能力**:SystemCapability.Communication.IPC.Core 5301 5302**参数:** 5303 5304 | 参数名 | 类型 | 必填 | 说明 | 5305 | --------- | -------- | ---- | -------------------- | 5306 | longArray | number[] | 是 | 要写入的长整数数组。 | 5307 5308**返回值:** 5309 5310 | 类型 | 说明 | 5311 | ------- | ----------------------------- | 5312 | boolean | true:写入成功,false:写入失败。| 5313 5314**示例:** 5315<!--deprecated_code_no_check--> 5316 ```ts 5317 import { rpc } from '@kit.IPCKit'; 5318 import { hilog } from '@kit.PerformanceAnalysisKit'; 5319 5320 try { 5321 let data = rpc.MessageParcel.create(); 5322 let result = data.writeLongArray([1111, 1112, 1113]); 5323 hilog.info(0x0000, 'testTag', 'writeLongArray is ' + result); 5324 } catch (error) { 5325 hilog.error(0x0000, 'testTag', 'error ' + error); 5326 } 5327 ``` 5328 5329### readLongArray 5330 5331readLongArray(dataIn: number[]): void 5332 5333从MessageParcel实例读取长整数数组。 5334 5335**系统能力**:SystemCapability.Communication.IPC.Core 5336 5337**参数:** 5338 5339 | 参数名 | 类型 | 必填 | 说明 | 5340 | ------ | -------- | ---- | -------------------- | 5341 | dataIn | number[] | 是 | 要读取的长整数数组。 | 5342 5343**示例:** 5344<!--deprecated_code_no_check--> 5345 ```ts 5346 import { rpc } from '@kit.IPCKit'; 5347 import { hilog } from '@kit.PerformanceAnalysisKit'; 5348 5349 try { 5350 let data = rpc.MessageParcel.create(); 5351 let result = data.writeLongArray([1111, 1112, 1113]); 5352 hilog.info(0x0000, 'testTag', 'writeLongArray is ' + result); 5353 let array: Array<number> = new Array(3); 5354 data.readLongArray(array); 5355 hilog.info(0x0000, 'testTag', 'readLongArray is ' + array); 5356 } catch (error) { 5357 hilog.error(0x0000, 'testTag', 'error ' + error); 5358 } 5359 ``` 5360 5361### readLongArray 5362 5363readLongArray(): number[] 5364 5365从MessageParcel实例中读取长整数数组。 5366 5367**系统能力**:SystemCapability.Communication.IPC.Core 5368 5369**返回值:** 5370 5371 | 类型 | 说明 | 5372 | -------- | ---------------- | 5373 | number[] | 返回长整数数组。 | 5374 5375**示例:** 5376<!--deprecated_code_no_check--> 5377 ```ts 5378 import { rpc } from '@kit.IPCKit'; 5379 import { hilog } from '@kit.PerformanceAnalysisKit'; 5380 5381 try { 5382 let data = rpc.MessageParcel.create(); 5383 let result = data.writeLongArray([1111, 1112, 1113]); 5384 hilog.info(0x0000, 'testTag', 'writeLongArray is ' + result); 5385 let array = data.readLongArray(); 5386 hilog.info(0x0000, 'testTag', 'readLongArray is ' + array); 5387 } catch (error) { 5388 hilog.error(0x0000, 'testTag', 'error ' + error); 5389 } 5390 ``` 5391 5392### writeFloatArray 5393 5394writeFloatArray(floatArray: number[]): boolean 5395 5396将双精度浮点数组写入MessageParcel实例。 5397 5398**系统能力**:SystemCapability.Communication.IPC.Core 5399 5400**参数:** 5401 5402 | 参数名 | 类型 | 必填 | 说明 | 5403 | ---------- | -------- | ---- | --- | 5404 | floatArray | number[] | 是 | 要写入的双精度浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5405 5406**返回值:** 5407 5408 | 类型 | 说明 | 5409 | ------- | -------------------------------- | 5410 | boolean | true:写入成功,false:写入失败。| 5411 5412**示例:** 5413<!--deprecated_code_no_check--> 5414 ```ts 5415 import { rpc } from '@kit.IPCKit'; 5416 import { hilog } from '@kit.PerformanceAnalysisKit'; 5417 5418 try { 5419 let data = rpc.MessageParcel.create(); 5420 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5421 hilog.info(0x0000, 'testTag', 'writeFloatArray is ' + result); 5422 } catch (error) { 5423 hilog.error(0x0000, 'testTag', 'error ' + error); 5424 } 5425 ``` 5426 5427### readFloatArray 5428 5429readFloatArray(dataIn: number[]): void 5430 5431从MessageParcel实例中读取双精度浮点数组。 5432 5433**系统能力**:SystemCapability.Communication.IPC.Core 5434 5435**参数:** 5436 5437 | 参数名 | 类型 | 必填 | 说明 | 5438 | ------ | -------- | ---- | ------ | 5439 | dataIn | number[] | 是 | 要读取的双精度浮点数组。由于系统内部对float类型的数据是按照double处理的,使用时对于数组所占的总字节数应按照double类型来计算。 | 5440 5441**示例:** 5442<!--deprecated_code_no_check--> 5443 ```ts 5444 import { rpc } from '@kit.IPCKit'; 5445 import { hilog } from '@kit.PerformanceAnalysisKit'; 5446 5447 try { 5448 let data = rpc.MessageParcel.create(); 5449 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5450 hilog.info(0x0000, 'testTag', 'writeFloatArray is ' + result); 5451 let array: Array<number> = new Array(3); 5452 data.readFloatArray(array); 5453 hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array); 5454 } catch (error) { 5455 hilog.error(0x0000, 'testTag', 'error ' + error); 5456 } 5457 ``` 5458 5459### readFloatArray 5460 5461readFloatArray(): number[] 5462 5463从MessageParcel实例中读取双精度浮点数组。 5464 5465**系统能力**:SystemCapability.Communication.IPC.Core 5466 5467**返回值:** 5468 5469 | 类型 | 说明 | 5470 | -------- | -------------- | 5471 | number[] | 返回双精度浮点数组。 | 5472 5473**示例:** 5474<!--deprecated_code_no_check--> 5475 ```ts 5476 import { rpc } from '@kit.IPCKit'; 5477 import { hilog } from '@kit.PerformanceAnalysisKit'; 5478 5479 try { 5480 let data = rpc.MessageParcel.create(); 5481 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5482 hilog.info(0x0000, 'testTag', 'writeFloatArray is ' + result); 5483 let array = data.readFloatArray(); 5484 hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array); 5485 } catch (error) { 5486 hilog.error(0x0000, 'testTag', 'error ' + error); 5487 } 5488 ``` 5489 5490### writeDoubleArray 5491 5492writeDoubleArray(doubleArray: number[]): boolean 5493 5494将双精度浮点数组写入MessageParcel实例。 5495 5496**系统能力**:SystemCapability.Communication.IPC.Core 5497 5498**参数:** 5499 5500 | 参数名 | 类型 | 必填 | 说明 | 5501 | ----------- | -------- | ---- | ------------------------ | 5502 | doubleArray | number[] | 是 | 要写入的双精度浮点数组。 | 5503 5504**返回值:** 5505 5506 | 类型 | 说明 | 5507 | ------- | -------------------------------- | 5508 | boolean | true:写入成功,false:写入失败。| 5509 5510**示例:** 5511<!--deprecated_code_no_check--> 5512 ```ts 5513 import { rpc } from '@kit.IPCKit'; 5514 import { hilog } from '@kit.PerformanceAnalysisKit'; 5515 5516 try { 5517 let data = rpc.MessageParcel.create(); 5518 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5519 hilog.info(0x0000, 'testTag', 'writeDoubleArray is ' + result); 5520 } catch (error) { 5521 hilog.error(0x0000, 'testTag', 'error ' + error); 5522 } 5523 ``` 5524 5525### readDoubleArray 5526 5527readDoubleArray(dataIn: number[]): void 5528 5529从MessageParcel实例中读取双精度浮点数组。 5530 5531**系统能力**:SystemCapability.Communication.IPC.Core 5532 5533**参数:** 5534 5535 | 参数名 | 类型 | 必填 | 说明 | 5536 | ------ | -------- | ---- | ------------------------ | 5537 | dataIn | number[] | 是 | 要读取的双精度浮点数组。 | 5538 5539**示例:** 5540<!--deprecated_code_no_check--> 5541 ```ts 5542 import { rpc } from '@kit.IPCKit'; 5543 import { hilog } from '@kit.PerformanceAnalysisKit'; 5544 5545 try { 5546 let data = rpc.MessageParcel.create(); 5547 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5548 hilog.info(0x0000, 'testTag', 'writeDoubleArray is ' + result); 5549 let array: Array<number> = new Array(3); 5550 data.readDoubleArray(array); 5551 hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array); 5552 } catch (error) { 5553 hilog.error(0x0000, 'testTag', 'error ' + error); 5554 } 5555 ``` 5556 5557### readDoubleArray 5558 5559readDoubleArray(): number[] 5560 5561从MessageParcel实例读取双精度浮点数组。 5562 5563**系统能力**:SystemCapability.Communication.IPC.Core 5564 5565**返回值:** 5566 5567 | 类型 | 说明 | 5568 | -------- | -------------------- | 5569 | number[] | 返回双精度浮点数组。 | 5570 5571**示例:** 5572<!--deprecated_code_no_check--> 5573 ```ts 5574 import { rpc } from '@kit.IPCKit'; 5575 import { hilog } from '@kit.PerformanceAnalysisKit'; 5576 5577 try { 5578 let data = rpc.MessageParcel.create(); 5579 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5580 hilog.info(0x0000, 'testTag', 'writeDoubleArray is ' + result); 5581 let array = data.readDoubleArray(); 5582 hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array); 5583 } catch (error) { 5584 hilog.error(0x0000, 'testTag', 'error ' + error); 5585 } 5586 ``` 5587 5588### writeBooleanArray 5589 5590writeBooleanArray(booleanArray: boolean[]): boolean 5591 5592将布尔数组写入MessageParcel实例。 5593 5594**系统能力**:SystemCapability.Communication.IPC.Core 5595 5596**参数:** 5597 5598 | 参数名 | 类型 | 必填 | 说明 | 5599 | ------------ | --------- | ---- | ------------------ | 5600 | booleanArray | boolean[] | 是 | 要写入的布尔数组。 | 5601 5602**返回值:** 5603 5604 | 类型 | 说明 | 5605 | ------- | -------------------------------- | 5606 | boolean | true:写入成功,false:写入失败。| 5607 5608**示例:** 5609<!--deprecated_code_no_check--> 5610 ```ts 5611 import { rpc } from '@kit.IPCKit'; 5612 import { hilog } from '@kit.PerformanceAnalysisKit'; 5613 5614 try { 5615 let data = rpc.MessageParcel.create(); 5616 let result = data.writeBooleanArray([false, true, false]); 5617 hilog.info(0x0000, 'testTag', 'writeBooleanArray is ' + result); 5618 } catch (error) { 5619 hilog.error(0x0000, 'testTag', 'error ' + error); 5620 } 5621 ``` 5622 5623### readBooleanArray 5624 5625readBooleanArray(dataIn: boolean[]): void 5626 5627从MessageParcel实例中读取布尔数组。 5628 5629**系统能力**:SystemCapability.Communication.IPC.Core 5630 5631**参数:** 5632 5633 | 参数名 | 类型 | 必填 | 说明 | 5634 | ------ | --------- | ---- | ------------------ | 5635 | dataIn | boolean[] | 是 | 要读取的布尔数组。 | 5636 5637**示例:** 5638<!--deprecated_code_no_check--> 5639 ```ts 5640 import { rpc } from '@kit.IPCKit'; 5641 import { hilog } from '@kit.PerformanceAnalysisKit'; 5642 5643 try { 5644 let data = rpc.MessageParcel.create(); 5645 let result = data.writeBooleanArray([false, true, false]); 5646 hilog.info(0x0000, 'testTag', 'writeBooleanArray is ' + result); 5647 let array: Array<boolean> = new Array(3); 5648 data.readBooleanArray(array); 5649 hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array); 5650 } catch (error) { 5651 hilog.error(0x0000, 'testTag', 'error ' + error); 5652 } 5653 ``` 5654 5655### readBooleanArray 5656 5657readBooleanArray(): boolean[] 5658 5659从MessageParcel实例中读取布尔数组。 5660 5661**系统能力**:SystemCapability.Communication.IPC.Core 5662 5663**返回值:** 5664 5665 | 类型 | 说明 | 5666 | --------- | -------------- | 5667 | boolean[] | 返回布尔数组。 | 5668 5669**示例:** 5670<!--deprecated_code_no_check--> 5671 ```ts 5672 import { rpc } from '@kit.IPCKit'; 5673 import { hilog } from '@kit.PerformanceAnalysisKit'; 5674 5675 try { 5676 let data = rpc.MessageParcel.create(); 5677 let result = data.writeBooleanArray([false, true, false]); 5678 hilog.info(0x0000, 'testTag', 'writeBooleanArray is ' + result); 5679 let array = data.readBooleanArray(); 5680 hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array); 5681 } catch (error) { 5682 hilog.error(0x0000, 'testTag', 'error ' + error); 5683 } 5684 ``` 5685 5686### writeCharArray 5687 5688writeCharArray(charArray: number[]): boolean 5689 5690将单个字符数组写入MessageParcel实例。 5691 5692**系统能力**:SystemCapability.Communication.IPC.Core 5693 5694**参数:** 5695 5696 | 参数名 | 类型 | 必填 | 说明 | 5697 | --------- | -------- | ---- | ---------------------- | 5698 | charArray | number[] | 是 | 要写入的单个字符数组。 | 5699 5700**返回值:** 5701 5702 | 类型 | 说明 | 5703 | ------- | -------------------------------- | 5704 | boolean | true:写入成功,false:写入失败。| 5705 5706**示例:** 5707<!--deprecated_code_no_check--> 5708 ```ts 5709 import { rpc } from '@kit.IPCKit'; 5710 import { hilog } from '@kit.PerformanceAnalysisKit'; 5711 5712 try { 5713 let data = rpc.MessageParcel.create(); 5714 let result = data.writeCharArray([97, 98, 88]); 5715 hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result); 5716 } catch (error) { 5717 hilog.error(0x0000, 'testTag', 'error ' + error); 5718 } 5719 ``` 5720 5721### readCharArray 5722 5723readCharArray(dataIn: number[]): void 5724 5725从MessageParcel实例中读取单个字符数组。 5726 5727**系统能力**:SystemCapability.Communication.IPC.Core 5728 5729**参数:** 5730 5731 | 参数名 | 类型 | 必填 | 说明 | 5732 | ------ | -------- | ---- | ---------------------- | 5733 | dataIn | number[] | 是 | 要读取的单个字符数组。 | 5734 5735**示例:** 5736<!--deprecated_code_no_check--> 5737 ```ts 5738 import { rpc } from '@kit.IPCKit'; 5739 import { hilog } from '@kit.PerformanceAnalysisKit'; 5740 5741 try { 5742 let data = rpc.MessageParcel.create(); 5743 let result = data.writeCharArray([97, 98, 99]); 5744 hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result); 5745 let array: Array<number> = new Array(3); 5746 data.readCharArray(array); 5747 hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result); 5748 } catch (error) { 5749 hilog.error(0x0000, 'testTag', 'error ' + error); 5750 } 5751 ``` 5752 5753### readCharArray 5754 5755readCharArray(): number[] 5756 5757从MessageParcel实例读取单个字符数组。 5758 5759**系统能力**:SystemCapability.Communication.IPC.Core 5760 5761**返回值:** 5762 5763 | 类型 | 说明 | 5764 | -------- | ------------------ | 5765 | number[] | 返回单个字符数组。 | 5766 5767**示例:** 5768<!--deprecated_code_no_check--> 5769 ```ts 5770 import { rpc } from '@kit.IPCKit'; 5771 import { hilog } from '@kit.PerformanceAnalysisKit'; 5772 5773 try { 5774 let data = rpc.MessageParcel.create(); 5775 let result = data.writeCharArray([97, 98, 99]); 5776 hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result); 5777 let array = data.readCharArray(); 5778 hilog.info(0x0000, 'testTag', 'readCharArray is ' + array); 5779 } catch (error) { 5780 hilog.error(0x0000, 'testTag', 'error ' + error); 5781 } 5782 ``` 5783 5784### writeStringArray 5785 5786writeStringArray(stringArray: string[]): boolean 5787 5788将字符串数组写入MessageParcel实例。 5789 5790**系统能力**:SystemCapability.Communication.IPC.Core 5791 5792**参数:** 5793 5794 | 参数名 | 类型 | 必填 | 说明 | 5795 | ----------- | -------- | ---- | ---------------- | 5796 | stringArray | string[] | 是 | 要写入的字符串数组,数组单个元素的长度应小于40960字节。 | 5797 5798**返回值:** 5799 5800 | 类型 | 说明 | 5801 | ------- | -------------------------------- | 5802 | boolean | true:写入成功,false:写入失败。| 5803 5804**示例:** 5805<!--deprecated_code_no_check--> 5806 ```ts 5807 import { rpc } from '@kit.IPCKit'; 5808 import { hilog } from '@kit.PerformanceAnalysisKit'; 5809 5810 try { 5811 let data = rpc.MessageParcel.create(); 5812 let result = data.writeStringArray(["abc", "def"]); 5813 hilog.info(0x0000, 'testTag', 'writeStringArray is ' + result); 5814 } catch (error) { 5815 hilog.error(0x0000, 'testTag', 'error ' + error); 5816 } 5817 ``` 5818 5819### readStringArray 5820 5821readStringArray(dataIn: string[]): void 5822 5823从MessageParcel实例读取字符串数组。 5824 5825**系统能力**:SystemCapability.Communication.IPC.Core 5826 5827**参数:** 5828 5829 | 参数名 | 类型 | 必填 | 说明 | 5830 | ------ | -------- | ---- | -------------------- | 5831 | dataIn | string[] | 是 | 要读取的字符串数组。 | 5832 5833**示例:** 5834<!--deprecated_code_no_check--> 5835 ```ts 5836 import { rpc } from '@kit.IPCKit'; 5837 import { hilog } from '@kit.PerformanceAnalysisKit'; 5838 5839 try { 5840 let data = rpc.MessageParcel.create(); 5841 let result = data.writeStringArray(["abc", "def"]); 5842 hilog.info(0x0000, 'testTag', 'writeStringArray is ' + result); 5843 let array: Array<string> = new Array(2); 5844 data.readStringArray(array); 5845 hilog.info(0x0000, 'testTag', 'readStringArray is ' + array); 5846 } catch (error) { 5847 hilog.error(0x0000, 'testTag', 'error ' + error); 5848 } 5849 ``` 5850 5851### readStringArray 5852 5853readStringArray(): string[] 5854 5855从MessageParcel实例读取字符串数组。 5856 5857**系统能力**:SystemCapability.Communication.IPC.Core 5858 5859**返回值:** 5860 5861 | 类型 | 说明 | 5862 | -------- | ---------------- | 5863 | string[] | 返回字符串数组。 | 5864 5865**示例:** 5866<!--deprecated_code_no_check--> 5867 ```ts 5868 import { rpc } from '@kit.IPCKit'; 5869 import { hilog } from '@kit.PerformanceAnalysisKit'; 5870 5871 try { 5872 let data = rpc.MessageParcel.create(); 5873 let result = data.writeStringArray(["abc", "def"]); 5874 hilog.info(0x0000, 'testTag', 'writeStringArray is ' + result); 5875 let array = data.readStringArray(); 5876 hilog.info(0x0000, 'testTag', 'readStringArray is ' + array); 5877 } catch (error) { 5878 hilog.error(0x0000, 'testTag', 'error ' + error); 5879 } 5880 ``` 5881 5882### writeNoException<sup>8+</sup> 5883 5884writeNoException(): void 5885 5886向MessageParcel写入“指示未发生异常”的信息。 5887 5888**系统能力**:SystemCapability.Communication.IPC.Core 5889 5890**示例:** 5891<!--deprecated_code_no_check--> 5892 ```ts 5893 import { rpc } from '@kit.IPCKit'; 5894 import { hilog } from '@kit.PerformanceAnalysisKit'; 5895 5896 class MyDeathRecipient implements rpc.DeathRecipient { 5897 onRemoteDied() { 5898 hilog.info(0x0000, 'testTag', 'server died'); 5899 } 5900 } 5901 class TestRemoteObject extends rpc.RemoteObject { 5902 constructor(descriptor: string) { 5903 super(descriptor); 5904 } 5905 5906 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5907 if (code === 1) { 5908 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5909 reply.writeNoException(); 5910 return true; 5911 } else { 5912 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5913 return false; 5914 } 5915 } 5916 } 5917 ``` 5918 5919### readException<sup>8+</sup> 5920 5921readException(): void 5922 5923从MessageParcel中读取异常。 5924 5925**系统能力**:SystemCapability.Communication.IPC.Core 5926 5927**示例:** 5928 5929>**说明:** 5930> 5931>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 5932 5933 <!--code_no_check--> 5934 ```ts 5935 // FA模型需要从@kit.AbilityKit导入featureAbility 5936 // import { featureAbility } from '@kit.AbilityKit'; 5937 import { rpc } from '@kit.IPCKit'; 5938 import { Want, common } from '@kit.AbilityKit'; 5939 import { hilog } from '@kit.PerformanceAnalysisKit'; 5940 5941 let proxy: rpc.IRemoteObject | undefined; 5942 let connect: common.ConnectOptions = { 5943 onConnect: (elementName, remoteProxy) => { 5944 hilog.info(0x0000, 'testTag', 'js onConnect called'); 5945 proxy = remoteProxy; 5946 }, 5947 onDisconnect: (elementName) => { 5948 hilog.info(0x0000, 'testTag', 'onDisconnect'); 5949 }, 5950 onFailed: () => { 5951 hilog.info(0x0000, 'testTag', 'onFailed'); 5952 } 5953 }; 5954 let want: Want = { 5955 bundleName: "com.ohos.server", 5956 abilityName: "com.ohos.server.EntryAbility", 5957 }; 5958 5959 // FA模型使用此方法连接服务 5960 // FA.connectAbility(want,connect); 5961 5962 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5963 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 5964 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 5965 let connectionId = context.connectServiceExtensionAbility(want, connect); 5966 ``` 5967 5968 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 5969<!--deprecated_code_no_check--> 5970 ```ts 5971 import { rpc } from '@kit.IPCKit'; 5972 import { hilog } from '@kit.PerformanceAnalysisKit'; 5973 5974 try { 5975 let option = new rpc.MessageOption(); 5976 let data = rpc.MessageParcel.create(); 5977 let reply = rpc.MessageParcel.create(); 5978 data.writeNoException(); 5979 data.writeString('hello'); 5980 if (proxy != undefined) { 5981 let a = proxy.sendRequest(1, data, reply, option) as Object; 5982 let b = a as Promise<rpc.SendRequestResult>; 5983 b.then((result: rpc.SendRequestResult) => { 5984 if (result.errCode === 0) { 5985 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5986 result.reply.readException(); 5987 let msg = result.reply.readString(); 5988 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 5989 } else { 5990 hilog.error(0x0000, 'testTag', 'sendRequest failed, errCode: ' + result.errCode); 5991 } 5992 }).catch((e: Error) => { 5993 hilog.error(0x0000, 'testTag', 'sendRequest got exception: ' + e); 5994 }).finally (() => { 5995 hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel'); 5996 data.reclaim(); 5997 reply.reclaim(); 5998 }); 5999 } 6000 } catch (error) { 6001 hilog.error(0x0000, 'testTag', 'error ' + error); 6002 } 6003 ``` 6004 6005### writeSequenceableArray 6006 6007writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 6008 6009将可序列化对象数组写入MessageParcel实例。 6010 6011**系统能力**:SystemCapability.Communication.IPC.Core 6012 6013**参数:** 6014 6015| 参数名 | 类型 | 必填 | 说明 | 6016| ----------------- | ----------------------------------------- | ---- | -------------------------- | 6017| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要写入的可序列化对象数组。 | 6018 6019**返回值:** 6020 6021 | 类型 | 说明 | 6022 | ------- | -------------------------------- | 6023 | boolean | true:写入成功,false:写入失败。| 6024 6025**示例:** 6026<!--deprecated_code_no_check--> 6027 ```ts 6028 import { rpc } from '@kit.IPCKit'; 6029 import { hilog } from '@kit.PerformanceAnalysisKit'; 6030 6031 class MySequenceable implements rpc.Sequenceable { 6032 num: number = 0; 6033 str: string = ''; 6034 constructor(num: number, str: string) { 6035 this.num = num; 6036 this.str = str; 6037 } 6038 marshalling(messageParcel: rpc.MessageParcel): boolean { 6039 messageParcel.writeInt(this.num); 6040 messageParcel.writeString(this.str); 6041 return true; 6042 } 6043 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6044 this.num = messageParcel.readInt(); 6045 this.str = messageParcel.readString(); 6046 return true; 6047 } 6048 } 6049 6050 try { 6051 let sequenceable = new MySequenceable(1, "aaa"); 6052 let sequenceable2 = new MySequenceable(2, "bbb"); 6053 let sequenceable3 = new MySequenceable(3, "ccc"); 6054 let a = [sequenceable, sequenceable2, sequenceable3]; 6055 let data = rpc.MessageParcel.create(); 6056 let result = data.writeSequenceableArray(a); 6057 hilog.info(0x0000, 'testTag', 'writeSequenceableArray is ' + result); 6058 } catch (error) { 6059 hilog.error(0x0000, 'testTag', 'error ' + error); 6060 } 6061 ``` 6062 6063### readSequenceableArray<sup>8+</sup> 6064 6065readSequenceableArray(sequenceableArray: Sequenceable[]): void 6066 6067从MessageParcel实例读取可序列化对象数组。 6068 6069**系统能力**:SystemCapability.Communication.IPC.Core 6070 6071**参数:** 6072 6073| 参数名 | 类型 | 必填 | 说明 | 6074| ----------------- | ----------------------------------------- | ---- | -------------------------- | 6075| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | 是 | 要读取的可序列化对象数组。 | 6076 6077**示例:** 6078<!--deprecated_code_no_check--> 6079 ```ts 6080 import { rpc } from '@kit.IPCKit'; 6081 import { hilog } from '@kit.PerformanceAnalysisKit'; 6082 6083 class MySequenceable implements rpc.Sequenceable { 6084 num: number = 0; 6085 str: string = ''; 6086 constructor(num: number, str: string) { 6087 this.num = num; 6088 this.str = str; 6089 } 6090 marshalling(messageParcel: rpc.MessageParcel): boolean { 6091 messageParcel.writeInt(this.num); 6092 messageParcel.writeString(this.str); 6093 return true; 6094 } 6095 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6096 this.num = messageParcel.readInt(); 6097 this.str = messageParcel.readString(); 6098 return true; 6099 } 6100 } 6101 6102 try { 6103 let sequenceable = new MySequenceable(1, "aaa"); 6104 let sequenceable2 = new MySequenceable(2, "bbb"); 6105 let sequenceable3 = new MySequenceable(3, "ccc"); 6106 let a = [sequenceable, sequenceable2, sequenceable3]; 6107 let data = rpc.MessageParcel.create(); 6108 let result = data.writeSequenceableArray(a); 6109 hilog.info(0x0000, 'testTag', 'writeSequenceableArray is ' + result); 6110 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 6111 data.readSequenceableArray(b); 6112 } catch (error) { 6113 hilog.error(0x0000, 'testTag', 'error ' + error); 6114 } 6115 ``` 6116 6117### writeRemoteObjectArray<sup>8+</sup> 6118 6119writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 6120 6121将IRemoteObject对象数组写入MessageParcel。 6122 6123**系统能力**:SystemCapability.Communication.IPC.Core 6124 6125**参数:** 6126 6127 | 参数名 | 类型 | 必填 | 说明 | 6128 | ----------- | --------------- | ---- | ----- | 6129 | objectArray | [IRemoteObject](#iremoteobject)[] | 是 | 要写入MessageParcel的IRemoteObject对象数组。 | 6130 6131**返回值:** 6132 6133 | 类型 | 说明 | 6134 | ------- | -------------------------------- | 6135 | boolean | true:写入成功,false:写入失败。| 6136 6137**示例:** 6138<!--deprecated_code_no_check--> 6139 ```ts 6140 import { rpc } from '@kit.IPCKit'; 6141 import { hilog } from '@kit.PerformanceAnalysisKit'; 6142 6143 class TestRemoteObject extends rpc.RemoteObject { 6144 constructor(descriptor: string) { 6145 super(descriptor); 6146 } 6147 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 6148 // 具体处理由业务决定 6149 return true; 6150 } 6151 } 6152 6153 try { 6154 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 6155 let data = rpc.MessageParcel.create(); 6156 let result = data.writeRemoteObjectArray(a); 6157 hilog.info(0x0000, 'testTag', 'writeRemoteObjectArray is ' + result); 6158 } catch (error) { 6159 hilog.error(0x0000, 'testTag', 'error ' + error); 6160 } 6161 ``` 6162 6163### readRemoteObjectArray<sup>8+</sup> 6164 6165readRemoteObjectArray(objects: IRemoteObject[]): void 6166 6167从MessageParcel读取IRemoteObject对象数组。 6168 6169**系统能力**:SystemCapability.Communication.IPC.Core 6170 6171**参数:** 6172 6173 | 参数名 | 类型 | 必填 | 说明 | 6174 | ------- | --------------- | ---- | --------- | 6175 | objects | [IRemoteObject](#iremoteobject)[] | 是 | 从MessageParcel读取的IRemoteObject对象数组。 | 6176 6177**示例:** 6178<!--deprecated_code_no_check--> 6179 ```ts 6180 import { rpc } from '@kit.IPCKit'; 6181 import { hilog } from '@kit.PerformanceAnalysisKit'; 6182 6183 class TestRemoteObject extends rpc.RemoteObject { 6184 constructor(descriptor: string) { 6185 super(descriptor); 6186 } 6187 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 6188 // 具体处理由业务决定 6189 return true; 6190 } 6191 } 6192 6193 try { 6194 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 6195 let data = rpc.MessageParcel.create(); 6196 data.writeRemoteObjectArray(a); 6197 let b: Array<rpc.IRemoteObject> = new Array(3); 6198 data.readRemoteObjectArray(b); 6199 hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b); 6200 } catch (error) { 6201 hilog.error(0x0000, 'testTag', 'error ' + error); 6202 } 6203 ``` 6204 6205### readRemoteObjectArray<sup>8+</sup> 6206 6207readRemoteObjectArray(): IRemoteObject[] 6208 6209从MessageParcel读取IRemoteObject对象数组。 6210 6211**系统能力**:SystemCapability.Communication.IPC.Core 6212 6213**返回值:** 6214 6215 | 类型 | 说明 | 6216 | --------------- | --------------------------- | 6217 | [IRemoteObject](#iremoteobject)[] | 返回IRemoteObject对象数组。 | 6218 6219**示例:** 6220<!--deprecated_code_no_check--> 6221 ```ts 6222 import { rpc } from '@kit.IPCKit'; 6223 import { hilog } from '@kit.PerformanceAnalysisKit'; 6224 6225 class TestRemoteObject extends rpc.RemoteObject { 6226 constructor(descriptor: string) { 6227 super(descriptor); 6228 } 6229 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 6230 // 具体处理由业务决定 6231 return true; 6232 } 6233 } 6234 6235 try { 6236 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 6237 let data = rpc.MessageParcel.create(); 6238 let result = data.writeRemoteObjectArray(a); 6239 hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + result); 6240 let b = data.readRemoteObjectArray(); 6241 hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b); 6242 } catch (error) { 6243 hilog.error(0x0000, 'testTag', 'error ' + error); 6244 } 6245 ``` 6246 6247### closeFileDescriptor<sup>8+</sup> 6248 6249static closeFileDescriptor(fd: number): void 6250 6251静态方法,关闭给定的文件描述符。 6252 6253**系统能力**:SystemCapability.Communication.IPC.Core 6254 6255**参数:** 6256 6257 | 参数名 | 类型 | 必填 | 说明 | 6258 | ------ | ------ | ---- | -------------------- | 6259 | fd | number | 是 | 要关闭的文件描述符。 | 6260 6261**示例:** 6262<!--deprecated_code_no_check--> 6263 ```ts 6264 import { rpc } from '@kit.IPCKit'; 6265 import { fileIo } from '@kit.CoreFileKit'; 6266 import { hilog } from '@kit.PerformanceAnalysisKit'; 6267 6268 try { 6269 let filePath = "path/to/file"; 6270 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6271 rpc.MessageParcel.closeFileDescriptor(file.fd); 6272 } catch (error) { 6273 hilog.error(0x0000, 'testTag', 'error ' + error); 6274 } 6275 ``` 6276 6277### dupFileDescriptor<sup>8+</sup> 6278 6279static dupFileDescriptor(fd: number) :number 6280 6281静态方法,复制给定的文件描述符。 6282 6283**系统能力**:SystemCapability.Communication.IPC.Core 6284 6285**参数:** 6286 6287 | 参数名 | 类型 | 必填 | 说明 | 6288 | ------ | ------ | ---- | ------------------------ | 6289 | fd | number | 是 | 表示已存在的文件描述符。 | 6290 6291**返回值:** 6292 6293 | 类型 | 说明 | 6294 | ------ | -------------------- | 6295 | number | 返回新的文件描述符。 | 6296 6297**示例:** 6298<!--deprecated_code_no_check--> 6299 ```ts 6300 import { rpc } from '@kit.IPCKit'; 6301 import { fileIo } from '@kit.CoreFileKit'; 6302 import { hilog } from '@kit.PerformanceAnalysisKit'; 6303 6304 try { 6305 let filePath = "path/to/file"; 6306 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6307 rpc.MessageParcel.dupFileDescriptor(file.fd); 6308 } catch (error) { 6309 hilog.error(0x0000, 'testTag', 'error ' + error); 6310 } 6311 ``` 6312 6313### containFileDescriptors<sup>8+</sup> 6314 6315containFileDescriptors(): boolean 6316 6317检查此MessageParcel对象是否包含文件描述符。 6318 6319**系统能力**:SystemCapability.Communication.IPC.Core 6320 6321**返回值:** 6322 6323 | 类型 | 说明 | 6324 | ------- | --------------------------------------------- | 6325 | boolean |true:包含文件描述符,false:未包含文件描述符。| 6326 6327**示例:** 6328<!--deprecated_code_no_check--> 6329 ```ts 6330 import { rpc } from '@kit.IPCKit'; 6331 import { fileIo } from '@kit.CoreFileKit'; 6332 import { hilog } from '@kit.PerformanceAnalysisKit'; 6333 6334 try { 6335 let parcel = new rpc.MessageParcel(); 6336 let filePath = "path/to/file"; 6337 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6338 let writeResult = parcel.writeFileDescriptor(file.fd); 6339 hilog.info(0x0000, 'testTag', 'parcel writeFd result is ' + writeResult); 6340 let containFD = parcel.containFileDescriptors(); 6341 hilog.info(0x0000, 'testTag', 'parcel after write fd containFd result is ' + containFD); 6342 } catch (error) { 6343 hilog.error(0x0000, 'testTag', 'error ' + error); 6344 } 6345 ``` 6346 6347### writeFileDescriptor<sup>8+</sup> 6348 6349writeFileDescriptor(fd: number): boolean 6350 6351写入文件描述符到MessageParcel。 6352 6353**系统能力**:SystemCapability.Communication.IPC.Core 6354 6355**参数:** 6356 6357 | 参数名 | 类型 | 必填 | 说明 | 6358 | ------ | ------ | ---- | ------------ | 6359 | fd | number | 是 | 文件描述符。 | 6360 6361**返回值:** 6362 6363 | 类型 | 说明 | 6364 | ------- | -------------------------------- | 6365 | boolean | true:操作成功,false:操作失败。| 6366 6367**示例:** 6368<!--deprecated_code_no_check--> 6369 ```ts 6370 import { rpc } from '@kit.IPCKit'; 6371 import { fileIo } from '@kit.CoreFileKit'; 6372 import { hilog } from '@kit.PerformanceAnalysisKit'; 6373 6374 try { 6375 let parcel = new rpc.MessageParcel(); 6376 let filePath = "path/to/file"; 6377 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6378 let writeResult = parcel.writeFileDescriptor(file.fd); 6379 hilog.info(0x0000, 'testTag', 'parcel writeFd result is ' + writeResult); 6380 } catch (error) { 6381 hilog.error(0x0000, 'testTag', 'error ' + error); 6382 } 6383 ``` 6384 6385### readFileDescriptor<sup>8+</sup> 6386 6387readFileDescriptor(): number 6388 6389从MessageParcel中读取文件描述符。 6390 6391**系统能力**:SystemCapability.Communication.IPC.Core 6392 6393**返回值:** 6394 6395 | 类型 | 说明 | 6396 | ------ | ---------------- | 6397 | number | 返回文件描述符。 | 6398 6399**示例:** 6400<!--deprecated_code_no_check--> 6401 ```ts 6402 import { rpc } from '@kit.IPCKit'; 6403 import { fileIo } from '@kit.CoreFileKit'; 6404 import { hilog } from '@kit.PerformanceAnalysisKit'; 6405 6406 try { 6407 let parcel = new rpc.MessageParcel(); 6408 let filePath = "path/to/file"; 6409 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6410 parcel.writeFileDescriptor(file.fd); 6411 let readFD = parcel.readFileDescriptor(); 6412 hilog.info(0x0000, 'testTag', 'parcel read fd is ' + readFD); 6413 } catch (error) { 6414 hilog.error(0x0000, 'testTag', 'error ' + error); 6415 } 6416 ``` 6417 6418### writeAshmem<sup>8+</sup> 6419 6420writeAshmem(ashmem: Ashmem): boolean 6421 6422将指定的匿名共享对象写入此MessageParcel。 6423 6424**系统能力**:SystemCapability.Communication.IPC.Core 6425 6426**参数:** 6427 6428| 参数名 | 类型 | 必填 | 说明 | 6429| ------ | ------ | ---- | ----------------------------------- | 6430| ashmem | [Ashmem](#ashmem8) | 是 | 要写入MessageParcel的匿名共享对象。 | 6431 6432**返回值:** 6433 6434 | 类型 | 说明 | 6435 | ------- | -------------------------------- | 6436 | boolean | true:写入成功,false:写入失败。| 6437 6438**示例:** 6439<!--deprecated_code_no_check--> 6440 ```ts 6441 import { rpc } from '@kit.IPCKit'; 6442 import { hilog } from '@kit.PerformanceAnalysisKit'; 6443 6444 try { 6445 let parcel = new rpc.MessageParcel(); 6446 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6447 let isWriteSuccess = parcel.writeAshmem(ashmem); 6448 hilog.info(0x0000, 'testTag', 'write ashmem to result is ' + isWriteSuccess); 6449 } catch (error) { 6450 hilog.error(0x0000, 'testTag', 'error ' + error); 6451 } 6452 ``` 6453 6454### readAshmem<sup>8+</sup> 6455 6456readAshmem(): Ashmem 6457 6458从MessageParcel读取匿名共享对象。 6459 6460**系统能力**:SystemCapability.Communication.IPC.Core 6461 6462**返回值:** 6463 6464| 类型 | 说明 | 6465| ------ | ------------------ | 6466| [Ashmem](#ashmem8) | 返回匿名共享对象。 | 6467 6468**示例:** 6469<!--deprecated_code_no_check--> 6470 ```ts 6471 import { rpc } from '@kit.IPCKit'; 6472 import { hilog } from '@kit.PerformanceAnalysisKit'; 6473 6474 try { 6475 let parcel = new rpc.MessageParcel(); 6476 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6477 let isWriteSuccess = parcel.writeAshmem(ashmem); 6478 hilog.info(0x0000, 'testTag', 'write ashmem to result is ' + isWriteSuccess); 6479 let readAshmem = parcel.readAshmem(); 6480 hilog.info(0x0000, 'testTag', 'read ashmem to result is ' + readAshmem); 6481 } catch (error) { 6482 hilog.error(0x0000, 'testTag', 'error ' + error); 6483 } 6484 ``` 6485 6486### getRawDataCapacity<sup>8+</sup> 6487 6488getRawDataCapacity(): number 6489 6490获取MessageParcel可以容纳的最大原始数据量。 6491 6492**系统能力**:SystemCapability.Communication.IPC.Core 6493 6494**返回值:** 6495 6496 | 类型 | 说明 | 6497 | ------ | ---------------------------------------------------------- | 6498 | number | 返回MessageParcel可以容纳的最大原始数据量,即128MB。 | 6499 6500**示例:** 6501<!--deprecated_code_no_check--> 6502 ```ts 6503 import { rpc } from '@kit.IPCKit'; 6504 import { hilog } from '@kit.PerformanceAnalysisKit'; 6505 6506 try { 6507 let parcel = new rpc.MessageParcel(); 6508 let result = parcel.getRawDataCapacity(); 6509 hilog.info(0x0000, 'testTag', 'parcel get RawDataCapacity result is ' + result); 6510 } catch (error) { 6511 hilog.error(0x0000, 'testTag', 'error ' + error); 6512 } 6513 ``` 6514 6515### writeRawData<sup>8+</sup> 6516 6517writeRawData(rawData: number[], size: number): boolean 6518 6519将原始数据写入MessageParcel对象。 6520 6521**系统能力**:SystemCapability.Communication.IPC.Core 6522 6523**参数:** 6524 6525 | 参数名 | 类型 | 必填 | 说明 | 6526 | ------- | -------- | ---- | ---------------------------------- | 6527 | rawData | number[] | 是 | 要写入的原始数据,大小不能超过128MB。 | 6528 | size | number | 是 | 发送的原始数据大小,以字节为单位。 | 6529 6530**返回值:** 6531 6532 | 类型 | 说明 | 6533 | ------- | -------------------------------- | 6534 | boolean | true:写入成功,false:写入失败。| 6535 6536**示例:** 6537<!--deprecated_code_no_check--> 6538 ```ts 6539 import { rpc } from '@kit.IPCKit'; 6540 import { hilog } from '@kit.PerformanceAnalysisKit'; 6541 6542 try { 6543 let parcel = new rpc.MessageParcel(); 6544 let arr = [1, 2, 3, 4, 5]; 6545 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6546 hilog.info(0x0000, 'testTag', 'parcel write raw data result is ' + isWriteSuccess); 6547 } catch (error) { 6548 hilog.error(0x0000, 'testTag', 'error ' + error); 6549 } 6550 ``` 6551 6552### readRawData<sup>8+</sup> 6553 6554readRawData(size: number): number[] 6555 6556从MessageParcel读取原始数据。 6557 6558**系统能力**:SystemCapability.Communication.IPC.Core 6559 6560**参数:** 6561 6562 | 参数名 | 类型 | 必填 | 说明 | 6563 | ------ | ------ | ---- | ------------------------ | 6564 | size | number | 是 | 要读取的原始数据的大小。 | 6565 6566**返回值:** 6567 6568 | 类型 | 说明 | 6569 | -------- | ------------------------------ | 6570 | number[] | 返回原始数据(以字节为单位)。 | 6571 6572**示例:** 6573<!--deprecated_code_no_check--> 6574 ```ts 6575 import { rpc } from '@kit.IPCKit'; 6576 import { hilog } from '@kit.PerformanceAnalysisKit'; 6577 6578 try { 6579 let parcel = new rpc.MessageParcel(); 6580 let arr = [1, 2, 3, 4, 5]; 6581 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6582 hilog.info(0x0000, 'testTag', 'parcel write raw data result is ' + isWriteSuccess); 6583 let result = parcel.readRawData(5); 6584 hilog.info(0x0000, 'testTag', 'parcel read raw data result is ' + result); 6585 } catch (error) { 6586 hilog.error(0x0000, 'testTag', 'error ' + error); 6587 } 6588 ``` 6589 6590## Parcelable<sup>9+</sup> 6591 6592在进程间通信(IPC)期间,将类的对象写入MessageSequence并从MessageSequence中恢复它们。 6593 6594### marshalling<sup>9+</sup> 6595 6596marshalling(dataOut: MessageSequence): boolean 6597 6598将此可序列对象封送到MessageSequence中。 6599 6600**系统能力**:SystemCapability.Communication.IPC.Core 6601 6602**参数:** 6603 6604| 参数名 | 类型 | 必填 | 说明 | 6605| ------- | --------------- | ---- | ------------------------------------------- | 6606| dataOut |[MessageSequence](#messagesequence9)| 是 | 可序列对象将被封送到的MessageSequence对象。 | 6607 6608**返回值:** 6609 6610 | 类型 | 说明 | 6611 | ------- | -------------------------------- | 6612 | boolean | true:封送成功,false:封送失败。| 6613 6614**示例:** 6615 6616 ```ts 6617 import { rpc } from '@kit.IPCKit'; 6618 import { hilog } from '@kit.PerformanceAnalysisKit'; 6619 6620 class MyParcelable implements rpc.Parcelable { 6621 num: number = 0; 6622 str: string = ''; 6623 constructor(num: number, str: string) { 6624 this.num = num; 6625 this.str = str; 6626 } 6627 marshalling(messageSequence: rpc.MessageSequence): boolean { 6628 messageSequence.writeInt(this.num); 6629 messageSequence.writeString(this.str); 6630 return true; 6631 } 6632 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6633 this.num = messageSequence.readInt(); 6634 this.str = messageSequence.readString(); 6635 hilog.info(0x0000, 'testTag', 'readInt is ' + this.num + ' readString is ' + this.str); 6636 return true; 6637 } 6638 } 6639 6640 try { 6641 let parcelable = new MyParcelable(1, "aaa"); 6642 let data = rpc.MessageSequence.create(); 6643 data.writeParcelable(parcelable); 6644 let ret = new MyParcelable(0, ""); 6645 data.readParcelable(ret); 6646 } catch (error) { 6647 hilog.error(0x0000, 'testTag', 'error ' + error); 6648 } 6649 ``` 6650 6651### unmarshalling<sup>9+</sup> 6652 6653unmarshalling(dataIn: MessageSequence): boolean 6654 6655从MessageSequence中解封此可序列对象。 6656 6657**系统能力**:SystemCapability.Communication.IPC.Core 6658 6659**参数:** 6660 6661| 参数名 | 类型 | 必填 | 说明 | 6662| ------ | --------------- | ---- | ----------------------------------------------- | 6663| dataIn | [MessageSequence](#messagesequence9) | 是 | 已将可序列对象封送到其中的MessageSequence对象。 | 6664 6665**返回值:** 6666 6667 | 类型 | 说明 | 6668 | ------- | ---------------------------------------- | 6669 | boolean | true:反序列化成功,false:反序列化失败。| 6670 6671**示例:** 6672 6673 ```ts 6674 import { rpc } from '@kit.IPCKit'; 6675 import { hilog } from '@kit.PerformanceAnalysisKit'; 6676 6677 class MyParcelable implements rpc.Parcelable { 6678 num: number = 0; 6679 str: string = ''; 6680 constructor(num: number, str: string) { 6681 this.num = num; 6682 this.str = str; 6683 } 6684 marshalling(messageSequence: rpc.MessageSequence): boolean { 6685 messageSequence.writeInt(this.num); 6686 messageSequence.writeString(this.str); 6687 return true; 6688 } 6689 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6690 this.num = messageSequence.readInt(); 6691 this.str = messageSequence.readString(); 6692 hilog.info(0x0000, 'testTag', 'readInt is ' + this.num + ' readString is ' + this.str); 6693 return true; 6694 } 6695 } 6696 6697 try { 6698 let parcelable = new MyParcelable(1, "aaa"); 6699 let data = rpc.MessageSequence.create(); 6700 data.writeParcelable(parcelable); 6701 let ret = new MyParcelable(0, ""); 6702 data.readParcelable(ret); 6703 } catch (error) { 6704 hilog.error(0x0000, 'testTag', 'error ' + error); 6705 } 6706 ``` 6707 6708## Sequenceable<sup>(deprecated)</sup> 6709 6710在进程间通信(IPC)期间,将类的对象写入MessageParcel并从MessageParcel中恢复它们。 6711 6712> **说明:** 6713> 6714> 从API version 9 开始废弃,建议使用[Parcelable](#parcelable9)替代。 6715 6716### marshalling 6717 6718marshalling(dataOut: MessageParcel): boolean 6719 6720将此可序列对象封送到MessageParcel中。 6721 6722**系统能力**:SystemCapability.Communication.IPC.Core 6723 6724**参数:** 6725 6726 | 参数名 | 类型 | 必填 | 说明 | 6727 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6728 | dataOut | [MessageParcel](#messageparceldeprecated) | 是 | 可序列对象将被封送到的MessageParcel对象。 | 6729 6730**返回值:** 6731 6732 | 类型 | 说明 | 6733 | ------- | -------------------------------- | 6734 | boolean | true:封送成功,false:封送失败。 | 6735 6736**示例:** 6737<!--deprecated_code_no_check--> 6738 ```ts 6739 import { rpc } from '@kit.IPCKit'; 6740 import { hilog } from '@kit.PerformanceAnalysisKit'; 6741 6742 class MySequenceable implements rpc.Sequenceable { 6743 num: number = 0; 6744 str: string = ''; 6745 constructor(num: number, str: string) { 6746 this.num = num; 6747 this.str = str; 6748 } 6749 marshalling(messageParcel: rpc.MessageParcel): boolean { 6750 messageParcel.writeInt(this.num); 6751 messageParcel.writeString(this.str); 6752 return true; 6753 } 6754 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6755 this.num = messageParcel.readInt(); 6756 this.str = messageParcel.readString(); 6757 return true; 6758 } 6759 } 6760 6761 try { 6762 let sequenceable = new MySequenceable(1, "aaa"); 6763 let data = rpc.MessageParcel.create(); 6764 let result = data.writeSequenceable(sequenceable); 6765 hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result); 6766 let ret = new MySequenceable(0, ""); 6767 let result2 = data.readSequenceable(ret); 6768 hilog.info(0x0000, 'testTag', 'readSequenceable is ' + result2); 6769 } catch (error) { 6770 hilog.error(0x0000, 'testTag', 'error ' + error); 6771 } 6772 ``` 6773 6774### unmarshalling 6775 6776unmarshalling(dataIn: MessageParcel): boolean 6777 6778从MessageParcel中解封此可序列对象。 6779 6780**系统能力**:SystemCapability.Communication.IPC.Core 6781 6782**参数:** 6783 6784 | 参数名 | 类型 | 必填 | 说明 | 6785 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6786 | dataIn | [MessageParcel](#messageparceldeprecated) | 是 | 已将可序列对象封送到其中的MessageParcel对象。 | 6787 6788**返回值:** 6789 6790 | 类型 | 说明 | 6791 | ------- | ---------------------------------------- | 6792 | boolean | true:反序列化成功,false:反序列化失败。| 6793 6794**示例:** 6795<!--deprecated_code_no_check--> 6796 ```ts 6797 import { rpc } from '@kit.IPCKit'; 6798 import { hilog } from '@kit.PerformanceAnalysisKit'; 6799 6800 class MySequenceable implements rpc.Sequenceable { 6801 num: number = 0; 6802 str: string = ''; 6803 constructor(num: number, str: string) { 6804 this.num = num; 6805 this.str = str; 6806 } 6807 marshalling(messageParcel: rpc.MessageParcel): boolean { 6808 messageParcel.writeInt(this.num); 6809 messageParcel.writeString(this.str); 6810 return true; 6811 } 6812 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6813 this.num = messageParcel.readInt(); 6814 this.str = messageParcel.readString(); 6815 return true; 6816 } 6817 } 6818 6819 try { 6820 let sequenceable = new MySequenceable(1, "aaa"); 6821 let data = rpc.MessageParcel.create(); 6822 let result = data.writeSequenceable(sequenceable); 6823 hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result); 6824 let ret = new MySequenceable(0, ""); 6825 let result2 = data.readSequenceable(ret); 6826 hilog.info(0x0000, 'testTag', 'readSequenceable is ' + result2); 6827 } catch (error) { 6828 hilog.error(0x0000, 'testTag', 'error ' + error); 6829 } 6830 ``` 6831 6832## IRemoteBroker 6833 6834远端对象的代理持有者。用于获取代理对象。 6835 6836### asObject 6837 6838asObject(): IRemoteObject 6839 6840需派生类实现,获取代理或远端对象。 6841 6842**系统能力**:SystemCapability.Communication.IPC.Core 6843 6844**返回值:** 6845 6846 | 类型 | 说明 | 6847 | ----- | ----- | 6848 | [IRemoteObject](#iremoteobject) | 如果调用者是RemoteObject对象,则直接返回本身;如果调用者是[RemoteProxy](#remoteproxy)对象,则返回它的持有者[IRemoteObject](#iremoteobject)。 | 6849 6850**示例:** 6851 6852 ```ts 6853 import { rpc } from '@kit.IPCKit'; 6854 6855 class TestAbility extends rpc.RemoteObject { 6856 asObject() { 6857 return this; 6858 } 6859 } 6860 let remoteObject = new TestAbility("testObject").asObject(); 6861 ``` 6862 6863**示例:** 6864 6865>**说明:** 6866> 6867>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 6868 6869 <!--code_no_check--> 6870 ```ts 6871 // FA模型需要从@kit.AbilityKit导入featureAbility 6872 // import { featureAbility } from '@kit.AbilityKit'; 6873 import { rpc } from '@kit.IPCKit'; 6874 import { Want, common } from '@kit.AbilityKit'; 6875 import { hilog } from '@kit.PerformanceAnalysisKit'; 6876 6877 let proxy: rpc.IRemoteObject | undefined; 6878 let connect: common.ConnectOptions = { 6879 onConnect: (elementName, remoteProxy) => { 6880 hilog.info(0x0000, 'testTag', 'js onConnect called'); 6881 proxy = remoteProxy; 6882 }, 6883 onDisconnect: (elementName) => { 6884 hilog.info(0x0000, 'testTag', 'onDisconnect'); 6885 }, 6886 onFailed: () => { 6887 hilog.info(0x0000, 'testTag', 'onFailed'); 6888 } 6889 }; 6890 let want: Want = { 6891 bundleName: "com.ohos.server", 6892 abilityName: "com.ohos.server.EntryAbility", 6893 }; 6894 6895 // FA模型使用此方法连接服务 6896 // FA.connectAbility(want,connect); 6897 6898 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6899 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 6900 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 6901 let connectionId = context.connectServiceExtensionAbility(want, connect); 6902 ``` 6903 6904 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象 6905 6906 ```ts 6907 import { rpc } from '@kit.IPCKit'; 6908 6909 class TestProxy { 6910 remote: rpc.IRemoteObject; 6911 constructor(remote: rpc.IRemoteObject) { 6912 this.remote = remote; 6913 } 6914 asObject() { 6915 return this.remote; 6916 } 6917 } 6918 if (proxy != undefined) { 6919 let iRemoteObject = new TestProxy(proxy).asObject(); 6920 } 6921 ``` 6922 6923## DeathRecipient 6924 6925用于订阅远端对象的死亡通知。当被订阅该通知的远端对象死亡时,本端可收到消息,调用[onRemoteDied](#onremotedied)接口。远端对象死亡可以为远端对象所在进程死亡,远端对象所在设备关机或重启,当远端对象与本端对象属于不同设备时,也可为远端对象离开组网时。 6926 6927### onRemoteDied 6928 6929onRemoteDied(): void 6930 6931在成功添加死亡通知订阅后,当远端对象死亡时,将自动调用本方法。 6932 6933**系统能力**:SystemCapability.Communication.IPC.Core 6934 6935**示例:** 6936 6937 ```ts 6938 import { rpc } from '@kit.IPCKit'; 6939 import { hilog } from '@kit.PerformanceAnalysisKit'; 6940 6941 class MyDeathRecipient implements rpc.DeathRecipient { 6942 onRemoteDied() { 6943 hilog.info(0x0000, 'testTag', 'server died'); 6944 } 6945 } 6946 ``` 6947 6948## RequestResult<sup>9+</sup> 6949 6950发送请求的响应结果。 6951 6952**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6953 6954| 名称 | 类型 | 可读 | 可写 | 说明 | 6955| ------- | --------------- | ---- | ---- |-------------------------------------- | 6956| errCode | number | 是 | 否 | 错误码。 | 6957| code | number | 是 | 否 | 消息代码。 | 6958| data | [MessageSequence](#messagesequence9) | 是 | 否 | 发送给对端进程的MessageSequence对象。 | 6959| reply | [MessageSequence](#messagesequence9) | 是 | 否 | 对端进程返回的MessageSequence对象。 | 6960 6961## SendRequestResult<sup>(deprecated)</sup> 6962 6963发送请求的响应结果。 6964 6965> **说明:** 6966> 6967> 从API version 8 开始支持,API version 9 开始废弃,建议使用[RequestResult](#requestresult9)替代。 6968 6969**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 6970 6971 | 名称 | 类型 | 可读 | 可写 | 说明 | 6972 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6973 | errCode | number | 是 | 否 | 错误码。 | 6974 | code | number | 是 | 否 | 消息代码。 | 6975 | data | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 发送给对端进程的MessageParcel对象。 | 6976 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 否 | 对端进程返回的MessageParcel对象。 | 6977 6978## IRemoteObject 6979 6980该接口可用于查询或获取接口描述符、添加或删除死亡通知、转储对象状态到特定文件、发送消息。 6981 6982### getLocalInterface<sup>9+</sup> 6983 6984getLocalInterface(descriptor: string): IRemoteBroker 6985 6986查询接口描述符的字符串。 6987 6988**系统能力**:SystemCapability.Communication.IPC.Core 6989 6990**参数:** 6991 6992 | 参数名 | 类型 | 必填 | 说明 | 6993 | ---------- | ------ | ---- | -------------------- | 6994 | descriptor | string | 是 | 接口描述符的字符串。 | 6995 6996**返回值:** 6997 6998| 类型 | 说明 | 6999| ------------- | --------------------------------------------- | 7000| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 7001 7002**错误码:** 7003 7004以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7005 7006 | 错误码ID | 错误信息 | 7007 | -------- | -------- | 7008 | 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. | 7009 7010### queryLocalInterface<sup>(deprecated)</sup> 7011 7012queryLocalInterface(descriptor: string): IRemoteBroker 7013 7014查询接口描述符的字符串。 7015 7016> **说明:** 7017> 7018> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9)替代。 7019 7020**系统能力**:SystemCapability.Communication.IPC.Core 7021 7022**参数:** 7023 7024 | 参数名 | 类型 | 必填 | 说明 | 7025 | ---------- | ------ | ---- | -------------------- | 7026 | descriptor | string | 是 | 接口描述符的字符串。 | 7027 7028**返回值:** 7029 7030| 类型 | 说明 | 7031| ------------- | --------------------------------------------- | 7032| [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 7033 7034### sendRequest<sup>(deprecated)</sup> 7035 7036sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7037 7038以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7039 7040> **说明:** 7041> 7042> 从API version 9开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 7043 7044**系统能力**:SystemCapability.Communication.IPC.Core 7045 7046**参数:** 7047 7048 | 参数名 | 类型 | 必填 | 说明 | 7049 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7050 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7051 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7052 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7053 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7054 7055**返回值:** 7056 7057 | 类型 | 说明 | 7058 | ------- | -------------------------------- | 7059 | boolean | true:发送成功,false:发送失败。| 7060 7061### sendMessageRequest<sup>9+</sup> 7062 7063sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7064 7065以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 7066 7067**系统能力**:SystemCapability.Communication.IPC.Core 7068 7069**参数:** 7070 7071 | 参数名 | 类型 | 必填 | 说明 | 7072 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7073 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7074 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7075 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7076 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7077 7078**返回值:** 7079 7080 | 类型 | 说明 | 7081 | ---------------------------- | ----------------------------------------- | 7082 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 7083 7084**错误码:** 7085 7086以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7087 7088 | 错误码ID | 错误信息 | 7089 | -------- | -------- | 7090 | 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. | 7091 7092### sendRequest<sup>(deprecated)</sup> 7093 7094sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7095 7096以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7097 7098> **说明:** 7099> 7100> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9)替代。 7101 7102**系统能力**:SystemCapability.Communication.IPC.Core 7103 7104**参数:** 7105 7106 | 参数名 | 类型 | 必填 | 说明 | 7107 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7108 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7109 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7110 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7111 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7112 7113**返回值:** 7114 7115| 类型 | 说明 | 7116| ------------------------------------------------------------ | --------------------------------------------- | 7117| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 7118 7119### sendMessageRequest<sup>9+</sup> 7120 7121sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7122 7123以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 7124 7125**系统能力**:SystemCapability.Communication.IPC.Core 7126 7127**参数:** 7128 7129 | 参数名 | 类型 | 必填 | 说明 | 7130 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7131 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7132 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7133 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7134 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7135 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 7136 7137**错误码:** 7138 7139以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7140 7141 | 错误码ID | 错误信息 | 7142 | -------- | -------- | 7143 | 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. | 7144 7145### sendRequest<sup>(deprecated)</sup> 7146 7147sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7148 7149以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 7150 7151> **说明:** 7152> 7153> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-1)替代。 7154 7155**系统能力**:SystemCapability.Communication.IPC.Core 7156 7157**参数:** 7158 7159| 参数名 | 类型 | 必填 | 说明 | 7160| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7161| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7162| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7163| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7164| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7165| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 7166 7167### registerDeathRecipient<sup>9+</sup> 7168 7169registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7170 7171注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7172 7173**系统能力**:SystemCapability.Communication.IPC.Core 7174 7175**参数:** 7176 7177 | 参数名 | 类型 | 必填 | 说明 | 7178 | --------- | --------------------------------- | ---- | -------------- | 7179 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7180 | flags | number | 是 | 死亡通知标志。 | 7181 7182**错误码:** 7183 7184以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7185 7186 | 错误码ID | 错误信息 | 7187 | -------- | -------- | 7188 | 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. | 7189 | 1900005 | Operation allowed only for the proxy object. | 7190 | 1900008 | The proxy or remote object is invalid. | 7191 7192### addDeathRecipient<sup>(deprecated)</sup> 7193 7194addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7195 7196注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7197 7198> **说明:** 7199> 7200> 从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9)替代。 7201 7202**系统能力**:SystemCapability.Communication.IPC.Core 7203 7204**参数:** 7205 7206 | 参数名 | 类型 | 必填 | 说明 | 7207 | --------- | --------------------------------- | ---- | -------------- | 7208 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7209 | flags | number | 是 | 死亡通知标志。 | 7210 7211**返回值:** 7212 7213 | 类型 | 说明 | 7214 | ------- | ---------------------------------------- | 7215 | boolean | true:回调注册成功,false:回调注册失败。| 7216 7217### unregisterDeathRecipient<sup>9+</sup> 7218 7219unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7220 7221注销用于接收远程对象死亡通知的回调。 7222 7223**系统能力**:SystemCapability.Communication.IPC.Core 7224 7225**参数:** 7226 7227 | 参数名 | 类型 | 必填 | 说明 | 7228 | --------- | --------------------------------- | ---- | -------------- | 7229 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 7230 | flags | number | 是 | 死亡通知标志。 | 7231 7232**错误码:** 7233 7234以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7235 7236 | 错误码ID | 错误信息 | 7237 | -------- | -------- | 7238 | 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. | 7239 | 1900005 | Operation allowed only for the proxy object. | 7240 | 1900008 | The proxy or remote object is invalid. | 7241 7242### removeDeathRecipient<sup>(deprecated)</sup> 7243 7244removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7245 7246注销用于接收远程对象死亡通知的回调。 7247 7248> **说明:** 7249> 7250> 从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9)替代。 7251 7252**系统能力**:SystemCapability.Communication.IPC.Core 7253 7254**参数:** 7255 7256 | 参数名 | 类型 | 必填 | 说明 | 7257 | --------- | --------------------------------- | ---- | -------------- | 7258 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 7259 | flags | number | 是 | 死亡通知标志。 | 7260 7261**返回值:** 7262 7263 | 类型 | 说明 | 7264 | ------- | -----------------------------------------| 7265 | boolean | true:回调注销成功,false:回调注销失败。| 7266 7267### getDescriptor<sup>9+</sup> 7268 7269getDescriptor(): string 7270 7271获取对象的接口描述符,接口描述符为字符串。 7272 7273**系统能力**:SystemCapability.Communication.IPC.Core 7274 7275**返回值:** 7276 7277 | 类型 | 说明 | 7278 | ------ | ---------------- | 7279 | string | 返回接口描述符。 | 7280 7281**错误码:** 7282 7283以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7284 7285 | 错误码ID | 错误信息 | 7286 | -------- | -------- | 7287 | 1900008 | The proxy or remote object is invalid. | 7288 7289### getInterfaceDescriptor<sup>(deprecated)</sup> 7290 7291getInterfaceDescriptor(): string 7292 7293获取对象的接口描述符,接口描述符为字符串。 7294 7295> **说明:** 7296> 7297> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9)替代。 7298 7299**系统能力**:SystemCapability.Communication.IPC.Core 7300 7301**返回值:** 7302 7303 | 类型 | 说明 | 7304 | ------ | ---------------- | 7305 | string | 返回接口描述符。 | 7306 7307### isObjectDead 7308 7309isObjectDead(): boolean 7310 7311检查当前对象是否死亡。 7312 7313**系统能力**:SystemCapability.Communication.IPC.Core 7314 7315**返回值:** 7316 7317 | 类型 | 说明 | 7318 | ------- | ---------------------------------- | 7319 | boolean | true:对象死亡,false:对象未死亡。| 7320 7321## RemoteProxy 7322 7323实现IRemoteObject代理对象。 7324 7325### 属性 7326 7327**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 7328 7329 | 名称 | 类型 | 可读 | 可写 | 说明 | 7330 | --------------------- | -------| ------|------|------------------------------------------ | 7331 | PING_TRANSACTION | number | 是 | 否 | 内部指令码,用于测试IPC服务是否正常。 | 7332 | DUMP_TRANSACTION | number | 是 | 否 | 内部指令码,获取IPC服务相关的状态信息。 | 7333 | INTERFACE_TRANSACTION | number | 是 | 否 | 内部指令码,获取对端接口描述符。 | 7334 | MIN_TRANSACTION_ID | number | 是 | 否 | 最小有效指令码。 | 7335 | MAX_TRANSACTION_ID | number | 是 | 否 | 最大有效指令码。 | 7336 7337 7338### sendRequest<sup>(deprecated)</sup> 7339 7340sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7341 7342以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7343 7344> **说明:** 7345> 7346> 从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 7347 7348**系统能力**:SystemCapability.Communication.IPC.Core 7349 7350**参数:** 7351 7352 | 参数名 | 类型 | 必填 | 说明 | 7353 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7354 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7355 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7356 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7357 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7358 7359**返回值:** 7360 7361 | 类型 | 说明 | 7362 | ------- | ---------------------------------| 7363 | boolean | true:发送成功,false:发送失败。| 7364 7365**示例:** 7366 7367>**说明:** 7368> 7369>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7370 7371 <!--code_no_check--> 7372 ```ts 7373 // FA模型需要从@kit.AbilityKit导入featureAbility 7374 // import { featureAbility } from '@kit.AbilityKit'; 7375 import { rpc } from '@kit.IPCKit'; 7376 import { Want, common } from '@kit.AbilityKit'; 7377 import { hilog } from '@kit.PerformanceAnalysisKit'; 7378 7379 let proxy: rpc.IRemoteObject | undefined; 7380 let connect: common.ConnectOptions = { 7381 onConnect: (elementName, remoteProxy) => { 7382 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7383 proxy = remoteProxy; 7384 }, 7385 onDisconnect: (elementName) => { 7386 hilog.info(0x0000, 'testTag', 'onDisconnect'); 7387 }, 7388 onFailed: () => { 7389 hilog.info(0x0000, 'testTag', 'onFailed'); 7390 } 7391 }; 7392 let want: Want = { 7393 bundleName: "com.ohos.server", 7394 abilityName: "com.ohos.server.EntryAbility", 7395 }; 7396 7397 // FA模型使用此方法连接服务 7398 // FA.connectAbility(want,connect); 7399 7400 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7401 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7402 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7403 let connectionId = context.connectServiceExtensionAbility(want, connect); 7404 ``` 7405 7406 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7407<!--deprecated_code_no_check--> 7408 ```ts 7409 import { rpc } from '@kit.IPCKit'; 7410 import { hilog } from '@kit.PerformanceAnalysisKit'; 7411 7412 try { 7413 let option = new rpc.MessageOption(); 7414 let data = rpc.MessageParcel.create(); 7415 let reply = rpc.MessageParcel.create(); 7416 data.writeInt(1); 7417 data.writeString("hello"); 7418 if (proxy != undefined) { 7419 let ret: boolean = proxy.sendRequest(1, data, reply, option); 7420 if (ret) { 7421 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7422 let msg = reply.readString(); 7423 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 7424 } else { 7425 hilog.error(0x0000, 'testTag', 'sendRequest failed'); 7426 } 7427 hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel'); 7428 data.reclaim(); 7429 reply.reclaim(); 7430 } 7431 } catch (error) { 7432 hilog.error(0x0000, 'testTag', 'error: ' + error); 7433 } 7434 ``` 7435 7436### sendMessageRequest<sup>9+</sup> 7437 7438sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7439 7440以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 7441 7442**系统能力**:SystemCapability.Communication.IPC.Core 7443 7444**参数:** 7445 7446 | 参数名 | 类型 | 必填 | 说明 | 7447 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7448 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7449 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7450 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7451 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7452 7453**返回值:** 7454 7455 | 类型 | 说明 | 7456 | ---------------------------- | ----------------------------------------- | 7457 | Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是requestResult实例。 | 7458 7459 7460**错误码:** 7461 7462以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7463 7464 | 错误码ID | 错误信息 | 7465 | -------- | -------- | 7466 | 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. | 7467 7468**示例:** 7469 7470>**说明:** 7471> 7472>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7473 7474 <!--code_no_check--> 7475 ```ts 7476 // FA模型需要从@kit.AbilityKit导入featureAbility 7477 // import { featureAbility } from '@kit.AbilityKit'; 7478 import { rpc } from '@kit.IPCKit'; 7479 import { Want, common } from '@kit.AbilityKit'; 7480 import { hilog } from '@kit.PerformanceAnalysisKit'; 7481 7482 let proxy: rpc.IRemoteObject | undefined; 7483 let connect: common.ConnectOptions = { 7484 onConnect: (elementName, remoteProxy) => { 7485 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7486 proxy = remoteProxy; 7487 }, 7488 onDisconnect: (elementName) => { 7489 hilog.info(0x0000, 'testTag', 'onDisconnect'); 7490 }, 7491 onFailed: () => { 7492 hilog.info(0x0000, 'testTag', 'onFailed'); 7493 } 7494 }; 7495 let want: Want = { 7496 bundleName: "com.ohos.server", 7497 abilityName: "com.ohos.server.EntryAbility", 7498 }; 7499 7500 // FA模型使用此方法连接服务 7501 // FA.connectAbility(want,connect); 7502 7503 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7504 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7505 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7506 let connectionId = context.connectServiceExtensionAbility(want, connect); 7507 ``` 7508 7509 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 7510 7511 ```ts 7512 import { rpc } from '@kit.IPCKit'; 7513 import { hilog } from '@kit.PerformanceAnalysisKit'; 7514 7515 try { 7516 let option = new rpc.MessageOption(); 7517 let data = rpc.MessageSequence.create(); 7518 let reply = rpc.MessageSequence.create(); 7519 data.writeInt(1); 7520 data.writeString("hello"); 7521 if (proxy != undefined) { 7522 proxy.sendMessageRequest(1, data, reply, option) 7523 .then((result: rpc.RequestResult) => { 7524 if (result.errCode === 0) { 7525 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7526 let num = result.reply.readInt(); 7527 let msg = result.reply.readString(); 7528 hilog.info(0x0000, 'testTag', 'reply num: ' + num); 7529 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 7530 } else { 7531 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode: ' + result.errCode); 7532 } 7533 }).catch((e: Error) => { 7534 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + e); 7535 }).finally (() => { 7536 hilog.info(0x0000, 'testTag', 'sendMessageRequest ends, reclaim parcel'); 7537 data.reclaim(); 7538 reply.reclaim(); 7539 }); 7540 } 7541 } catch (error) { 7542 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + error); 7543 } 7544 ``` 7545 7546### sendRequest<sup>(deprecated)</sup> 7547 7548sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7549 7550以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 7551 7552> **说明:** 7553> 7554> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-2)替代。 7555 7556**系统能力**:SystemCapability.Communication.IPC.Core 7557 7558**参数:** 7559 7560 | 参数名 | 类型 | 必填 | 说明 | 7561 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7562 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7563 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7564 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7565 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7566 7567**返回值:** 7568 7569| 类型 | 说明 | 7570| ------------------------------------------------------------ | --------------------------------------------- | 7571| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 7572 7573**示例:** 7574 7575>**说明:** 7576> 7577>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7578 7579 <!--code_no_check--> 7580 ```ts 7581 // FA模型需要从@kit.AbilityKit导入featureAbility 7582 // import { featureAbility } from '@kit.AbilityKit'; 7583 import { rpc } from '@kit.IPCKit'; 7584 import { Want, common } from '@kit.AbilityKit'; 7585 import { hilog } from '@kit.PerformanceAnalysisKit'; 7586 7587 let proxy: rpc.IRemoteObject | undefined; 7588 let connect: common.ConnectOptions = { 7589 onConnect: (elementName, remoteProxy) => { 7590 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7591 proxy = remoteProxy; 7592 }, 7593 onDisconnect: (elementName) => { 7594 hilog.info(0x0000, 'testTag', 'onDisconnect'); 7595 }, 7596 onFailed: () => { 7597 hilog.info(0x0000, 'testTag', 'onFailed'); 7598 } 7599 }; 7600 let want: Want = { 7601 bundleName: "com.ohos.server", 7602 abilityName: "com.ohos.server.EntryAbility", 7603 }; 7604 7605 // FA模型使用此方法连接服务 7606 // FA.connectAbility(want,connect); 7607 7608 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7609 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7610 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7611 let connectionId = context.connectServiceExtensionAbility(want, connect); 7612 ``` 7613 7614 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 7615<!--deprecated_code_no_check--> 7616 ```ts 7617 import { rpc } from '@kit.IPCKit'; 7618 import { hilog } from '@kit.PerformanceAnalysisKit'; 7619 7620 try { 7621 let option = new rpc.MessageOption(); 7622 let data = rpc.MessageParcel.create(); 7623 let reply = rpc.MessageParcel.create(); 7624 data.writeInt(1); 7625 data.writeString("hello"); 7626 if (proxy != undefined) { 7627 let a = proxy.sendRequest(1, data, reply, option) as Object; 7628 let b = a as Promise<rpc.SendRequestResult>; 7629 b.then((result: rpc.SendRequestResult) => { 7630 if (result.errCode === 0) { 7631 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7632 let num = result.reply.readInt(); 7633 let msg = result.reply.readString(); 7634 hilog.info(0x0000, 'testTag', 'reply num: ' + num); 7635 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 7636 } else { 7637 hilog.error(0x0000, 'testTag', 'sendRequest failed, errCode: ' + result.errCode); 7638 } 7639 }).catch((e: Error) => { 7640 hilog.error(0x0000, 'testTag', 'sendRequest failed, error: ' + e); 7641 }).finally (() => { 7642 hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel'); 7643 data.reclaim(); 7644 reply.reclaim(); 7645 }); 7646 } 7647 } catch (error) { 7648 hilog.error(0x0000, 'testTag', 'sendRequest failed, error: ' + error); 7649 } 7650 ``` 7651 7652### sendMessageRequest<sup>9+</sup> 7653 7654sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7655 7656以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回后的某个时机执行回调,回复内容在RequestResult的reply报文里。 7657 7658**系统能力**:SystemCapability.Communication.IPC.Core 7659 7660**参数:** 7661 7662 | 参数名 | 类型 | 必填 | 说明 | 7663 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7664 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7665 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 7666 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 7667 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7668 | callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 7669 7670 7671**错误码:** 7672 7673以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7674 7675 | 错误码ID | 错误信息 | 7676 | -------- | -------- | 7677 | 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. | 7678 7679### sendRequest<sup>(deprecated)</sup> 7680 7681sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7682 7683以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 7684 7685> **说明:** 7686> 7687> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-3)替代。 7688 7689**系统能力**:SystemCapability.Communication.IPC.Core 7690 7691**参数:** 7692 7693| 参数名 | 类型 | 必填 | 说明 | 7694| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7695| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 7696| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 7697| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 7698| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 7699| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 7700 7701### getLocalInterface<sup>9+</sup> 7702 7703getLocalInterface(interface: string): IRemoteBroker 7704 7705查询并获取当前接口描述符对应的本地接口对象。 7706 7707**系统能力**:SystemCapability.Communication.IPC.Core 7708 7709**参数:** 7710 7711 | 参数名 | 类型 | 必填 | 说明 | 7712 | --------- | ------ | ---- | ---------------------- | 7713 | interface | string | 是 | 需要查询的接口描述符。 | 7714 7715**返回值:** 7716 7717| 类型 | 说明 | 7718| ------------------------------- | ------------------------------------------ | 7719| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7720 7721**错误码:** 7722 7723以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7724 7725 | 错误码ID | 错误信息 | 7726 | -------- | -------- | 7727 | 401 | check param failed | 7728 | 1900006 | Operation allowed only for the remote object. | 7729 7730**示例:** 7731 7732>**说明:** 7733> 7734>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7735 7736 <!--code_no_check--> 7737 ```ts 7738 // FA模型需要从@kit.AbilityKit导入featureAbility 7739 // import { featureAbility } from '@kit.AbilityKit'; 7740 import { rpc } from '@kit.IPCKit'; 7741 import { Want, common } from '@kit.AbilityKit'; 7742 import { hilog } from '@kit.PerformanceAnalysisKit'; 7743 7744 let proxy: rpc.IRemoteObject | undefined; 7745 let connect: common.ConnectOptions = { 7746 onConnect: (elementName, remoteProxy) => { 7747 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7748 proxy = remoteProxy; 7749 }, 7750 onDisconnect: (elementName) => { 7751 hilog.info(0x0000, 'testTag', 'onDisconnect'); 7752 }, 7753 onFailed: () => { 7754 hilog.info(0x0000, 'testTag', 'onFailed'); 7755 } 7756 }; 7757 let want: Want = { 7758 bundleName: "com.ohos.server", 7759 abilityName: "com.ohos.server.EntryAbility", 7760 }; 7761 7762 // FA模型使用此方法连接服务 7763 // FA.connectAbility(want,connect); 7764 7765 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7766 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7767 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7768 let connectionId = context.connectServiceExtensionAbility(want, connect); 7769 ``` 7770 7771 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象 7772<!--deprecated_code_no_check--> 7773 ```ts 7774 import { rpc } from '@kit.IPCKit'; 7775 import { hilog } from '@kit.PerformanceAnalysisKit'; 7776 import { BusinessError } from '@kit.BasicServicesKit'; 7777 7778 if (proxy != undefined) { 7779 try { 7780 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7781 hilog.info(0x0000, 'testTag', 'getLocalInterface is ' + broker); 7782 } catch (error) { 7783 let e: BusinessError = error as BusinessError; 7784 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7785 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7786 } 7787 } 7788 ``` 7789 7790### queryLocalInterface<sup>(deprecated)</sup> 7791 7792queryLocalInterface(interface: string): IRemoteBroker 7793 7794查询并获取当前接口描述符对应的本地接口对象。 7795 7796> **说明:** 7797> 7798> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-1)替代。 7799 7800**系统能力**:SystemCapability.Communication.IPC.Core 7801 7802**参数:** 7803 7804 | 参数名 | 类型 | 必填 | 说明 | 7805 | --------- | ------ | ---- | ---------------------- | 7806 | interface | string | 是 | 需要查询的接口描述符。 | 7807 7808**返回值:** 7809 7810| 类型 | 说明 | 7811| ------------------------------- | ------------------------------------------ | 7812| [IRemoteBroker](#iremotebroker) | 默认返回Null,标识该接口是一个代理侧接口。 | 7813 7814**示例:** 7815 7816>**说明:** 7817> 7818>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7819 7820 <!--code_no_check--> 7821 ```ts 7822 // FA模型需要从@kit.AbilityKit导入featureAbility 7823 // import { featureAbility } from '@kit.AbilityKit'; 7824 import { rpc } from '@kit.IPCKit'; 7825 import { Want, common } from '@kit.AbilityKit'; 7826 import { hilog } from '@kit.PerformanceAnalysisKit'; 7827 7828 let proxy: rpc.IRemoteObject | undefined; 7829 let connect: common.ConnectOptions = { 7830 onConnect: (elementName, remoteProxy) => { 7831 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7832 proxy = remoteProxy; 7833 }, 7834 onDisconnect: (elementName) => { 7835 hilog.info(0x0000, 'testTag', 'onDisconnect'); 7836 }, 7837 onFailed: () => { 7838 hilog.info(0x0000, 'testTag', 'onFailed'); 7839 } 7840 }; 7841 let want: Want = { 7842 bundleName: "com.ohos.server", 7843 abilityName: "com.ohos.server.EntryAbility", 7844 }; 7845 7846 // FA模型使用此方法连接服务 7847 // FA.connectAbility(want,connect); 7848 7849 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7850 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7851 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7852 let connectionId = context.connectServiceExtensionAbility(want, connect); 7853 ``` 7854 7855 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象 7856<!--deprecated_code_no_check--> 7857 ```ts 7858 import { rpc } from '@kit.IPCKit'; 7859 import { hilog } from '@kit.PerformanceAnalysisKit'; 7860 7861 if (proxy != undefined) { 7862 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7863 hilog.info(0x0000, 'testTag', 'queryLocalInterface is ' + broker); 7864 } 7865 ``` 7866 7867### registerDeathRecipient<sup>9+</sup> 7868 7869registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7870 7871注册用于接收远程对象死亡通知的回调。如果与RemoteProxy对象匹配的远程对象进程死亡,则调用此方法。 7872 7873**系统能力**:SystemCapability.Communication.IPC.Core 7874 7875**参数:** 7876 7877 | 参数名 | 类型 | 必填 | 说明 | 7878 | --------- | --------------------------------- | ---- | -------------- | 7879 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注册的回调。 | 7880 | flags | number | 是 | 死亡通知标志。 | 7881 7882**错误码:** 7883 7884以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 7885 7886 | 错误码ID | 错误信息 | 7887 | -------- | -------- | 7888 | 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. | 7889 | 1900008 | The proxy or remote object is invalid. | 7890 7891**示例:** 7892 7893>**说明:** 7894> 7895>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7896 7897 <!--code_no_check--> 7898 ```ts 7899 // FA模型需要从@kit.AbilityKit导入featureAbility 7900 // import { featureAbility } from '@kit.AbilityKit'; 7901 import { rpc } from '@kit.IPCKit'; 7902 import { Want, common } from '@kit.AbilityKit'; 7903 import { hilog } from '@kit.PerformanceAnalysisKit'; 7904 7905 let proxy: rpc.IRemoteObject | undefined; 7906 let connect: common.ConnectOptions = { 7907 onConnect: (elementName, remoteProxy) => { 7908 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7909 proxy = remoteProxy; 7910 }, 7911 onDisconnect: (elementName) => { 7912 hilog.info(0x0000, 'testTag', 'onDisconnect'); 7913 }, 7914 onFailed: () => { 7915 hilog.info(0x0000, 'testTag', 'onFailed'); 7916 } 7917 }; 7918 let want: Want = { 7919 bundleName: "com.ohos.server", 7920 abilityName: "com.ohos.server.EntryAbility", 7921 }; 7922 7923 // FA模型使用此方法连接服务 7924 // FA.connectAbility(want,connect); 7925 7926 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7927 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7928 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 7929 let connectionId = context.connectServiceExtensionAbility(want, connect); 7930 ``` 7931 7932 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调 7933 7934 ```ts 7935 import { rpc } from '@kit.IPCKit'; 7936 import { hilog } from '@kit.PerformanceAnalysisKit'; 7937 import { BusinessError } from '@kit.BasicServicesKit'; 7938 7939 class MyDeathRecipient implements rpc.DeathRecipient { 7940 onRemoteDied() { 7941 hilog.info(0x0000, 'testTag', 'server died'); 7942 } 7943 } 7944 if (proxy != undefined) { 7945 try { 7946 let deathRecipient = new MyDeathRecipient(); 7947 proxy.registerDeathRecipient(deathRecipient, 0); 7948 } catch (error) { 7949 let e: BusinessError = error as BusinessError; 7950 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7951 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7952 } 7953 } 7954 ``` 7955 7956### addDeathRecipient<sup>(deprecated)</sup> 7957 7958addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7959 7960注册用于接收远程对象死亡通知的回调,增加proxy对象上的死亡通知。 7961 7962> **说明:** 7963> 7964> 从API version 9 开始废弃,建议使用[registerDeathRecipient](#registerdeathrecipient9-1)类替代。 7965 7966**系统能力**:SystemCapability.Communication.IPC.Core 7967 7968**参数:** 7969 7970 | 参数名 | 类型 | 必填 | 说明 | 7971 | --------- | --------------------------------- | ---- | --------------------------------- | 7972 | recipient | [DeathRecipient](#deathrecipient) | 是 | 收件人表示要注册的回调。 | 7973 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 7974 7975**返回值:** 7976 7977 | 类型 | 说明 | 7978 | ------- | ---------------------------------------- | 7979 | boolean | true:回调注册成功,false:回调注册失败。| 7980 7981**示例:** 7982 7983>**说明:** 7984> 7985>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 7986 7987 <!--code_no_check--> 7988 ```ts 7989 // FA模型需要从@kit.AbilityKit导入featureAbility 7990 // import { featureAbility } from '@kit.AbilityKit'; 7991 import { rpc } from '@kit.IPCKit'; 7992 import { Want, common } from '@kit.AbilityKit'; 7993 import { hilog } from '@kit.PerformanceAnalysisKit'; 7994 7995 let proxy: rpc.IRemoteObject | undefined; 7996 let connect: common.ConnectOptions = { 7997 onConnect: (elementName, remoteProxy) => { 7998 hilog.info(0x0000, 'testTag', 'js onConnect called'); 7999 proxy = remoteProxy; 8000 }, 8001 onDisconnect: (elementName) => { 8002 hilog.info(0x0000, 'testTag', 'onDisconnect'); 8003 }, 8004 onFailed: () => { 8005 hilog.info(0x0000, 'testTag', 'onFailed'); 8006 } 8007 }; 8008 let want: Want = { 8009 bundleName: "com.ohos.server", 8010 abilityName: "com.ohos.server.EntryAbility", 8011 }; 8012 8013 // FA模型使用此方法连接服务 8014 // FA.connectAbility(want,connect); 8015 8016 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8017 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8018 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8019 let connectionId = context.connectServiceExtensionAbility(want, connect); 8020 ``` 8021 8022 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecipient接口方法新增死亡回调 8023<!--deprecated_code_no_check--> 8024 ```ts 8025 import { rpc } from '@kit.IPCKit'; 8026 import { hilog } from '@kit.PerformanceAnalysisKit'; 8027 8028 class MyDeathRecipient implements rpc.DeathRecipient { 8029 onRemoteDied() { 8030 hilog.info(0x0000, 'testTag', 'server died'); 8031 } 8032 } 8033 if (proxy != undefined) { 8034 let deathRecipient = new MyDeathRecipient(); 8035 proxy.addDeathRecipient(deathRecipient, 0); 8036 } 8037 ``` 8038 8039### unregisterDeathRecipient<sup>9+</sup> 8040 8041unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 8042 8043注销用于接收远程对象死亡通知的回调。 8044 8045**系统能力**:SystemCapability.Communication.IPC.Core 8046 8047**参数:** 8048 8049 | 参数名 | 类型 | 必填 | 说明 | 8050 | --------- | --------------------------------- | ---- | -------------- | 8051 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的回调。 | 8052 | flags | number | 是 | 死亡通知标志。 | 8053 8054**错误码:** 8055 8056以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8057 8058 | 错误码ID | 错误信息 | 8059 | -------- | -------- | 8060 | 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. | 8061 | 1900008 | The proxy or remote object is invalid. | 8062 8063**示例:** 8064 8065>**说明:** 8066> 8067>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 8068 8069 <!--code_no_check--> 8070 ```ts 8071 // FA模型需要从@kit.AbilityKit导入featureAbility 8072 // import { featureAbility } from '@kit.AbilityKit'; 8073 import { rpc } from '@kit.IPCKit'; 8074 import { Want, common } from '@kit.AbilityKit'; 8075 import { hilog } from '@kit.PerformanceAnalysisKit'; 8076 8077 let proxy: rpc.IRemoteObject | undefined; 8078 let connect: common.ConnectOptions = { 8079 onConnect: (elementName, remoteProxy) => { 8080 hilog.info(0x0000, 'testTag', 'js onConnect called'); 8081 proxy = remoteProxy; 8082 }, 8083 onDisconnect: (elementName) => { 8084 hilog.info(0x0000, 'testTag', 'onDisconnect'); 8085 }, 8086 onFailed: () => { 8087 hilog.info(0x0000, 'testTag', 'onFailed'); 8088 } 8089 }; 8090 let want: Want = { 8091 bundleName: "com.ohos.server", 8092 abilityName: "com.ohos.server.EntryAbility", 8093 }; 8094 8095 // FA模型使用此方法连接服务 8096 // FA.connectAbility(want,connect); 8097 8098 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8099 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8100 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8101 let connectionId = context.connectServiceExtensionAbility(want, connect); 8102 ``` 8103 8104 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 8105 8106 ```ts 8107 import { rpc } from '@kit.IPCKit'; 8108 import { hilog } from '@kit.PerformanceAnalysisKit'; 8109 import { BusinessError } from '@kit.BasicServicesKit'; 8110 8111 class MyDeathRecipient implements rpc.DeathRecipient { 8112 onRemoteDied() { 8113 hilog.info(0x0000, 'testTag', 'server died'); 8114 } 8115 } 8116 if (proxy != undefined) { 8117 try { 8118 let deathRecipient = new MyDeathRecipient(); 8119 proxy.registerDeathRecipient(deathRecipient, 0); 8120 proxy.unregisterDeathRecipient(deathRecipient, 0); 8121 } catch (error) { 8122 let e: BusinessError = error as BusinessError; 8123 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 8124 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 8125 } 8126 } 8127 ``` 8128 8129### removeDeathRecipient<sup>(deprecated)</sup> 8130 8131removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 8132 8133注销用于接收远程对象死亡通知的回调。 8134 8135> **说明:** 8136> 8137> 从API version 9 开始废弃,建议使用[unregisterDeathRecipient](#unregisterdeathrecipient9-1)替代。 8138 8139**系统能力**:SystemCapability.Communication.IPC.Core 8140 8141**参数:** 8142 8143 | 参数名 | 类型 | 必填 | 说明 | 8144 | --------- | --------------------------------- | ---- | --------------------------------- | 8145 | recipient | [DeathRecipient](#deathrecipient) | 是 | 要注销的死亡回调。 | 8146 | flags | number | 是 | 死亡通知标志。保留参数。设置为0。 | 8147 8148**返回值:** 8149 8150 | 类型 | 说明 | 8151 | ------- | ---------------------------------------- | 8152 | boolean | true:回调注销成功,false:回调注销失败。| 8153 8154**示例:** 8155 8156>**说明:** 8157> 8158>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 8159 8160 <!--code_no_check--> 8161 ```ts 8162 // FA模型需要从@kit.AbilityKit导入featureAbility 8163 // import { featureAbility } from '@kit.AbilityKit'; 8164 import { rpc } from '@kit.IPCKit'; 8165 import { Want, common } from '@kit.AbilityKit'; 8166 import { hilog } from '@kit.PerformanceAnalysisKit'; 8167 8168 let proxy: rpc.IRemoteObject | undefined; 8169 let connect: common.ConnectOptions = { 8170 onConnect: (elementName, remoteProxy) => { 8171 hilog.info(0x0000, 'testTag', 'js onConnect called'); 8172 proxy = remoteProxy; 8173 }, 8174 onDisconnect: (elementName) => { 8175 hilog.info(0x0000, 'testTag', 'onDisconnect'); 8176 }, 8177 onFailed: () => { 8178 hilog.info(0x0000, 'testTag', 'onFailed'); 8179 } 8180 }; 8181 let want: Want = { 8182 bundleName: "com.ohos.server", 8183 abilityName: "com.ohos.server.EntryAbility", 8184 }; 8185 8186 // FA模型使用此方法连接服务 8187 // FA.connectAbility(want,connect); 8188 8189 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8190 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8191 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8192 let connectionId = context.connectServiceExtensionAbility(want, connect); 8193 ``` 8194 8195 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调 8196<!--deprecated_code_no_check--> 8197 ```ts 8198 import { rpc } from '@kit.IPCKit'; 8199 import { hilog } from '@kit.PerformanceAnalysisKit'; 8200 8201 class MyDeathRecipient implements rpc.DeathRecipient { 8202 onRemoteDied() { 8203 hilog.info(0x0000, 'testTag', 'server died'); 8204 } 8205 } 8206 if (proxy != undefined) { 8207 let deathRecipient = new MyDeathRecipient(); 8208 proxy.addDeathRecipient(deathRecipient, 0); 8209 proxy.removeDeathRecipient(deathRecipient, 0); 8210 } 8211 ``` 8212 8213### getDescriptor<sup>9+</sup> 8214 8215getDescriptor(): string 8216 8217获取对象的接口描述符,接口描述符为字符串。 8218 8219**系统能力**:SystemCapability.Communication.IPC.Core 8220 8221**返回值:** 8222 8223 | 类型 | 说明 | 8224 | ------ | ---------------- | 8225 | string | 返回接口描述符。 | 8226 8227**错误码:** 8228 8229以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8230 8231 | 错误码ID | 错误信息 | 8232 | -------- | -------- | 8233 | 1900007 | communication failed. | 8234 | 1900008 | The proxy or remote object is invalid. | 8235 8236**示例:** 8237 8238>**说明:** 8239> 8240>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 8241 8242 <!--code_no_check--> 8243 ```ts 8244 // FA模型需要从@kit.AbilityKit导入featureAbility 8245 // import { featureAbility } from '@kit.AbilityKit'; 8246 import { rpc } from '@kit.IPCKit'; 8247 import { Want, common } from '@kit.AbilityKit'; 8248 import { hilog } from '@kit.PerformanceAnalysisKit'; 8249 8250 let proxy: rpc.IRemoteObject | undefined; 8251 let connect: common.ConnectOptions = { 8252 onConnect: (elementName, remoteProxy) => { 8253 hilog.info(0x0000, 'testTag', 'js onConnect called'); 8254 proxy = remoteProxy; 8255 }, 8256 onDisconnect: (elementName) => { 8257 hilog.info(0x0000, 'testTag', 'onDisconnect'); 8258 }, 8259 onFailed: () => { 8260 hilog.info(0x0000, 'testTag', 'onFailed'); 8261 } 8262 }; 8263 let want: Want = { 8264 bundleName: "com.ohos.server", 8265 abilityName: "com.ohos.server.EntryAbility", 8266 }; 8267 8268 // FA模型使用此方法连接服务 8269 // FA.connectAbility(want,connect); 8270 8271 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8272 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8273 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8274 let connectionId = context.connectServiceExtensionAbility(want, connect); 8275 ``` 8276 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符 8277 8278 ```ts 8279 import { rpc } from '@kit.IPCKit'; 8280 import { hilog } from '@kit.PerformanceAnalysisKit'; 8281 import { BusinessError } from '@kit.BasicServicesKit'; 8282 8283 if (proxy != undefined) { 8284 try { 8285 let descriptor: string = proxy.getDescriptor(); 8286 hilog.info(0x0000, 'testTag', 'descriptor is ' + descriptor); 8287 } catch (error) { 8288 let e: BusinessError = error as BusinessError; 8289 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 8290 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 8291 } 8292 } 8293 ``` 8294 8295### getInterfaceDescriptor<sup>(deprecated)</sup> 8296 8297getInterfaceDescriptor(): string 8298 8299查询当前代理对象接口的描述符。 8300 8301> **说明:** 8302> 8303> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-1)替代。 8304 8305**系统能力**:SystemCapability.Communication.IPC.Core 8306 8307**返回值:** 8308 8309 | 类型 | 说明 | 8310 | ------ | ------------------ | 8311 | string | 当前的接口描述符。 | 8312 8313**示例:** 8314 8315>**说明:** 8316> 8317>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 8318 8319 <!--code_no_check--> 8320 ```ts 8321 // FA模型需要从@kit.AbilityKit导入featureAbility 8322 // import { featureAbility } from '@kit.AbilityKit'; 8323 import { rpc } from '@kit.IPCKit'; 8324 import { Want, common } from '@kit.AbilityKit'; 8325 import { hilog } from '@kit.PerformanceAnalysisKit'; 8326 8327 let proxy: rpc.IRemoteObject | undefined; 8328 let connect: common.ConnectOptions = { 8329 onConnect: (elementName, remoteProxy) => { 8330 hilog.info(0x0000, 'testTag', 'js onConnect called'); 8331 proxy = remoteProxy; 8332 }, 8333 onDisconnect: (elementName) => { 8334 hilog.info(0x0000, 'testTag', 'onDisconnect'); 8335 }, 8336 onFailed: () => { 8337 hilog.info(0x0000, 'testTag', 'onFailed'); 8338 } 8339 }; 8340 let want: Want = { 8341 bundleName: "com.ohos.server", 8342 abilityName: "com.ohos.server.EntryAbility", 8343 }; 8344 8345 // FA模型使用此方法连接服务 8346 // FA.connectAbility(want,connect); 8347 8348 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8349 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8350 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8351 let connectionId = context.connectServiceExtensionAbility(want, connect); 8352 ``` 8353 8354 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符 8355<!--deprecated_code_no_check--> 8356 ```ts 8357 import { rpc } from '@kit.IPCKit'; 8358 import { hilog } from '@kit.PerformanceAnalysisKit'; 8359 8360 if (proxy != undefined) { 8361 let descriptor: string = proxy.getInterfaceDescriptor(); 8362 hilog.info(0x0000, 'testTag', 'descriptor is ' + descriptor); 8363 } 8364 ``` 8365 8366### isObjectDead 8367 8368isObjectDead(): boolean 8369 8370指示对应的RemoteObject是否死亡。 8371 8372**系统能力**:SystemCapability.Communication.IPC.Core 8373 8374**返回值:** 8375 8376 | 类型 | 说明 | 8377 | ------- | ------------------------------------------------- | 8378 | boolean | true:对应的对象已经死亡,false:对应的对象未死亡。 | 8379 8380**示例:** 8381 8382>**说明:** 8383> 8384>在本文档的示例中,通过this.getUIContext().getHostContext()来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需要在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 8385 8386 <!--code_no_check--> 8387 ```ts 8388 // FA模型需要从@kit.AbilityKit导入featureAbility 8389 // import { featureAbility } from '@kit.AbilityKit'; 8390 import { rpc } from '@kit.IPCKit'; 8391 import { Want, common } from '@kit.AbilityKit'; 8392 import { hilog } from '@kit.PerformanceAnalysisKit'; 8393 8394 let proxy: rpc.IRemoteObject | undefined; 8395 let connect: common.ConnectOptions = { 8396 onConnect: (elementName, remoteProxy) => { 8397 hilog.info(0x0000, 'testTag', 'js onConnect called'); 8398 proxy = remoteProxy; 8399 }, 8400 onDisconnect: (elementName) => { 8401 hilog.info(0x0000, 'testTag', 'onDisconnect'); 8402 }, 8403 onFailed: () => { 8404 hilog.info(0x0000, 'testTag', 'onFailed'); 8405 } 8406 }; 8407 let want: Want = { 8408 bundleName: "com.ohos.server", 8409 abilityName: "com.ohos.server.EntryAbility", 8410 }; 8411 8412 // FA模型使用此方法连接服务 8413 // FA.connectAbility(want,connect); 8414 8415 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8416 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8417 // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 8418 let connectionId = context.connectServiceExtensionAbility(want, connect); 8419 ``` 8420 8421 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡 8422 8423 ```ts 8424 import { rpc } from '@kit.IPCKit'; 8425 import { hilog } from '@kit.PerformanceAnalysisKit'; 8426 8427 if (proxy != undefined) { 8428 let isDead: boolean = proxy.isObjectDead(); 8429 hilog.info(0x0000, 'testTag', 'isObjectDead is ' + isDead); 8430 } 8431 ``` 8432 8433## MessageOption 8434 8435公共消息选项,使用指定的标志类型,构造指定的MessageOption对象。 8436 8437### 属性 8438 8439**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 8440 8441 | 名称 | 类型 | 可读 | 可写 | 说明 | 8442 | ------------- | ------ | ----- | ----- | ------------------------------------------------------------------------ | 8443 | TF_SYNC | number | 是 | 否 | 同步调用标识。 | 8444 | TF_ASYNC | number | 是 | 否 | 异步调用标识。 | 8445 | TF_ACCEPT_FDS | number | 是 | 否 | 指示sendMessageRequest<sup>9+</sup>接口可以传递文件描述符。 | 8446 | TF_WAIT_TIME | number | 是 | 是 | RPC等待时间(单位/秒),IPC场景下无效。默认等待为8秒(不建议修改等待时间)。 | 8447 8448### constructor<sup>9+</sup> 8449 8450constructor(async?: boolean) 8451 8452MessageOption构造函数。 8453 8454**系统能力**:SystemCapability.Communication.IPC.Core 8455 8456**参数:** 8457 8458| 参数名 | 类型 | 必填 | 说明 | 8459| ------ | ------- | ---- | ------------------------------------------------------------ | 8460| async | boolean | 否 | true:表示异步调用标志,false:表示同步调用标志。默认同步调用。 | 8461 8462**示例:** 8463 8464 ```ts 8465 import { rpc } from '@kit.IPCKit'; 8466 8467 class TestRemoteObject extends rpc.MessageOption { 8468 constructor(async: boolean) { 8469 super(async); 8470 } 8471 } 8472 ``` 8473 8474### constructor 8475 8476constructor(syncFlags?: number, waitTime?: number) 8477 8478MessageOption构造函数。 8479 8480**系统能力**:SystemCapability.Communication.IPC.Core 8481 8482**参数:** 8483 8484 | 参数名 | 类型 | 必填 | 说明 | 8485 | --------- | ------ | ---- | --------------------------------------------- | 8486 | syncFlags | number | 否 | 同步调用或异步调用标志,同步调用标志:0;异步调用标志:1。默认同步调用。 | 8487 | waitTime | number | 否 | 调用rpc最长等待时间。默认TF_WAIT_TIME。 | 8488 8489**示例:** 8490 8491 ```ts 8492 import { rpc } from '@kit.IPCKit'; 8493 8494 class TestRemoteObject extends rpc.MessageOption { 8495 constructor(syncFlags?: number,waitTime?: number) { 8496 super(syncFlags,waitTime); 8497 } 8498 } 8499 ``` 8500### isAsync<sup>9+</sup> 8501 8502isAsync(): boolean 8503 8504获取SendMessageRequest调用中确定同步或是异步的标志。 8505 8506**系统能力**:SystemCapability.Communication.IPC.Core 8507 8508**返回值:** 8509 8510 | 类型 | 说明 | 8511 | ------- | ---------------------------------------- | 8512 | boolean | true:异步调用成功,false:同步调用成功。| 8513 8514**示例:** 8515 8516 ```ts 8517 import { rpc } from '@kit.IPCKit'; 8518 import { hilog } from '@kit.PerformanceAnalysisKit'; 8519 8520 try { 8521 let option = new rpc.MessageOption(); 8522 let result = option.isAsync(); 8523 } catch (error) { 8524 hilog.info(0x0000, 'testTag', 'error ' + error); 8525 } 8526 ``` 8527 8528### setAsync<sup>9+</sup> 8529 8530setAsync(async: boolean): void 8531 8532设置SendMessageRequest调用中确定同步或是异步的标志。 8533 8534**系统能力**:SystemCapability.Communication.IPC.Core 8535 8536**参数:** 8537 8538| 参数名 | 类型 | 必填 | 说明 | 8539| ------ | ------- | ---- | ------------------------------------------------- | 8540| async | boolean | 是 | true:表示异步调用标志,false:表示同步调用标志。 | 8541 8542**示例:** 8543 8544 ```ts 8545 import { rpc } from '@kit.IPCKit'; 8546 import { hilog } from '@kit.PerformanceAnalysisKit'; 8547 8548 try { 8549 let option = new rpc.MessageOption(); 8550 option.setAsync(true); 8551 } catch (error) { 8552 hilog.info(0x0000, 'testTag', 'error ' + error); 8553 } 8554 ``` 8555 8556### getFlags 8557 8558getFlags(): number 8559 8560获取同步调用或异步调用标志。 8561 8562**系统能力**:SystemCapability.Communication.IPC.Core 8563 8564**返回值:** 8565 8566 | 类型 | 说明 | 8567 | ------ | ------------------------------------ | 8568 | number | 调用成功返回同步调用或异步调用标志,同步调用标志:0;异步调用标志:1。 | 8569 8570**示例:** 8571 8572 ```ts 8573 import { rpc } from '@kit.IPCKit'; 8574 import { hilog } from '@kit.PerformanceAnalysisKit'; 8575 8576 try { 8577 let option = new rpc.MessageOption(); 8578 hilog.info(0x0000, 'testTag', 'create object successfully'); 8579 let flag = option.getFlags(); 8580 hilog.info(0x0000, 'testTag', 'run getFlags success, flag is ' + flag); 8581 option.setFlags(rpc.MessageOption.TF_ASYNC); 8582 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8583 let flag2 = option.getFlags(); 8584 hilog.info(0x0000, 'testTag', 'run getFlags success, flag2 is ' + flag2); 8585 } catch (error) { 8586 hilog.error(0x0000, 'testTag', 'error ' + error); 8587 } 8588 ``` 8589 8590### setFlags 8591 8592setFlags(flags: number): void 8593 8594设置同步调用或异步调用标志。 8595 8596**系统能力**:SystemCapability.Communication.IPC.Core 8597 8598**参数:** 8599 8600 | 参数名 | 类型 | 必填 | 说明 | 8601 | ------ | ------ | ---- | ------------------------ | 8602 | flags | number | 是 | 同步调用或异步调用标志,同步调用标志:0;异步调用标志:1。 | 8603 8604**示例:** 8605 8606 ```ts 8607 import { rpc } from '@kit.IPCKit'; 8608 import { hilog } from '@kit.PerformanceAnalysisKit'; 8609 8610 try { 8611 let option = new rpc.MessageOption(); 8612 option.setFlags(rpc.MessageOption.TF_ASYNC); 8613 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8614 let flag = option.getFlags(); 8615 hilog.info(0x0000, 'testTag', 'run getFlags success, flag is ' + flag); 8616 } catch (error) { 8617 hilog.error(0x0000, 'testTag', 'error ' + error); 8618 } 8619 ``` 8620 8621### getWaitTime 8622 8623getWaitTime(): number 8624 8625获取rpc调用的最长等待时间。 8626 8627**系统能力**:SystemCapability.Communication.IPC.Core 8628 8629**返回值:** 8630 8631 | 类型 | 说明 | 8632 | ------ | ----------------- | 8633 | number | rpc最长等待时间。默认TF_WAIT_TIME。| 8634 8635**示例:** 8636 8637 ```ts 8638 import { rpc } from '@kit.IPCKit'; 8639 import { hilog } from '@kit.PerformanceAnalysisKit'; 8640 8641 try { 8642 let option = new rpc.MessageOption(); 8643 let time = option.getWaitTime(); 8644 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8645 option.setWaitTime(16); 8646 let time2 = option.getWaitTime(); 8647 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8648 } catch (error) { 8649 hilog.error(0x0000, 'testTag', 'error ' + error); 8650 } 8651 ``` 8652 8653### setWaitTime 8654 8655setWaitTime(waitTime: number): void 8656 8657设置rpc调用最长等待时间。 8658 8659**系统能力**:SystemCapability.Communication.IPC.Core 8660 8661**参数:** 8662 8663 | 参数名 | 类型 | 必填 | 说明 | 8664 | -------- | ------ | ---- | --------------------- | 8665 | waitTime | number | 是 | rpc调用最长等待时间,上限为3000秒。 | 8666 8667**示例:** 8668 8669 ```ts 8670 import { rpc } from '@kit.IPCKit'; 8671 import { hilog } from '@kit.PerformanceAnalysisKit'; 8672 8673 try { 8674 let option = new rpc.MessageOption(); 8675 option.setWaitTime(16); 8676 let time = option.getWaitTime(); 8677 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8678 } catch (error) { 8679 hilog.error(0x0000, 'testTag', 'error ' + error); 8680 } 8681 ``` 8682 8683## IPCSkeleton 8684 8685用于获取IPC上下文信息,包括获取UID和PID、获取本端和对端设备ID、检查接口调用是否在同一设备上。 8686 8687### getContextObject 8688 8689static getContextObject(): IRemoteObject 8690 8691静态方法,获取系统能力的管理者。 8692 8693**系统能力**:SystemCapability.Communication.IPC.Core 8694 8695**返回值:** 8696 8697 | 类型 | 说明 | 8698 | ------------------------------- | -------------------- | 8699 | [IRemoteObject](#iremoteobject) | 返回系统能力管理者。 | 8700 8701**示例:** 8702 8703 ```ts 8704 import { rpc } from '@kit.IPCKit'; 8705 import { hilog } from '@kit.PerformanceAnalysisKit'; 8706 8707 try { 8708 let samgr = rpc.IPCSkeleton.getContextObject(); 8709 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8710 } catch (error) { 8711 hilog.error(0x0000, 'testTag', 'error ' + error); 8712 } 8713 ``` 8714 8715### getCallingPid 8716 8717static getCallingPid(): number 8718 8719静态方法,获取调用者的PID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的PID。 8720 8721**系统能力**:SystemCapability.Communication.IPC.Core 8722 8723**返回值:** 8724 8725 | 类型 | 说明 | 8726 | ------ | ----------------- | 8727 | number | 返回调用者的PID。 | 8728 8729**示例:** 8730 8731 ```ts 8732 import { rpc } from '@kit.IPCKit'; 8733 import { hilog } from '@kit.PerformanceAnalysisKit'; 8734 8735 class Stub extends rpc.RemoteObject { 8736 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8737 try { 8738 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8739 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8740 } catch (error) { 8741 hilog.error(0x0000, 'testTag', 'error ' + error); 8742 } 8743 return true; 8744 } 8745 } 8746 ``` 8747 8748### getCallingUid 8749 8750static getCallingUid(): number 8751 8752静态方法,获取调用者的UID。此方法由RemoteObject对象在onRemoteRequest方法中调用,不在IPC上下文环境(onRemoteRequest)中调用则返回本进程的UID。 8753 8754**系统能力**:SystemCapability.Communication.IPC.Core 8755 8756**返回值:** 8757 8758 | 类型 | 说明 | 8759 | ------ | ----------------- | 8760 | number | 返回调用者的UID。 | 8761 8762**示例:** 8763 8764 ```ts 8765 import { rpc } from '@kit.IPCKit'; 8766 import { hilog } from '@kit.PerformanceAnalysisKit'; 8767 8768 class Stub extends rpc.RemoteObject { 8769 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8770 try { 8771 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8772 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8773 } catch (error) { 8774 hilog.error(0x0000, 'testTag', 'error ' + error); 8775 } 8776 return true; 8777 } 8778 } 8779 ``` 8780 8781### getCallingTokenId<sup>8+</sup> 8782 8783static getCallingTokenId(): number 8784 8785静态方法,获取调用者的TokenId,用于被调用方对调用方的身份校验。 8786 8787**系统能力**:SystemCapability.Communication.IPC.Core 8788 8789**返回值:** 8790 8791 | 类型 | 说明 | 8792 | ------ | --------------------- | 8793 | number | 返回调用者的TokenId。 | 8794 8795**示例:** 8796 8797 ```ts 8798 import { rpc } from '@kit.IPCKit'; 8799 import { hilog } from '@kit.PerformanceAnalysisKit'; 8800 8801 class Stub extends rpc.RemoteObject { 8802 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8803 try { 8804 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8805 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8806 } catch (error) { 8807 hilog.error(0x0000, 'testTag', 'error ' + error); 8808 } 8809 return true; 8810 } 8811 } 8812 ``` 8813 8814### getCallingDeviceID 8815 8816static getCallingDeviceID(): string 8817 8818静态方法,获取调用者进程所在的设备ID。 8819 8820**系统能力**:SystemCapability.Communication.IPC.Core 8821 8822**返回值:** 8823 8824 | 类型 | 说明 | 8825 | ------ | ---------------------------- | 8826 | string | 返回调用者进程所在的设备ID。 | 8827 8828**示例:** 8829 8830 ```ts 8831 import { rpc } from '@kit.IPCKit'; 8832 import { hilog } from '@kit.PerformanceAnalysisKit'; 8833 8834 class Stub extends rpc.RemoteObject { 8835 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8836 try { 8837 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8838 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8839 } catch (error) { 8840 hilog.error(0x0000, 'testTag', 'error ' + error); 8841 } 8842 return true; 8843 } 8844 } 8845 ``` 8846 8847### getLocalDeviceID 8848 8849static getLocalDeviceID(): string 8850 8851静态方法,获取本端设备ID。 8852 8853**系统能力**:SystemCapability.Communication.IPC.Core 8854 8855**返回值:** 8856 8857 | 类型 | 说明 | 8858 | ------ | ------------------ | 8859 | string | 返回本地设备的ID。 | 8860 8861**示例:** 8862 8863 ```ts 8864 import { rpc } from '@kit.IPCKit'; 8865 import { hilog } from '@kit.PerformanceAnalysisKit'; 8866 8867 class Stub extends rpc.RemoteObject { 8868 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8869 try { 8870 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8871 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8872 } catch (error) { 8873 hilog.error(0x0000, 'testTag', 'error ' + error); 8874 } 8875 return true; 8876 } 8877 } 8878 ``` 8879 8880### isLocalCalling 8881 8882static isLocalCalling(): boolean 8883 8884静态方法,检查当前通信对端是否是本设备的进程。 8885 8886**系统能力**:SystemCapability.Communication.IPC.Core 8887 8888**返回值:** 8889 8890 | 类型 | 说明 | 8891 | ------- | -------------------------------------------------- | 8892 | boolean | true:调用在同一台设备,false:调用未在同一台设备。| 8893 8894**示例:** 8895 8896 ```ts 8897 import { rpc } from '@kit.IPCKit'; 8898 import { hilog } from '@kit.PerformanceAnalysisKit'; 8899 8900 class Stub extends rpc.RemoteObject { 8901 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8902 try { 8903 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8904 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8905 } catch (error) { 8906 hilog.error(0x0000, 'testTag', 'error ' + error); 8907 } 8908 return true; 8909 } 8910 } 8911 ``` 8912 8913### flushCmdBuffer<sup>9+</sup> 8914 8915static flushCmdBuffer(object: IRemoteObject): void 8916 8917静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8918 8919**系统能力**:SystemCapability.Communication.IPC.Core 8920 8921**参数:** 8922 8923 | 参数名 | 类型 | 必填 | 说明 | 8924 | ------ | ------------------------------- | ---- | ------------------- | 8925 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8926 8927**错误码:** 8928 8929以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 8930 8931 | 错误码ID | 错误信息 | 8932 | -------- | -------- | 8933 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 8934 8935**示例:** 8936 8937 ```ts 8938 import { rpc } from '@kit.IPCKit'; 8939 import { hilog } from '@kit.PerformanceAnalysisKit'; 8940 import { BusinessError } from '@kit.BasicServicesKit'; 8941 8942 class TestRemoteObject extends rpc.RemoteObject { 8943 constructor(descriptor: string) { 8944 super(descriptor); 8945 } 8946 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8947 // 根据业务实际逻辑,进行相应处理 8948 return true; 8949 } 8950 } 8951 try { 8952 let remoteObject = new TestRemoteObject("aaa"); 8953 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8954 } catch (error) { 8955 let e: BusinessError = error as BusinessError; 8956 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8957 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8958 } 8959 ``` 8960 8961### flushCommands<sup>(deprecated)</sup> 8962 8963static flushCommands(object: IRemoteObject): number 8964 8965静态方法,将所有挂起的命令从指定的RemoteProxy刷新到相应的RemoteObject。建议在任何时间执行敏感操作之前调用此方法。 8966 8967> **说明:** 8968> 8969> 从API version 9 开始废弃,建议使用[flushCmdBuffer](#flushcmdbuffer9)替代。 8970 8971**系统能力**:SystemCapability.Communication.IPC.Core 8972 8973**参数:** 8974 8975 | 参数名 | 类型 | 必填 | 说明 | 8976 | ------ | ------------------------------- | ---- | ------------------- | 8977 | object | [IRemoteObject](#iremoteobject) | 是 | 指定的RemoteProxy。 | 8978 8979**返回值:** 8980 8981 | 类型 | 说明 | 8982 | ------ | --------------------------------------------------------------------------------- | 8983 | number | 如果操作成功,返回0;如果输入对象为空或RemoteObject,或者操作失败,返回错误代码。 | 8984 8985**示例:** 8986<!--deprecated_code_no_check--> 8987 ```ts 8988 import { rpc } from '@kit.IPCKit'; 8989 import { hilog } from '@kit.PerformanceAnalysisKit'; 8990 8991 class TestRemoteObject extends rpc.RemoteObject { 8992 constructor(descriptor: string) { 8993 super(descriptor); 8994 } 8995 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8996 // 根据业务实际逻辑,进行相应处理 8997 return true; 8998 } 8999 } 9000 try { 9001 let remoteObject = new TestRemoteObject("aaa"); 9002 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 9003 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 9004 } catch (error) { 9005 let e: BusinessError = error as BusinessError; 9006 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 9007 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 9008 } 9009 ``` 9010 9011### resetCallingIdentity 9012 9013static resetCallingIdentity(): string 9014 9015静态方法,将远程用户的UID和PID替换为本地用户的UID和PID。它可以用于身份验证等场景。 9016 9017**系统能力**:SystemCapability.Communication.IPC.Core 9018 9019**返回值:** 9020 9021 | 类型 | 说明 | 9022 | ------ | ------------------------------------ | 9023 | string | 返回包含远程用户的UID和PID的字符串。 | 9024 9025**示例:** 9026 9027 ```ts 9028 import { rpc } from '@kit.IPCKit'; 9029 import { hilog } from '@kit.PerformanceAnalysisKit'; 9030 9031 class Stub extends rpc.RemoteObject { 9032 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9033 try { 9034 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 9035 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 9036 } catch (error) { 9037 hilog.error(0x0000, 'testTag', 'error ' + error); 9038 } 9039 return true; 9040 } 9041 } 9042 ``` 9043 9044### restoreCallingIdentity<sup>9+</sup> 9045 9046static restoreCallingIdentity(identity: string): void 9047 9048静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 9049 9050**系统能力**:SystemCapability.Communication.IPC.Core 9051 9052**参数:** 9053 9054 | 参数名 | 类型 | 必填 | 说明 | 9055 | -------- | ------ | ---- | ------------------------------------------------------------------ | 9056 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串,其长度应小于40960字节。由resetCallingIdentity返回。 | 9057 9058**错误码:** 9059 9060以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9061 9062 | 错误码ID | 错误信息 | 9063 | -------- | -------- | 9064 | 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. | 9065 9066**示例:** 9067 9068 ```ts 9069 import { rpc } from '@kit.IPCKit'; 9070 import { hilog } from '@kit.PerformanceAnalysisKit'; 9071 9072 class Stub extends rpc.RemoteObject { 9073 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9074 try { 9075 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 9076 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 9077 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 9078 } catch (error) { 9079 hilog.error(0x0000, 'testTag', 'error ' + error); 9080 } 9081 return true; 9082 } 9083 } 9084 ``` 9085 9086### setCallingIdentity<sup>(deprecated)</sup> 9087 9088static setCallingIdentity(identity: string): boolean 9089 9090静态方法,将UID和PID恢复为远程用户的UID和PID。它通常在使用resetCallingIdentity后调用,需要resetCallingIdentity返回的远程用户的UID和PID。 9091 9092> **说明:** 9093> 9094> 从API version 9 开始废弃,建议使用[restoreCallingIdentity](#restorecallingidentity9)替代。 9095 9096**系统能力**:SystemCapability.Communication.IPC.Core 9097 9098**参数:** 9099 9100 | 参数名 | 类型 | 必填 | 说明 | 9101 | -------- | ------ | ---- | ------------------------------------------------------------------ | 9102 | identity | string | 是 | 标识表示包含远程用户UID和PID的字符串。由resetCallingIdentity返回。 | 9103 9104**返回值:** 9105 9106 | 类型 | 说明 | 9107 | ------- | ---------------------------------| 9108 | boolean | true:设置成功,false:设置失败。| 9109 9110**示例:** 9111<!--deprecated_code_no_check--> 9112 ```ts 9113 import { rpc } from '@kit.IPCKit'; 9114 import { hilog } from '@kit.PerformanceAnalysisKit'; 9115 9116 class Stub extends rpc.RemoteObject { 9117 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9118 try { 9119 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 9120 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 9121 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 9122 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 9123 } catch (error) { 9124 hilog.error(0x0000, 'testTag', 'error ' + error); 9125 } 9126 return true; 9127 } 9128 } 9129 ``` 9130 9131## RemoteObject 9132 9133实现远程对象。服务提供者必须继承此类。 9134 9135### constructor 9136 9137constructor(descriptor: string) 9138 9139RemoteObject构造函数。 9140 9141**系统能力**:SystemCapability.Communication.IPC.Core 9142 9143**参数:** 9144 9145 | 参数名 | 类型 | 必填 | 说明 | 9146 | ---------- | ------ | ---- | ------------ | 9147 | descriptor | string | 是 | 接口描述符,其长度应小于40960字节。 | 9148 9149**示例:** 9150 9151 ```ts 9152 import { rpc } from '@kit.IPCKit'; 9153 9154 class TestRemoteObject extends rpc.RemoteObject { 9155 constructor(descriptor: string) { 9156 super(descriptor); 9157 } 9158 } 9159 ``` 9160### sendRequest<sup>(deprecated)</sup> 9161 9162sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9163 9164以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 9165 9166> **说明:** 9167> 9168> 从API version 8 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 9169 9170**系统能力**:SystemCapability.Communication.IPC.Core 9171 9172**参数:** 9173 9174 | 参数名 | 类型 | 必填 | 说明 | 9175 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 9176 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 9177 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 9178 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 9179 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 9180 9181**返回值:** 9182 9183 | 类型 | 说明 | 9184 | ------- | -------------------------------- | 9185 | boolean | true:发送成功,false:发送失败。| 9186 9187**示例:** 9188<!--deprecated_code_no_check--> 9189 ```ts 9190 import { rpc } from '@kit.IPCKit'; 9191 import { hilog } from '@kit.PerformanceAnalysisKit'; 9192 9193 class testRemoteObject extends rpc.RemoteObject { 9194 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9195 // 根据业务实际逻辑,进行相应处理 9196 return true; 9197 } 9198 } 9199 try { 9200 let testRemoteObject = new TestRemoteObject("testObject"); 9201 let option = new rpc.MessageOption(); 9202 let data = rpc.MessageParcel.create(); 9203 let reply = rpc.MessageParcel.create(); 9204 data.writeInt(1); 9205 data.writeString("hello"); 9206 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 9207 if (ret) { 9208 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 9209 let msg = reply.readString(); 9210 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 9211 } else { 9212 hilog.error(0x0000, 'testTag', 'sendRequest failed'); 9213 } 9214 hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel'); 9215 data.reclaim(); 9216 reply.reclaim(); 9217 } catch (error) { 9218 hilog.error(0x0000, 'testTag', 'error ' + error); 9219 } 9220 ``` 9221 9222### sendMessageRequest<sup>9+</sup> 9223 9224sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 9225 9226以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendMessageRequest返回时兑现,回复内容在reply报文里。 9227 9228**系统能力**:SystemCapability.Communication.IPC.Core 9229 9230**参数:** 9231 9232 | 参数名 | 类型 | 必填 | 说明 | 9233 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 9234 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 9235 | data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 9236 | reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 9237 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 9238 9239**返回值:** 9240 9241| 类型 | 说明 | 9242| ----------------------------------------------- | ----------------------------------------- | 9243| Promise<[RequestResult](#requestresult9)> | 返回一个期约,兑现值是RequestResult实例。 | 9244 9245**错误码:** 9246 9247以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9248 9249 | 错误码ID | 错误信息 | 9250 | -------- | -------- | 9251 | 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. | 9252 9253**示例:** 9254 9255 ```ts 9256 import { rpc } from '@kit.IPCKit'; 9257 import { hilog } from '@kit.PerformanceAnalysisKit'; 9258 9259 class TestRemoteObject extends rpc.RemoteObject { 9260 constructor(descriptor: string) { 9261 super(descriptor); 9262 } 9263 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9264 // 根据业务实际逻辑,进行相应处理 9265 return true; 9266 } 9267 } 9268 try { 9269 let testRemoteObject = new TestRemoteObject("testObject"); 9270 let option = new rpc.MessageOption(); 9271 let data = rpc.MessageSequence.create(); 9272 let reply = rpc.MessageSequence.create(); 9273 data.writeInt(1); 9274 data.writeString("hello"); 9275 testRemoteObject.sendMessageRequest(1, data, reply, option) 9276 .then((result: rpc.RequestResult) => { 9277 if (result.errCode === 0) { 9278 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 9279 let num = result.reply.readInt(); 9280 let msg = result.reply.readString(); 9281 hilog.info(0x0000, 'testTag', 'reply num: ' + num); 9282 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 9283 } else { 9284 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode: ' + result.errCode); 9285 } 9286 }).catch((e: Error) => { 9287 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + e); 9288 }).finally (() => { 9289 hilog.info(0x0000, 'testTag', 'sendMessageRequest ends, reclaim parcel'); 9290 data.reclaim(); 9291 reply.reclaim(); 9292 }); 9293 } catch (error) { 9294 hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + error); 9295 } 9296 ``` 9297 9298### sendRequest<sup>(deprecated)</sup> 9299 9300sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 9301 9302以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则期约立即兑现,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则期约将在sendRequest返回时兑现,回复内容在reply报文里。 9303 9304> **说明:** 9305> 9306> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-4)替代。 9307 9308**系统能力**:SystemCapability.Communication.IPC.Core 9309 9310**参数:** 9311 9312 | 参数名 | 类型 | 必填 | 说明 | 9313 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 9314 | code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 9315 | data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 9316 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 9317 | options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 9318 9319**返回值:** 9320 9321| 类型 | 说明 | 9322| ------------------------------------------------------------ | --------------------------------------------- | 9323| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | 返回一个期约,兑现值是sendRequestResult实例。 | 9324 9325**示例:** 9326<!--deprecated_code_no_check--> 9327 ```ts 9328 import { rpc } from '@kit.IPCKit'; 9329 import { hilog } from '@kit.PerformanceAnalysisKit'; 9330 9331 class TestRemoteObject extends rpc.RemoteObject { 9332 constructor(descriptor: string) { 9333 super(descriptor); 9334 } 9335 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9336 // 根据业务实际逻辑,进行相应处理 9337 return true; 9338 } 9339 } 9340 try { 9341 let testRemoteObject = new TestRemoteObject("testObject"); 9342 let option = new rpc.MessageOption(); 9343 let data = rpc.MessageParcel.create(); 9344 let reply = rpc.MessageParcel.create(); 9345 data.writeInt(1); 9346 data.writeString("hello"); 9347 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 9348 let b = a as Promise<rpc.SendRequestResult>; 9349 b.then((result: rpc.SendRequestResult) => { 9350 if (result.errCode === 0) { 9351 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 9352 let num = result.reply.readInt(); 9353 let msg = result.reply.readString(); 9354 hilog.info(0x0000, 'testTag', 'reply num: ' + num); 9355 hilog.info(0x0000, 'testTag', 'reply msg: ' + msg); 9356 } else { 9357 hilog.error(0x0000, 'testTag', 'sendRequest failed, errCode: ' + result.errCode); 9358 } 9359 }).catch((e: Error) => { 9360 hilog.error(0x0000, 'testTag', 'sendRequest failed, error: ' + e); 9361 }).finally (() => { 9362 hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel'); 9363 data.reclaim(); 9364 reply.reclaim(); 9365 }); 9366 } catch (error) { 9367 hilog.error(0x0000, 'testTag', 'error: ' + error); 9368 } 9369 ``` 9370 9371### sendMessageRequest<sup>9+</sup> 9372 9373sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 9374 9375以同步或异步方式向对端进程发送MessageSequence消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendMessageRequest返回时收到回调,回复内容在reply报文里。 9376 9377**系统能力**:SystemCapability.Communication.IPC.Core 9378 9379**参数:** 9380 9381| 参数名 | 类型 | 必填 | 说明 | 9382| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 9383| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 9384| data | [MessageSequence](#messagesequence9) | 是 | 保存待发送数据的MessageSequence对象。 | 9385| reply | [MessageSequence](#messagesequence9) | 是 | 接收应答数据的MessageSequence对象。 | 9386| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 9387| callback | AsyncCallback<[RequestResult](#requestresult9)> | 是 | 接收发送结果的回调。 | 9388 9389**错误码:** 9390 9391以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9392 9393 | 错误码ID | 错误信息 | 9394 | -------- | -------- | 9395 | 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. | 9396 9397### sendRequest<sup>(deprecated)</sup> 9398 9399sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 9400 9401以同步或异步方式向对端进程发送MessageParcel消息。如果为选项设置了异步模式,则立即收到回调,reply报文里没有内容,具体回复需要在业务侧的回调中获取。如果为选项设置了同步模式,则将在sendRequest返回时收到回调,回复内容在reply报文里。 9402 9403> **说明:** 9404> 9405> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[sendMessageRequest](#sendmessagerequest9-5)替代。 9406 9407**系统能力**:SystemCapability.Communication.IPC.Core 9408 9409**参数:** 9410 9411| 参数名 | 类型 | 必填 | 说明 | 9412| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 9413| code | number | 是 | 本次请求调用的消息码(1-16777215),由通信双方确定。如果接口由IDL工具生成,则消息代码由IDL自动生成。 | 9414| data | [MessageParcel](#messageparceldeprecated) | 是 | 保存待发送数据的MessageParcel对象。 | 9415| reply | [MessageParcel](#messageparceldeprecated) | 是 | 接收应答数据的MessageParcel对象。 | 9416| options | [MessageOption](#messageoption) | 是 | 本次请求的同异步模式,默认同步调用。 | 9417| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | 是 | 接收发送结果的回调。 | 9418 9419### onRemoteMessageRequest<sup>9+</sup> 9420 9421onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 9422 9423> **说明:** 9424> 9425> 开发者应优先选择重载onRemoteMessageRequest方法,其中可以自由实现同步和异步的消息处理。 9426> 开发者同时重载onRemoteRequest和onRemoteMessageRequest方法时,仅onRemoteMessageRequest方法生效。 9427 9428sendMessageRequest请求的响应处理函数,服务端在该函数里同步或异步地处理请求,回复结果。 9429 9430**系统能力**:SystemCapability.Communication.IPC.Core 9431 9432**参数:** 9433 9434 | 参数名 | 类型 | 必填 | 说明 | 9435 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 9436 | code | number | 是 | 对端发送的服务请求码。 | 9437 | data | [MessageSequence](#messagesequence9) | 是 | 携带客户端调用参数的MessageSequence对象。 | 9438 | reply | [MessageSequence](#messagesequence9) | 是 | 写入结果的MessageSequence对象。 | 9439 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9440 9441**返回值:** 9442 9443 | 类型 | 说明 | 9444 | ----------------- | ----------------------------------------------------------------------------------------------- | 9445 | boolean \| Promise\<boolean> | - 若在onRemoteMessageRequest中同步处理请求,则返回一个布尔值。返回true表示操作成功,返回false表示操作失败。</br>- 若在onRemoteMessageRequest中异步处理请求,则返回一个Promise对象。返回true表示操作成功,返回false表示操作失败。| 9446 9447**重载onRemoteMessageRequest方法同步处理请求示例:** 9448 9449 ```ts 9450 import { rpc } from '@kit.IPCKit'; 9451 import { hilog } from '@kit.PerformanceAnalysisKit'; 9452 9453 class TestRemoteObject extends rpc.RemoteObject { 9454 constructor(descriptor: string) { 9455 super(descriptor); 9456 } 9457 9458 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9459 if (code === 1) { 9460 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9461 return true; 9462 } else { 9463 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9464 return false; 9465 } 9466 } 9467 } 9468 ``` 9469 9470 **重载onRemoteMessageRequest方法异步处理请求示例:** 9471 9472 ```ts 9473 import { rpc } from '@kit.IPCKit'; 9474 import { hilog } from '@kit.PerformanceAnalysisKit'; 9475 9476 class TestRemoteObject extends rpc.RemoteObject { 9477 constructor(descriptor: string) { 9478 super(descriptor); 9479 } 9480 9481 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9482 if (code === 1) { 9483 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9484 } else { 9485 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9486 return false; 9487 } 9488 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9489 setTimeout(resolve, 100); 9490 }) 9491 return true; 9492 } 9493 } 9494 ``` 9495 9496**同时重载onRemoteMessageRequest和onRemoteRequest方法同步处理请求示例:** 9497 9498 ```ts 9499 import { rpc } from '@kit.IPCKit'; 9500 import { hilog } from '@kit.PerformanceAnalysisKit'; 9501 9502 class TestRemoteObject extends rpc.RemoteObject { 9503 constructor(descriptor: string) { 9504 super(descriptor); 9505 } 9506 9507 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9508 if (code === 1) { 9509 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9510 return true; 9511 } else { 9512 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9513 return false; 9514 } 9515 } 9516 // 同时调用仅会执行onRemoteMessageRequest 9517 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9518 if (code === 1) { 9519 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9520 } else { 9521 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9522 return false; 9523 } 9524 return true; 9525 } 9526 } 9527 ``` 9528 9529 **同时重载onRemoteMessageRequest和onRemoteRequest方法异步处理请求示例:** 9530 9531 ```ts 9532 import { rpc } from '@kit.IPCKit'; 9533 import { hilog } from '@kit.PerformanceAnalysisKit'; 9534 class TestRemoteObject extends rpc.RemoteObject { 9535 constructor(descriptor: string) { 9536 super(descriptor); 9537 } 9538 9539 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9540 if (code === 1) { 9541 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9542 return true; 9543 } else { 9544 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9545 return false; 9546 } 9547 } 9548 // 同时调用仅会执行onRemoteMessageRequest 9549 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9550 if (code === 1) { 9551 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9552 } else { 9553 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9554 return false; 9555 } 9556 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9557 setTimeout(resolve, 100); 9558 }) 9559 return true; 9560 } 9561 } 9562 ``` 9563 9564### onRemoteRequest<sup>(deprecated)</sup> 9565 9566onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9567 9568sendRequest请求的响应处理函数,服务端在该函数里处理请求,回复结果。 9569 9570> **说明:** 9571> 9572> 从API version 9 开始废弃,建议使用[onRemoteMessageRequest](#onremotemessagerequest9)替代。 9573 9574**系统能力**:SystemCapability.Communication.IPC.Core 9575 9576**参数:** 9577 9578 | 参数名 | 类型 | 必填 | 说明 | 9579 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 9580 | code | number | 是 | 对端发送的服务请求码。 | 9581 | data | [MessageParcel](#messageparceldeprecated) | 是 | 携带客户端调用参数的MessageParcel对象。 | 9582 | reply | [MessageParcel](#messageparceldeprecated) | 是 | 写入结果的MessageParcel对象。 | 9583 | options | [MessageOption](#messageoption) | 是 | 指示操作是同步还是异步。 | 9584 9585**返回值:** 9586 9587 | 类型 | 说明 | 9588 | ------- | -------------------------------- | 9589 | boolean | true:操作成功,false:操作失败。| 9590 9591**示例:** 9592<!--deprecated_code_no_check--> 9593 ```ts 9594 import { rpc } from '@kit.IPCKit'; 9595 import { hilog } from '@kit.PerformanceAnalysisKit'; 9596 9597 class TestRemoteObject extends rpc.RemoteObject { 9598 constructor(descriptor: string) { 9599 super(descriptor); 9600 } 9601 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9602 if (code === 1) { 9603 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9604 return true; 9605 } else { 9606 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9607 return false; 9608 } 9609 } 9610 } 9611 ``` 9612 9613### getCallingUid 9614 9615getCallingUid(): number 9616 9617获取通信对端的进程Uid。 9618 9619**系统能力**:SystemCapability.Communication.IPC.Core 9620 9621**返回值:** 9622 | 类型 | 说明 | 9623 | ------ | ----------------------- | 9624 | number | 返回通信对端的进程Uid。 | 9625 9626**示例:** 9627 9628 ```ts 9629 import { rpc } from '@kit.IPCKit'; 9630 import { hilog } from '@kit.PerformanceAnalysisKit'; 9631 9632 class TestRemoteObject extends rpc.RemoteObject { 9633 constructor(descriptor: string) { 9634 super(descriptor); 9635 } 9636 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9637 // 根据业务实际逻辑,进行相应处理 9638 return true; 9639 } 9640 } 9641 try { 9642 let testRemoteObject = new TestRemoteObject("testObject"); 9643 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9644 } catch (error) { 9645 hilog.error(0x0000, 'testTag', 'error: ' + error); 9646 } 9647 ``` 9648 9649### getCallingPid 9650 9651getCallingPid(): number 9652 9653获取通信对端的进程Pid。 9654 9655**系统能力**:SystemCapability.Communication.IPC.Core 9656 9657**返回值:** 9658 9659 | 类型 | 说明 | 9660 | ------ | ----------------------- | 9661 | number | 返回通信对端的进程Pid。 | 9662 9663**示例:** 9664 9665 ```ts 9666 import { rpc } from '@kit.IPCKit'; 9667 import { hilog } from '@kit.PerformanceAnalysisKit'; 9668 9669 class TestRemoteObject extends rpc.RemoteObject { 9670 constructor(descriptor: string) { 9671 super(descriptor); 9672 } 9673 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9674 // 根据业务实际逻辑,进行相应处理 9675 return true; 9676 } 9677 } 9678 try { 9679 let testRemoteObject = new TestRemoteObject("testObject"); 9680 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9681 } catch (error) { 9682 hilog.error(0x0000, 'testTag', 'error: ' + error); 9683 } 9684 ``` 9685 9686### getLocalInterface<sup>9+</sup> 9687 9688getLocalInterface(descriptor: string): IRemoteBroker 9689 9690查询接口描述符的字符串。 9691 9692**系统能力**:SystemCapability.Communication.IPC.Core 9693 9694**参数:** 9695 9696 | 参数名 | 类型 | 必填 | 说明 | 9697 | ---------- | ------ | ---- | -------------------- | 9698 | descriptor | string | 是 | 接口描述符的字符串,其长度应小于40960字节。 | 9699 9700**返回值:** 9701 9702 | 类型 | 说明 | 9703 | ------------- | --------------------------------------------- | 9704 | [IRemoteBroker](#iremotebroker) | 返回绑定到指定接口描述符的IRemoteBroker对象。 | 9705 9706**错误码:** 9707 9708以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9709 9710 | 错误码ID | 错误信息 | 9711 | -------- | -------- | 9712 | 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. | 9713 9714**示例:** 9715 9716 ```ts 9717 import { rpc } from '@kit.IPCKit'; 9718 import { hilog } from '@kit.PerformanceAnalysisKit'; 9719 import { BusinessError } from '@kit.BasicServicesKit'; 9720 9721 class TestRemoteObject extends rpc.RemoteObject { 9722 constructor(descriptor: string) { 9723 super(descriptor); 9724 } 9725 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9726 // 根据业务实际逻辑,进行相应处理 9727 return true; 9728 } 9729 } 9730 try { 9731 let testRemoteObject = new TestRemoteObject("testObject"); 9732 testRemoteObject.getLocalInterface("testObject"); 9733 } catch (error) { 9734 let e: BusinessError = error as BusinessError; 9735 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 9736 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 9737 } 9738 ``` 9739 9740### queryLocalInterface<sup>(deprecated)</sup> 9741 9742queryLocalInterface(descriptor: string): IRemoteBroker 9743 9744查询并获取当前接口描述符对应的远端对象是否已经存在。 9745 9746> **说明:** 9747> 9748> 从API version 9 开始废弃,建议使用[getLocalInterface](#getlocalinterface9-2)替代。 9749 9750**系统能力**:SystemCapability.Communication.IPC.Core 9751 9752**参数:** 9753 9754 | 参数名 | 类型 | 必填 | 说明 | 9755 | ---------- | ------ | ---- | ---------------------- | 9756 | descriptor | string | 是 | 需要查询的接口描述符。 | 9757 9758**返回值:** 9759 9760 | 类型 | 说明 | 9761 | ------------- | ------------------------------------------------------------------ | 9762 | [IRemoteBroker](#iremotebroker) | 如果接口描述符对应的远端对象存在,则返回该远端对象,否则返回Null。 | 9763 9764**示例:** 9765<!--deprecated_code_no_check--> 9766 ```ts 9767 import { rpc } from '@kit.IPCKit'; 9768 import { hilog } from '@kit.PerformanceAnalysisKit'; 9769 9770 class TestRemoteObject extends rpc.RemoteObject { 9771 constructor(descriptor: string) { 9772 super(descriptor); 9773 } 9774 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9775 // 根据业务实际逻辑,进行相应处理 9776 return true; 9777 } 9778 } 9779 try { 9780 let testRemoteObject = new TestRemoteObject("testObject"); 9781 testRemoteObject.queryLocalInterface("testObject"); 9782 } catch (error) { 9783 hilog.error(0x0000, 'testTag', 'error: ' + error); 9784 } 9785 ``` 9786 9787### getDescriptor<sup>9+</sup> 9788 9789getDescriptor(): string 9790 9791获取对象的接口描述符。接口描述符为字符串。 9792 9793**系统能力**:SystemCapability.Communication.IPC.Core 9794 9795**返回值:** 9796 9797 | 类型 | 说明 | 9798 | ------ | ---------------- | 9799 | string | 返回接口描述符。 | 9800 9801**错误码:** 9802 9803以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9804 9805 | 错误码ID | 错误信息 | 9806 | -------- | -------- | 9807 | 1900008 | The proxy or remote object is invalid. | 9808 9809**示例:** 9810 9811 ```ts 9812 import { rpc } from '@kit.IPCKit'; 9813 import { hilog } from '@kit.PerformanceAnalysisKit'; 9814 import { BusinessError } from '@kit.BasicServicesKit'; 9815 9816 class TestRemoteObject extends rpc.RemoteObject { 9817 constructor(descriptor: string) { 9818 super(descriptor); 9819 } 9820 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9821 // 根据业务实际逻辑,进行相应处理 9822 return true; 9823 } 9824 } 9825 try { 9826 let descriptor = testRemoteObject.getDescriptor(); 9827 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9828 } catch (error) { 9829 let e: BusinessError = error as BusinessError; 9830 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 9831 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 9832 } 9833 ``` 9834 9835### getInterfaceDescriptor<sup>(deprecated)</sup> 9836 9837getInterfaceDescriptor(): string 9838 9839查询接口描述符。 9840 9841> **说明:** 9842> 9843> 从API version 9 开始废弃,建议使用[getDescriptor](#getdescriptor9-2)替代。 9844 9845**系统能力**:SystemCapability.Communication.IPC.Core 9846 9847**返回值:** 9848 9849 | 类型 | 说明 | 9850 | ------ | ---------------- | 9851 | string | 返回接口描述符。 | 9852 9853**示例:** 9854<!--deprecated_code_no_check--> 9855 ```ts 9856 import { rpc } from '@kit.IPCKit'; 9857 import { hilog } from '@kit.PerformanceAnalysisKit'; 9858 9859 class TestRemoteObject extends rpc.RemoteObject { 9860 constructor(descriptor: string) { 9861 super(descriptor); 9862 } 9863 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9864 // 根据业务实际逻辑,进行相应处理 9865 return true; 9866 } 9867 } 9868 9869 try { 9870 let testRemoteObject = new TestRemoteObject("testObject"); 9871 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9872 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9873 } catch (error) { 9874 hilog.error(0x0000, 'testTag', 'error ' + error); 9875 } 9876 ``` 9877 9878### modifyLocalInterface<sup>9+</sup> 9879 9880modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9881 9882此接口用于把接口描述符和IRemoteBroker对象绑定。 9883 9884**系统能力**:SystemCapability.Communication.IPC.Core 9885 9886**参数:** 9887 9888| 参数名 | 类型 | 必填 | 说明 | 9889| -------------- | ------------------------------- | ---- | ------------------------------------- | 9890| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9891| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符,其长度应小于40960字节。 | 9892 9893**错误码:** 9894 9895以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 9896 9897 | 错误码ID | 错误信息 | 9898 | -------- | -------- | 9899 | 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. | 9900 9901**示例:** 9902 9903 ```ts 9904 import { rpc } from '@kit.IPCKit'; 9905 import { hilog } from '@kit.PerformanceAnalysisKit'; 9906 import { BusinessError } from '@kit.BasicServicesKit'; 9907 9908 class MyDeathRecipient implements rpc.DeathRecipient { 9909 onRemoteDied() { 9910 hilog.info(0x0000, 'testTag', 'server died'); 9911 } 9912 } 9913 class TestRemoteObject extends rpc.RemoteObject { 9914 constructor(descriptor: string) { 9915 super(descriptor); 9916 try { 9917 this.modifyLocalInterface(this, descriptor); 9918 } catch (error) { 9919 let e: BusinessError = error as BusinessError; 9920 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 9921 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 9922 } 9923 } 9924 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9925 // 方法逻辑需开发者根据业务需要实现 9926 } 9927 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9928 // 方法逻辑需开发者根据业务需要实现 9929 } 9930 } 9931 let testRemoteObject = new TestRemoteObject("testObject"); 9932 ``` 9933 9934### attachLocalInterface<sup>(deprecated)</sup> 9935 9936attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9937 9938此接口用于把接口描述符和IRemoteBroker对象绑定。 9939 9940> **说明:** 9941> 9942> 从API version 9 开始废弃,建议使用[modifyLocalInterface](#modifylocalinterface9)替代。 9943 9944**系统能力**:SystemCapability.Communication.IPC.Core 9945 9946**参数:** 9947 9948| 参数名 | 类型 | 必填 | 说明 | 9949| -------------- | ------------------------------- | ---- | ------------------------------------- | 9950| localInterface | [IRemoteBroker](#iremotebroker) | 是 | 将与描述符绑定的IRemoteBroker对象。 | 9951| descriptor | string | 是 | 用于与IRemoteBroker对象绑定的描述符。 | 9952 9953**示例:** 9954<!--deprecated_code_no_check--> 9955 ```ts 9956 import { rpc } from '@kit.IPCKit'; 9957 import { hilog } from '@kit.PerformanceAnalysisKit'; 9958 9959 class MyDeathRecipient implements rpc.DeathRecipient { 9960 onRemoteDied() { 9961 hilog.info(0x0000, 'testTag', 'server died'); 9962 } 9963 } 9964 class TestRemoteObject extends rpc.RemoteObject { 9965 constructor(descriptor: string) { 9966 super(descriptor); 9967 this.attachLocalInterface(this, descriptor); 9968 } 9969 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9970 // 方法逻辑需开发者根据业务需要实现 9971 return true; 9972 } 9973 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9974 // 方法逻辑需开发者根据业务需要实现 9975 return true; 9976 } 9977 9978 } 9979 let testRemoteObject = new TestRemoteObject("testObject"); 9980 ``` 9981 9982## Ashmem<sup>8+</sup> 9983 9984提供与匿名共享内存对象相关的方法,包括创建、关闭、映射和取消映射Ashmem、从Ashmem读取数据和写入数据、获取Ashmem大小、设置Ashmem保护。 9985共享内存只适用与本设备内跨进程通信。 9986 9987### 属性 9988 9989**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.IPC.Core。 9990 9991 | 名称 | 类型 | 可读 | 可写 | 说明 | 9992 | ---------- | ------ | ----- | ----- |----------------------------------------- | 9993 | PROT_EXEC | number | 是 | 否 | 映射内存保护类型,代表映射的内存可执行。 | 9994 | PROT_NONE | number | 是 | 否 | 映射内存保护类型,代表映射的内存不可访问。| 9995 | PROT_READ | number | 是 | 否 | 映射内存保护类型,代表映射的内存可读。 | 9996 | PROT_WRITE | number | 是 | 否 | 映射内存保护类型,代表映射的内存可写。 | 9997 9998### create<sup>9+</sup> 9999 10000static create(name: string, size: number): Ashmem 10001 10002静态方法,根据指定的名称和大小创建Ashmem对象。 10003 10004**系统能力**:SystemCapability.Communication.IPC.Core 10005 10006**参数:** 10007 10008 | 参数名 | 类型 | 必填 | 说明 | 10009 | ------ | ------ | ---- | ---------------------------- | 10010 | name | string | 是 | Ashmem名称,用于查询Ashmem信息,其长度不能为0。 | 10011 | size | number | 是 | Ashmem的大小,其大小应大于0,以字节为单位。 | 10012 10013**返回值:** 10014 10015| 类型 | 说明 | 10016| ------------------ | ---------------------------------------------- | 10017| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 10018 10019**错误码:** 10020 10021以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10022 10023 | 错误码ID | 错误信息 | 10024 | -------- | -------- | 10025 | 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. | 10026 10027**示例:** 10028 10029 ```ts 10030 import { rpc } from '@kit.IPCKit'; 10031 import { hilog } from '@kit.PerformanceAnalysisKit'; 10032 import { BusinessError } from '@kit.BasicServicesKit'; 10033 10034 try { 10035 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10036 hilog.info(0x0000, 'testTag', 'create ashmem: ' + ashmem); 10037 let size = ashmem.getAshmemSize(); 10038 hilog.info(0x0000, 'testTag', 'size is ' + size); 10039 } catch (error) { 10040 let e: BusinessError = error as BusinessError; 10041 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10042 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10043 } 10044 ``` 10045 10046### createAshmem<sup>(deprecated)</sup> 10047 10048static createAshmem(name: string, size: number): Ashmem 10049 10050静态方法,根据指定的名称和大小创建Ashmem对象。 10051 10052> **说明:** 10053> 10054> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9)替代。 10055 10056**系统能力**:SystemCapability.Communication.IPC.Core 10057 10058**参数:** 10059 10060 | 参数名 | 类型 | 必填 | 说明 | 10061 | ------ | ------ | ---- | ---------------------------- | 10062 | name | string | 是 | 名称,用于查询Ashmem信息。 | 10063 | size | number | 是 | Ashmem的大小,以字节为单位。 | 10064 10065**返回值:** 10066 10067| 类型 | 说明 | 10068| ------------------ | ---------------------------------------------- | 10069| [Ashmem](#ashmem8) | 返回创建的Ashmem对象;如果创建失败,返回null。 | 10070 10071**示例:** 10072<!--deprecated_code_no_check--> 10073 ```ts 10074 import { rpc } from '@kit.IPCKit'; 10075 import { hilog } from '@kit.PerformanceAnalysisKit'; 10076 10077 try { 10078 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 10079 hilog.info(0x0000, 'testTag', 'create ashmem: ' + ashmem); 10080 let size = ashmem.getAshmemSize(); 10081 hilog.info(0x0000, 'testTag', 'size is ' + size); 10082 } catch (error) { 10083 hilog.error(0x0000, 'testTag', 'error ' + error); 10084 } 10085 10086 ``` 10087 10088### create<sup>9+</sup> 10089 10090static create(ashmem: Ashmem): Ashmem 10091 10092静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 10093 10094**系统能力**:SystemCapability.Communication.IPC.Core 10095 10096**参数:** 10097 10098| 参数名 | 类型 | 必填 | 说明 | 10099| ------ | ------------------ | ---- | -------------------- | 10100| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 10101 10102**返回值:** 10103 10104| 类型 | 说明 | 10105| ------------------ | ---------------------- | 10106| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 10107 10108**错误码:** 10109 10110以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10111 10112 | 错误码ID | 错误信息 | 10113 | -------- | -------- | 10114 | 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. | 10115 10116**示例:** 10117 10118 ```ts 10119 import { rpc } from '@kit.IPCKit'; 10120 import { hilog } from '@kit.PerformanceAnalysisKit'; 10121 import { BusinessError } from '@kit.BasicServicesKit'; 10122 10123 try { 10124 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10125 let ashmem2 = rpc.Ashmem.create(ashmem); 10126 let size = ashmem2.getAshmemSize(); 10127 hilog.info(0x0000, 'testTag', 'size is ' + size); 10128 } catch (error) { 10129 let e: BusinessError = error as BusinessError; 10130 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10131 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10132 } 10133 ``` 10134 10135### createAshmemFromExisting<sup>(deprecated)</sup> 10136 10137static createAshmemFromExisting(ashmem: Ashmem): Ashmem 10138 10139静态方法,通过复制现有Ashmem对象的文件描述符(fd)来创建Ashmem对象。两个Ashmem对象指向同一个共享内存区域。 10140 10141> **说明:** 10142> 10143> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[create](#create9-1)替代。 10144 10145**系统能力**:SystemCapability.Communication.IPC.Core 10146 10147**参数:** 10148 10149| 参数名 | 类型 | 必填 | 说明 | 10150| ------ | ------------------ | ---- | -------------------- | 10151| ashmem | [Ashmem](#ashmem8) | 是 | 已存在的Ashmem对象。 | 10152 10153**返回值:** 10154 10155| 类型 | 说明 | 10156| ------------------ | ---------------------- | 10157| [Ashmem](#ashmem8) | 返回创建的Ashmem对象。 | 10158 10159**示例:** 10160<!--deprecated_code_no_check--> 10161 ```ts 10162 import { rpc } from '@kit.IPCKit'; 10163 import { hilog } from '@kit.PerformanceAnalysisKit'; 10164 10165 try { 10166 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10167 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 10168 let size = ashmem2.getAshmemSize(); 10169 hilog.info(0x0000, 'testTag', 'size is ' + size); 10170 } catch (error) { 10171 hilog.error(0x0000, 'testTag', 'error is ' + error); 10172 } 10173 ``` 10174 10175### closeAshmem<sup>8+</sup> 10176 10177closeAshmem(): void 10178 10179关闭这个Ashmem。 10180 10181> **说明:** 10182> 10183> 关闭Ashmem对象前需要先解除地址映射。 10184 10185**系统能力**:SystemCapability.Communication.IPC.Core 10186 10187**示例:** 10188 10189 ```ts 10190 import { rpc } from '@kit.IPCKit'; 10191 import { hilog } from '@kit.PerformanceAnalysisKit'; 10192 10193 try { 10194 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10195 ashmem.closeAshmem(); 10196 } catch (error) { 10197 hilog.error(0x0000, 'testTag', 'error is ' + error); 10198 } 10199 ``` 10200 10201### unmapAshmem<sup>8+</sup> 10202 10203unmapAshmem(): void 10204 10205删除该Ashmem对象的地址映射。 10206 10207**系统能力**:SystemCapability.Communication.IPC.Core 10208 10209**示例:** 10210 10211 ```ts 10212 import { rpc } from '@kit.IPCKit'; 10213 import { hilog } from '@kit.PerformanceAnalysisKit'; 10214 10215 try { 10216 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10217 ashmem.unmapAshmem(); 10218 } catch (error) { 10219 hilog.error(0x0000, 'testTag', 'error is ' + error); 10220 } 10221 ``` 10222 10223### getAshmemSize<sup>8+</sup> 10224 10225getAshmemSize(): number 10226 10227获取Ashmem对象的内存大小。 10228 10229**系统能力**:SystemCapability.Communication.IPC.Core 10230 10231**返回值:** 10232 10233 | 类型 | 说明 | 10234 | ------ | -------------------------- | 10235 | number | 返回Ashmem对象的内存大小。 | 10236 10237**示例:** 10238 10239 ```ts 10240 import { rpc } from '@kit.IPCKit'; 10241 import { hilog } from '@kit.PerformanceAnalysisKit'; 10242 10243 try { 10244 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10245 let size = ashmem.getAshmemSize(); 10246 hilog.info(0x0000, 'testTag', ' size is ' + size); 10247 } catch (error) { 10248 hilog.error(0x0000, 'testTag', 'error is ' + error); 10249 } 10250 ``` 10251 10252### mapTypedAshmem<sup>9+</sup> 10253 10254mapTypedAshmem(mapType: number): void 10255 10256在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 10257 10258**系统能力**:SystemCapability.Communication.IPC.Core 10259 10260**参数:** 10261 10262 | 参数名 | 类型 | 必填 | 说明 | 10263 | ------- | ------ | ---- | ------------------------------ | 10264 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 10265 10266**错误码:** 10267 10268以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10269 10270 | 错误码ID | 错误信息 | 10271 | -------- | -------- | 10272 | 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. | 10273 | 1900001 | Failed to call mmap. | 10274 10275**示例:** 10276 10277 ```ts 10278 import { rpc } from '@kit.IPCKit'; 10279 import { hilog } from '@kit.PerformanceAnalysisKit'; 10280 import { BusinessError } from '@kit.BasicServicesKit'; 10281 10282 try { 10283 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10284 ashmem.mapTypedAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE); 10285 } catch (error) { 10286 let e: BusinessError = error as BusinessError; 10287 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10288 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10289 } 10290 ``` 10291 10292### mapAshmem<sup>(deprecated)</sup> 10293 10294mapAshmem(mapType: number): boolean 10295 10296在此进程的虚拟地址空间上创建共享文件映射,映射区域大小由此Ashmem对象指定。 10297 10298> **说明:** 10299> 10300> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapTypedAshmem](#maptypedashmem9)替代。 10301 10302**系统能力**:SystemCapability.Communication.IPC.Core 10303 10304**参数:** 10305 10306 | 参数名 | 类型 | 必填 | 说明 | 10307 | ------- | ------ | ---- | ------------------------------ | 10308 | mapType | number | 是 | 指定映射的内存区域的保护等级。 | 10309 10310**返回值:** 10311 10312 | 类型 | 说明 | 10313 | ------- | -------------------------------- | 10314 | boolean | true:映射成功,false:映射失败。| 10315 10316**示例:** 10317<!--deprecated_code_no_check--> 10318 ```ts 10319 import { rpc } from '@kit.IPCKit'; 10320 import { hilog } from '@kit.PerformanceAnalysisKit'; 10321 10322 try { 10323 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10324 let mapReadAndWrite = ashmem.mapAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE); 10325 hilog.info(0x0000, 'testTag', 'map ashmem result is ' + mapReadAndWrite); 10326 } catch (error) { 10327 hilog.error(0x0000, 'testTag', 'error is ' + error); 10328 } 10329 ``` 10330 10331### mapReadWriteAshmem<sup>9+</sup> 10332 10333mapReadWriteAshmem(): void 10334 10335在此进程虚拟地址空间上创建可读写的共享文件映射。 10336 10337**系统能力**:SystemCapability.Communication.IPC.Core 10338 10339**错误码:** 10340 10341以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10342 10343 | 错误码ID | 错误信息 | 10344 | -------- | -------- | 10345 | 1900001 | Failed to call mmap. | 10346 10347**示例:** 10348 10349 ```ts 10350 import { rpc } from '@kit.IPCKit'; 10351 import { hilog } from '@kit.PerformanceAnalysisKit'; 10352 import { BusinessError } from '@kit.BasicServicesKit'; 10353 10354 try { 10355 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10356 ashmem.mapReadWriteAshmem(); 10357 } catch (error) { 10358 let e: BusinessError = error as BusinessError; 10359 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10360 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10361 } 10362 ``` 10363 10364### mapReadAndWriteAshmem<sup>(deprecated)</sup> 10365 10366mapReadAndWriteAshmem(): boolean 10367 10368在此进程虚拟地址空间上创建可读写的共享文件映射。 10369 10370> **说明:** 10371> 10372> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadWriteAshmem](#mapreadwriteashmem9)替代。 10373 10374**系统能力**:SystemCapability.Communication.IPC.Core 10375 10376**返回值:** 10377 10378 | 类型 | 说明 | 10379 | ------- | -------------------------------- | 10380 | boolean | true:映射成功,false:映射失败。| 10381 10382**示例:** 10383<!--deprecated_code_no_check--> 10384 ```ts 10385 import { rpc } from '@kit.IPCKit'; 10386 import { hilog } from '@kit.PerformanceAnalysisKit'; 10387 10388 try { 10389 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10390 let mapResult = ashmem.mapReadAndWriteAshmem(); 10391 hilog.info(0x0000, 'testTag', 'map ashmem result is ' + mapResult); 10392 } catch (error) { 10393 hilog.error(0x0000, 'testTag', 'error is ' + error); 10394 } 10395 ``` 10396 10397### mapReadonlyAshmem<sup>9+</sup> 10398 10399mapReadonlyAshmem(): void 10400 10401在此进程虚拟地址空间上创建只读的共享文件映射。 10402 10403**系统能力**:SystemCapability.Communication.IPC.Core 10404 10405**错误码:** 10406 10407以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10408 10409 | 错误码ID | 错误信息 | 10410 | -------- | -------- | 10411 | 1900001 | Failed to call mmap. | 10412 10413**示例:** 10414 10415 ```ts 10416 import { rpc } from '@kit.IPCKit'; 10417 import { hilog } from '@kit.PerformanceAnalysisKit'; 10418 import { BusinessError } from '@kit.BasicServicesKit'; 10419 10420 try { 10421 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10422 ashmem.mapReadonlyAshmem(); 10423 } catch (error) { 10424 let e: BusinessError = error as BusinessError; 10425 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10426 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10427 } 10428 ``` 10429 10430### mapReadOnlyAshmem<sup>(deprecated)</sup> 10431 10432mapReadOnlyAshmem(): boolean 10433 10434在此进程虚拟地址空间上创建只读的共享文件映射。 10435 10436> **说明:** 10437> 10438> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[mapReadonlyAshmem](#mapreadonlyashmem9)替代。 10439 10440**系统能力**:SystemCapability.Communication.IPC.Core 10441 10442**返回值:** 10443 10444 | 类型 | 说明 | 10445 | ------- | -------------------------------- | 10446 | boolean | true:映射成功,false:映射失败。| 10447 10448**示例:** 10449<!--deprecated_code_no_check--> 10450 ```ts 10451 import { rpc } from '@kit.IPCKit'; 10452 import { hilog } from '@kit.PerformanceAnalysisKit'; 10453 10454 try { 10455 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10456 let mapResult = ashmem.mapReadOnlyAshmem(); 10457 hilog.info(0x0000, 'testTag', 'Ashmem mapReadOnlyAshmem result is ' + mapResult); 10458 } catch (error) { 10459 hilog.error(0x0000, 'testTag', 'error is ' + error); 10460 } 10461 ``` 10462 10463### setProtectionType<sup>9+</sup> 10464 10465setProtectionType(protectionType: number): void 10466 10467设置映射内存区域的保护等级。 10468 10469**系统能力**:SystemCapability.Communication.IPC.Core 10470 10471**参数:** 10472 10473 | 参数名 | 类型 | 必填 | 说明 | 10474 | -------------- | ------ | ---- | ------------------ | 10475 | protectionType | number | 是 | 要设置的保护类型。 | 10476 10477**错误码:** 10478 10479以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10480 10481 | 错误码ID | 错误信息 | 10482 | -------- | -------- | 10483 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10484 | 1900002 | Failed to call ioctl. | 10485 10486**示例:** 10487 10488 ```ts 10489 import { rpc } from '@kit.IPCKit'; 10490 import { hilog } from '@kit.PerformanceAnalysisKit'; 10491 import { BusinessError } from '@kit.BasicServicesKit'; 10492 10493 try { 10494 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10495 ashmem.setProtectionType(rpc.Ashmem.PROT_READ); 10496 } catch (error) { 10497 let e: BusinessError = error as BusinessError; 10498 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10499 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10500 } 10501 ``` 10502 10503### setProtection<sup>(deprecated)</sup> 10504 10505setProtection(protectionType: number): boolean 10506 10507设置映射内存区域的保护等级。 10508 10509> **说明:** 10510> 10511> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[setProtectionType](#setprotectiontype9)替代。 10512 10513**系统能力**:SystemCapability.Communication.IPC.Core 10514 10515**参数:** 10516 10517 | 参数名 | 类型 | 必填 | 说明 | 10518 | -------------- | ------ | ---- | ------------------ | 10519 | protectionType | number | 是 | 要设置的保护类型。 | 10520 10521**返回值:** 10522 10523 | 类型 | 说明 | 10524 | ------- | -------------------------------- | 10525 | boolean | true:设置成功,false:设置失败。| 10526 10527**示例:** 10528<!--deprecated_code_no_check--> 10529 ```ts 10530 import { rpc } from '@kit.IPCKit'; 10531 import { hilog } from '@kit.PerformanceAnalysisKit'; 10532 10533 try { 10534 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10535 let result = ashmem.setProtection(rpc.Ashmem.PROT_READ); 10536 hilog.info(0x0000, 'testTag', 'Ashmem setProtection result is ' + result); 10537 } catch (error) { 10538 let e: BusinessError = error as BusinessError; 10539 hilog.error(0x0000, 'testTag', 'error ' + error); 10540 } 10541 ``` 10542 10543### writeDataToAshmem<sup>11+</sup> 10544 10545writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10546 10547将数据写入此Ashmem对象关联的共享文件。 10548 10549> **说明:** 10550> 10551> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10552 10553**系统能力**:SystemCapability.Communication.IPC.Core 10554 10555**参数:** 10556 10557 | 参数名 | 类型 | 必填 | 说明 | 10558 | ------ | -------- | ---- | -------------------------------------------------- | 10559 | buf | ArrayBuffer | 是 | 写入Ashmem对象的数据。 | 10560 | size | number | 是 | 要写入的数据大小。 | 10561 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10562 10563**错误码:** 10564 10565以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10566 10567 | 错误码ID | 错误信息 | 10568 | -------- | -------- | 10569 | 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. | 10570 | 1900003 | Failed to write data to the shared memory. | 10571 10572**示例:** 10573 10574 ```ts 10575 import { rpc } from '@kit.IPCKit'; 10576 import { hilog } from '@kit.PerformanceAnalysisKit'; 10577 import { BusinessError } from '@kit.BasicServicesKit'; 10578 10579 try { 10580 let buffer = new ArrayBuffer(1024); 10581 let int32View = new Int32Array(buffer); 10582 for (let i = 0; i < int32View.length; i++) { 10583 int32View[i] = i * 2 + 1; 10584 } 10585 let size = buffer.byteLength; 10586 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10587 ashmem.mapReadWriteAshmem(); 10588 ashmem.writeDataToAshmem(buffer, size, 0); 10589 } catch (error) { 10590 let e: BusinessError = error as BusinessError; 10591 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10592 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10593 } 10594 ``` 10595 10596### writeAshmem<sup>(deprecated)</sup> 10597 10598writeAshmem(buf: number[], size: number, offset: number): void 10599 10600将数据写入此Ashmem对象关联的共享文件。 10601 10602> **说明:** 10603> 10604> 从API version 9 开始支持,从API version 11 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10605> 10606> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10607 10608**系统能力**:SystemCapability.Communication.IPC.Core 10609 10610**参数:** 10611 10612 | 参数名 | 类型 | 必填 | 说明 | 10613 | ------ | -------- | ---- | -------------------------------------------------- | 10614 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10615 | size | number | 是 | 要写入的数据大小。 | 10616 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10617 10618**错误码:** 10619 10620以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10621 10622 | 错误码ID | 错误信息 | 10623 | -------- | -------- | 10624 | 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. | 10625 | 1900003 | Failed to write data to the shared memory. | 10626 10627**示例:** 10628<!--deprecated_code_no_check--> 10629 ```ts 10630 import { rpc } from '@kit.IPCKit'; 10631 import { hilog } from '@kit.PerformanceAnalysisKit'; 10632 import { BusinessError } from '@kit.BasicServicesKit'; 10633 10634 try { 10635 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10636 ashmem.mapReadWriteAshmem(); 10637 let ByteArrayVar = [1, 2, 3, 4, 5]; 10638 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10639 } catch (error) { 10640 let e: BusinessError = error as BusinessError; 10641 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10642 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10643 } 10644 ``` 10645 10646### writeToAshmem<sup>(deprecated)</sup> 10647 10648writeToAshmem(buf: number[], size: number, offset: number): boolean 10649 10650将数据写入此Ashmem对象关联的共享文件。 10651 10652> **说明:** 10653> 10654> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[writeDataToAshmem](#writedatatoashmem11)替代。 10655> 10656> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10657 10658**系统能力**:SystemCapability.Communication.IPC.Core 10659 10660**参数:** 10661 10662 | 参数名 | 类型 | 必填 | 说明 | 10663 | ------ | -------- | ---- | -------------------------------------------------- | 10664 | buf | number[] | 是 | 写入Ashmem对象的数据。 | 10665 | size | number | 是 | 要写入的数据大小。 | 10666 | offset | number | 是 | 要写入的数据在此Ashmem对象关联的内存区间的起始位置。 | 10667 10668**返回值:** 10669 10670 | 类型 | 说明 | 10671 | ------- | ----------------------------------------------------------------------------- | 10672 | boolean | true:如果数据写入成功,false:在其他情况下,如数据写入越界或未获得写入权限。 | 10673 10674**示例:** 10675<!--deprecated_code_no_check--> 10676 ```ts 10677 import { rpc } from '@kit.IPCKit'; 10678 import { hilog } from '@kit.PerformanceAnalysisKit'; 10679 10680 try { 10681 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10682 let mapResult = ashmem.mapReadAndWriteAshmem(); 10683 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10684 let ByteArrayVar = [1, 2, 3, 4, 5]; 10685 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10686 hilog.info(0x0000, 'testTag', 'write to Ashmem result is ' + writeResult); 10687 } catch (error) { 10688 hilog.error(0x0000, 'testTag', 'error is ' + error); 10689 } 10690 ``` 10691 10692### readDataFromAshmem<sup>11+</sup> 10693 10694readDataFromAshmem(size: number, offset: number): ArrayBuffer 10695 10696从此Ashmem对象关联的共享文件中读取数据。 10697 10698> **说明:** 10699> 10700> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10701 10702**系统能力**:SystemCapability.Communication.IPC.Core 10703 10704**参数:** 10705 10706 | 参数名 | 类型 | 必填 | 说明 | 10707 | ------ | ------ | ---- | -------------------------------------------------- | 10708 | size | number | 是 | 要读取的数据的大小。 | 10709 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10710 10711**返回值:** 10712 10713 | 类型 | 说明 | 10714 | -------- | ---------------- | 10715 | ArrayBuffer | 返回读取的数据。 | 10716 10717**错误码:** 10718 10719以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10720 10721 | 错误码ID | 错误信息 | 10722 | -------- | -------- | 10723 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10724 | 1900004 | Failed to read data from the shared memory. | 10725 10726**示例:** 10727 10728 ```ts 10729 import { rpc } from '@kit.IPCKit'; 10730 import { hilog } from '@kit.PerformanceAnalysisKit'; 10731 import { BusinessError } from '@kit.BasicServicesKit'; 10732 10733 try { 10734 let buffer = new ArrayBuffer(1024); 10735 let int32View = new Int32Array(buffer); 10736 for (let i = 0; i < int32View.length; i++) { 10737 int32View[i] = i * 2 + 1; 10738 } 10739 let size = buffer.byteLength; 10740 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10741 ashmem.mapReadWriteAshmem(); 10742 ashmem.writeDataToAshmem(buffer, size, 0); 10743 let readResult = ashmem.readDataFromAshmem(size, 0); 10744 let readInt32View = new Int32Array(readResult); 10745 hilog.info(0x0000, 'testTag', 'read from Ashmem result is ' + readInt32View); 10746 } catch (error) { 10747 let e: BusinessError = error as BusinessError; 10748 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10749 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10750 } 10751 ``` 10752 10753### readAshmem<sup>(deprecated)</sup> 10754 10755readAshmem(size: number, offset: number): number[] 10756 10757从此Ashmem对象关联的共享文件中读取数据。 10758 10759> **说明:** 10760> 10761> 从API version 9 开始支持,从API version 11 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10762> 10763> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10764 10765 10766**系统能力**:SystemCapability.Communication.IPC.Core 10767 10768**参数:** 10769 10770 | 参数名 | 类型 | 必填 | 说明 | 10771 | ------ | ------ | ---- | -------------------------------------------------- | 10772 | size | number | 是 | 要读取的数据的大小。 | 10773 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10774 10775**返回值:** 10776 10777 | 类型 | 说明 | 10778 | -------- | ---------------- | 10779 | number[] | 返回读取的数据。 | 10780 10781**错误码:** 10782 10783以下错误码的详细介绍请参见[ohos.rpc错误码](errorcode-rpc.md) 10784 10785 | 错误码ID | 错误信息 | 10786 | -------- | -------- | 10787 | 401 | Parameter error. Possible causes: <br/> 1.The number of parameters is incorrect; <br/> 2.The parameter type does not match. | 10788 | 1900004 | Failed to read data from the shared memory. | 10789 10790**示例:** 10791<!--deprecated_code_no_check--> 10792 ```ts 10793 import { rpc } from '@kit.IPCKit'; 10794 import { hilog } from '@kit.PerformanceAnalysisKit'; 10795 import { BusinessError } from '@kit.BasicServicesKit'; 10796 10797 try { 10798 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10799 ashmem.mapReadWriteAshmem(); 10800 let ByteArrayVar = [1, 2, 3, 4, 5]; 10801 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10802 let readResult = ashmem.readAshmem(5, 0); 10803 hilog.info(0x0000, 'testTag', 'read from Ashmem result is ' + readResult); 10804 } catch (error) { 10805 let e: BusinessError = error as BusinessError; 10806 hilog.error(0x0000, 'testTag', 'errorCode ' + e.code); 10807 hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message); 10808 } 10809 ``` 10810 10811### readFromAshmem<sup>(deprecated)</sup> 10812 10813readFromAshmem(size: number, offset: number): number[] 10814 10815从此Ashmem对象关联的共享文件中读取数据。 10816 10817> **说明:** 10818> 10819> 从API version 8 开始支持,从API version 9 开始废弃,建议使用[readDataFromAshmem](#readdatafromashmem11)替代。 10820> 10821> 对Ashmem对象进行写操作时,需要先调用[mapReadWriteAshmem](#mapreadwriteashmem9)进行映射。 10822 10823**系统能力**:SystemCapability.Communication.IPC.Core 10824 10825**参数:** 10826 10827 | 参数名 | 类型 | 必填 | 说明 | 10828 | ------ | ------ | ---- | -------------------------------------------------- | 10829 | size | number | 是 | 要读取的数据的大小。 | 10830 | offset | number | 是 | 要读取的数据在此Ashmem对象关联的内存区间的起始位置。 | 10831 10832**返回值:** 10833 10834 | 类型 | 说明 | 10835 | -------- | ---------------- | 10836 | number[] | 返回读取的数据。 | 10837 10838**示例:** 10839<!--deprecated_code_no_check--> 10840 ``` ts 10841 import { rpc } from '@kit.IPCKit'; 10842 import { hilog } from '@kit.PerformanceAnalysisKit'; 10843 10844 try { 10845 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10846 let mapResult = ashmem.mapReadAndWriteAshmem(); 10847 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10848 let ByteArrayVar = [1, 2, 3, 4, 5]; 10849 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10850 hilog.info(0x0000, 'testTag', 'write to Ashmem result is ' + writeResult); 10851 let readResult = ashmem.readFromAshmem(5, 0); 10852 hilog.info(0x0000, 'testTag', 'read to Ashmem result is ' + readResult); 10853 } catch (error) { 10854 hilog.error(0x0000, 'testTag', 'error is ' + error); 10855 } 10856 ``` 10857