We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

@ohos.app.ability.EnvironmentCallback (System Environment Change Listener)

The EnvironmentCallback module provides capabilities to listen for system environment changes.

NOTE

The initial APIs of this module are supported since API version 9. 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

Collapse
Word wrap
Dark theme
Copy code
  1. import { EnvironmentCallback } from '@kit.AbilityKit';

EnvironmentCallback

onConfigurationUpdated

onConfigurationUpdated(config: Configuration): void

Called when the system configuration changes, after a listener has been registered for such events.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Expand
Name Type Mandatory Description
config Configuration Yes Configuration object after the change.

Example

See Usage of EnvironmentCallback.

onMemoryLevel

onMemoryLevel(level: AbilityConstant.MemoryLevel): void

Called when the system memory level changes, after a listener has been registered for such events.

NOTE

Releasing UI components in the onMemoryLevel callback may block the main thread tasks of the current process. Therefore, you are advised not to release UI components in this callback.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Expand
Name Type Mandatory Description
level AbilityConstant.MemoryLevel Yes Memory level, indicating the available memory of the entire device.

Example

See Usage of EnvironmentCallback.

Usage of EnvironmentCallback

Example

Collapse
Word wrap
Dark theme
Copy code
  1. import { UIAbility, EnvironmentCallback } from '@kit.AbilityKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let callbackId: number;
  4. export default class MyAbility extends UIAbility {
  5. onCreate() {
  6. console.info('MyAbility onCreate');
  7. let environmentCallback: EnvironmentCallback = {
  8. onConfigurationUpdated(config){
  9. console.info(`onConfigurationUpdated config: ${JSON.stringify(config)}`);
  10. },
  11. onMemoryLevel(level){
  12. console.info(`onMemoryLevel level: ${JSON.stringify(level)}`);
  13. }
  14. };
  15. // 1. Obtain an applicationContext object.
  16. let applicationContext = this.context.getApplicationContext();
  17. try {
  18. // 2. Register a listener for the environment changes through the applicationContext object.
  19. callbackId = applicationContext.on('environment', environmentCallback);
  20. } catch (paramError) {
  21. console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
  22. }
  23. console.info(`registerEnvironmentCallback number: ${JSON.stringify(callbackId)}`);
  24. }
  25. onDestroy() {
  26. let applicationContext = this.context.getApplicationContext();
  27. try {
  28. applicationContext.off('environment', callbackId, (error, data) => {
  29. if (error && error.code !== 0) {
  30. console.error(`unregisterEnvironmentCallback fail, error: ${JSON.stringify(error)}`);
  31. } else {
  32. console.info(`unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`);
  33. }
  34. });
  35. } catch (paramError) {
  36. console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
  37. }
  38. }
  39. }
Search in References
Enter a keyword.