diff --git a/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md b/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md index f17f6a11c76ba5d2c733dc71d63f54953b56b9e6..e87855efabaa9f26d3f17e72789ba4b18a866841 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md +++ b/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md @@ -21,6 +21,32 @@ onApplicationForeground(): void; **系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore +```ts +import UIAbility from '@ohos.app.ability.UIAbility'; + +globalThis.applicationStateChangeCallback = { + onApplicationForeground() { + console.info('applicationStateChangeCallback onApplicationForeground'); + } +} + +export default class MyAbility extends UIAbility { + onCreate() { + console.log('MyAbility onCreate'); + globalThis.applicationContext = this.context.getApplicationContext(); + // 1.获取applicationContext + let applicationContext = globalThis.applicationContext; + // 2.通过applicationContext注册应用前后台状态监听 + applicationContext.on('applicationStateChange', globalThis.ApplicationStateChangeCallback); + } + onDestroy() { + let applicationContext = globalThis.applicationContext; + // 1.通过applicationContext解除注册应用前后台状态监听 + applicationContext.off('applicationStateChange', globalThis.ApplicationStateChangeCallback); + } +} +``` + ## ApplicationStateChangeCallback.onApplicationBackground onApplicationBackground(): void; @@ -35,9 +61,6 @@ onApplicationBackground(): void; import UIAbility from '@ohos.app.ability.UIAbility'; globalThis.applicationStateChangeCallback = { - onApplicationForeground() { - console.info('applicationStateChangeCallback onApplicationForeground'); - }, onApplicationBackground() { console.info('applicationStateChangeCallback onApplicationBackground'); } diff --git a/zh-cn/application-dev/reference/apis/js-apis-inner-application-applicationContext.md b/zh-cn/application-dev/reference/apis/js-apis-inner-application-applicationContext.md index 3cfa95ca080a7da81cbf308efe58d23428d6b82c..553ddfac5cb797bf22dc883afc16fbcdfef87ff5 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-inner-application-applicationContext.md +++ b/zh-cn/application-dev/reference/apis/js-apis-inner-application-applicationContext.md @@ -291,8 +291,8 @@ on(type: 'applicationStateChange', callback: ApplicationStateChangeCallback): ** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------------------------ | ---- | ---------------- | -| type | 'applicationStateChange' | 是 | 监听事件的类型。 | -| callback | [ApplicationStateChangeCallback](#js-apis-app-ability-applicationStateChangeCallback.md) | 是 | 回调方法。 | +| type | string | 是 | 监听事件类型,必须为'applicationStateChange'。 | +| callback | [ApplicationStateChangeCallback](#js-apis-app-ability-applicationStateChangeCallback.md) | 是 | 对于该事件监听的回调方法,可以对应用从后台切换到前台,以及前台切换到后台分别定义回调。 | **示例:** @@ -325,7 +325,7 @@ export default class MyAbility extends UIAbility { off(type: 'applicationStateChange', callback: AsyncCallback<**void**>): **void**; -取消对当前应用前后台变化的监听。删除指定callback对应的注册监听。 +取消当前应用指定的前后台变化的监听。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core @@ -333,7 +333,7 @@ off(type: 'applicationStateChange', callback: AsyncCallback<**void**>): **void** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------ | ---- | -------------------------------- | -| type | 'applicationStateChange' | 是 | 取消监听事件的类型。 | +| type | string | 是 | 取消监听事件的类型,必须为'applicationStateChange'。 | | callback | AsyncCallback\ | 否 | 删除指定callback对应的注册监听。 | **示例:** @@ -361,7 +361,7 @@ export default class EntryAbility extends UIAbility { off(type: 'applicationStateChange'): **void**; -取消对当前应用前后台变化的监听。删除全部的注册监听。 +取消当前应用注册的前后台变化的全部监听。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core @@ -369,7 +369,7 @@ off(type: 'applicationStateChange'): **void**; | 参数名 | 类型 | 必填 | 说明 | | ------ | ------------- | ---- | -------------------- | -| type | 'applicationStateChange' | 是 | 取消监听事件的类型。 | +| type | string | 是 | 取消监听事件的类型,必须为'applicationStateChange'。 | **示例:**