HarmonyOS 鸿蒙Next extensionability 如何与UIability进行IPC通信demo

发布于 1周前 作者 sinazl 来自 鸿蒙OS

HarmonyOS 鸿蒙Next extensionability 如何与UIability进行IPC通信demo

最小化entensionability与UIability进行IPC通信的demo

当前集成了driverextensionabiltiy、statubarextensionabiltiy,他们都与UIability之间有交互,1.要从driverextensionabiltiy中发起,在UIability中接收,弹框输入密码 2.要从statusbarextensionability 中发起,拉起UIability 。他们都与UIability属于不同进程,需要进行进程间的通信,看了IPC/RPC的开发指导,两个进程如何使用鸿蒙提供的IPC/RPC文档进行通信,但仍未尝试成功

2 回复
import hilog from '[@ohos](/user/ohos).hilog';
import { BusinessError } from '[@ohos](/user/ohos).base';
import { rpc } from '[@kit](/user/kit).IPCKit';
import web_webview from '[@ohos](/user/ohos).web.webview'

[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
[@State](/user/State) message: string = 'Hello World';
private data: rpc.MessageSequence = rpc.MessageSequence.create();
[@State](/user/State) myremoteObject: rpc.IRemoteObject | null = null;

writeData() {
let arr: number[] = [];
for (let i = 0, j = 1927414; i < j; ++i) {
arr[i] = i + 1;
}
let ashmem = rpc.Ashmem.create("ashmem", 1927414 * 4);
ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE)
ashmem.mapReadWriteAshmem();
try {
ashmem.writeAshmem(arr, arr.length, 0);
try {
this.data.writeAshmem(ashmem)
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('rpc write ashmem fail' + JSON.stringify(e));
}
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('ashmem write data fail ' + JSON.stringify(e));
}
}

readData() {
try {
let ashmem = this.data.readAshmem()
ashmem.mapReadWriteAshmem();
console.info('rpc get ashmem success ' + JSON.stringify(ashmem));
try {
let arr = ashmem.readAshmem(1927414, 0)
console.info('rpc read ashmem success ' + arr.length + arr);
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('ashmem read data fail ' + JSON.stringify(e));
}
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('rpc get ashmem fail ' + JSON.stringify(e));
}
}

build() {
Column() {
Button('写数据').onClick(() => {
this.writeData()
})
Button('读数据').onClick(() => {
this.readData()
})

Button('rpc Ablility通信', { type: ButtonType.Capsule, stateEffect: false })
.backgroundColor(0x317aff)
.width(200)
.height(60)
.onClick(() => {
let option: rpc.MessageOption = new rpc.MessageOption();
let data: rpc.MessageSequence = rpc.MessageSequence.create();
let reply: rpc.MessageSequence = rpc.MessageSequence.create();
let code = 2;
//code和data内容取决于驱动提供的接口
if (this.myremoteObject != null) {
(this.myremoteObject as rpc.IRemoteObject).sendMessageRequest(code, data, reply, option)
.then((result: rpc.RequestResult) => {
let strFrontDataLen = result.reply.readInt();
try {
console.info('####################rpc get result ' + JSON.stringify(result));
let ashmem = result.reply.readAshmem()
ashmem.mapReadWriteAshmem();
console.info('rpc get ashmem success ' + JSON.stringify(ashmem));
try {
let arr = ashmem.readAshmem(strFrontDataLen, 0)
console.info('rpc read ashmem success: ' + arr.length + "\n" + arr);
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('ashmem read data fail ' + JSON.stringify(e));
}
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('rpc get ashmem fail ' + JSON.stringify(e));
}
console.info('sendMessageRequest finish.');
}).catch((error: BusinessError) => {
let errCode = (error as BusinessError).code;
console.error('sendMessageRequest 读写设备 fail. code:' + errCode);
});
}
})
}
.height('100%')
.width('100%')
}
}

TestAbility.ets
import { Want } from '[@kit](/user/kit).AbilityKit';
import { rpc } from '[@kit](/user/kit).IPCKit';
import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';
import { DriverExtensionAbility } from '[@kit](/user/kit).DriverDevelopmentKit';


class StubTest extends rpc.RemoteObject {
constructor(des: string) {
super(des);
}

//接收应用传递过来的消息处理,以及将处理的结果返回给应用
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption) {
console.info('caLL FROM APPLICATION');
if (code === 1) {
console.info('onRemoteMessageRequest REQUEST_CODE is', `${code}`);
reply.writeInt(0);
console.info('onRemoteMessageRequest 1 return 0');
}

if (code === 2) {
console.info('onRemoteMessageRequest REQUEST_CODE is', `${code}`);
let len: number = 1932486;
reply.writeInt(0);
reply.writeInt(len);
reply.writeInt(len);
let arr: number[] = [];
for (let i = 0, j = len; i < j; ++i) { //1927414
arr[i] = i + 1;
}
let ashmem = rpc.Ashmem.create("ashmem", len * 4);
ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE)
ashmem.mapReadWriteAshmem();

try {
ashmem.writeAshmem(arr, arr.length, 0);
console.info('rpc write ashmem success: ' + arr.length + "\n" + arr);
// 把ashmem存入rpc.MessageSequence
try {
reply.writeAshmem(ashmem)
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('rpc write ashmem fail' + JSON.stringify(e));
}
} catch (error) {
let e: BusinessError = error as BusinessError;
console.info('ashmem write data fail ' + JSON.stringify(e));
}
}

if (code === 3) {
console.info('onRemoteMessageRequest REQUEST_CODE is', `${code}`);
reply.writeInt(0);
console.info('onRemoteMessageRequest 3 return 0');
}
return true;
}
}


export default class TestAbility extends DriverExtensionAbility {
onInit(want: Want) {
}

onRelease() {
}

onConnect(want: Want) {
return new StubTest("test");
}

onDisconnect(want: Want) {
}

onDump(params: Array<string>) {
return ['params'];
}
}

在HarmonyOS鸿蒙Next系统中,extensionability(如FormExtensionAbility)与UIAbility进行IPC(进程间通信)通信,通常可以通过系统提供的IPC机制实现。以下是一个简要的通信demo说明:

  1. 使用IRemoteObject

    • FormExtensionAbility可以通过系统提供的IRemoteObject接口进行跨进程通信,请求UIAbility执行特定操作或获取数据。
    • 需要实现Stub(服务端)和Proxy(客户端)来进行数据的发送和接收。
  2. Intent机制

    • FormExtensionAbility可以通过发送Intent来启动UIAbility,并携带必要的参数和数据。
    • UIAbility在接收到Intent后,根据参数进行相应的处理和界面展示。
  3. 数据序列化与反序列化

    • 在进行IPC通信时,需要确保数据能够被正确序列化和反序列化,以便在进程间传输。
  4. 注意事项

    • IPC通信可能会带来性能开销,因此应合理设计通信频率和数据量。
    • 确保通信双方的数据格式和协议一致,以避免通信失败或数据错误。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部