1e41f4b71Sopenharmony_ci# @ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciApplicationStateChangeCallback模块提供应用上下文ApplicationContext对当前应用前后台变化监听回调的能力。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport { ApplicationStateChangeCallback } from '@kit.AbilityKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## ApplicationStateChangeCallback.onApplicationForeground
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cionApplicationForeground(): void
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci注册当前应用前后台变化的监听后,在当前应用从后台切换到前台时触发回调。
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**示例:**
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci参见[onApplicationBackground](#applicationstatechangecallbackonapplicationbackground)。
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci## ApplicationStateChangeCallback.onApplicationBackground
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_cionApplicationBackground(): void
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci注册当前应用前后台变化的监听后,在当前应用从前台切换到后台时触发回调。
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**示例:**
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci```ts
44e41f4b71Sopenharmony_ciimport { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
45e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_cilet applicationStateChangeCallback: ApplicationStateChangeCallback = {
48e41f4b71Sopenharmony_ci  onApplicationForeground() {
49e41f4b71Sopenharmony_ci    console.info('applicationStateChangeCallback onApplicationForeground');
50e41f4b71Sopenharmony_ci  },
51e41f4b71Sopenharmony_ci  onApplicationBackground() {
52e41f4b71Sopenharmony_ci    console.info('applicationStateChangeCallback onApplicationBackground');
53e41f4b71Sopenharmony_ci  }
54e41f4b71Sopenharmony_ci};
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility {
57e41f4b71Sopenharmony_ci  onCreate() {
58e41f4b71Sopenharmony_ci    console.log('MyAbility onCreate');
59e41f4b71Sopenharmony_ci    // 1.获取applicationContext
60e41f4b71Sopenharmony_ci    let applicationContext = this.context.getApplicationContext();
61e41f4b71Sopenharmony_ci    try {
62e41f4b71Sopenharmony_ci      // 2.通过applicationContext注册应用前后台状态监听
63e41f4b71Sopenharmony_ci      if (applicationContext != undefined) {
64e41f4b71Sopenharmony_ci        applicationContext.on('applicationStateChange', applicationStateChangeCallback);
65e41f4b71Sopenharmony_ci      }
66e41f4b71Sopenharmony_ci    } catch (paramError) {
67e41f4b71Sopenharmony_ci      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
68e41f4b71Sopenharmony_ci    }
69e41f4b71Sopenharmony_ci    console.log('Resgiter applicationStateChangeCallback');
70e41f4b71Sopenharmony_ci  }
71e41f4b71Sopenharmony_ci  onDestroy() {
72e41f4b71Sopenharmony_ci    let applicationContext = this.context.getApplicationContext();
73e41f4b71Sopenharmony_ci    try {
74e41f4b71Sopenharmony_ci      // 1.通过applicationContext解除注册应用前后台状态监听
75e41f4b71Sopenharmony_ci      if (applicationContext != undefined) {
76e41f4b71Sopenharmony_ci        applicationContext.off('applicationStateChange', applicationStateChangeCallback);
77e41f4b71Sopenharmony_ci      } 
78e41f4b71Sopenharmony_ci    } catch (paramError) {
79e41f4b71Sopenharmony_ci      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
80e41f4b71Sopenharmony_ci    }
81e41f4b71Sopenharmony_ci  }
82e41f4b71Sopenharmony_ci}
83e41f4b71Sopenharmony_ci```
84