diff --git a/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md b/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md index 5cda83f99b457bde0348c2cf99d9e170e9e2d659..b8e47f7d035c7efc9b2e25c193689c9a4709210e 100755 --- a/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md +++ b/zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md @@ -180,7 +180,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, 1. 添加依赖 ```ts - import rpc from "@ohos.rpc"; + import rpc from '@ohos.rpc'; // 仅FA模型需要导入@ohos.ability.featureAbility // import featureAbility from "@ohos.ability.featureAbility"; ``` @@ -188,17 +188,20 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, Stage模型需要获取context ```ts - import Ability from "@ohos.app.ability.UIAbility"; + import UIAbility from '@ohos.app.ability.UIAbility'; + import Want from '@ohos.app.ability.Want'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + import window from '@ohos.window'; - export default class MainAbility extends Ability { - onCreate(want, launchParam) { + export default class MainAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { console.log("[Demo] MainAbility onCreate"); - globalThis.context = this.context; + let context = this.context; } onDestroy() { console.log("[Demo] MainAbility onDestroy"); } - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { // Main window is created, set main page for this ability console.log("[Demo] MainAbility onWindowStageCreate"); } @@ -222,53 +225,62 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, 首先,构造变量want,指定要绑定的Ability所在应用的包名、组件名,如果是跨设备的场景,还需要绑定目标设备NetworkId(组网场景下对应设备的标识符,可以使用deviceManager获取目标设备的NetworkId);然后,构造变量connect,指定绑定成功、绑定失败、断开连接时的回调函数;最后,FA模型使用featureAbility提供的接口绑定Ability,Stage模型通过context获取服务后用提供的接口绑定Ability。 ```ts - import rpc from "@ohos.rpc"; // 仅FA模型需要导入@ohos.ability.featureAbility // import featureAbility from "@ohos.ability.featureAbility"; + import rpc from '@ohos.rpc'; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import deviceManager from '@ohos.distributedHardware.deviceManager'; - let proxy = null; - let connectId = null; + let proxy: rpc.RemoteProxy; + let connectId: number; // 单个设备绑定Ability - let want = { + let want: Want = { // 包名和组件名写实际的值 - "bundleName": "ohos.rpc.test.server", - "abilityName": "ohos.rpc.test.server.ServiceAbility", + bundleName: "ohos.rpc.test.server", + abilityName: "ohos.rpc.test.server.ServiceAbility", }; - let connect = { - onConnect:function(elementName, remote) { + let connect: common.ConnectOptions = { + onConnect: (elementName, remote: rpc.RemoteProxy) => { proxy = remote; }, - onDisconnect:function(elementName) { + onDisconnect: (elementName) => { }, - onFailed:function() { - proxy = null; + onFailed: () => { + proxy; } }; // FA模型使用此方法连接服务 // connectId = featureAbility.connectAbility(want, connect); - connectId = globalThis.context.connectServiceExtensionAbility(want,connect); + connectId = this.context.connectServiceExtensionAbility(want,connect); - // 如果是跨设备绑定,可以使用deviceManager获取目标设备NetworkId - import deviceManager from '@ohos.distributedHardware.deviceManager'; - function deviceManagerCallback(deviceManager) { - let deviceList = deviceManager.getTrustedDeviceListSync(); - let networkId = deviceList[0].networkId; - let want = { - "bundleName": "ohos.rpc.test.server", - "abilityName": "ohos.rpc.test.service.ServiceAbility", - "networkId": networkId, - "flags": 256 - }; - // 建立连接后返回的Id需要保存下来,在断开连接时需要作为参数传入 - // FA模型使用此方法连接服务 - // connectId = featureAbility.connectAbility(want, connect); - - connectId = globalThis.context.connectServiceExtensionAbility(want,connect); - } + // 跨设备绑定 // 第一个参数是本应用的包名,第二个参数是接收deviceManager的回调函数 - deviceManager.createDeviceManager("ohos.rpc.test", deviceManagerCallback); + deviceManager.createDeviceManager("ohos.rpc.test", (err: Error, data: deviceManager.DeviceManager) => { + if (err) { + console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); + return; + } + console.info("createDeviceManager success"); + let dmInstance = data; + }); + + // 使用deviceManager获取目标设备NetworkId + let deviceList: Array = dmInstance.getTrustedDeviceListSync(); + let networkId: string = deviceList[0].networkId; + let want: Want = { + bundleName: "ohos.rpc.test.server", + abilityName: "ohos.rpc.test.service.ServiceAbility", + deviceId: networkId, + flags: 256 + }; + // 建立连接后返回的Id需要保存下来,在断开连接时需要作为参数传入 + // FA模型使用此方法连接服务 + // connectId = featureAbility.connectAbility(want, connect); + + connectId = this.context.connectServiceExtensionAbility(want,connect); ``` @@ -278,14 +290,14 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ```ts onConnect(want: Want) { - var robj:rpc.RemoteObject = new Stub("rpcTestAbility"); + const robj: rpc.RemoteObject = new Stub("rpcTestAbility"); return robj; } class Stub extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { // 根据code处理客户端的请求 return true; } @@ -297,20 +309,21 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, 客户端在onConnect回调里接收到代理对象,调用sendRequest方法发起请求,在期约(JavaScript期约:用于表示一个异步操作的最终完成或失败及其结果值)或者回调函数里接收结果。 ```ts + import rpc from '@ohos.rpc'; // 使用期约 let option = new rpc.MessageOption(); let data = rpc.MessageParcel.create(); let reply = rpc.MessageParcel.create(); // 往data里写入参数 proxy.sendRequest(1, data, reply, option) - .then(function(result) { + .then((result: rpc.SendRequestResult) => { if (result.errCode != 0) { console.error("send request failed, errCode: " + result.errCode); return; } // 从result.reply里读取结果 }) - .catch(function(e) { + .catch((e: Error) => { console.error("send request got exception: " + e); }) .finally(() => { @@ -319,7 +332,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, }) // 使用回调函数 - function sendRequestCallback(result) { + function sendRequestCallback(result: rpc.SendRequestResult) { try { if (result.errCode != 0) { console.error("send request failed, errCode: " + result.errCode); @@ -352,7 +365,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, // FA模型使用此方法断开连接 // featureAbility.disconnectAbility(connectId, disconnectCallback); - globalThis.context.disconnectServiceExtensionAbility(connectId); + this.context.disconnectServiceExtensionAbility(connectId); ``` ## 相关实例 diff --git a/zh-cn/application-dev/connectivity/subscribe-remote-state.md b/zh-cn/application-dev/connectivity/subscribe-remote-state.md index f53964f6908212a3bf04e89a38541fac8962fd01..15c6fc81feba681b646214925331346561d104ff 100755 --- a/zh-cn/application-dev/connectivity/subscribe-remote-state.md +++ b/zh-cn/application-dev/connectivity/subscribe-remote-state.md @@ -93,17 +93,21 @@ result = object->RemoveDeathRecipient(deathRecipient); // 移除消亡通知 Stage模型在连接服务前需要先获取context ```ts -import Ability from "@ohos.app.ability.UIAbility"; +import UIAbility from '@ohos.app.ability.UIAbility'; +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import window from '@ohos.window'; -export default class MainAbility extends Ability { - onCreate(want, launchParam) { + +export default class MainAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { console.log("[Demo] MainAbility onCreate"); - globalThis.context = this.context; + let context = this.context; } onDestroy() { console.log("[Demo] MainAbility onDestroy"); } - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { // Main window is created, set main page for this ability console.log("[Demo] MainAbility onWindowStageCreate"); } @@ -127,36 +131,42 @@ export default class MainAbility extends Ability { ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; +import Want from '@ohos.app.ability.Want'; +import common from '@ohos.app.ability.common'; -let proxy; -let connect = { - onConnect: function(elementName, remoteProxy) { +let proxy: rpc.RemoteProxy; +let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; -let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", +let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型通过此方法连接服务 // FA.connectAbility(want, connect); -globalThis.context.connectServiceExtensionAbility(want, connect); +this.context.connectServiceExtensionAbility(want, connect); +``` + +上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 -class MyDeathRecipient { +```ts +class MyDeathRecipient implements rpc.DeathRecipient{ onRemoteDied() { console.log("server died"); } } let deathRecipient = new MyDeathRecipient(); -proxy.registerDeathRecippient(deathRecipient, 0); +proxy.registerDeathRecipient(deathRecipient, 0); proxy.unregisterDeathRecipient(deathRecipient, 0); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-rpc.md b/zh-cn/application-dev/reference/apis/js-apis-rpc.md index 5be9c53081d4145130204de058e4cfdc1c13adc2..42db98cdbb0189569ff1773a03eeb43930c7575c 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-rpc.md +++ b/zh-cn/application-dev/reference/apis/js-apis-rpc.md @@ -104,8 +104,10 @@ writeRemoteObject(object: [IRemoteObject](#iremoteobject)): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } @@ -114,8 +116,9 @@ writeRemoteObject(object: [IRemoteObject](#iremoteobject)): void try { data.writeRemoteObject(testRemoteObject); } catch(error) { - console.info("Rpc write remote object fail, errorCode " + error.code); - console.info("Rpc write remote object fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc write remote object fail, errorCode " + e.code); + console.info("Rpc write remote object fail, errorMessage " + e.message); } ``` @@ -145,8 +148,10 @@ readRemoteObject(): IRemoteObject **示例:** ```ts + import { BussinessError } from '@ohos.base'; + class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } @@ -156,8 +161,9 @@ readRemoteObject(): IRemoteObject data.writeRemoteObject(testRemoteObject); let proxy = data.readRemoteObject(); } catch(error) { - console.info("Rpc write remote object fail, errorCode " + error.code); - console.info("Rpc write remote object fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc write remote object fail, errorCode " + e.code); + console.info("Rpc write remote object fail, errorMessage " + e.message); } ``` @@ -186,12 +192,15 @@ writeInterfaceToken(token: string): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeInterfaceToken("aaa"); } catch(error) { - console.info("rpc write interface fail, errorCode " + error.code); - console.info("rpc write interface fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write interface fail, errorCode " + e.code); + console.info("rpc write interface fail, errorMessage " + e.message); } ``` @@ -220,14 +229,17 @@ readInterfaceToken(): string **示例:** ```ts +import { BussinessError } from '@ohos.base'; + class Stub extends rpc.RemoteObject { - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { try { let interfaceToken = data.readInterfaceToken(); console.log("RpcServer: interfaceToken is " + interfaceToken); } catch(error) { - console.info("RpcServer: read interfaceToken failed, errorCode " + error.code); - console.info("RpcServer: read interfaceToken failed, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("RpcServer: read interfaceToken failed, errorCode " + e.code); + console.info("RpcServer: read interfaceToken failed, errorMessage " + e.message); } return true; } @@ -295,13 +307,16 @@ setSize(size: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.setSize(16); console.log("RpcClient: setSize is " + data.getSize()); } catch(error) { - console.info("rpc set size of MessageSequence fail, errorCode " + error.code); - console.info("rpc set size of MessageSequence fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc set size of MessageSequence fail, errorCode " + e.code); + console.info("rpc set size of MessageSequence fail, errorMessage " + e.message); } ``` @@ -330,13 +345,16 @@ setCapacity(size: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.setCapacity(100); console.log("RpcClient: setCapacity is " + data.getCapacity()); } catch(error) { - console.info("rpc memory alloc fail, errorCode " + error.code); - console.info("rpc memory alloc fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc memory alloc fail, errorCode " + e.code); + console.info("rpc memory alloc fail, errorMessage " + e.message); } ``` @@ -358,7 +376,7 @@ getWritableBytes(): number ```ts class Stub extends rpc.RemoteObject { - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { let getWritableBytes = data.getWritableBytes(); console.log("RpcServer: getWritableBytes is " + getWritableBytes); return true; @@ -384,7 +402,7 @@ getReadableBytes(): number ```ts class Stub extends rpc.RemoteObject { - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { let result = data.getReadableBytes(); console.log("RpcServer: getReadableBytes is " + result); return true; @@ -454,6 +472,8 @@ rewindRead(pos: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); data.writeInt(12); data.writeString("sequence"); @@ -462,8 +482,9 @@ rewindRead(pos: number): void try { data.rewindRead(0); } catch(error) { - console.info("rpc rewind read data fail, errorCode " + error.code); - console.info("rpc rewind read data fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc rewind read data fail, errorCode " + e.code); + console.info("rpc rewind read data fail, errorMessage " + e.message); } let number2 = data.readInt(); console.log("RpcClient: rewindRead is " + number2); @@ -486,13 +507,16 @@ rewindWrite(pos: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); data.writeInt(4); try { data.rewindWrite(0); } catch(error) { - console.info("rpc rewind read data fail, errorCode " + error.code); - console.info("rpc rewind read data fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc rewind read data fail, errorCode " + e.code); + console.info("rpc rewind read data fail, errorMessage " + e.message); } data.writeInt(5); let number = data.readInt(); @@ -524,12 +548,15 @@ writeByte(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeByte(2); } catch(error) { - console.info("rpc write byte fail, errorCode " + error.code); - console.info("rpc write byte fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byte fail, errorCode " + e.code); + console.info("rpc write byte fail, errorMessage" + e.message); } ``` @@ -558,19 +585,23 @@ readByte(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeByte(2); } catch(error) { - console.info("rpc write byte fail, errorCode " + error.code); - console.info("rpc write byte fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byte fail, errorCode " + e.code); + console.info("rpc write byte fail, errorMessage" + e.message); } try { let ret = data.readByte(); console.log("RpcClient: readByte is: " + ret); } catch(error) { - console.info("rpc write byte fail, errorCode " + error.code); - console.info("rpc write byte fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byte fail, errorCode " + e.code); + console.info("rpc write byte fail, errorMessage" + e.message); } ``` @@ -599,12 +630,15 @@ writeShort(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeShort(8); } catch(error) { - console.info("rpc write short fail, errorCode " + error.code); - console.info("rpc write short fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write short fail, errorCode " + e.code); + console.info("rpc write short fail, errorMessage" + e.message); } ``` @@ -633,19 +667,23 @@ readShort(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeShort(8); } catch(error) { - console.info("rpc write short fail, errorCode " + error.code); - console.info("rpc write short fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write short fail, errorCode " + e.code); + console.info("rpc write short fail, errorMessage" + e.message); } try { let ret = data.readShort(); console.log("RpcClient: readByte is: " + ret); } catch(error) { - console.info("rpc read short fail, errorCode " + error.code); - console.info("rpc read short fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read short fail, errorCode " + e.code); + console.info("rpc read short fail, errorMessage" + e.message); } ``` @@ -674,12 +712,15 @@ writeInt(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeInt(10); } catch(error) { - console.info("rpc write int fail, errorCode " + error.code); - console.info("rpc write int fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write int fail, errorCode " + e.code); + console.info("rpc write int fail, errorMessage" + e.message); } ``` @@ -708,19 +749,23 @@ readInt(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeInt(10); } catch(error) { - console.info("rpc write int fail, errorCode " + error.code); - console.info("rpc write int fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write int fail, errorCode " + e.code); + console.info("rpc write int fail, errorMessage" + e.message); } try { let ret = data.readInt(); console.log("RpcClient: readInt is " + ret); } catch(error) { - console.info("rpc read int fail, errorCode " + error.code); - console.info("rpc read int fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read int fail, errorCode " + e.code); + console.info("rpc read int fail, errorMessage" + e.message); } ``` @@ -749,12 +794,15 @@ writeLong(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeLong(10000); } catch(error) { - console.info("rpc write long fail, errorCode " + error.code); - console.info("rpc write long fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write long fail, errorCode " + e.code); + console.info("rpc write long fail, errorMessage" + e.message); } ``` @@ -783,19 +831,23 @@ readLong(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeLong(10000); } catch(error) { - console.info("rpc write long fail, errorCode " + error.code); - console.info("rpc write long fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write long fail, errorCode " + e.code); + console.info("rpc write long fail, errorMessage" + e.message); } try { let ret = data.readLong(); console.log("RpcClient: readLong is " + ret); } catch(error) { - console.info("rpc read long fail, errorCode " + error.code); - console.info("rpc read long fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read long fail, errorCode " + e.code); + console.info("rpc read long fail, errorMessage" + e.message); } ``` @@ -824,12 +876,15 @@ writeFloat(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeFloat(1.2); } catch(error) { - console.info("rpc write float fail, errorCode " + error.code); - console.info("rpc write float fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write float fail, errorCode " + e.code); + console.info("rpc write float fail, errorMessage" + e.message); } ``` @@ -858,19 +913,23 @@ readFloat(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeFloat(1.2); } catch(error) { - console.info("rpc write float fail, errorCode " + error.code); - console.info("rpc write float fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write float fail, errorCode " + e.code); + console.info("rpc write float fail, errorMessage" + e.message); } try { let ret = data.readFloat(); console.log("RpcClient: readFloat is " + ret); } catch(error) { - console.info("rpc read float fail, errorCode " + error.code); - console.info("rpc read float fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read float fail, errorCode " + e.code); + console.info("rpc read float fail, errorMessage" + e.message); } ``` @@ -899,12 +958,15 @@ writeDouble(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeDouble(10.2); } catch(error) { - console.info("rpc read float fail, errorCode " + error.code); - console.info("rpc read float fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read float fail, errorCode " + e.code); + console.info("rpc read float fail, errorMessage" + e.message); } ``` @@ -933,19 +995,23 @@ readDouble(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeDouble(10.2); } catch(error) { - console.info("rpc write double fail, errorCode " + error.code); - console.info("rpc write double fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write double fail, errorCode " + e.code); + console.info("rpc write double fail, errorMessage" + e.message); } try { let ret = data.readDouble(); console.log("RpcClient: readDouble is " + ret); } catch(error) { - console.info("rpc read double fail, errorCode " + error.code); - console.info("rpc read double fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read double fail, errorCode " + e.code); + console.info("rpc read double fail, errorMessage" + e.message); } ``` @@ -974,12 +1040,15 @@ writeBoolean(val: boolean): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeBoolean(false); } catch(error) { - console.info("rpc write boolean fail, errorCode " + error.code); - console.info("rpc write boolean fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write boolean fail, errorCode " + e.code); + console.info("rpc write boolean fail, errorMessage" + e.message); } ``` @@ -1008,19 +1077,23 @@ readBoolean(): boolean **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeBoolean(false); } catch(error) { - console.info("rpc write boolean fail, errorCode " + error.code); - console.info("rpc write boolean fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write boolean fail, errorCode " + e.code); + console.info("rpc write boolean fail, errorMessage" + e.message); } try { let ret = data.readBoolean(); console.log("RpcClient: readBoolean is " + ret); } catch(error) { - console.info("rpc read boolean fail, errorCode " + error.code); - console.info("rpc read boolean fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read boolean fail, errorCode " + e.code); + console.info("rpc read boolean fail, errorMessage" + e.message); } ``` @@ -1049,12 +1122,15 @@ writeChar(val: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeChar(97); } catch(error) { - console.info("rpc write char fail, errorCode " + error.code); - console.info("rpc write char fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write char fail, errorCode " + e.code); + console.info("rpc write char fail, errorMessage" + e.message); } ``` @@ -1083,19 +1159,23 @@ readChar(): number **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeChar(97); } catch(error) { - console.info("rpc write char fail, errorCode " + error.code); - console.info("rpc write char fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write char fail, errorCode " + e.code); + console.info("rpc write char fail, errorMessage" + e.message); } try { let ret = data.readChar(); console.log("RpcClient: readChar is " + ret); } catch(error) { - console.info("rpc read char fail, errorCode " + error.code); - console.info("rpc read char fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read char fail, errorCode " + e.code); + console.info("rpc read char fail, errorMessage" + e.message); } ``` @@ -1124,12 +1204,15 @@ writeString(val: string): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeString('abc'); } catch(error) { - console.info("rpc write string fail, errorCode " + error.code); - console.info("rpc write string fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write string fail, errorCode " + e.code); + console.info("rpc write string fail, errorMessage" + e.message); } ``` @@ -1158,19 +1241,23 @@ readString(): string **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeString('abc'); } catch(error) { - console.info("rpc write string fail, errorCode " + error.code); - console.info("rpc write string fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write string fail, errorCode " + e.code); + console.info("rpc write string fail, errorMessage" + e.message); } try { let ret = data.readString(); console.log("RpcClient: readString is " + ret); } catch(error) { - console.info("rpc read string fail, errorCode " + error.code); - console.info("rpc read string fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read string fail, errorCode " + e.code); + console.info("rpc read string fail, errorMessage" + e.message); } ``` @@ -1199,31 +1286,34 @@ writeParcelable(val: Parcelable): void **示例:** ```ts - class MySequenceable { + import { BussinessError } from '@ohos.base'; + + class MyParcelable implements rpc.Parcelable { num: number; str: string; - constructor(num, str) { + constructor( num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageSequence): boolean { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageSequence): boolean { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; } } - let parcelable = new MySequenceable(1, "aaa"); + let parcelable = new MyParcelable(1, "aaa"); let data = rpc.MessageSequence.create(); try { data.writeParcelable(parcelable); } catch(error) { - console.info("rpc write parcelable fail, errorCode " + error.code); - console.info("rpc write parcelable fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write parcelable fail, errorCode " + e.code); + console.info("rpc write parcelable fail, errorMessage" + e.message); } ``` @@ -1253,33 +1343,36 @@ readParcelable(dataIn: Parcelable): void **示例:** ```ts - class MySequenceable { + import { BussinessError } from '@ohos.base'; + + class MyParcelable implements rpc.Parcelable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageSequence): boolean { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageSequence): boolean { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; } } - let parcelable = new MySequenceable(1, "aaa"); + let parcelable = new MyParcelable(1, "aaa"); let data = rpc.MessageSequence.create(); data.writeParcelable(parcelable); - let ret = new MySequenceable(0, ""); + let ret = new MyParcelable(0, ""); try { data.readParcelable(ret); }catch(error) { - console.info("rpc read parcelable fail, errorCode " + error.code); - console.info("rpc read parcelable fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read parcelable fail, errorCode " + e.code); + console.info("rpc read parcelable fail, errorMessage" + e.message); } ``` @@ -1308,13 +1401,16 @@ writeByteArray(byteArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); let ByteArrayVar = [1, 2, 3, 4, 5]; try { data.writeByteArray(ByteArrayVar); } catch(error) { - console.info("rpc write byteArray fail, errorCode " + error.code); - console.info("rpc write byteArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byteArray fail, errorCode " + e.code); + console.info("rpc write byteArray fail, errorMessage" + e.message); } ``` @@ -1343,20 +1439,24 @@ readByteArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); let ByteArrayVar = [1, 2, 3, 4, 5]; try { data.writeByteArray(ByteArrayVar); } catch(error) { - console.info("rpc write byteArray fail, errorCode " + error.code); - console.info("rpc write byteArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byteArray fail, errorCode " + e.code); + console.info("rpc write byteArray fail, errorMessage" + e.message); } try { - let array = new Array(5); + let array: Array = new Array(5); data.readByteArray(array); } catch(error) { - console.info("rpc write byteArray fail, errorCode " + error.code); - console.info("rpc write byteArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byteArray fail, errorCode " + e.code); + console.info("rpc write byteArray fail, errorMessage" + e.message); } ``` @@ -1385,20 +1485,24 @@ readByteArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); let byteArrayVar = [1, 2, 3, 4, 5]; try { data.writeByteArray(byteArrayVar); } catch(error) { - console.info("rpc write byteArray fail, errorCode " + error.code); - console.info("rpc write byteArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write byteArray fail, errorCode " + e.code); + console.info("rpc write byteArray fail, errorMessage" + e.message); } try { let array = data.readByteArray(); console.log("RpcClient: readByteArray is " + array); } catch(error) { - console.info("rpc read byteArray fail, errorCode " + error.code); - console.info("rpc read byteArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read byteArray fail, errorCode " + e.code); + console.info("rpc read byteArray fail, errorMessage" + e.message); } ``` @@ -1427,12 +1531,15 @@ writeShortArray(shortArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeShortArray([11, 12, 13]); } catch(error) { - console.info("rpc read byteArray fail, errorCode " + error.code); - console.info("rpc read byteArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read byteArray fail, errorCode " + e.code); + console.info("rpc read byteArray fail, errorMessage" + e.message); } ``` @@ -1461,19 +1568,23 @@ readShortArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeShortArray([11, 12, 13]); } catch(error) { - console.info("rpc write shortArray fail, errorCode " + error.code); - console.info("rpc write shortArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write shortArray fail, errorCode " + e.code); + console.info("rpc write shortArray fail, errorMessage" + e.message); } try { - let array = new Array(3); + let array: Array = new Array(3); data.readShortArray(array); } catch(error) { - console.info("rpc read shortArray fail, errorCode " + error.code); - console.info("rpc read shortArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read shortArray fail, errorCode " + e.code); + console.info("rpc read shortArray fail, errorMessage" + e.message); } ``` @@ -1502,19 +1613,23 @@ readShortArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeShortArray([11, 12, 13]); } catch(error) { - console.info("rpc write shortArray fail, errorCode " + error.code); - console.info("rpc write shortArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write shortArray fail, errorCode " + e.code); + console.info("rpc write shortArray fail, errorMessage" + e.message); } try { let array = data.readShortArray(); console.log("RpcClient: readShortArray is " + array); } catch(error) { - console.info("rpc read shortArray fail, errorCode " + error.code); - console.info("rpc read shortArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read shortArray fail, errorCode " + e.code); + console.info("rpc read shortArray fail, errorMessage" + e.message); } ``` @@ -1543,12 +1658,15 @@ writeIntArray(intArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeIntArray([100, 111, 112]); } catch(error) { - console.info("rpc write intArray fail, errorCode " + error.code); - console.info("rpc write intArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write intArray fail, errorCode " + e.code); + console.info("rpc write intArray fail, errorMessage" + e.message); } ``` @@ -1577,19 +1695,23 @@ readIntArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeIntArray([100, 111, 112]); } catch(error) { - console.info("rpc write intArray fail, errorCode " + error.code); - console.info("rpc write intArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write intArray fail, errorCode " + e.code); + console.info("rpc write intArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { data.readIntArray(array); } catch(error) { - console.info("rpc read intArray fail, errorCode " + error.code); - console.info("rpc read intArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read intArray fail, errorCode " + e.code); + console.info("rpc read intArray fail, errorMessage" + e.message); } ``` @@ -1618,19 +1740,23 @@ readIntArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeIntArray([100, 111, 112]); } catch(error) { - console.info("rpc write intArray fail, errorCode " + error.code); - console.info("rpc write intArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write intArray fail, errorCode " + e.code); + console.info("rpc write intArray fail, errorMessage" + e.message); } try { let array = data.readIntArray(); console.log("RpcClient: readIntArray is " + array); } catch(error) { - console.info("rpc read intArray fail, errorCode " + error.code); - console.info("rpc read intArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read intArray fail, errorCode " + e.code); + console.info("rpc read intArray fail, errorMessage" + e.message); } ``` @@ -1659,12 +1785,15 @@ writeLongArray(longArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeLongArray([1111, 1112, 1113]); }catch(error){ - console.info("rpc write longArray fail, errorCode " + error.code); - console.info("rpc write longArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write longArray fail, errorCode " + e.code); + console.info("rpc write longArray fail, errorMessage" + e.message); } ``` @@ -1693,19 +1822,23 @@ readLongArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeLongArray([1111, 1112, 1113]); } catch(error) { - console.info("rpc write longArray fail, errorCode " + error.code); - console.info("rpc write longArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write longArray fail, errorCode " + e.code); + console.info("rpc write longArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { data.readLongArray(array); } catch(error) { - console.info("rpc read longArray fail, errorCode " + error.code); - console.info("rpc read longArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read longArray fail, errorCode " + e.code); + console.info("rpc read longArray fail, errorMessage" + e.message); } ``` @@ -1734,19 +1867,23 @@ readLongArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeLongArray([1111, 1112, 1113]); } catch(error) { - console.info("rpc write longArray fail, errorCode " + error.code); - console.info("rpc write longArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write longArray fail, errorCode " + e.code); + console.info("rpc write longArray fail, errorMessage" + e.message); } try { let array = data.readLongArray(); console.log("RpcClient: readLongArray is " + array); } catch(error) { - console.info("rpc read longArray fail, errorCode " + error.code); - console.info("rpc read longArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read longArray fail, errorCode " + e.code); + console.info("rpc read longArray fail, errorMessage" + e.message); } ``` @@ -1775,12 +1912,15 @@ writeFloatArray(floatArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeFloatArray([1.2, 1.3, 1.4]); } catch(error) { - console.info("rpc write floatArray fail, errorCode " + error.code); - console.info("rpc write floatArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write floatArray fail, errorCode " + e.code); + console.info("rpc write floatArray fail, errorMessage" + e.message); } ``` @@ -1809,19 +1949,23 @@ readFloatArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeFloatArray([1.2, 1.3, 1.4]); }catch(error){ - console.info("rpc write floatArray fail, errorCode " + error.code); - console.info("rpc write floatArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write floatArray fail, errorCode " + e.code); + console.info("rpc write floatArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { data.readFloatArray(array); } catch(error) { - console.info("rpc read floatArray fail, errorCode " + error.code); - console.info("rpc read floatArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read floatArray fail, errorCode " + e.code); + console.info("rpc read floatArray fail, errorMessage" + e.message); } ``` @@ -1850,19 +1994,23 @@ readFloatArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeFloatArray([1.2, 1.3, 1.4]); } catch(error) { - console.info("rpc write floatArray fail, errorCode " + error.code); - console.info("rpc write floatArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write floatArray fail, errorCode " + e.code); + console.info("rpc write floatArray fail, errorMessage" + e.message); } try { let array = data.readFloatArray(); console.log("RpcClient: readFloatArray is " + array); } catch(error) { - console.info("rpc read floatArray fail, errorCode " + error.code); - console.info("rpc read floatArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read floatArray fail, errorCode " + e.code); + console.info("rpc read floatArray fail, errorMessage" + e.message); } ``` @@ -1891,12 +2039,15 @@ writeDoubleArray(doubleArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeDoubleArray([11.1, 12.2, 13.3]); } catch(error) { - console.info("rpc write doubleArray fail, errorCode " + error.code); - console.info("rpc write doubleArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write doubleArray fail, errorCode " + e.code); + console.info("rpc write doubleArray fail, errorMessage" + e.message); } ``` @@ -1925,19 +2076,23 @@ readDoubleArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeDoubleArray([11.1, 12.2, 13.3]); } catch(error) { - console.info("rpc write doubleArray fail, errorCode " + error.code); - console.info("rpc write doubleArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write doubleArray fail, errorCode " + e.code); + console.info("rpc write doubleArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { data.readDoubleArray(array); } catch(error) { - console.info("rpc read doubleArray fail, errorCode " + error.code); - console.info("rpc read doubleArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read doubleArray fail, errorCode " + e.code); + console.info("rpc read doubleArray fail, errorMessage" + e.message); } ``` @@ -1966,19 +2121,23 @@ readDoubleArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeDoubleArray([11.1, 12.2, 13.3]); } catch(error) { - console.info("rpc write doubleArray fail, errorCode " + error.code); - console.info("rpc write doubleArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write doubleArray fail, errorCode " + e.code); + console.info("rpc write doubleArray fail, errorMessage" + e.message); } try { let array = data.readDoubleArray(); console.log("RpcClient: readDoubleArray is " + array); } catch(error) { - console.info("rpc read doubleArray fail, errorCode " + error.code); - console.info("rpc read doubleArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read doubleArray fail, errorCode " + e.code); + console.info("rpc read doubleArray fail, errorMessage" + e.message); } ``` @@ -2007,12 +2166,15 @@ writeBooleanArray(booleanArray: boolean[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeBooleanArray([false, true, false]); } catch(error) { - console.info("rpc write booleanArray fail, errorCode " + error.code); - console.info("rpc write booleanArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write booleanArray fail, errorCode " + e.code); + console.info("rpc write booleanArray fail, errorMessage" + e.message); } ``` @@ -2041,19 +2203,23 @@ readBooleanArray(dataIn: boolean[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeBooleanArray([false, true, false]); } catch(error) { - console.info("rpc write booleanArray fail, errorCode " + error.code); - console.info("rpc write booleanArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write booleanArray fail, errorCode " + e.code); + console.info("rpc write booleanArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { data.readBooleanArray(array); } catch(error) { - console.info("rpc read booleanArray fail, errorCode " + error.code); - console.info("rpc read booleanArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read booleanArray fail, errorCode " + e.code); + console.info("rpc read booleanArray fail, errorMessage" + e.message); } ``` @@ -2082,19 +2248,23 @@ readBooleanArray(): boolean[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeBooleanArray([false, true, false]); } catch(error) { - console.info("rpc write booleanArray fail, errorCode " + error.code); - console.info("rpc write booleanArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write booleanArray fail, errorCode " + e.code); + console.info("rpc write booleanArray fail, errorMessage" + e.message); } try { let array = data.readBooleanArray(); console.log("RpcClient: readBooleanArray is " + array); } catch(error) { - console.info("rpc read booleanArray fail, errorCode " + error.code); - console.info("rpc read booleanArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read booleanArray fail, errorCode " + e.code); + console.info("rpc read booleanArray fail, errorMessage" + e.message); } ``` @@ -2123,12 +2293,15 @@ writeCharArray(charArray: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeCharArray([97, 98, 88]); } catch(error) { - console.info("rpc write charArray fail, errorCode " + error.code); - console.info("rpc write charArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write charArray fail, errorCode " + e.code); + console.info("rpc write charArray fail, errorMessage" + e.message); } ``` @@ -2157,19 +2330,23 @@ readCharArray(dataIn: number[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeCharArray([97, 98, 88]); } catch(error) { - console.info("rpc write charArray fail, errorCode " + error.code); - console.info("rpc write charArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write charArray fail, errorCode " + e.code); + console.info("rpc write charArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { data.readCharArray(array); } catch(error) { - console.info("rpc read charArray fail, errorCode " + error.code); - console.info("rpc read charArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read charArray fail, errorCode " + e.code); + console.info("rpc read charArray fail, errorMessage" + e.message); } ``` @@ -2198,20 +2375,24 @@ readCharArray(): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeCharArray([97, 98, 88]); } catch(error) { - console.info("rpc write charArray fail, errorCode " + error.code); - console.info("rpc write charArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write charArray fail, errorCode " + e.code); + console.info("rpc write charArray fail, errorMessage" + e.message); } - let array = new Array(3); + let array: Array = new Array(3); try { let array = data.readCharArray(); console.log("RpcClient: readCharArray is " + array); } catch(error) { - console.info("rpc read charArray fail, errorCode " + error.code); - console.info("rpc read charArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read charArray fail, errorCode " + e.code); + console.info("rpc read charArray fail, errorMessage" + e.message); } ``` @@ -2240,12 +2421,15 @@ writeStringArray(stringArray: string[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeStringArray(["abc", "def"]); } catch(error) { - console.info("rpc write stringArray fail, errorCode " + error.code); - console.info("rpc write stringArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write stringArray fail, errorCode " + e.code); + console.info("rpc write stringArray fail, errorMessage" + e.message); } ``` @@ -2274,19 +2458,23 @@ readStringArray(dataIn: string[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeStringArray(["abc", "def"]); } catch(error) { - console.info("rpc write stringArray fail, errorCode " + error.code); - console.info("rpc write stringArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write stringArray fail, errorCode " + e.code); + console.info("rpc write stringArray fail, errorMessage" + e.message); } - let array = new Array(2); + let array: Array = new Array(2); try { data.readStringArray(array); } catch(error) { - console.info("rpc read stringArray fail, errorCode " + error.code); - console.info("rpc read stringArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read stringArray fail, errorCode " + e.code); + console.info("rpc read stringArray fail, errorMessage" + e.message); } ``` @@ -2315,19 +2503,23 @@ readStringArray(): string[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let data = rpc.MessageSequence.create(); try { data.writeStringArray(["abc", "def"]); } catch(error) { - console.info("rpc write stringArray fail, errorCode " + error.code); - console.info("rpc write stringArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write stringArray fail, errorCode " + e.code); + console.info("rpc write stringArray fail, errorMessage" + e.message); } try { let array = data.readStringArray(); console.log("RpcClient: readStringArray is " + array); } catch(error) { - console.info("rpc read stringArray fail, errorCode " + error.code); - console.info("rpc read stringArray fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read stringArray fail, errorCode " + e.code); + console.info("rpc read stringArray fail, errorMessage" + e.message); } ``` @@ -2350,19 +2542,22 @@ writeNoException(): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { if (code === 1) { console.log("RpcServer: onRemoteRequest called"); try { reply.writeNoException(); } catch(error) { - console.info("rpc write no exception fail, errorCode " + error.code); - console.info("rpc write no exception fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write no exception fail, errorCode " + e.code); + console.info("rpc write no exception fail, errorMessage" + e.message); } return true; } else { @@ -2396,29 +2591,32 @@ readException(): void ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BussinessError } from '@ohos.base'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 @@ -2430,21 +2628,22 @@ readException(): void data.writeInt(1); data.writeString("hello"); proxy.sendMessageRequest(1, data, reply, option) - .then(function(errCode) { + .then((errCode: number) => { if (errCode === 0) { console.log("sendMessageRequest got result"); try { reply.readException(); } catch(error) { - console.info("rpc read exception fail, errorCode " + error.code); - console.info("rpc read no exception fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read exception fail, errorCode " + e.code); + console.info("rpc read no exception fail, errorMessage" + e.message); } let msg = reply.readString(); console.log("RPCTest: reply msg: " + msg); } else { console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode); } - }).catch(function(e) { + }).catch((e: Error) => { console.log("RPCTest: sendMessageRequest got exception: " + e.message); }).finally (() => { console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); @@ -2478,19 +2677,21 @@ writeParcelableArray(parcelableArray: Parcelable[]): void **示例:** ```ts - class MyParcelable { + import { BussinessError } from '@ohos.base'; + + class MyParcelable implements rpc.Parcelable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageSequence): boolean { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageSequence): boolean { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; @@ -2504,8 +2705,9 @@ writeParcelableArray(parcelableArray: Parcelable[]): void try { data.writeParcelableArray(a); } catch(error) { - console.info("rpc write parcelable array fail, errorCode " + error.code); - console.info("rpc write parcelable array fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write parcelable array fail, errorCode " + e.code); + console.info("rpc write parcelable array fail, errorMessage" + e.message); } ``` @@ -2535,19 +2737,21 @@ readParcelableArray(parcelableArray: Parcelable[]): void **示例:** ```ts - class MyParcelable { + import { BussinessError } from '@ohos.base'; + + class MyParcelable implements rpc.Parcelable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageSequence): boolean { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageSequence): boolean { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; @@ -2564,8 +2768,9 @@ readParcelableArray(parcelableArray: Parcelable[]): void try { data.readParcelableArray(b); } catch(error) { - console.info("rpc read parcelable array fail, errorCode " + error.code); - console.info("rpc read parcelable array fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read parcelable array fail, errorCode " + e.code); + console.info("rpc read parcelable array fail, errorMessage" + e.message); } data.readParcelableArray(b); ``` @@ -2595,8 +2800,10 @@ writeRemoteObjectArray(objectArray: IRemoteObject[]): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.modifyLocalInterface(this, descriptor); } @@ -2611,8 +2818,9 @@ writeRemoteObjectArray(objectArray: IRemoteObject[]): void try { data.writeRemoteObjectArray(a); } catch(error) { - console.info("rpc write remote object array fail, errorCode " + error.code); - console.info("rpc write remote object array fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write remote object array fail, errorCode " + e.code); + console.info("rpc write remote object array fail, errorMessage" + e.message); } console.log("RpcClient: writeRemoteObjectArray is " + result); ``` @@ -2642,13 +2850,15 @@ readRemoteObjectArray(objects: IRemoteObject[]): void **示例:** ```ts - class MyDeathRecipient { + import { BussinessError } from '@ohos.base'; + + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.modifyLocalInterface(this, descriptor); } @@ -2660,12 +2870,13 @@ readRemoteObjectArray(objects: IRemoteObject[]): void let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; let data = rpc.MessageSequence.create(); data.writeRemoteObjectArray(a); - let b = new Array(3); + let b: Array = new Array(3); try { data.readRemoteObjectArray(b); } catch(error) { - console.info("rpc read remote object array fail, errorCode " + error.code); - console.info("rpc read remote object array fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read remote object array fail, errorCode " + e.code); + console.info("rpc read remote object array fail, errorMessage" + e.message); } data.readRemoteObjectArray(b); ``` @@ -2695,8 +2906,10 @@ readRemoteObjectArray(): IRemoteObject[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.modifyLocalInterface(this, descriptor); } @@ -2712,8 +2925,9 @@ readRemoteObjectArray(): IRemoteObject[] let b = data.readRemoteObjectArray(); console.log("RpcClient: readRemoteObjectArray is " + b); } catch(error) { - console.info("rpc read remote object array fail, errorCode " + error.code); - console.info("rpc read remote object array fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read remote object array fail, errorCode " + e.code); + console.info("rpc read remote object array fail, errorMessage" + e.message); } ``` @@ -2735,13 +2949,16 @@ static closeFileDescriptor(fd: number): void ```ts import fs from '@ohos.file.fs'; + import { BussinessError } from '@ohos.base'; + let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); try { rpc.MessageSequence.closeFileDescriptor(file.fd); } catch(error) { - console.info("rpc close file descriptor fail, errorCode " + error.code); - console.info("rpc close file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc close file descriptor fail, errorCode " + e.code); + console.info("rpc close file descriptor fail, errorMessage" + e.message); } ``` @@ -2777,13 +2994,16 @@ static dupFileDescriptor(fd: number) :number ```ts import fs from '@ohos.file.fs'; + import { BussinessError } from '@ohos.base'; + let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); try { let newFd = rpc.MessageSequence.dupFileDescriptor(file.fd); } catch(error) { - console.info("rpc dup file descriptor fail, errorCode " + error.code); - console.info("rpc dup file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc dup file descriptor fail, errorCode " + e.code); + console.info("rpc dup file descriptor fail, errorMessage" + e.message); } ``` @@ -2805,6 +3025,8 @@ containFileDescriptors(): boolean ```ts import fs from '@ohos.file.fs'; + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); let filePath = "path/to/file"; let r1 = sequence.containFileDescriptors(); @@ -2812,15 +3034,17 @@ containFileDescriptors(): boolean try { sequence.writeFileDescriptor(file.fd); } catch(error) { - console.info("rpc write file descriptor fail, errorCode " + error.code); - console.info("rpc write file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write file descriptor fail, errorCode " + e.code); + console.info("rpc write file descriptor fail, errorMessage" + e.message); } try { let containFD = sequence.containFileDescriptors(); console.log("RpcTest: sequence after write fd containFd result is : " + containFD); } catch(error) { - console.info("rpc contain file descriptor fail, errorCode " + error.code); - console.info("rpc contain file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc contain file descriptor fail, errorCode " + e.code); + console.info("rpc contain file descriptor fail, errorMessage" + e.message); } ``` @@ -2850,14 +3074,17 @@ writeFileDescriptor(fd: number): void ```ts import fs from '@ohos.file.fs'; + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); try { sequence.writeFileDescriptor(file.fd); } catch(error) { - console.info("rpc write file descriptor fail, errorCode " + error.code); - console.info("rpc write file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write file descriptor fail, errorCode " + e.code); + console.info("rpc write file descriptor fail, errorMessage" + e.message); } ``` @@ -2887,20 +3114,24 @@ readFileDescriptor(): number ```ts import fs from '@ohos.file.fs'; + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); try { sequence.writeFileDescriptor(file.fd); } catch(error) { - console.info("rpc write file descriptor fail, errorCode " + error.code); - console.info("rpc write file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write file descriptor fail, errorCode " + e.code); + console.info("rpc write file descriptor fail, errorMessage" + e.message); } try { let readFD = sequence.readFileDescriptor(); } catch(error) { - console.info("rpc read file descriptor fail, errorCode " + error.code); - console.info("rpc read file descriptor fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read file descriptor fail, errorCode " + e.code); + console.info("rpc read file descriptor fail, errorMessage" + e.message); } ``` @@ -2929,19 +3160,23 @@ writeAshmem(ashmem: Ashmem): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); - let ashmem; + let ashmem: rpc.Ashmem; try { ashmem = rpc.Ashmem.create("ashmem", 1024); + try { + sequence.writeAshmem(ashmem); + } catch(error) { + let e: BussinessError = error as BussinessError; + console.info("rpc write ashmem fail, errorCode " + e.code); + console.info("rpc write ashmem fail, errorMessage" + e.message); + } } catch(error) { - console.info("rpc create ashmem fail, errorCode " + error.code); - console.info("rpc creat ashmem fail, errorMessage" + error.message); - } - try { - sequence.writeAshmem(ashmem); - } catch(error) { - console.info("rpc write ashmem fail, errorCode " + error.code); - console.info("rpc write ashmem fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc create ashmem fail, errorCode " + e.code); + console.info("rpc creat ashmem fail, errorMessage" + e.message); } ``` @@ -2970,26 +3205,32 @@ readAshmem(): Ashmem **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); - let ashmem; + let ashmem: rpc.Ashmem; try { ashmem = rpc.Ashmem.create("ashmem", 1024); + try { + sequence.writeAshmem(ashmem); + } catch(error) { + let e: BussinessError = error as BussinessError; + console.info("rpc write ashmem fail, errorCode " + e.code); + console.info("rpc write ashmem fail, errorMessage" + e.message); + } } catch(error) { - console.info("rpc create ashmem fail, errorCode " + error.code); - console.info("rpc creat ashmem fail, errorMessage" + error.message); - } - try { - sequence.writeAshmem(ashmem); - } catch(error) { - console.info("rpc write ashmem fail, errorCode " + error.code); - console.info("rpc write ashmem fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc create ashmem fail, errorCode " + e.code); + console.info("rpc creat ashmem fail, errorMessage" + e.message); } + try { let readAshmem = sequence.readAshmem(); console.log("RpcTest: read ashmem to result is : " + readAshmem); } catch(error) { - console.info("rpc read ashmem fail, errorCode " + error.code); - console.info("rpc read ashmem fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read ashmem fail, errorCode " + e.code); + console.info("rpc read ashmem fail, errorMessage" + e.message); } ``` @@ -3041,13 +3282,16 @@ writeRawData(rawData: number[], size: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); let arr = [1, 2, 3, 4, 5]; try { sequence.writeRawData(arr, arr.length); } catch(error) { - console.info("rpc write rawdata fail, errorCode " + error.code); - console.info("rpc write rawdata fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write rawdata fail, errorCode " + e.code); + console.info("rpc write rawdata fail, errorMessage" + e.message); } ``` @@ -3082,20 +3326,24 @@ readRawData(size: number): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let sequence = new rpc.MessageSequence(); let arr = [1, 2, 3, 4, 5]; try { sequence.writeRawData(arr, arr.length); } catch(error) { - console.info("rpc write rawdata fail, errorCode " + error.code); - console.info("rpc write rawdata fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc write rawdata fail, errorCode " + e.code); + console.info("rpc write rawdata fail, errorMessage" + e.message); } try { let result = sequence.readRawData(5); console.log("RpcTest: sequence read raw data result is : " + result); } catch(error) { - console.info("rpc read rawdata fail, errorCode " + error.code); - console.info("rpc read rawdata fail, errorMessage" + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc read rawdata fail, errorCode " + e.code); + console.info("rpc read rawdata fail, errorMessage" + e.message); } ``` @@ -3164,13 +3412,13 @@ writeRemoteObject(object: [IRemoteObject](#iremoteobject)): boolean **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -3205,13 +3453,13 @@ readRemoteObject(): IRemoteObject **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -3276,7 +3524,7 @@ readInterfaceToken(): string ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let interfaceToken = data.readInterfaceToken(); console.log("RpcServer: interfaceToken is " + interfaceToken); return true; @@ -3402,7 +3650,7 @@ getWritableBytes(): number ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let getWritableBytes = data.getWritableBytes(); console.log("RpcServer: getWritableBytes is " + getWritableBytes); return true; @@ -3428,7 +3676,7 @@ getReadableBytes(): number ```ts class Stub extends rpc.RemoteObject { - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { let result = data.getReadableBytes(); console.log("RpcServer: getReadableBytes is " + result); return true; @@ -4036,19 +4284,19 @@ writeSequenceable(val: Sequenceable): boolean **示例:** ```ts - class MySequenceable { + class MySequenceable implements rpc.Sequenceable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageParcel) { + marshalling(messageParcel: rpc.MessageParcel): boolean { messageParcel.writeInt(this.num); messageParcel.writeString(this.str); return true; } - unmarshalling(messageParcel) { + unmarshalling(messageParcel: rpc.MessageParcel): boolean { this.num = messageParcel.readInt(); this.str = messageParcel.readString(); return true; @@ -4083,19 +4331,19 @@ readSequenceable(dataIn: Sequenceable): boolean **示例:** ```ts - class MySequenceable { + class MySequenceable implements rpc.Sequenceable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageParcel) { + marshalling(messageParcel: rpc.MessageParcel): boolean { messageParcel.writeInt(this.num); messageParcel.writeString(this.str); return true; } - unmarshalling(messageParcel) { + unmarshalling(messageParcel: rpc.MessageParcel): boolean { this.num = messageParcel.readInt(); this.str = messageParcel.readString(); return true; @@ -4160,7 +4408,7 @@ readByteArray(dataIn: number[]): void let ByteArrayVar = [1, 2, 3, 4, 5]; let result = data.writeByteArray(ByteArrayVar); console.log("RpcClient: writeByteArray is " + result); - let array = new Array(5); + let array: Array = new Array(5); data.readByteArray(array); ``` @@ -4237,7 +4485,7 @@ readShortArray(dataIn: number[]): void let data = rpc.MessageParcel.create(); let result = data.writeShortArray([11, 12, 13]); console.log("RpcClient: writeShortArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readShortArray(array); ``` @@ -4313,7 +4561,7 @@ readIntArray(dataIn: number[]): void let data = rpc.MessageParcel.create(); let result = data.writeIntArray([100, 111, 112]); console.log("RpcClient: writeIntArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readIntArray(array); ``` @@ -4389,7 +4637,7 @@ readLongArray(dataIn: number[]): void let data = rpc.MessageParcel.create(); let result = data.writeLongArray([1111, 1112, 1113]); console.log("RpcClient: writeLongArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readLongArray(array); ``` @@ -4465,7 +4713,7 @@ readFloatArray(dataIn: number[]): void let data = rpc.MessageParcel.create(); let result = data.writeFloatArray([1.2, 1.3, 1.4]); console.log("RpcClient: writeFloatArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readFloatArray(array); ``` @@ -4541,7 +4789,7 @@ readDoubleArray(dataIn: number[]): void let data = rpc.MessageParcel.create(); let result = data.writeDoubleArray([11.1, 12.2, 13.3]); console.log("RpcClient: writeDoubleArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readDoubleArray(array); ``` @@ -4617,7 +4865,7 @@ readBooleanArray(dataIn: boolean[]): void let data = rpc.MessageParcel.create(); let result = data.writeBooleanArray([false, true, false]); console.log("RpcClient: writeBooleanArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readBooleanArray(array); ``` @@ -4693,7 +4941,7 @@ readCharArray(dataIn: number[]): void let data = rpc.MessageParcel.create(); let result = data.writeCharArray([97, 98, 99]); console.log("RpcClient: writeCharArray is " + result); - let array = new Array(3); + let array: Array = new Array(3); data.readCharArray(array); ``` @@ -4769,7 +5017,7 @@ readStringArray(dataIn: string[]): void let data = rpc.MessageParcel.create(); let result = data.writeStringArray(["abc", "def"]); console.log("RpcClient: writeStringArray is " + result); - let array = new Array(2); + let array: Array = new Array(2); data.readStringArray(array); ``` @@ -4808,13 +5056,13 @@ writeNoException(): void **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -4826,7 +5074,7 @@ writeNoException(): void isObjectDead(): boolean { return false; } - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { if (code === 1) { console.log("RpcServer: onRemoteRequest called"); reply.writeNoException(); @@ -4854,29 +5102,31 @@ readException(): void ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 @@ -4888,7 +5138,7 @@ readException(): void data.writeInt(1); data.writeString("hello"); proxy.sendMessageRequest(1, data, reply, option) - .then(function(errCode) { + .then((errCode: number) => { if (errCode === 0) { console.log("sendMessageRequest got result"); reply.readException(); @@ -4897,8 +5147,8 @@ readException(): void } else { console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode); } - }).catch(function(e) { - console.log("RPCTest: sendMessageRequest got exception: " + e.message); + }).catch((error: Error) => { + console.log("RPCTest: sendMessageRequest got exception: " + error.message); }).finally (() => { console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); data.reclaim(); @@ -4929,19 +5179,19 @@ writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean **示例:** ```ts - class MySequenceable { + class MySequenceable implements rpc.Sequenceable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageParcel) { + marshalling(messageParcel: rpc.MessageParcel): boolean { messageParcel.writeInt(this.num); messageParcel.writeString(this.str); return true; } - unmarshalling(messageParcel) { + unmarshalling(messageParcel: rpc.MessageParcel): boolean { this.num = messageParcel.readInt(); this.str = messageParcel.readString(); return true; @@ -4973,19 +5223,19 @@ readSequenceableArray(sequenceableArray: Sequenceable[]): void **示例:** ```ts - class MySequenceable { + class MySequenceable implements rpc.Sequenceable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageParcel) { + marshalling(messageParcel: rpc.MessageParcel): boolean { messageParcel.writeInt(this.num); messageParcel.writeString(this.str); return true; } - unmarshalling(messageParcel) { + unmarshalling(messageParcel: rpc.MessageParcel): boolean { this.num = messageParcel.readInt(); this.str = messageParcel.readString(); return true; @@ -5025,13 +5275,13 @@ writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.attachLocalInterface(this, descriptor); } @@ -5071,13 +5321,13 @@ readRemoteObjectArray(objects: IRemoteObject[]): void **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.attachLocalInterface(this, descriptor); } @@ -5097,7 +5347,7 @@ readRemoteObjectArray(objects: IRemoteObject[]): void let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; let data = rpc.MessageParcel.create(); let result = data.writeRemoteObjectArray(a); - let b = new Array(3); + let b: Array = new Array(3); data.readRemoteObjectArray(b); ``` @@ -5118,13 +5368,13 @@ readRemoteObjectArray(): IRemoteObject[] **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.attachLocalInterface(this, descriptor); } @@ -5167,6 +5417,7 @@ static closeFileDescriptor(fd: number): void ```ts import fs from '@ohos.file.fs'; + let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); rpc.MessageParcel.closeFileDescriptor(file.fd); @@ -5196,6 +5447,7 @@ static dupFileDescriptor(fd: number) :number ```ts import fs from '@ohos.file.fs'; + let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); let newFd = rpc.MessageParcel.dupFileDescriptor(file.fd); @@ -5219,6 +5471,7 @@ containFileDescriptors(): boolean ```ts import fs from '@ohos.file.fs'; + let parcel = new rpc.MessageParcel(); let filePath = "path/to/file"; let r1 = parcel.containFileDescriptors(); @@ -5253,6 +5506,7 @@ writeFileDescriptor(fd: number): boolean ```ts import fs from '@ohos.file.fs'; + let parcel = new rpc.MessageParcel(); let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); @@ -5278,6 +5532,7 @@ readFileDescriptor(): number ```ts import fs from '@ohos.file.fs'; + let parcel = new rpc.MessageParcel(); let filePath = "path/to/file"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); @@ -5450,19 +5705,19 @@ marshalling(dataOut: MessageSequence): boolean **示例:** ```ts - class MyParcelable { + class MyParcelable implements rpc.Parcelable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageSequence): boolean { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageSequence): boolean { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; @@ -5500,19 +5755,19 @@ unmarshalling(dataIn: MessageSequence): boolean **示例:** ```ts - class MyParcelable { + class MyParcelable implements rpc.Parcelable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageSequence): boolean { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageSequence): boolean { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; @@ -5556,19 +5811,19 @@ marshalling(dataOut: MessageParcel): boolean **示例:** ```ts - class MySequenceable { + class MySequenceable implements rpc.Sequenceable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageParcel) { + marshalling(messageParcel: rpc.MessageParcel): boolean { messageParcel.writeInt(this.num); messageParcel.writeString(this.str); return true; } - unmarshalling(messageParcel) { + unmarshalling(messageParcel: rpc.MessageParcel): boolean { this.num = messageParcel.readInt(); this.str = messageParcel.readString(); return true; @@ -5606,19 +5861,19 @@ unmarshalling(dataIn: MessageParcel): boolean **示例:** ```ts - class MySequenceable { + class MySequenceable implements rpc.Sequenceable { num: number; str: string; - constructor(num, str) { + constructor(num: number, str: string) { this.num = num; this.str = str; } - marshalling(messageParcel) { + marshalling(messageParcel: rpc.MessageParcel): boolean { messageParcel.writeInt(this.num); messageParcel.writeString(this.str); return true; } - unmarshalling(messageParcel) { + unmarshalling(messageParcel: rpc.MessageParcel): boolean { this.num = messageParcel.readInt(); this.str = messageParcel.readString(); return true; @@ -5670,28 +5925,31 @@ asObject(): IRemoteObject // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的asObject接口方法获取代理或远端对象 @@ -5699,7 +5957,7 @@ asObject(): IRemoteObject ```ts class TestProxy { remote: rpc.RemoteObject; - constructor(remote) { + constructor(remote: rpc.RemoteObject) { this.remote = remote; } asObject() { @@ -5724,7 +5982,7 @@ onRemoteDied(): void **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } @@ -6107,28 +6365,31 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 @@ -6182,29 +6443,31 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 @@ -6216,7 +6479,7 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, data.writeInt(1); data.writeString("hello"); proxy.sendMessageRequest(1, data, reply, option) - .then(function(result) { + .then((result: rpc.RequestResult) => { if (result.errCode === 0) { console.log("sendMessageRequest got result"); result.reply.readException(); @@ -6225,7 +6488,7 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, } else { console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); } - }).catch(function(e) { + }).catch((e: Error) => { console.log("RPCTest: sendMessageRequest got exception: " + e.message); }).finally (() => { console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); @@ -6266,29 +6529,31 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 @@ -6300,7 +6565,7 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me data.writeInt(1); data.writeString("hello"); proxy.sendRequest(1, data, reply, option) - .then(function(result) { + .then((result: rpc.SendRequestResult) => { if (result.errCode === 0) { console.log("sendRequest got result"); result.reply.readException(); @@ -6309,7 +6574,7 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me } else { console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); } - }).catch(function(e) { + }).catch((e: Error) => { console.log("RPCTest: sendRequest got exception: " + e.message); }).finally (() => { console.log("RPCTest: sendRequest ends, reclaim parcel"); @@ -6343,42 +6608,45 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BussinessError } from '@ohos.base'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; - function sendRequestCallback(result) { - if (result.errCode === 0) { - console.log("sendRequest got result"); - result.reply.readException(); - let msg = result.reply.readString(); - console.log("RPCTest: reply msg: " + msg); - } else { - console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); - } - console.log("RPCTest: sendRequest ends, reclaim parcel"); - result.data.reclaim(); - result.reply.reclaim(); - } + function sendRequestCallback(result: rpc.RequestResult) { + if (result.errCode === 0) { + console.log("sendRequest got result"); + result.reply.readException(); + let msg = result.reply.readString(); + console.log("RPCTest: reply msg: " + msg); + } else { + console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); + } + console.log("RPCTest: sendRequest ends, reclaim parcel"); + result.data.reclaim(); + result.reply.reclaim(); +} // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendMessageRequest接口方法发送消息 @@ -6392,8 +6660,9 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, try { proxy.sendMessageRequest(1, data, reply, option, sendRequestCallback); } catch(error) { - console.info("rpc send sequence request fail, errorCode " + error.code); - console.info("rpc send sequence request fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc send sequence request fail, errorCode " + e.code); + console.info("rpc send sequence request fail, errorMessage " + e.message); } ``` @@ -6424,42 +6693,44 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; - function sendRequestCallback(result) { - if (result.errCode === 0) { - console.log("sendRequest got result"); - result.reply.readException(); - let msg = result.reply.readString(); - console.log("RPCTest: reply msg: " + msg); - } else { - console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); - } - console.log("RPCTest: sendRequest ends, reclaim parcel"); - result.data.reclaim(); - result.reply.reclaim(); - } + function sendRequestCallback(result: rpc.SendRequestResult) { + if (result.errCode === 0) { + console.log("sendRequest got result"); + result.reply.readException(); + let msg = result.reply.readString(); + console.log("RPCTest: reply msg: " + msg); + } else { + console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); + } + console.log("RPCTest: sendRequest ends, reclaim parcel"); + result.data.reclaim(); + result.reply.reclaim(); +} // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的sendRequest接口方法发送消息 @@ -6508,40 +6779,44 @@ getLocalInterface(interface: string): IRemoteBroker ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BussinessError } from '@ohos.base'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getLocalInterface接口方法查询接口对象 ```ts try { - let broker = proxy.getLocalInterface("testObject"); + let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); console.log("RpcClient: getLocalInterface is " + broker); } catch(error) { - console.info("rpc get local interface fail, errorCode " + error.code); - console.info("rpc get local interface fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc get local interface fail, errorCode " + e.code); + console.info("rpc get local interface fail, errorMessage " + e.message); } ``` @@ -6574,35 +6849,37 @@ queryLocalInterface(interface: string): IRemoteBroker ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的queryLocalInterface接口获取接口对象 ```ts - let broker = proxy.queryLocalInterface("testObject"); + let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); console.log("RpcClient: queryLocalInterface is " + broker); ``` @@ -6636,35 +6913,38 @@ registerDeathRecipient(recipient: DeathRecipient, flags: number): void ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BussinessError } from '@ohos.base'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的registerDeathRecipient接口注册死亡回调 ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } @@ -6673,8 +6953,9 @@ registerDeathRecipient(recipient: DeathRecipient, flags: number): void try { proxy.registerDeathRecipient(deathRecipient, 0); } catch(error) { - console.info("proxy register deathRecipient fail, errorCode " + error.code); - console.info("proxy register deathRecipient fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("proxy register deathRecipient fail, errorCode " + e.code); + console.info("proxy register deathRecipient fail, errorMessage " + e.message); } ``` @@ -6708,35 +6989,37 @@ addDeathRecipient(recipient: DeathRecipient, flags: number): boolean ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的addDeathRecipient接口方法新增死亡回调 ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } @@ -6775,35 +7058,38 @@ unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BussinessError } from '@ohos.base'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调 ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } @@ -6813,8 +7099,9 @@ unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void proxy.registerDeathRecipient(deathRecipient, 0); proxy.unregisterDeathRecipient(deathRecipient, 0); } catch(error) { - console.info("proxy register deathRecipient fail, errorCode " + error.code); - console.info("proxy register deathRecipient fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("proxy register deathRecipient fail, errorCode " + e.code); + console.info("proxy register deathRecipient fail, errorMessage " + e.message); } ``` @@ -6848,35 +7135,37 @@ removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的removeDeathRecipient接口方法去注册死亡回调 ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } @@ -6916,39 +7205,43 @@ getDescriptor(): string ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BussinessError } from '@ohos.base'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getDescriptor接口方法获取对象的接口描述符 ```ts try { - let descriptor = proxy.getDescriptor(); + let descriptor: string = proxy.getDescriptor(); console.log("RpcClient: descriptor is " + descriptor); } catch(error) { - console.info("rpc get interface descriptor fail, errorCode " + error.code); - console.info("rpc get interface descriptor fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc get interface descriptor fail, errorCode " + e.code); + console.info("rpc get interface descriptor fail, errorMessage " + e.message); } ``` @@ -6975,35 +7268,37 @@ getInterfaceDescriptor(): string ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的getInterfaceDescriptor接口方法查询当前代理对象接口的描述符 ```ts - let descriptor = proxy.getInterfaceDescriptor(); + let descriptor: string = proxy.getInterfaceDescriptor(); console.log("RpcClient: descriptor is " + descriptor); ``` @@ -7028,35 +7323,37 @@ isObjectDead(): boolean ```ts // 仅FA模型需要导入@ohos.ability.featureAbility // import FA from "@ohos.ability.featureAbility"; + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; - let proxy; - let connect = { - onConnect: function(elementName, remoteProxy) { + let proxy: rpc.RemoteProxy; + let connect: common.ConnectOptions = { + onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => { console.log("RpcClient: js onConnect called."); proxy = remoteProxy; }, - onDisconnect: function(elementName) { + onDisconnect: (elementName) => { console.log("RpcClient: onDisconnect"); }, - onFailed: function() { + onFailed: () => { console.log("RpcClient: onFailed"); } }; - let want = { - "bundleName": "com.ohos.server", - "abilityName": "com.ohos.server.EntryAbility", + let want: Want = { + bundleName: "com.ohos.server", + abilityName: "com.ohos.server.EntryAbility", }; // FA模型使用此方法连接服务 // FA.connectAbility(want,connect); - globalThis.context.connectServiceExtensionAbility(want, connect); + this.context.connectServiceExtensionAbility(want, connect); ``` 上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的isObjectDead接口方法判断当前对象是否已经死亡 ```ts - let isDead = proxy.isObjectDead(); + let isDead: boolean = proxy.isObjectDead(); console.log("RpcClient: isObjectDead is " + isDead); ``` @@ -7091,7 +7388,7 @@ MessageOption构造函数。 ```ts class TestRemoteObject extends rpc.MessageOption { - constructor(async) { + constructor(async: boolean) { super(async); } } @@ -7116,7 +7413,7 @@ MessageOption构造函数。 ```ts class TestRemoteObject extends rpc.MessageOption { - constructor(syncFlags,waitTime) { + constructor(syncFlags?: number,waitTime?: number) { super(syncFlags,waitTime); } } @@ -7316,7 +7613,7 @@ static getCallingPid(): number ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let callerPid = rpc.IPCSkeleton.getCallingPid(); console.log("RpcServer: getCallingPid result: " + callerPid); return true; @@ -7342,7 +7639,7 @@ static getCallingUid(): number ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let callerUid = rpc.IPCSkeleton.getCallingUid(); console.log("RpcServer: getCallingUid result: " + callerUid); return true; @@ -7368,7 +7665,7 @@ static getCallingTokenId(): number; ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); console.log("RpcServer: getCallingTokenId result: " + callerTokenId); return true; @@ -7394,7 +7691,7 @@ static getCallingDeviceID(): string ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); console.log("RpcServer: callerDeviceID is: " + callerDeviceID); return true; @@ -7420,7 +7717,7 @@ static getLocalDeviceID(): string ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); console.log("RpcServer: localDeviceID is: " + localDeviceID); return true; @@ -7446,7 +7743,7 @@ static isLocalCalling(): boolean ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); console.log("RpcServer: isLocalCalling is: " + isLocalCalling); return true; @@ -7471,8 +7768,10 @@ static flushCmdBuffer(object: IRemoteObject): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } @@ -7480,8 +7779,9 @@ static flushCmdBuffer(object: IRemoteObject): void try { rpc.IPCSkeleton.flushCmdBuffer(remoteObject); } catch(error) { - console.info("proxy set calling identity fail, errorCode " + error.code); - console.info("proxy set calling identity fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("proxy set calling identity fail, errorCode " + e.code); + console.info("proxy set calling identity fail, errorMessage " + e.message); } ``` @@ -7510,13 +7810,13 @@ static flushCommands(object: IRemoteObject): number **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -7552,7 +7852,7 @@ static resetCallingIdentity(): string ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); console.log("RpcServer: callingIdentity is: " + callingIdentity); return true; @@ -7578,8 +7878,8 @@ static restoreCallingIdentity(identity: string): void ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { - let callingIdentity = null; + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { + let callingIdentity: rpc.IPCSkeleton; try { callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); console.log("RpcServer: callingIdentity is: " + callingIdentity); @@ -7617,8 +7917,8 @@ static setCallingIdentity(identity: string): boolean ```ts class Stub extends rpc.RemoteObject { - onRemoteMessageRequest(code, data, reply, option) { - let callingIdentity = null; + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { + let callingIdentity: rpc.IPCSkeleton; try { callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); console.log("RpcServer: callingIdentity is: " + callingIdentity); @@ -7677,13 +7977,13 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -7743,13 +8043,13 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -7769,7 +8069,7 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me data.writeInt(1); data.writeString("hello"); testRemoteObject.sendRequest(1, data, reply, option) - .then(function(result) { + .then((result: rpc.SendRequestResult) => { if (result.errCode === 0) { console.log("sendRequest got result"); result.reply.readException(); @@ -7778,7 +8078,7 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me } else { console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); } - }).catch(function(e) { + }).catch((e: Error) => { console.log("RPCTest: sendRequest got exception: " + e.message); }).finally (() => { console.log("RPCTest: sendRequest ends, reclaim parcel"); @@ -7814,7 +8114,7 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } @@ -7825,7 +8125,7 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, data.writeInt(1); data.writeString("hello"); testRemoteObject.sendMessageRequest(1, data, reply, option) - .then(function(result) { + .then((result: rpc.RequestResult) => { if (result.errCode === 0) { console.log("sendMessageRequest got result"); result.reply.readException(); @@ -7834,7 +8134,7 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, } else { console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); } - }).catch(function(e) { + }).catch((e: Error) => { console.log("RPCTest: sendMessageRequest got exception: " + e.message); }).finally (() => { console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); @@ -7865,11 +8165,11 @@ sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } - function sendRequestCallback(result) { + function sendRequestCallback(result: rpc.RequestResult) { if (result.errCode === 0) { console.log("sendRequest got result"); result.reply.readException(); @@ -7914,13 +8214,13 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -7933,7 +8233,7 @@ sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: Me return false; } } - function sendRequestCallback(result) { + function sendRequestCallback(result: rpc.SendRequestResult) { if (result.errCode === 0) { console.log("sendRequest got result"); result.reply.readException(); @@ -7983,13 +8283,13 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里处理 **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -8001,7 +8301,7 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里处理 isObjectDead(): boolean { return false; } - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { if (code === 1) { console.log("RpcServer: onRemoteRequest called"); return true; @@ -8046,11 +8346,11 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里同步 ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { if (code === 1) { console.log("RpcServer: sync onRemoteMessageRequest is called"); return true; @@ -8066,18 +8366,18 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里同步 ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } - async onRemoteMessageRequest(code, data, reply, option) { + async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { if (code === 1) { console.log("RpcServer: async onRemoteMessageRequest is called"); } else { console.log("RpcServer: unknown code: " + code); return false; } - await new Promise((resolve) => { + await new Promise((resolve: (data: rpc.RequestResult) =>void) => { setTimeout(resolve, 100); }) return true; @@ -8089,11 +8389,11 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里同步 ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { if (code === 1) { console.log("RpcServer: sync onRemoteMessageRequest is called"); return true; @@ -8103,7 +8403,7 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里同步 } } // 同时调用仅会执行onRemoteMessageRequest - onRemoteMessageRequest(code, data, reply, option) { + onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { if (code === 1) { console.log("RpcServer: async onRemoteMessageRequest is called"); } else { @@ -8120,11 +8420,11 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里同步 ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } - onRemoteRequest(code, data, reply, option) { + onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { if (code === 1) { console.log("RpcServer: sync onRemoteRequest is called"); return true; @@ -8134,14 +8434,14 @@ sendMessageRequest请求的响应处理函数,服务端在该函数里同步 } } // 同时调用仅会执行onRemoteMessageRequest - async onRemoteMessageRequest(code, data, reply, option) { + async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise { if (code === 1) { console.log("RpcServer: async onRemoteMessageRequest is called"); } else { console.log("RpcServer: unknown code: " + code); return false; } - await new Promise((resolve) => { + await new Promise((resolve: (data: rpc.RequestResult) =>void) => { setTimeout(resolve, 100); }) return true; @@ -8166,7 +8466,7 @@ getCallingUid(): number ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } @@ -8192,7 +8492,7 @@ getCallingPid(): number ```ts class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } } @@ -8223,13 +8523,15 @@ getLocalInterface(descriptor: string): IRemoteBroker **示例:** ```ts - class MyDeathRecipient { + import { BussinessError } from '@ohos.base'; + + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { @@ -8246,8 +8548,9 @@ getLocalInterface(descriptor: string): IRemoteBroker try { let broker = testRemoteObject.getLocalInterface("testObject"); } catch(error) { - console.info("rpc get local interface fail, errorCode " + error.code); - console.info("rpc get local interface fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc get local interface fail, errorCode " + e.code); + console.info("rpc get local interface fail, errorMessage " + e.message); } ``` @@ -8276,13 +8579,13 @@ queryLocalInterface(descriptor: string): IRemoteBroker **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -8324,13 +8627,15 @@ getDescriptor(): string **示例:** ```ts - class MyDeathRecipient { + import { BussinessError } from '@ohos.base'; + + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { @@ -8348,8 +8653,9 @@ getDescriptor(): string try { let descriptor = testRemoteObject.getDescriptor(); } catch(error) { - console.info("rpc get local interface fail, errorCode " + error.code); - console.info("rpc get local interface fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("rpc get local interface fail, errorCode " + e.code); + console.info("rpc get local interface fail, errorMessage " + e.message); } ``` @@ -8372,13 +8678,13 @@ getInterfaceDescriptor(): string **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); } addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { @@ -8414,19 +8720,22 @@ modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void **示例:** ```ts - class MyDeathRecipient { + import { BussinessError } from '@ohos.base'; + + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); try { this.modifyLocalInterface(this, descriptor); } catch(error) { - console.info(" rpc attach local interface fail, errorCode " + error.code); - console.info(" rpc attach local interface fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info(" rpc attach local interface fail, errorCode " + e.code); + console.info(" rpc attach local interface fail, errorMessage " + e.message); } } registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { @@ -8465,13 +8774,13 @@ attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void **示例:** ```ts - class MyDeathRecipient { + class MyDeathRecipient implements rpc.DeathRecipient { onRemoteDied() { console.log("server died"); } } class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor) { + constructor(descriptor: string) { super(descriptor); this.attachLocalInterface(this, descriptor); } @@ -8530,15 +8839,18 @@ static create(name: string, size: number): Ashmem **示例:** ```ts - let ashmem; + import { BussinessError } from '@ohos.base'; + + let ashmem: rpc.Ashmem; try { ashmem = rpc.Ashmem.create("ashmem", 1024*1024); + let size = ashmem.getAshmemSize(); + console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size); } catch(error) { - console.info("Rpc creat ashmem fail, errorCode " + error.code); - console.info("Rpc creat ashmem fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc creat ashmem fail, errorCode " + e.code); + console.info("Rpc creat ashmem fail, errorMessage " + e.message); } - let size = ashmem.getAshmemSize(); - console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size); ``` ### createAshmem8+(deprecated) @@ -8595,16 +8907,18 @@ static create(ashmem: Ashmem): Ashmem **示例:** ```ts - let ashmem2; + import { BussinessError } from '@ohos.base'; + let ashmem2: rpc.Ashmem; try { let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); let ashmem2 = rpc.Ashmem.create(ashmem); + let size = ashmem2.getAshmemSize(); + console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size); } catch(error) { - console.info("Rpc creat ashmem from existing fail, errorCode " + error.code); - console.info("Rpc creat ashmem from existing fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc creat ashmem from existing fail, errorCode " + e.code); + console.info("Rpc creat ashmem from existing fail, errorMessage " + e.message); } - let size = ashmem2.getAshmemSize(); - console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size); ``` ### createAshmemFromExisting8+(deprecated) @@ -8715,12 +9029,15 @@ mapTypedAshmem(mapType: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); try { ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); } catch(error) { - console.info("Rpc map ashmem fail, errorCode " + error.code); - console.info("Rpc map ashmem fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc map ashmem fail, errorCode " + e.code); + console.info("Rpc map ashmem fail, errorMessage " + e.message); } ``` @@ -8773,12 +9090,15 @@ mapReadWriteAshmem(): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); try { ashmem.mapReadWriteAshmem(); } catch(error) { - console.info("Rpc map read and write ashmem fail, errorCode " + error.code); - console.info("Rpc map read and write ashmem fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc map read and write ashmem fail, errorCode " + e.code); + console.info("Rpc map read and write ashmem fail, errorMessage " + e.message); } ``` @@ -8825,12 +9145,15 @@ mapReadonlyAshmem(): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); try { ashmem.mapReadonlyAshmem(); } catch(error) { - console.info("Rpc map read and write ashmem fail, errorCode " + error.code); - console.info("Rpc map read and write ashmem fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc map read and write ashmem fail, errorCode " + e.code); + console.info("Rpc map read and write ashmem fail, errorMessage " + e.message); } ``` @@ -8883,12 +9206,15 @@ setProtectionType(protectionType: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); try { ashmem.setProtection(ashmem.PROT_READ); } catch(error) { - console.info("Rpc set protection type fail, errorCode " + error.code); - console.info("Rpc set protection type fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc set protection type fail, errorCode " + e.code); + console.info("Rpc set protection type fail, errorMessage " + e.message); } ``` @@ -8949,14 +9275,17 @@ writeAshmem(buf: number[], size: number, offset: number): void **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); ashmem.mapReadWriteAshmem(); - var ByteArrayVar = [1, 2, 3, 4, 5]; + let ByteArrayVar = [1, 2, 3, 4, 5]; try { ashmem.writeAshmem(ByteArrayVar, 5, 0); } catch(error) { - console.info("Rpc write to ashmem fail, errorCode " + error.code); - console.info("Rpc write to ashmem fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc write to ashmem fail, errorCode " + e.code); + console.info("Rpc write to ashmem fail, errorMessage " + e.message); } ``` @@ -8990,7 +9319,7 @@ writeToAshmem(buf: number[], size: number, offset: number): boolean let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); let mapResult = ashmem.mapReadAndWriteAshmem(); console.info("RpcTest map ashmem result is " + mapResult); - var ByteArrayVar = [1, 2, 3, 4, 5]; + let ByteArrayVar = [1, 2, 3, 4, 5]; let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); console.log("RpcTest: write to Ashmem result is : " + writeResult); ``` @@ -9027,16 +9356,19 @@ readAshmem(size: number, offset: number): number[] **示例:** ```ts + import { BussinessError } from '@ohos.base'; + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); ashmem.mapReadWriteAshmem(); - var ByteArrayVar = [1, 2, 3, 4, 5]; + let ByteArrayVar = [1, 2, 3, 4, 5]; ashmem.writeAshmem(ByteArrayVar, 5, 0); try { let readResult = ashmem.readAshmem(5, 0); console.log("RpcTest: read from Ashmem result is : " + readResult); } catch(error) { - console.info("Rpc read from ashmem fail, errorCode " + error.code); - console.info("Rpc read from ashmem fail, errorMessage " + error.message); + let e: BussinessError = error as BussinessError; + console.info("Rpc read from ashmem fail, errorCode " + e.code); + console.info("Rpc read from ashmem fail, errorMessage " + e.message); } ``` @@ -9069,7 +9401,7 @@ readFromAshmem(size: number, offset: number): number[] let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); let mapResult = ashmem.mapReadAndWriteAshmem(); console.info("RpcTest map ashmem result is " + mapResult); - var ByteArrayVar = [1, 2, 3, 4, 5]; + let ByteArrayVar = [1, 2, 3, 4, 5]; let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); console.log("RpcTest: write to Ashmem result is : " + writeResult); let readResult = ashmem.readFromAshmem(5, 0); @@ -9079,19 +9411,22 @@ readFromAshmem(size: number, offset: number): number[] ## 获取context **示例:** - +此处只介绍一种获取context的方式,更多获取方式请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 ```ts import Ability from '@ohos.app.ability.UIAbility'; + import Want from '@ohos.app.ability.Want'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + import window from '@ohos.window'; export default class MainAbility extends Ability { - onCreate(want, launchParam) { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { console.log("[Demo] MainAbility onCreate"); - globalThis.context = this.context; + let context = this.context; } onDestroy() { console.log("[Demo] MainAbility onDestroy"); } - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { // Main window is created, set main page for this ability console.log("[Demo] MainAbility onWindowStageCreate"); }