js-apis-app-ability-applicationStateChangeCallback.md 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# @ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)

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

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

## 导入模块

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

## ApplicationStateChangeCallback.onApplicationForeground

onApplicationForeground(): void;

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

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

X
xinking129 已提交
24 25
**示例:**

26 27
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
28 29 30
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';

let applicationStateChangeCallback: ApplicationStateChangeCallback = {
31 32
    onApplicationForeground() {
        console.info('applicationStateChangeCallback onApplicationForeground');
L
liuliu 已提交
33 34 35
    },
    onApplicationBackground() {
        console.info('applicationStateChangeCallback onApplicationBackground');
36 37 38 39 40 41 42
    }
}

export default class MyAbility extends UIAbility {
    onCreate() {
        console.log('MyAbility onCreate');
        // 1.获取applicationContext
L
liuliu 已提交
43
        let applicationContext = this.context.getApplicationContext();
44
        // 2.通过applicationContext注册应用前后台状态监听
L
liuliu 已提交
45
        if (applicationContext != undefined) {
L
liuliu 已提交
46
            applicationContext.on('applicationStateChange', applicationStateChangeCallback);
L
liuliu 已提交
47
        }
48 49
    }
    onDestroy() {
L
liuliu 已提交
50
        let applicationContext = this.context.getApplicationContext();
51
        // 1.通过applicationContext解除注册应用前后台状态监听
L
liuliu 已提交
52
        if (applicationContext != undefined) {
L
liuliu 已提交
53
            applicationContext.off('applicationStateChange', applicationStateChangeCallback);
L
liuliu 已提交
54
        }
55 56 57 58
    }
}
```

59 60 61 62 63 64 65 66 67 68 69 70
## ApplicationStateChangeCallback.onApplicationBackground

onApplicationBackground(): void;

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

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

**示例:**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
71 72 73
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';

let applicationStateChangeCallback: ApplicationStateChangeCallback = {
L
liuliu 已提交
74 75 76
    onApplicationForeground() {
        console.info('applicationStateChangeCallback onApplicationForeground');
    },
77 78 79 80 81 82 83 84 85
    onApplicationBackground() {
        console.info('applicationStateChangeCallback onApplicationBackground');
    }
}

export default class MyAbility extends UIAbility {
    onCreate() {
        console.log('MyAbility onCreate');
        // 1.获取applicationContext
L
liuliu 已提交
86
        let applicationContext = this.context.getApplicationContext();
87
        // 2.通过applicationContext注册应用前后台状态监听
L
liuliu 已提交
88
        if (applicationContext != undefined) {
L
liuliu 已提交
89
            applicationContext.on('applicationStateChange', applicationStateChangeCallback);
L
liuliu 已提交
90
        }
91 92 93
        console.log('Resgiter applicationStateChangeCallback');
    }
    onDestroy() {
L
liuliu 已提交
94
        let applicationContext = this.context.getApplicationContext();
95
        // 1.通过applicationContext解除注册应用前后台状态监听
L
liuliu 已提交
96
        if (applicationContext != undefined) {
L
liuliu 已提交
97
            applicationContext.off('applicationStateChange', applicationStateChangeCallback);
L
liuliu 已提交
98
        }
99 100 101
    }
}
```