美文网首页
鸿蒙-进程模型以及公共事件订阅(进程间通讯:commonEven

鸿蒙-进程模型以及公共事件订阅(进程间通讯:commonEven

作者: 胡修波 | 来源:发表于2023-12-16 21:43 被阅读0次

HarmonyOS的进程模型:

  • 应用中(同一包名)的所有UIAbility运行在同一个独立进程中。

  • WebView拥有独立的渲染进程。

公共事件订阅

// 引入事件包
import commonEvent from '@ohos.commonEventManager'; 

const EVENT_NAME = "testEvent"
const TAG = "huxiubo"

let subscribeInfo = {
  events: [EVENT_NAME] // 指定订阅的事件名称
}

@Entry
@Component
struct Index {
  @State text: string = "";
  @State publish: string = "";
  private subscriber = null;

  /**创建订阅者*/
  private createSubscriber() {
    if (this.subscriber) {
      this.text = "subscriber already created";
    } else {
      commonEvent.createSubscriber(subscribeInfo, (err, subscriber) => { // 创建结果的回调
        if (err) {
          console.info(TAG, "Failure Create subscriber");
          this.text = "create subscriber failure"
        } else {
          console.info(TAG, "Successed  Create subscriber");
          this.subscriber = subscriber; // 创建订阅成功
          this.text = "create subscriber success";
        }
      })
    }
  }

  // 订阅
  private subscribe() {
    if (this.subscriber) {
      // 根据创建的subscriber开始订阅事件
      commonEvent.subscribe(this.subscriber, (err, data) => {
        if (err) {
          // 异常处理
          this.text = "subscribe event failure: " + err;
        } else {
          // 接收到事件
          this.text = "subscribe event success: " + JSON.stringify(data.event) + ", " + JSON.stringify(data);
        }
      })
    } else {
      this.text = "please create subscriber";
    }
  }

  // 取消订阅
  private unsubscribe() {
    if (this.subscriber) {
      commonEvent.unsubscribe(this.subscriber, (err) => { // 取消订阅事件
        if (err) {
          this.text = "unsubscribe event failure: " + err;
        } else {
          this.subscriber = null;
          this.text = "unsubscribe event success: ";
        }
      })
    } else {
      this.text = "already subscribed";
    }
  }


  // 发布事件,事件名称为testEvent
  private publishEvent() {
    commonEvent.publish(EVENT_NAME, (err) => {
      if (err) { // 结果回调
        this.publish = "publish event error: " + err.code + ", " + err.message + ", " + err.name + ", " + err.stack;
      } else {
        this.publish = "publish event success";
      }
    })
  }


  // 发布事件,事件名称为testEvent
  private publishEventWithData() {
    commonEvent.publish(EVENT_NAME, {
      code: 10086, // 事件携带的参数
      data: "publish with data",
      parameters: {
        id: 1,
        content: "坚果"
      }
      // 事件携带的参数
    }, (err) => { // 结果回调
      if (err) {
        this.publish = "publish event error: " + err.code + ", " + err.message + ", " + err.name + ", " + err.stack;
      } else {
        this.publish = "publish event with data success";
      }
    })
  }

  build() {
    Column({ space: 10 }) {
      Button("创建订阅者")
        .size({ width: 260, height: 50 })
        .onClick(() => {
          this.createSubscriber();
        })
      Button("订阅公共事件")
        .size({ width: 260, height: 50 })
        .onClick(() => {
          this.subscribe();
        })

      Button("取消订阅")
        .size({ width: 260, height: 50 })
        .onClick(() => {
          this.unsubscribe();
        })

      Text(this.text)
        .size({ width: 260, height: 260 })
        .fontSize(22)
        .backgroundColor("#dbdbdb")

      Divider()
        .size({ width: 260, height: 5 })

      Button("发布公共事件")
        .size({ width: 260, height: 50 })
        .onClick(() => {
          this.publishEvent();
        })

      Button("发布公共事件指定公共信息")
        .size({ width: 260, height: 50 })
        .onClick(() => {
          this.publishEventWithData();
        })

      Text(this.publish)
        .size({ width: 260, height: 150 })
        .fontSize(22)
        .backgroundColor("#dbdbdb")

    }
    .padding(10)
    .size({ width: "100%", height: '100%' })
  }
}

相关文章

  • Android Binder

    进程间通讯 1. 操作系统的进程间通讯 进程间通讯 根据名字描述就是进程之间的信息交换进程间的互斥和同步 由于交换...

  • 进程间通讯

    一、进程间通讯的方式进程间通讯的方式有很多,常用的有共享内存(内存映射文件、共享内存DLL、剪切板等)、命名管道和...

  • 进程间通讯

    进程间通讯原理 现代操作系统的运行模式都是在保护模式。进程运行在虚拟内存中,进程之间相互隔离,进程成为了操作系统分...

  • 进程间通讯

    本地的进程间通信(IPC)有很多种方式,但可以总结为下面4类: 消息传递(管道、FIFO、消息队列)同步(互斥量、...

  • 进程间通讯

    经典实现 1、文件共享 两个进程约定磁盘空间上的某个文件为信息交互的媒介,这种情况要注意不同进程间访问共享文件的同...

  • Andoird进程间通讯Binder相关内容

    android进程间通讯开发了一套Binder机制,用来进行进程间通讯; 进程间传输涉及序列化,需要区分java的...

  • 系统编程-------进程间通讯

    进程间通讯 pipe, 亲属间进程通讯, 参数: pipedfd :用于接收pipe函数创建的管道文件的读写...

  • Android进程间通讯(二)AIDL、Binder源码分析

    Android进程间通讯(一)Binder介绍及AIDL的具体使用Android进程间通讯(二)AIDL、Bind...

  • Android进程间通讯(一)Binder介绍及AIDL的具体使

    Android进程间通讯(一)Binder介绍及AIDL的具体使用Android进程间通讯(二)AIDL、Bind...

  • android进程间通讯(3)--使用socket

    android进程间通讯(3)–使用Socket 前言:本文记录android进程间通讯的另一种通讯方式–Sock...

网友评论

      本文标题:鸿蒙-进程模型以及公共事件订阅(进程间通讯:commonEven

      本文链接:https://www.haomeiwen.com/subject/wgpngdtx.html