openharmony 鸿蒙 js-apis-commonEventManager-sys

2025-06-12 浏览 (1)

@ohos.commonEventManager (公共事件模块)(系统应用)

本模块提供了公共事件相关的能力,包括发布公共事件、订阅公共事件以及退订公共事件。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

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

导入模块

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

Support

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

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

commonEventManager.publishAsUser

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

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

系统能力: SystemCapability.Notification.CommonEvent

系统API:此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
eventstring表示要发送的公共事件。详见系统公共事件定义
userIdnumber表示指定向该用户ID发送此公共事件。
callbackAsyncCallback<void>表示被指定的回调方法。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
202not system app.
1500003The common event sending frequency too high.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.
1500009Failed to obtain system parameters.

示例:

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

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

//发布公共事件
try {
    commonEventManager.publishAsUser('event', userId, (err: BusinessError) => {
      if (err) {
        console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
        return;
      }
      console.info('publishAsUser');
    });
} catch (error) {
    let err: BusinessError = error as BusinessError;
    console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
}

commonEventManager.publishAsUser

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

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

系统能力: SystemCapability.Notification.CommonEvent

系统API:此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
eventstring表示要发布的公共事件。详见系统公共事件定义
userIdnumber表示指定向该用户ID发送此公共事件。
optionsCommonEventPublishData表示发布公共事件的属性。
callbackAsyncCallback<void>表示被指定的回调方法。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
202not system app.
1500003The common event sending frequency too high.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.
1500009Failed to obtain system parameters.

示例:

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

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

// 指定发送的用户
let userId = 100;
// 发布公共事件
try {
  commonEventManager.publishAsUser('event', userId, options, (err: BusinessError) => {
    if (err) {
      console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    console.info('publishAsUser');
  });
} catch (error) {
  let err: BusinessError = error as BusinessError;
  console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
}

commonEventManager.removeStickyCommonEvent10+

removeStickyCommonEvent(event: string, callback: AsyncCallback<void>): void

以回调形式移除粘性公共事件。

系统能力: SystemCapability.Notification.CommonEvent

需要权限: ohos.permission.COMMONEVENT_STICKY

系统API:此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
eventstring表示被移除的粘性公共事件。详见系统公共事件定义
callbackAsyncCallback<void>表示移除粘性公共事件的回调方法。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
201The application dose not have permission to call the interface.
202not system app.
401Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed.
1500004A third-party application cannot send system common events.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.

示例:

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

commonEventManager.removeStickyCommonEvent('sticky_event', (err: BusinessError) => {
  if (err) {
    console.error(`removeStickyCommonEvent failed, errCode: ${err.code}, errMes: ${err.message}`);
    return;
  }
  console.info(`removeStickyCommonEvent success`);
});

commonEventManager.removeStickyCommonEvent10+

removeStickyCommonEvent(event: string): Promise<void>

以Promise形式移除粘性公共事件。

系统能力: SystemCapability.Notification.CommonEvent

需要权限: ohos.permission.COMMONEVENT_STICKY

系统API:此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
eventstring表示被移除的粘性公共事件。详见系统公共事件定义

返回值:

类型说明
Promise<void>表示移除粘性公共事件的对象。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
201The application dose not have permission to call the interface.
202not system app.
401Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed.
1500004A third-party application cannot send system common events.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.

示例:

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

commonEventManager.removeStickyCommonEvent('sticky_event').then(() => {
  console.info(`removeStickyCommonEvent success`);
}).catch ((err: BusinessError) => {
  console.error(`removeStickyCommonEvent failed, errCode: ${err.code}, errMes: ${err.message}`);
});

commonEventManager.setStaticSubscriberState10+

setStaticSubscriberState(enable: boolean, callback: AsyncCallback<void>): void

方法介绍:为当前应用设置静态订阅事件使能或去使能状态。使用callback异步回调。

模型约束:此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Notification.CommonEvent

系统API:此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
enableboolean表示静态订阅事件使能状态。 true:使能 false:去使能。
callbackAsyncCallback<void>表示设置静态订阅事件使能状态的回调方法。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
202not system app.
401Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.

示例:

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

commonEventManager.setStaticSubscriberState(true, (err: BusinessError) => {
  if (err.code != 0) {
    console.error(`setStaticSubscriberState failed, errCode: ${err.code}, errMes: ${err.message}`);
    return;
  }
  console.info(`setStaticSubscriberState success`);
});

commonEventManager.setStaticSubscriberState10+

setStaticSubscriberState(enable: boolean): Promise<void>

方法介绍:为当前应用设置静态订阅事件使能或去使能状态。使用Promise异步回调。

模型约束:此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Notification.CommonEvent

系统API:此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
enableboolean表示静态订阅事件使能状态。 true:使能 false:去使能。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
202not system app.
401Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.

示例:

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

commonEventManager.setStaticSubscriberState(false).then(() => {
  console.info(`setStaticSubscriberState success`);
}).catch ((err: BusinessError) => {
  console.error(`setStaticSubscriberState failed, errCode: ${err.code}, errMes: ${err.message}`);
});

commonEventManager.setStaticSubscriberState12+

setStaticSubscriberState(enable: boolean, events?: Array<string>): Promise<void>

为当前应用设置静态订阅事件的使能状态,并且记录事件名称。使用Promise异步回调。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Notification.CommonEvent

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

参数:

参数名类型必填说明
enableboolean表示静态订阅事件使能状态。 true:使能 false:去使能。
eventsArray<string>表示记录事件名称。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见通用错误码事件错误码

错误码ID错误信息
202not system app.
401Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed.
1500007Failed to send the message to the common event service.
1500008Failed to initialize the common event service.

示例:

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

let evenName: string[] = ['usual.event.SEND_DATA'];
commonEventManager.setStaticSubscriberState(true, evenName).then(() => {
  console.info(`setStaticSubscriberState success, state is ${true}`);
}).catch((err: BusinessError) => {
  console.error(`setStaticSubscriberState failed, errCode: ${err.code}, errMes: ${err.message}`);
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit(基础服务)

harmony 鸿蒙DeviceInfo

harmony 鸿蒙OH_Print

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

harmony 鸿蒙Print_Range

harmony 鸿蒙TimeService

harmony 鸿蒙ohscan.h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/nZzt6o