We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Subscribing to Time-Consuming Launch Events (ArkTS)

API Description

For details about how to use the API (parameter usage restrictions and value ranges), see @ohos.hiviewdfx.hiAppEvent (Application Event Logging).

Expand
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.

Development Procedure

The following describes how to subscribe to the startup duration event generated when a user runs an application project.

  1. Edit the entry > src > main > ets > entryability > EntryAbility.ets file in the project to add system subscription. The sample code is as follows:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { hiAppEvent, hilog } from '@kit.PerformanceAnalysisKit';
    2. hiAppEvent.addWatcher({
    3. // Set the watcher name. The system identifies different watchers based on their names.
    4. name: "watcher",
    5. // You can subscribe to system events that you are interested in. Here, the time-consuming launch event is subscribed to.
    6. appEventFilters: [
    7. {
    8. domain: hiAppEvent.domain.OS,
    9. names: [hiAppEvent.event.APP_LAUNCH]
    10. }
    11. ],
    12. // Implement a callback for the registered system event so that you can apply custom processing to the event data obtained.
    13. onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => {
    14. hilog.info(0x0000, 'testTag', `HiAppEvent onReceive: domain=${domain}`);
    15. for (const eventGroup of appEventGroups) {
    16. // The event name uniquely identifies a system event.
    17. hilog.info(0x0000, 'testTag', `HiAppEvent eventName=${eventGroup.name}`);
    18. for (const eventInfo of eventGroup.appEventInfos) {
    19. // Apply custom processing to the event data obtained, for example, print the event data in the log.
    20. hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo=${JSON.stringify(eventInfo)}`);
    21. }
    22. }
    23. }
    24. });
    NOTE
    • After the APP_LAUNCH event is subscribed to, if the app exits or crashes when the app is started and the APP_LAUNCH event is triggered, the onReceive callback API may not be triggered. As a result, the event callback fails. When the app is restarted, the callback function transfers the content of the previous uncompleted callback to the app. As a result, the onReceive callback may be called multiple times during an app startup.

    • You are not advised to execute any key service logic (such as removing an event observer and releasing resources) in onReceive. Otherwise, service code may be repeatedly executed and resources may be released for multiple times, causing program errors or even application breakdown.

    • You are advised to use onReceive only for event receiving and simple recording.

  2. Click the Run button in DevEco Studio to run the application project, add a system event subscriber, and exit the application. Click the application icon on the home screen again to trigger another time-consuming launch event.

  3. After restarting the application, you can view the system event processing log in the Log window.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. HiAppEvent onReceive: domain=OS
    2. HiAppEvent eventName=APP_LAUNCH
    3. HiAppEvent eventInfo={"domain":"OS","name":"APP_LAUNCH","eventType":4,"params":{"animation_finish_time":662,"bundle_name":"com.example.myapplication","bundle_version":"1.0.0","extend_time":0,"icon_input_time":1709367533224,"process_name":"com.example.myapplication","start_type":0,"time":1709367533901}}
Search in Guides
Enter a keyword.