鸿蒙5.0开发进阶:Stage模型-@ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)

往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)


@ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)

ApplicationStateChangeCallback模块提供应用上下文ApplicationContext对当前应用前后台变化监听回调的能力。

说明

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

本模块接口仅可在Stage模型下使用。

导入模块

import { ApplicationStateChangeCallback } from '@kit.AbilityKit';

ApplicationStateChangeCallback.onApplicationForeground

onApplicationForeground(): void

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

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

ApplicationStateChangeCallback.onApplicationBackground

onApplicationBackground(): void

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

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

示例:

import { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

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();
    try {
      // 2.通过applicationContext注册应用前后台状态监听
      if (applicationContext != undefined) {
        applicationContext.on('applicationStateChange', applicationStateChangeCallback);
      }
    } catch (paramError) {
      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
    }
    console.log('Resgiter applicationStateChangeCallback');
  }
  onDestroy() {
    let applicationContext = this.context.getApplicationContext();
    try {
      // 1.通过applicationContext解除注册应用前后台状态监听
      if (applicationContext != undefined) {
        applicationContext.off('applicationStateChange', applicationStateChangeCallback);
      } 
    } catch (paramError) {
      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
    }
  }
}

ysql001 ~]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl \Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package zabbix-apache-conf-scl.noarch 0:5.0.47-1.el7 will be installed --> Processing Dependency: zabbix-web-deps-scl = 5.0.47-1.el7 for package: zabbix-apache-conf-scl-5.0.47-1.el7.noarch ---> Package zabbix-web-mysql-scl.noarch 0:5.0.47-1.el7 will be installed --> Processing Dependency: rh-php72-php-mysqlnd for package: zabbix-web-mysql-scl-5.0.47-1.el7.noarch --> Running transaction check ---> Package zabbix-web-deps-scl.noarch 0:5.0.47-1.el7 will be installed --> Processing Dependency: rh-php72 for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch --> Processing Dependency: rh-php72-php-bcmath for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch --> Processing Dependency: rh-php72-php-fpm for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch --> Processing Dependency: rh-php72-php-gd for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch --> Processing Dependency: rh-php72-php-ldap for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch --> Processing Dependency: rh-php72-php-mbstring for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch --> Processing Dependency: rh-php72-php-xml for package: zabbix-web-deps-scl-5.0.47-1.el7.noarch ---> Package zabbix-web-mysql-scl.noarch 0:5.0.47-1.el7 will be installed --> Processing Dependency: rh-php72-php-mysqlnd for package: zabbix-web-mysql-scl-5.0.47-1.el7.noarch --> Finished Dependency Resolution Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-fpm Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-ldap Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-xml Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-mbstring Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-bcmath Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-gd Error: Package: zabbix-web-mysql-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72-php-mysqlnd Error: Package: zabbix-web-deps-scl-5.0.47-1.el7.noarch (zabbix-frontend) Requires: rh-php72 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest为啥
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值