中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何监听手机屏幕的灭屏和亮屏?
微信扫码分享
// 订阅者信息,其中的event字段需要替换为实际的事件名称。 subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: [ "usual.event.SCREEN_UNLOCKED","usual.event.SCREEN_LOCKED"],//灭屏和亮屏的事件参数 }; subscriber: commonEventManager.CommonEventSubscriber | null = null; aboutToAppear(): void { // 创建订阅者回调 commonEventManager.createSubscriber(subscribeInfo, (err: Base.BusinessError, data: commonEventManager.CommonEventSubscriber) => { if (err) { console.info(`==========Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); return; } console.info('==========Succeeded in creating subscriber.'); this.subscriber = data; this.scribeSuccess = true // 订阅公共事件回调 if (this.subscriber !== null) { commonEventManager.subscribe(this.subscriber, (err: Base.BusinessError, data: commonEventManager.CommonEventData) => { if (err) { console.info(`==========Failed to subscribe common event. Code is ${err.code}, message is ${err.message}`); return; } console.info("==============success:"+data.event) }) } else { console.info(`==========Need create subscriber`); } }) }