harmony 鸿蒙@ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)
@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
import { ApplicationStateChangeCallback } from '@kit.AbilityKit';
ApplicationStateChangeCallback.onApplicationForeground
onApplicationForeground(): void
Called when the application is switched from the background to the foreground.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore
Example
For details, see onApplicationBackground.
ApplicationStateChangeCallback.onApplicationBackground
onApplicationBackground(): void
Called when the application is switched from the foreground to the background.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore
Example
import { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let applicationStateChangeCallback: ApplicationStateChangeCallback = {
onApplicationForeground() {
console.info('applicationStateChangeCallback onApplicationForeground');
},
onApplicationBackground() {
console.info('applicationStateChangeCallback onApplicationBackground');
}
};
export default class MyAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate');
// 1. Obtain an applicationContext object.
let applicationContext = this.context.getApplicationContext();
try {
// 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
if (applicationContext != undefined) {
applicationContext.on('applicationStateChange', applicationStateChangeCallback);
}
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
console.log('Register applicationStateChangeCallback');
}
onDestroy() {
let applicationContext = this.context.getApplicationContext();
try {
// 1. Use applicationContext.off() to unsubscribe from the 'applicationStateChange' event.
if (applicationContext != undefined) {
applicationContext.off('applicationStateChange', applicationStateChangeCallback);
}
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙AbilityAccessControl
harmony 鸿蒙OH_NativeBundle_ApplicationInfo
harmony 鸿蒙OH_NativeBundle_ElementName
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦