@ohos.commonEvent (公共事件模块)(系统应用)(已废弃)

本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。

说明:

从API version 7 开始支持,从API version 9 开始废弃,建议使用@ohos.commonEventManager替代。

当前界面仅包含本模块的系统接口,其他公开接口参见CommonEvent

导入模块

import commonEvent from '@ohos.commonEvent';

Support

系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。

全部系统公共事件枚举定义请参见系统公共事件定义

commonEvent.publishAsUser(deprecated)

publishAsUser(event: string, userId: number, callback: AsyncCallback<void>): void

以回调的形式向指定用户发布公共事件。

说明:

从API version 8 开始支持,从API version 9 开始废弃,建议使用commonEventManager.publishAsUser替代。

系统能力: SystemCapability.Notification.CommonEvent

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
event string 表示要发送的公共事件。
userId number 表示指定向该用户ID发送此公共事件。
callback AsyncCallback<void> 表示被指定的回调方法。

示例:

import Base from '@ohos.base';

// 发布公共事件回调
function publishCB(err:Base.BusinessError) {
    if (err.code) {
        console.error(`publishAsUser failed, code is ${err.code}`);
    } else {
        console.info("publishAsUser");
    }
}

// 指定发送的用户
let userId = 100;

// 发布公共事件
commonEvent.publishAsUser("event", userId, publishCB);

commonEvent.publishAsUser(deprecated)

publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback<void>): void

以回调形式向指定用户发布公共事件并指定发布信息。

说明: 从API version 8 开始支持,从API version 9 开始废弃,建议使用commonEventManager.publishAsUser替代。

系统能力: SystemCapability.Notification.CommonEvent

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
event string 表示要发布的公共事件。
userId number 表示指定向该用户ID发送此公共事件。
options CommonEventPublishData 表示发布公共事件的属性。
callback AsyncCallback<void> 表示被指定的回调方法。

示例:

import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';

// 公共事件相关信息
let options:CommonEventManager.CommonEventPublishData = {
    code: 0,             // 公共事件的初始代码
    data: "initial data",// 公共事件的初始数据
}

// 发布公共事件回调
function publishCB(err:Base.BusinessError) {
    if (err.code) {
        console.error(`publishAsUser failed, code is ${err.code}`);
    } else {
        console.info("publishAsUser");
    }
}

// 指定发送的用户
let userId = 100;

// 发布公共事件
commonEvent.publishAsUser("event", userId, options, publishCB);