目录

Subscribing to Workspace Events

Since API version 6.0.0(20), the capabilities of subscribing to and unsubscribing from workspace events are supported.

Use Cases

Enterprise Space Kit provides the capability of subscribing to workspace events for enterprise apps. After subscribing to a workspace switching event, an app will receive a notification when the workspace is switched and perform the preset custom action. In addition, Enterprise Space Kit provides the capability of unsubscribing from a workspace event. Your app can flexibly manage workspace event subscriptions in specific scenarios.

API Description

For details about these APIs, please refer to the API Reference.

API Description
subscribeEvent(eventId: EventType[], callback: AsyncCallback<EventData>): number Subscribes to a workspace event. Your app will be notified of the event when it is triggered.
unsubscribeEvent(subscribeId: number): void Unsubscribes from a workspace event. Your app will no longer be notified of the event when it is triggered.

Development Procedure

  1. Import the dependencies of the API module for subscribing to workspace events.
import { AsyncCallback } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { spaceManager } from '@kit.EnterpriseSpaceKit';
import { ErrCode } from '../../common/ErrCode';
  1. Encapsulate the API for subscribing to workspace events.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

export class SubscribeSpaceEventApi {
  static subscribeEvent(eventIds: spaceManager.EventType[], callback: AsyncCallback<spaceManager.EventData>): number {
    try {
      const subscribeId = spaceManager.subscribeEvent(eventIds, callback);
      hilog.info(DOMAIN, TAG, `Succeeded in subscribing event. subscribeId: ${subscribeId}`);
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to subscribe event. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }

  static unsubscribeEvent(subscribeId: number): void {
    try {
      spaceManager.unsubscribeEvent(subscribeId);
      hilog.info(DOMAIN, TAG, 'Succeeded in unsubscribing event');
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to unsubscribe event. Code: ${err.code}, message: ${err.message}`);
    }
  }
}
  1. Import the dependencies of the service implementation for subscribing to workspace events.
import { router } from '@kit.ArkUI';
import { spaceManager } from '@kit.EnterpriseSpaceKit';
import { SubscribeSpaceEventApi } from '../api/SubscribeSpaceEventApi'
import { ErrCode } from '../../common/ErrCode';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Implement the workspace event subscription service.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry
@Component
struct SubscribeSpaceEventPage {
  subscribe() {
    let eventIds: spaceManager.EventType[] = [spaceManager.EventType.EVENT_WORKSPACE_SWITCHED];
    let callBack = (error: BusinessError, data: spaceManager.EventData) => {
      if (error) {
        hilog.error(DOMAIN, TAG, `error info:${error?.code}, err message:${error?.message}`);
      } else {
        hilog.info(DOMAIN, TAG, `event: ${data.event},currentWorkSpaceId: ${data.currentWorkspaceId}`);
        // Process the event.
      }
    };
    if (SubscribeSpaceEventApi.subscribeEvent(eventIds, callBack) !== ErrCode.OK) {
      // Handle the subscription failure.
      hilog.error(DOMAIN, TAG, 'Failed to subscribe event!');
      return;
    }

    // Handle the subscription success.
  }

  unSubscribe() {
    let subscribeId = 100; // Subscription ID obtained when a workspace event is successfully subscribed to.
    SubscribeSpaceEventApi.unsubscribeEvent(subscribeId);
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.subscribe'))
          .buttonCommonStyle()
          .onClick(() => {
            this.subscribe();
          })
      }

      Row() {
        Button($r('app.string.unSubscribe'))
          .buttonCommonStyle()
          .onClick(() => {
            this.unSubscribe();
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(200)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 5 })
}