提交 934ad955 编写于 作者: G Gloria

Update docs against 20353

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 88fa9035
# @ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)
The **ApplicationStateChangeCallback** module provides callbacks for the application context to listen for application foreground/background state changes.
> **NOTE**
>
> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module can be used only in the stage model.
## Modules to Import
```ts
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';
```
## ApplicationStateChangeCallback.onApplicationForeground
onApplicationForeground(): void;
Called when the application is switched from the background to the foreground.
**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
**Example**
```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. Obtain an applicationContext object.
let applicationContext = globalThis.applicationContext;
// 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
applicationContext.on('applicationStateChange', globalThis.ApplicationStateChangeCallback);
}
onDestroy() {
let applicationContext = globalThis.applicationContext;
// 3. Use applicationContext.off() to unsubscribe from the 'applicationStateChange' event.
applicationContext.off('applicationStateChange', globalThis.ApplicationStateChangeCallback);
}
}
```
## ApplicationStateChangeCallback.onApplicationBackground
onApplicationBackground(): void;
Called when the application is switched from the foreground to the background.
**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
**Example**
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
globalThis.applicationStateChangeCallback = {
onApplicationBackground() {
console.info('applicationStateChangeCallback onApplicationBackground');
}
}
export default class MyAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate');
globalThis.applicationContext = this.context.getApplicationContext();
// 1. Obtain an applicationContext object.
let applicationContext = globalThis.applicationContext;
// 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
applicationContext.on('applicationStateChange', globalThis.ApplicationStateChangeCallback);
console.log('Resgiter applicationStateChangeCallback');
}
onDestroy() {
let applicationContext = globalThis.applicationContext;
// 3. Use applicationContext.off() to unsubscribe from the 'applicationStateChange' event.
applicationContext.off('applicationStateChange', globalThis.ApplicationStateChangeCallback);
}
}
```
......@@ -87,7 +87,7 @@ export default class EntryAbility extends UIAbility {
}
// 1. Obtain applicationContext through the context attribute.
let applicationContext = this.context.getApplicationContext();
// 2. Use applicationContext to register a listener for the ability lifecycle in the application.
// 2. Use applicationContext.on to subscribe to the 'abilityLifecycle' event.
lifecycleId = applicationContext.on('abilityLifecycle', AbilityLifecycleCallback);
console.log('registerAbilityLifecycleCallback lifecycleId: ${lifecycleId)}');
}
......@@ -205,7 +205,7 @@ export default class EntryAbility extends UIAbility {
};
// 1. Obtain an applicationContext object.
let applicationContext = globalThis.applicationContext;
// 2. Use applicationContext to register a listener for system environment changes.
// 2. Use applicationContext.on() to subscribe to the 'environment' event.
callbackId = applicationContext.on('environment', environmentCallback);
console.log('registerEnvironmentCallback callbackId: ${callbackId}');
}
......@@ -314,7 +314,7 @@ export default class MyAbility extends UIAbility {
globalThis.applicationContext = this.context.getApplicationContext();
// 1. Obtain an applicationContext object.
let applicationContext = globalThis.applicationContext;
// 2. Use applicationContext to subscribe to the 'applicationStateChange' event.
// 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
applicationContext.on('applicationStateChange', globalThis.ApplicationStateChangeCallback);
console.log('Resgiter applicationStateChangeCallback');
}
......@@ -334,7 +334,7 @@ Deregisters the listener for application foreground/background state changes bas
| Name | Type | Mandatory| Description |
| -------- | ------------------------ | ---- | -------------------------------- |
| type | string | Yes | Event type. The value is fixed at **'applicationStateChange'**, indicating that the application switches from the foreground to the background or vice versa. |
| callback | AsyncCallback\<void> | No | Callback that has been used for the registration|
| callback | AsyncCallback\<void> | No | Callback that has been used for the registration.|
**Example**
......@@ -359,7 +359,7 @@ export default class EntryAbility extends UIAbility {
## ApplicationContext.off(type: 'applicationStateChange')<sup>10+</sup>
off(type: 'applicationStateChange'): **void**;
off(type: 'applicationStateChange', callback?: ApplicationStateChangeCallback): **void**;
Deregisters all the listeners for application foreground/background state changes.
......@@ -370,6 +370,7 @@ Deregisters all the listeners for application foreground/background state change
| Name| Type | Mandatory| Description |
| ------ | ------------- | ---- | -------------------- |
| type | string | Yes | Event type. The value is fixed at **'applicationStateChange'**, indicating that the application switches from the foreground to the background or vice versa.|
| callback | [ApplicationStateChangeCallback](#js-apis-app-ability-applicationStateChangeCallback.md) | No | Callback used to return the result. |
**Example**
......
......@@ -38,3 +38,4 @@ appManager.getRunningProcessInformation((error, data) => {
| uid | number | Yes| No| User ID.|
| processName | string | Yes| No| Process name.|
| bundleNames | Array&lt;string&gt; | Yes| No| Names of all running bundles in the process.|
| state<sup>10+</sup> | [appManager.ProcessState](js-apis-app-ability-appManager.md#processstate)| Yes| No| Running status of the process.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册