From 73c9998a171687df2110de3f45cf904007ff8c90 Mon Sep 17 00:00:00 2001 From: lloyd <353627866@qq.com> Date: Fri, 12 Sep 2025 14:40:11 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AE=9E=E7=8E=B0=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD=E3=80=91?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/model/EventSubscription.ets | 52 +++++++++++-------- hvigor/hvigor-config.json5 | 2 +- oh-package.json5 | 2 +- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/entry/src/main/ets/model/EventSubscription.ets b/entry/src/main/ets/model/EventSubscription.ets index 54bd72e..1392ff2 100644 --- a/entry/src/main/ets/model/EventSubscription.ets +++ b/entry/src/main/ets/model/EventSubscription.ets @@ -15,32 +15,38 @@ import { hiAppEvent } from '@kit.PerformanceAnalysisKit'; import { Logger } from '../log/Logger'; +import { BusinessError } from '@kit.BasicServicesKit'; const TAG: string = 'eventSubscription'; export function eventSubscription(): void { - // Add application event observer method, which can be used to subscribe to application events - hiAppEvent.addWatcher({ - // Developers can customize observer names, which the system will use to identify different observers - name: 'mst', - // Developers can subscribe to system events of interest, where they have subscribed to crash events - appEventFilters: [ - { - domain: hiAppEvent.domain.OS, - names: [hiAppEvent.event.APP_CRASH, hiAppEvent.event.APP_FREEZE] + try { + // Add application event observer method, which can be used to subscribe to application events + hiAppEvent.addWatcher({ + // Developers can customize observer names, which the system will use to identify different observers + name: 'mst', + // Developers can subscribe to system events of interest, where they have subscribed to crash events + appEventFilters: [ + { + domain: hiAppEvent.domain.OS, + names: [hiAppEvent.event.APP_CRASH, hiAppEvent.event.APP_FREEZE] + } + ], + // Knowledge point: Obtain event group information. + // Developers can implement subscription callback functions themselves, + // and crash and freeze events will be called back to developers the next time the application starts + onReceive: async (domain: string, appEventGroups: Array) => { + Logger.info(TAG, `HiAppEvent onReceive: domain=${domain}`); + // Obtain event group information and perform bidirectional data binding with + // @StorageLink('faultMessage') faultMessage in the ApplicationException file + // Performance concerns: If developers have synchronized code that requires time-consuming operations, + // it is recommended to start worker or taskpool threads to handle it. + // But if developers use storage and preferences, they can simply call them. + AppStorage.setOrCreate('appEventGroups', appEventGroups); } - ], - // Knowledge point: Obtain event group information. - // Developers can implement subscription callback functions themselves, - // and crash and freeze events will be called back to developers the next time the application starts - onReceive: async (domain: string, appEventGroups: Array) => { - Logger.info(TAG, `HiAppEvent onReceive: domain=${domain}`); - // Obtain event group information and perform bidirectional data binding with - // @StorageLink('faultMessage') faultMessage in the ApplicationException file - // Performance concerns: If developers have synchronized code that requires time-consuming operations, - // it is recommended to start worker or taskpool threads to handle it. - // But if developers use storage and preferences, they can simply call them. - AppStorage.setOrCreate('appEventGroups', appEventGroups); - } - }); + }); + } catch (error) { + let err = error as BusinessError; + Logger.error(TAG, `addWatcher failed, error code=${err.code}, message=${err.message}`); + } } \ No newline at end of file diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 index 06b2783..5bebc97 100644 --- a/hvigor/hvigor-config.json5 +++ b/hvigor/hvigor-config.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.0", + "modelVersion": "5.0.5", "dependencies": { }, "execution": { diff --git a/oh-package.json5 b/oh-package.json5 index 8bf916d..2d9c74f 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.0", + "modelVersion": "5.0.5", "description": "Please describe the basic information.", "dependencies": { }, -- Gitee