import { hiAppEvent } from '@kit.PerformanceAnalysisKit'; import { router } from '@kit.ArkUI'; /** * 自定义崩溃处理工具类 * 替代 spider-man 的默认崩溃页面,使用我们自定义的崩溃页面 */ export class CustomCrashHandler { /** * 初始化自定义崩溃处理,替代 spider-man 的默认行为 */ static init() { // 添加自己的崩溃监听器 hiAppEvent.addWatcher({ name: 'CustomSpiderMan', appEventFilters: [{ domain: hiAppEvent.domain.OS, names: [hiAppEvent.event.APP_CRASH] }], onReceive: (domain: string, appEventGroups: Array) => { console.error('CustomCrashHandler onReceive -- ' + domain); if (appEventGroups.length < 1) { console.error('appEventGroups.length < 1'); return; } // 取第一个崩溃事件组 const appEventGroup = appEventGroups[0]; CustomCrashHandler.openCustomCrashPage(appEventGroup); }, }); } /** * 打开自定义崩溃页面 */ static openCustomCrashPage(appEventGroup: hiAppEvent.AppEventGroup) { setTimeout(() => { router.pushNamedRoute({ name: 'CustomCrashPage', params: appEventGroup }).then(() => { console.error('CustomCrashPage pushNamedRoute success'); }).catch(() => { console.error('CustomCrashPage pushNamedRoute failed'); // 如果命名路由失败,尝试使用普通路由 router.pushUrl({ url: 'pages/CustomCrashPage', params: appEventGroup }).catch(() => { console.error('CustomCrashPage pushUrl also failed'); }); }); }, 1000); } }