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

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

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

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

HarmonyOS
3天前
浏览
收藏 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);
  });
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS 屏幕和解锁屏幕
11浏览 • 1回复 待解决
焦点事件onBlur/onFocus无法触发
2053浏览 • 1回复 待解决
HarmonyOS 事件
73浏览 • 1回复 待解决
HarmonyOS 如何获取事件
86浏览 • 1回复 待解决
HarmonyOS 点击事件方法
55浏览 • 1回复 待解决
HarmonyOS 如何监听截事件触发
36浏览 • 1回复 待解决
HarmonyOS Scroll组件事件问题
226浏览 • 1回复 待解决
用户订阅系统公共事件
1158浏览 • 1回复 待解决