CommonEventSubscriber
描述公共事件的订阅者。
说明:
本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
使用说明
在使用CommonEventSubscriber的功能前,需要通过CommonEventManager.createSubscriber获取subscriber对象。
import CommonEventManager from '@ohos.commonEventManager';
import Base from '@ohos.base';
let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: ["event"]
};
// 创建订阅者回调
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
if (err.code !== undefined && err.code != null) {
console.error(`createSubscriber failed, code is ${err.code}`);
} else {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
}
}
// 创建订阅者
CommonEventManager.createSubscriber(subscribeInfo, createCB);
getSubscribeInfo
getSubscribeInfo(callback: AsyncCallback<CommonEventSubscribeInfo>): void
以回调形式获取订阅者的订阅信息。
系统能力:SystemCapability.Notification.CommonEvent
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<CommonEventSubscribeInfo> | 是 | 表示订阅者的订阅信息。 |
示例:
//获取订阅者信息回调
function getCB(err:Base.BusinessError, subscribeInfo:CommonEventManager.CommonEventSubscribeInfo) {
if (err.code !== undefined && err.code != null) {
console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("subscribeInfo " + JSON.stringify(subscribeInfo));
}
}
subscriber.getSubscribeInfo(getCB);
getSubscribeInfo
getSubscribeInfo(): Promise<CommonEventSubscribeInfo>
以Promise形式获取订阅者的订阅信息。
系统能力:SystemCapability.Notification.CommonEvent
返回值:
| 类型 | 说明 |
|---|---|
| Promise<CommonEventSubscribeInfo> | 表示订阅者的订阅信息。 |
示例:
subscriber.getSubscribeInfo().then((subscribeInfo:CommonEventManager.CommonEventSubscribeInfo) => {
console.info("subscribeInfo " + JSON.stringify(subscribeInfo));
}).catch((err:Base.BusinessError) => {
console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
});