diff --git a/entry/src/main/ets/model/EventSubscription.ets b/entry/src/main/ets/model/EventSubscription.ets index 54bd72ee585b668c9e058e60c3a79461690a6d2d..1392ff27dc8a00fb93c9eeda3d0683e2486b204e 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 06b2783670a348f95533b352c1ceda909a842bbc..5bebc9755447385d82ce4138f54d991b1f85f348 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 8bf916d4a165e0466c65bc49f58ab2e3c27e8931..2d9c74ff34f1383ec920815a40399dd0fe46a6c5 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": { },