js-apis-app-ability-applicationStateChangeCallback.md 5.3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';

// 构造单例对象
export class GlobalContext {
    private constructor() {}
    private static instance: GlobalContext;
    private _objects = new Map<string, Object>();

    public static getContext(): GlobalContext {
        if (!GlobalContext.instance) {
            GlobalContext.instance = new GlobalContext();
        }
        return GlobalContext.instance;
    }

    getObject(value: string): Object | undefined {
        return this._objects.get(value);
    }
46

L
liuliu 已提交
47 48 49 50 51 52
    setObject(key: string, objectClass: Object): void {
        this._objects.set(key, objectClass);
    }
}

let applicationStateChangeCallback: ApplicationStateChangeCallback = {
53 54 55 56
    onApplicationForeground() {
        console.info('applicationStateChangeCallback onApplicationForeground');
    }
}
L
liuliu 已提交
57
GlobalContext.getContext().setObject("applicationStateChangeCallback", applicationStateChangeCallback);
58 59 60 61

export default class MyAbility extends UIAbility {
    onCreate() {
        console.log('MyAbility onCreate');
L
liuliu 已提交
62
        GlobalContext.getContext().setObject("applicationContext", this.context.getApplicationContext());
63
        // 1.获取applicationContext
L
liuliu 已提交
64
        let applicationContext = GlobalContext.getContext().getObject("applicationContext");
65
        // 2.通过applicationContext注册应用前后台状态监听
L
liuliu 已提交
66 67 68 69
        if (applicationContext != undefined) {
            applicationContext.on('applicationStateChange',
                GlobalContext.getContext().getObject("applicationStateChangeCallback"));
        }
70 71
    }
    onDestroy() {
L
liuliu 已提交
72
        let applicationContext = GlobalContext.getContext().getObject("applicationContext");
73
        // 1.通过applicationContext解除注册应用前后台状态监听
L
liuliu 已提交
74 75 76 77
        if (applicationContext != undefined) {
            applicationContext.off('applicationStateChange',
                GlobalContext.getContext().getObject("applicationStateChangeCallback"));
        }
78 79 80 81
    }
}
```

82 83 84 85 86 87 88 89 90 91 92 93
## ApplicationStateChangeCallback.onApplicationBackground

onApplicationBackground(): void;

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

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

**示例:**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';

export class GlobalContext {
    private constructor() {}
    private static instance: GlobalContext;
    private _objects = new Map<string, Object>();

    public static getContext(): GlobalContext {
        if (!GlobalContext.instance) {
            GlobalContext.instance = new GlobalContext();
        }
        return GlobalContext.instance;
    }

    getObject(value: string): Object | undefined {
        return this._objects.get(value);
    }

    setObject(key: string, objectClass: Object): void {
        this._objects.set(key, objectClass);
    }
}
116

L
liuliu 已提交
117
let applicationStateChangeCallback: ApplicationStateChangeCallback = {
118 119 120 121
    onApplicationBackground() {
        console.info('applicationStateChangeCallback onApplicationBackground');
    }
}
L
liuliu 已提交
122
GlobalContext.getContext().setObject("applicationStateChangeCallback", applicationStateChangeCallback);
123 124 125 126

export default class MyAbility extends UIAbility {
    onCreate() {
        console.log('MyAbility onCreate');
L
liuliu 已提交
127
        GlobalContext.getContext().setObject("applicationContext", this.context.getApplicationContext());
128
        // 1.获取applicationContext
L
liuliu 已提交
129
        let applicationContext = GlobalContext.getContext().getObject("applicationContext");
130
        // 2.通过applicationContext注册应用前后台状态监听
L
liuliu 已提交
131 132 133 134
        if (applicationContext != undefined) {
            applicationContext.on('applicationStateChange',
                GlobalContext.getContext().getObject("applicationStateChangeCallback"));
        }
135 136 137
        console.log('Resgiter applicationStateChangeCallback');
    }
    onDestroy() {
L
liuliu 已提交
138
        let applicationContext = GlobalContext.getContext().getObject("applicationContext");
139
        // 1.通过applicationContext解除注册应用前后台状态监听
L
liuliu 已提交
140 141 142 143
        if (applicationContext != undefined) {
            applicationContext.off('applicationStateChange',
                GlobalContext.getContext().getObject("applicationStateChangeCallback"));
        }
144 145 146
    }
}
```