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

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

@ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)

ApplicationStateChangeCallback模块提供应用上下文ApplicationContext对当前应用前后台变化监听回调的能力,包括onApplicationForeground、onApplicationBackground方法。

说明:

本模块首批接口从API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。

导入模块

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

ApplicationStateChangeCallback.onApplicationForeground

onApplicationForeground(): void;

注册当前应用前后台变化的监听后,在当前应用从后台切换到前台时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

示例:

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.获取applicationContext
        let applicationContext = this.context.getApplicationContext();
        // 2.通过applicationContext注册应用前后台状态监听
        if (applicationContext != undefined) {
            applicationContext.on('applicationStateChange', applicationStateChangeCallback);
        }
    }
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        // 1.通过applicationContext解除注册应用前后台状态监听
        if (applicationContext != undefined) {
            applicationContext.off('applicationStateChange', applicationStateChangeCallback);
        }
    }
}

ApplicationStateChangeCallback.onApplicationBackground

onApplicationBackground(): void;

注册当前应用前后台变化的监听后,在当前应用从前台切换到后台时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

示例:

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.获取applicationContext
        let applicationContext = this.context.getApplicationContext();
        // 2.通过applicationContext注册应用前后台状态监听
        if (applicationContext != undefined) {
            applicationContext.on('applicationStateChange', applicationStateChangeCallback);
        }
        console.log('Resgiter applicationStateChangeCallback');
    }
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        // 1.通过applicationContext解除注册应用前后台状态监听
        if (applicationContext != undefined) {
            applicationContext.off('applicationStateChange', applicationStateChangeCallback);
        }
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

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

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞