1e41f4b71Sopenharmony_ci# ApplicationStateObserver (系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci定义应用状态监听,可以作为[on](js-apis-app-ability-appManager-sys.md#appmanageronapplicationstate)的入参监听当前应用的生命周期变化。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 本模块接口为系统接口。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { appManager } from '@kit.AbilityKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## 属性 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci**系统接口**:该接口为系统接口,三方应用不支持调用。 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 23e41f4b71Sopenharmony_ci| -------------------------------- | ---------------------- | ---- | ---- | ------------------ | 24e41f4b71Sopenharmony_ci| onForegroundApplicationChanged | AsyncCallback\<void> | 是 | 否 | 应用前后台状态发生变化时执行的回调函数。传入参数类型是[AppStateData](js-apis-inner-application-appStateData-sys.md)。 | 25e41f4b71Sopenharmony_ci| onAbilityStateChanged | AsyncCallback\<void> | 是 | 否 | ability状态发生变化时执行的回调函数。传入参数类型是[AbilityStateData](js-apis-inner-application-abilityStateData-sys.md)。 | 26e41f4b71Sopenharmony_ci| onProcessCreated | AsyncCallback\<void> | 是 | 否 | 进程创建时执行的回调函数。传入参数类型是[ProcessData](js-apis-inner-application-processData-sys.md)。 | 27e41f4b71Sopenharmony_ci| onProcessDied | AsyncCallback\<void> | 是 | 否 | 进程销毁时执行的回调函数。传入参数类型是[ProcessData](js-apis-inner-application-processData-sys.md)。 | 28e41f4b71Sopenharmony_ci| onProcessStateChanged<sup>9+</sup> | AsyncCallback\<void> | 是 | 否 | 进程状态更新时执行的回调函数。传入参数类型是[ProcessData](js-apis-inner-application-processData-sys.md)。 | 29e41f4b71Sopenharmony_ci| onAppStarted<sup>12+</sup> | AsyncCallback\<void> | 是 | 否 | 应用第一个进程创建时执行的回调函数。传入参数类型是[AppStateData](js-apis-inner-application-appStateData-sys.md)。 | 30e41f4b71Sopenharmony_ci| onAppStopped<sup>12+</sup> | AsyncCallback\<void> | 是 | 否 | 应用最后一个进程销毁时执行的回调函数。传入参数类型是[AppStateData](js-apis-inner-application-appStateData-sys.md)。 | 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**示例:** 33e41f4b71Sopenharmony_ci```ts 34e41f4b71Sopenharmony_ciimport { appManager } from '@kit.AbilityKit'; 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_cilet applicationStateObserver: appManager.ApplicationStateObserver = { 37e41f4b71Sopenharmony_ci onForegroundApplicationChanged(appStateData) { 38e41f4b71Sopenharmony_ci console.log(`onForegroundApplicationChanged appStateData: ${JSON.stringify(appStateData)}`); 39e41f4b71Sopenharmony_ci }, 40e41f4b71Sopenharmony_ci onAbilityStateChanged(abilityStateData) { 41e41f4b71Sopenharmony_ci console.log(`onAbilityStateChanged onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`); 42e41f4b71Sopenharmony_ci }, 43e41f4b71Sopenharmony_ci onProcessCreated(processData) { 44e41f4b71Sopenharmony_ci console.log(`onProcessCreated onProcessCreated: ${JSON.stringify(processData)}`); 45e41f4b71Sopenharmony_ci }, 46e41f4b71Sopenharmony_ci onProcessDied(processData) { 47e41f4b71Sopenharmony_ci console.log(`onProcessDied onProcessDied: ${JSON.stringify(processData)}`); 48e41f4b71Sopenharmony_ci }, 49e41f4b71Sopenharmony_ci onProcessStateChanged(processData) { 50e41f4b71Sopenharmony_ci console.log(`onProcessStateChanged onProcessStateChanged: ${JSON.stringify(processData)}`); 51e41f4b71Sopenharmony_ci }, 52e41f4b71Sopenharmony_ci onAppStarted(appStateData) { 53e41f4b71Sopenharmony_ci console.log(`onAppStarted appStateData: ${JSON.stringify(appStateData)}`); 54e41f4b71Sopenharmony_ci }, 55e41f4b71Sopenharmony_ci onAppStopped(appStateData) { 56e41f4b71Sopenharmony_ci console.log(`onAppStopped appStateData: ${JSON.stringify(appStateData)}`); 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci}; 59e41f4b71Sopenharmony_cilet observerCode = appManager.on('applicationState', applicationStateObserver); 60e41f4b71Sopenharmony_ci```