harmony 鸿蒙ApplicationStateObserver

  • 2022-12-22
  • 浏览 (657)

ApplicationStateObserver

The ApplicationStateObserver module defines an observer to listen for application state changes. It can be used as an input parameter in registerApplicationStateObserver to listen for lifecycle changes of the current application.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import appManager from '@ohos.app.ability.appManager';

Attributes

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Name Type Readable Writable Description
onForegroundApplicationChanged AsyncCallback<void> Yes No Callback invoked when the foreground or background state of an application changes. The parameter type passed in is AppStateData.
onAbilityStateChanged AsyncCallback<void> Yes No Callback invoked when the ability state changes. The parameter type passed in is AppStateData.
onProcessCreated AsyncCallback<void> Yes No Callback invoked when a process is created. The parameter type passed in is ProcessData.
onProcessDied AsyncCallback<void> Yes No Callback invoked when a process is destroyed. The parameter type passed in is ProcessData.
onProcessStateChanged9+ AsyncCallback<void> Yes No Callback invoked when the process state is changed. The parameter type passed in is ProcessData.

Example

import appManager from '@ohos.app.ability.appManager';

let applicationStateObserver: appManager.ApplicationStateObserver = {
    onForegroundApplicationChanged(appStateData) {
        console.log(`onForegroundApplicationChanged appStateData: ${JSON.stringify(appStateData)}`);
    },
    onAbilityStateChanged(abilityStateData) {
        console.log(`onAbilityStateChanged onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
    },
    onProcessCreated(processData) {
        console.log(`onProcessCreated onProcessCreated: ${JSON.stringify(processData)}`);
    },
    onProcessDied(processData) {
        console.log(`onProcessDied onProcessDied: ${JSON.stringify(processData)}`);
    },
    onProcessStateChanged(processData) {
        console.log(`onProcessStateChanged onProcessStateChanged: ${JSON.stringify(processData)}`);
    }
};
let observerCode = appManager.on('applicationState', applicationStateObserver);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞