harmony 鸿蒙@ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)

  • 2023-10-30
  • 浏览 (387)

@ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)

The ApplicationStateChangeCallback module provides callbacks for the application context to listen for application foreground/background state changes.

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. The APIs of this module can be used only in the stage model.

Modules to Import

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

ApplicationStateChangeCallback.onApplicationForeground

onApplicationForeground(): void;

Called when the application is switched from the background to the foreground.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Example

import UIAbility from '@ohos.app.ability.UIAbility';
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';

let applicationStateChangeCallback: ApplicationStateChangeCallback = {
    onApplicationForeground() {
        console.info('applicationStateChangeCallback onApplicationForeground');
    },
    onApplicationBackground() {
        console.info('applicationStateChangeCallback onApplicationBackground');
    }
}

export default class MyAbility extends UIAbility {
    onCreate() {
        console.log('MyAbility onCreate');
        // 1. Obtain an applicationContext object.
        let applicationContext = this.context.getApplicationContext();
        // 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
        if (applicationContext != undefined) {
            applicationContext.on('applicationStateChange', applicationStateChangeCallback);
        }
    }
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        // 1. Use applicationContext.off() to unsubscribe from the 'applicationStateChange' event.
        if (applicationContext != undefined) {
            applicationContext.off('applicationStateChange', applicationStateChangeCallback);
        }
    }
}

ApplicationStateChangeCallback.onApplicationBackground

onApplicationBackground(): void;

Called when the application is switched from the foreground to the background.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Example

import UIAbility from '@ohos.app.ability.UIAbility';
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';

let applicationStateChangeCallback: ApplicationStateChangeCallback = {
    onApplicationForeground() {
        console.info('applicationStateChangeCallback onApplicationForeground');
    },
    onApplicationBackground() {
        console.info('applicationStateChangeCallback onApplicationBackground');
    }
}

export default class MyAbility extends UIAbility {
    onCreate() {
        console.log('MyAbility onCreate');
        // 1. Obtain an applicationContext object.
        let applicationContext = this.context.getApplicationContext();
        // 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
        if (applicationContext != undefined) {
            applicationContext.on('applicationStateChange', applicationStateChangeCallback);
        }
        console.log('Resgiter applicationStateChangeCallback');
    }
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        // 1. Use applicationContext.off() to unsubscribe from the 'applicationStateChange' event.
        if (applicationContext != undefined) {
            applicationContext.off('applicationStateChange', applicationStateChangeCallback);
        }
    }
}

你可能感兴趣的鸿蒙文章

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  赞