美文网首页
鸿蒙-进程模型以及公共事件订阅(进程间通讯: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%' })
      }
    }
    
    

    相关文章

      网友评论

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

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