harmony 鸿蒙Subscribing to Freeze Events (ArkTS)
Subscribing to Freeze Events (ArkTS)
Available APIs
For details about how to use the APIs (such as parameter usage constraints and value ranges), see Application Event Logging.
API for Setting Custom Event Parameters
API | Description |
---|---|
setEventParam(params: Record<string, ParamType>, domain: string, name?: string): Promise<void> | Sets custom event parameters. |
Subscription APIs
API | Description |
---|---|
addWatcher(watcher: Watcher): AppEventPackageHolder | Adds a watcher to listen for application events. |
removeWatcher(watcher: Watcher): void | Removes a watcher to unsubscribe from application events. |
How to Develop
The following describes how to subscribe to the freeze event triggered by a button click.
- Create an ArkTS application project. In the entry/src/main/ets/entryability/EntryAbility.ets file, import the dependent modules.
import { BusinessError } from '@kit.BasicServicesKit';
import { hiAppEvent, hilog } from '@kit.PerformanceAnalysisKit';
- In the entry/src/main/ets/entryability/EntryAbility.ets file, set the custom parameters in onCreate(). The sample code is as follows:
// Assign a value to params, which is a key-value pair.
let params: Record<string, hiAppEvent.ParamType> = {
"test_data": 100,
};
// Set custom parameters for the freeze event.
hiAppEvent.setEventParam(params, hiAppEvent.domain.OS, hiAppEvent.event.APP_FREEZE).then(() => {
hilog.info(0x0000, 'testTag', `HiAppEvent success to set svent param`);
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', `HiAppEvent code: ${err.code}, message: ${err.message}`);
});
- In the entry/src/main/ets/entryability/EntryAbility.ets file, add a watcher in onCreate() to subscribe to system events. The sample code is as follows:
hiAppEvent.addWatcher({
// Set the watcher name. The system identifies different watchers based on their names.
name: "watcher",
// Add the system events to watch, for example, freeze events.
appEventFilters: [
{
domain: hiAppEvent.domain.OS,
names: [hiAppEvent.event.APP_FREEZE]
}
],
// Implement a callback for the registered system event so that you can apply custom processing to the event data obtained.
onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => {
hilog.info(0x0000, 'testTag', `HiAppEvent onReceive: domain=${domain}`);
for (const eventGroup of appEventGroups) {
// The event name uniquely identifies a system event.
hilog.info(0x0000, 'testTag', `HiAppEvent eventName=${eventGroup.name}`);
for (const eventInfo of eventGroup.appEventInfos) {
// Apply custom processing to the event data obtained, for example, print the event data in the log.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.domain=${eventInfo.domain}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.name=${eventInfo.name}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.eventType=${eventInfo.eventType}`);
// Obtain the timestamp of the freeze event.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.time=${eventInfo.params['time']}`);
// Obtain the foreground and background status of the application when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.foreground=${eventInfo.params['foreground']}`);
// Obtain the version information of the application.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.bundle_version=${eventInfo.params['bundle_version']}`);
// Obtain the bundle name of the application.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.bundle_name=${eventInfo.params['bundle_name']}`);
// Obtain the process name of the application.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.process_name=${eventInfo.params['process_name']}`);
// Obtain the process ID of the application.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.pid=${eventInfo.params['pid']}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.uid=${eventInfo.params['uid']}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.uuid=${eventInfo.params['uuid']}`);
// Obtain the exception type and cause of the freeze event.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.exception=${JSON.stringify(eventInfo.params['exception'])}`);
// Obtain the log information when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.hilog.size=${eventInfo.params['hilog'].length}`);
// Obtain the messages that are not yet processed by the main thread when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.event_handler.size=${eventInfo.params['event_handler'].length}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.event_handler_size_3s=${eventInfo.params['event_handler_size_3s']}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.event_handler_size_6s=${eventInfo.params['event_handler_size_6s']}`);
// Obtain the synchronous binder call information when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.peer_binder.size=${eventInfo.params['peer_binder'].length}`);
// Obtain the full thread call stack when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.threads.size=${eventInfo.params['threads'].length}`);
// Obtain the memory information when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.memory=${JSON.stringify(eventInfo.params['memory'])}`);
// Obtain the error log file when the freeze event occurs.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.external_log=${JSON.stringify(eventInfo.params['external_log'])}`);
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.log_over_limit=${eventInfo.params['log_over_limit']}`);
// Obtain the custom test_data of the freeze event.
hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.test_data=${eventInfo.params['test_data']}`);
}
}
}
});
- In the entry/src/main/ets/pages/index.ets file, add the appFreeze button and construct a scenario for triggering a freeze event in onClick(). The sample code is as follows:
Button("appFreeze").onClick(()=>{
// Construct a scenario in onClick() for triggering a freeze event.
setTimeout(() => {
while (true) {}
}, 1000)
})
In DevEco Studio, click the Run button to run the project. Then, click the appfreeze button to trigger a freeze event.
The application crashes. After restarting the application, you can view the following event information in the Log window.
HiAppEvent onReceive: domain=OS
HiAppEvent eventName=APP_FREEZE
HiAppEvent eventInfo.domain=OS
HiAppEvent eventInfo.name=APP_FREEZE
HiAppEvent eventInfo.eventType=1
HiAppEvent eventInfo.params.time=1711440881768
HiAppEvent eventInfo.params.foreground=true
HiAppEvent eventInfo.params.bundle_version=1.0.0
HiAppEvent eventInfo.params.bundle_name=com.example.myapplication
HiAppEvent eventInfo.params.process_name=com.example.myapplication
HiAppEvent eventInfo.params.pid=3197
HiAppEvent eventInfo.params.uid=20010043
HiAppEvent eventInfo.params.uuid=27fac7098da46efe1cae9904946ec06c5acc91689c365efeefb7a23a0c37df77
HiAppEvent eventInfo.params.exception={"message":"App main thread is not response!","name":"THREAD_BLOCK_6S"}
HiAppEvent eventInfo.params.hilog.size=77
HiAppEvent eventInfo.params.event_handler.size=6
HiAppEvent eventInfo.params.event_handler_size_3s=5
HiAppEvent eventInfo.params.event_handler_size_6s=6
HiAppEvent eventInfo.params.peer_binder.size=0
HiAppEvent eventInfo.params.threads.size=28
HiAppEvent eventInfo.params.memory={"pss":0,"rss":0,"sys_avail_mem":1361464,"sys_free_mem":796232,"sys_total_mem":1992340,"vss":0}
HiAppEvent eventInfo.params.external_log=["/data/storage/el2/log/hiappevent/APP_FREEZE_1711440899240_3197.log"]
HiAppEvent eventInfo.params.log_over_limit=false
HiAppEvent eventInfo.params.test_data=100
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Performance Analysis Kit
harmony 鸿蒙Analyzing Application Freeze
harmony 鸿蒙Development of Application Recovery
harmony 鸿蒙Development of Error Manager
harmony 鸿蒙Introduction to HiAppEvent
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦