ExtensionAbility如何与主进程通信
========================

实现步骤：

ExtensionAbility端发布事件：

import { commonEventManager } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

_// Publish public event callbacks_
function publishCB(err: BusinessError) {
  if (err) {
    console.error(\`Failed to publish common event. Code is ${err.code}, message is ${err.message}\`);
  } else {
    console.info(\`Succeeded in publishing common event.\`);
  }
}
_// Publish public events_
try {
  commonEventManager.publish("event", publishCB);
} catch (error) {
  let err: BusinessError = error as BusinessError;
  console.error(\`Failed to publish common event. Code is ${err.code}, message is ${err.message}\`);
}

主进程端订阅事件：

import { BusinessError } from '@kit.BasicServicesKit';
import { commonEventManager } from '@kit.BasicServicesKit';

_// Define subscribers to save successfully created subscriber objects, which can be used to complete subscription and unsubscribe_ actions in the future
let subscriber: commonEventManager.CommonEventSubscriber;
_// Subscriber information_
let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
  events: \["event"\]
};
_// Subscribe to public event callbacks_
function SubscribeCB(err: BusinessError, data: commonEventManager.CommonEventData) {
  if (err) {
    console.error(\`Failed to subscribe. Code is ${err.code}, message is ${err.message}\`);
  } else {
    console.info(\`Succeeded in subscribing, data is \` + JSON.stringify(data));
  }
}
_// Create subscriber callback_
function createCB(err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) {
  if(!err) {
    console.info(\`Succeeded in creating subscriber.\`);
    subscriber = commonEventSubscriber;
    _// Subscribe to public events_
    try {
      commonEventManager.subscribe(subscriber, SubscribeCB);
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(\`Failed to subscribe. Code is ${err.code}, message is ${err.message}\`);
    }
  } else {
    console.error(\`Failed to create subscriber. Code is ${err.code}, message is ${err.message}\`);
  }
}
_// Create subscribers_
try {
  commonEventManager.createSubscriber(subscribeInfo, createCB);
} catch (error) {
  let err: BusinessError = error as BusinessError;
  console.error(\`Failed to create subscriber. Code is ${err.code}, message is ${err.message}\`);
}