1e41f4b71Sopenharmony_ci# ExtensionContext
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciExtensionContext是Extension的上下文环境,继承自Context。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciExtensionContext模块提供访问特定Extension的资源的能力,对于拓展的Extension,可直接将ExtensionContext作为上下文环境,或者定义一个继承自ExtensionContext的类型作为上下文环境。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10e41f4b71Sopenharmony_ci>  - 本模块接口仅可在Stage模型下使用。
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## 导入模块
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci```ts
15e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
16e41f4b71Sopenharmony_ci```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## 属性
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 
25e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
26e41f4b71Sopenharmony_ci| currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | 是 | 否 | 所属Hap包的信息。<br>(详见SDK目录下的 `api\bundle\hapModuleInfo.d.ts`)  |
27e41f4b71Sopenharmony_ci| config   | [Configuration](js-apis-app-ability-configuration.md) | 是 | 否 | 所属Module的配置信息。<br>(详见SDK目录下的 `api\@ohos.app.ability.Configuration.d.ts`) |
28e41f4b71Sopenharmony_ci| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundleManager-extensionAbilityInfo.md) | 是 | 否 | 所属Extension的信息。<br>(详见SDK目录下的 `api\bundle\extensionAbilityInfo.d.ts`) |
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci## 使用场景
31e41f4b71Sopenharmony_ciExtensionContext主要用于查询所属Extension的信息、Module的配置信息以及HAP包的信息,开发者可根据自身业务需求使用对应的信息。
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**示例:**
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci```ts
36e41f4b71Sopenharmony_ci// 单例对象 GlobalContext.ts
37e41f4b71Sopenharmony_ciexport class GlobalContext {
38e41f4b71Sopenharmony_ci  private static instance: GlobalContext;
39e41f4b71Sopenharmony_ci  private _objects = new Map<string, Object>();
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci  private constructor() {
42e41f4b71Sopenharmony_ci  }
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci  public static getContext(): GlobalContext {
45e41f4b71Sopenharmony_ci    if (!GlobalContext.instance) {
46e41f4b71Sopenharmony_ci      GlobalContext.instance = new GlobalContext();
47e41f4b71Sopenharmony_ci    }
48e41f4b71Sopenharmony_ci    return GlobalContext.instance;
49e41f4b71Sopenharmony_ci  }
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci  getObject(value: string): Object | undefined {
52e41f4b71Sopenharmony_ci    return this._objects.get(value);
53e41f4b71Sopenharmony_ci  }
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci  setObject(key: string, objectClass: Object): void {
56e41f4b71Sopenharmony_ci    this._objects.set(key, objectClass);
57e41f4b71Sopenharmony_ci  }
58e41f4b71Sopenharmony_ci}
59e41f4b71Sopenharmony_ci```
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci```ts
62e41f4b71Sopenharmony_ciimport { FormExtensionAbility, formBindingData } from '@kit.FormKit';
63e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
64e41f4b71Sopenharmony_ciimport { GlobalContext } from '../GlobalContext';
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciexport default class MyFormExtensionAbility extends FormExtensionAbility {
67e41f4b71Sopenharmony_ci  onAddForm(want: Want) {
68e41f4b71Sopenharmony_ci    console.log(`FormExtensionAbility onAddForm, want: ${want.abilityName}`);
69e41f4b71Sopenharmony_ci    let dataObj1: Record<string, string> = {
70e41f4b71Sopenharmony_ci      'temperature': '11c',
71e41f4b71Sopenharmony_ci      'time': '11:00'
72e41f4b71Sopenharmony_ci    };
73e41f4b71Sopenharmony_ci    GlobalContext.getContext().setObject("ExtensionContext", this.context);
74e41f4b71Sopenharmony_ci    let obj1: formBindingData.FormBindingData = formBindingData.createFormBindingData(dataObj1);
75e41f4b71Sopenharmony_ci    return obj1;
76e41f4b71Sopenharmony_ci  }
77e41f4b71Sopenharmony_ci};
78e41f4b71Sopenharmony_ci```
79