Location>code7788 >text

Getting to know the Hongmeng Context

Popularity:980 ℃/2024-11-06 02:23:00

Context is the context of the object in the application, which provides some basic information about the application, such as resourceManager (resource management), applicationInfo (current application information), dir (application file path), area (file partition), etc., as well as some basic methods of the application, such as createBundleContext (), getApplicationContext (), etc. UIAbility components and a variety of ExtensionAbility derived components have their own different Context class. There are base class Context, ApplicationContext, AbilityStageContext, UIAbilityContext, ExtensionContext, ServiceExtensionContext and other Context.

Context inheritance relationship

img1

Get UIAbilityContext

Each UIAbility contains a Context property that provides the ability to manipulate the application component, get configuration information about the application component, etc.

import { UIAbility, AbilityConstant, Want } from '@';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: ): void {
    let uiAbilityContext = ;
    //...
  }
}

Get scene-specific ExtensionContext

ServiceExtensionContext, for example, represents the context environment of the background service, inherited from ExtensionContext, and provides interface capabilities related to the background service.

import { ServiceExtensionAbility, Want } from '@';

export default class ServiceExtAbility extends ServiceExtensionAbility {
  onCreate(want: Want) {
    let serviceExtensionContext = ;
    //...
  }
}

Get AbilityStageContext

Module-level Context, compared with the base class Context, provides additional information such as HapModuleInfo, Configuration, etc.

import { AbilityStage } from '@';

export default class MyAbilityStage extends AbilityStage {
  onCreate(): void {
    let abilityStageContext = ;
    //...
  }
}

Get ApplicationContext

Application-level Context.ApplicationContext provides the ability to subscribe to changes in the lifecycle of application components within an application, subscribe to changes in system memory, and subscribe to changes in the system environment within an application on top of the base class Context, available in UIAbility, ExtensionAbility, AbilityStage are available.

import { UIAbility, AbilityConstant, Want } from '@';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: ): void {
    let applicationContext = ();
    //...
  }
}