HarmonyOS 共事件模块,针对锁屏和解锁,回调事件并不会触发?

阅读 443
1 个回答

参考示例代码:

import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
import WantAgent, { WantAgent as _WantAgent } from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { notificationManager } from '@kit.NotificationKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

export const LOG_TAG = 'subscriber'

@Entry
@Component
struct Index {
  aboutToAppear(): void {
    notificationManager.requestEnableNotification();
  }

  build() {
    Row() {
      Column() {
        Text('订阅方')
          .fontSize(30)
          .fontWeight(500)
        Button('订阅公共事件')
          .fontSize(16)
          .fontWeight(500)
          .padding(15)
          .margin(10)
          .width('300')
          .height('60')
          .onClick(() => {
            //创建订阅者
            try {
              CommonEventManager.createSubscriber(subscribeInfo, createCB);
            } catch (error) {
              let err: Base.BusinessError = error as Base.BusinessError;
              hilog.error(0xFF00, LOG_TAG, `createSubscriber failed, code is ${err.code}, message is ${err.message}`);
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

let subscriber: CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let subscribeInfo: CommonEventManager.CommonEventSubscribeInfo = {
  events: ['usual.event.SCREEN_UNLOCKED']
};

//订阅公共事件回调
function SubscribeCB(err: Base.BusinessError, data: CommonEventManager.CommonEventData) {
  if (err) {
    hilog.error(0xFF00, LOG_TAG, `subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    publishNotification();
    hilog.info(0xFF00, LOG_TAG, 'subscribe success');
  }
}

//创建订阅者回调
function createCB(err: Base.BusinessError, commonEventSubscriber: CommonEventManager.CommonEventSubscriber) {
  if (!err) {
    hilog.info(0xFF00, LOG_TAG, 'createSubscriber');
    subscriber = commonEventSubscriber;
    //订阅公共事件
    try {
      CommonEventManager.subscribe(subscriber, SubscribeCB);
    } catch (error) {
      let err: Base.BusinessError = error as Base.BusinessError;
      hilog.error(0xFF00, LOG_TAG, `subscribe failed, code is ${err.code}, message is ${err.message}`);
    }
  } else {
    hilog.error(0xFF00, LOG_TAG, `createSubscriber failed, code is ${err.code}, message is ${err.message}`);
  }
}

async function publishNotification() {
  let wantAgent: _WantAgent;
  //WantAgentInfo对象
  let wantAgentInfo: WantAgent.WantAgentInfo = {
    wants: [
      {
        bundleName: 'com.example.mysubscriber',
        abilityName: 'EntryAbility',
      } as Want
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags: [WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  };

  WantAgent.getWantAgent(wantAgentInfo).then((data) => {
    wantAgent = data;
    let notificationRequest: notificationManager.NotificationRequest = {
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: '公共事件',
          text: '系统公共事件',
          additionalText: 'Test_AdditionalText',
        },
      },
      id: 6,
      notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION, //社交类型通知
      label: 'Receive CommonEvent',
      wantAgent: wantAgent,
    };
    notificationManager.publish(notificationRequest);
  });
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏