openharmony 鸿蒙 js-apis-commonEventManager-sys

2025-06-12 浏览 (1)

@ohos.commonEventManager (Common Event) (System API)

This module provides common event capabilities, including publishing, subscribing to, and unsubscribing from common events.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This topic describes only system APIs provided by the module. For details about its public APIs, see CommonEventManager.

Modules to Import

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

Support

A system common event is an event that is published by a system service or system application and requires specific permissions to subscribe to. To publish or subscribe to this type of event, you must follow the event-specific definitions.

For details about the enumerations of all system common events, see System Common Events.

commonEventManager.publishAsUser

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

Publishes a common event to a specific user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.CommonEvent

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
eventstringYesName of the common event to publish. For details, see System Common Events.
userIdnumberYesUser ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
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.
1500009Failed to obtain system parameters.

Example

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

// Specify the user to whom the common event will be published.
let userId = 100;

// Publish a common event.
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

Publishes a common event with given attributes to a specific user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.CommonEvent

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
eventstringYesName of the common event to publish. For details, see System Common Events.
userIdnumberYesUser ID.
optionsCommonEventPublishDataYesAttributes of the common event to publish.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
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.
1500009Failed to obtain system parameters.

Example

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

// Attributes of a common event.
let options:commonEventManager.CommonEventPublishData = {
  code: 0,			 // Result code of the common event.
  data: 'initial data', // Initial data of the common event.
}

// Specify the user to whom the common event will be published.
let userId = 100;
// Publish a common event.
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

Removes a sticky common event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.CommonEvent

Required permissions: ohos.permission.COMMONEVENT_STICKY

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
eventstringYesSticky common event to remove. For details, see System Common Events.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
201The application does 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.

Example

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>

Removes a sticky common event. This API uses a promise to return the result.

System capability: SystemCapability.Notification.CommonEvent

Required permissions: ohos.permission.COMMONEVENT_STICKY

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
eventstringYesSticky common event to remove. For details, see System Common Events.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
201The application does 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.

Example

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

Enables or disables static subscription for the current application. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Notification.CommonEvent

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether static subscription is enabled.
true: enabled.
false: disabled.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
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.

Example

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>

Enables or disables static subscription for the current application. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Notification.CommonEvent

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether static subscription is enabled.
true: enabled.
false: disabled.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
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.

Example

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>

Enables or disables the static subscription event for the current application and records the event name. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Notification.CommonEvent

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether static subscription is enabled.
true: enabled.
false: disabled.
eventsArray<string>NoName of a recorded event.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Event Error Codes.

IDError Message
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.

Example

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 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

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