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

共事件模块,针对锁屏和解锁,回调事件并不会触发但是使用已经废弃的这个api,是可以监听到的,

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-screen-lock-V5

麻烦帮忙看下为什么共事件模块,针对锁屏和解锁,回调事件并不会触发

HarmonyOS
2024-12-23 16:49:06
9016浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

参考示例代码:

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);
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
分享
微博
QQ
微信
回复
2024-12-23 20:58:28


相关问题
HarmonyOS 屏幕和解锁屏幕
1047浏览 • 1回复 待解决
焦点事件onBlur/onFocus无法触发
2863浏览 • 1回复 待解决
HarmonyOS 事件
1030浏览 • 1回复 待解决
HarmonyOS 如何获取事件
920浏览 • 1回复 待解决
HarmonyOS 如何监听截事件触发
1019浏览 • 1回复 待解决
HarmonyOS 点击事件方法
1037浏览 • 1回复 待解决
HarmonyOS Scroll组件事件问题
977浏览 • 1回复 待解决
用户订阅系统公共事件
1949浏览 • 1回复 待解决
共事件有哪些简单使用
1727浏览 • 1回复 待解决
共事件实现跨进程通信
1616浏览 • 1回复 待解决