1# Context (系统接口)
2
3Context模块提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。
4
5> **说明:**
6>
7>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>  - 本模块接口仅可在Stage模型下使用。
9>  - 本模块接口为系统接口。
10
11## 导入模块
12
13```ts
14import { common } from '@kit.AbilityKit';
15```
16
17## Context.createBundleContext<sup>(deprecated)</sup>
18
19createBundleContext(bundleName: string): Context
20
21根据Bundle名称创建安装包的上下文。
22
23> **说明:**
24>
25> stage模型多module的情况下可能发生资源id冲突的情况,建议使用[application.createModuleContext](./js-apis-app-ability-application-sys.md#applicationcreatemodulecontext12)替代。
26>
27> 从 API Version 12 开始废弃,建议使用[application.createBundleContext](./js-apis-app-ability-application-sys.md#applicationcreatebundlecontext12)替代。
28
29**系统接口**:此接口为系统接口。
30
31**需要权限**:ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
32
33**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
34
35**参数:**
36
37| 参数名       | 类型                     | 必填   | 说明            |
38| -------- | ---------------------- | ---- | ------------- |
39| bundleName | string | 是    | Bundle名称。 |
40
41**返回值:**
42
43| 类型 | 说明 |
44| -------- | -------- |
45| Context | 安装包的上下文。 |
46
47**错误码**:
48
49以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
50
51| 错误码ID | 错误信息 |
52| ------- | -------------------------------- |
53| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
54
55**示例:**
56
57```ts
58import { common, UIAbility } from '@kit.AbilityKit';
59import { BusinessError } from '@kit.BasicServicesKit';
60
61export default class EntryAbility extends UIAbility {
62  onCreate() {
63    console.log('MyAbility onCreate');
64    let bundleContext: common.Context;
65    try {
66      bundleContext = this.context.createBundleContext('com.example.test');
67    } catch (error) {
68      console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
69    }
70  }
71}
72```
73
74## Context.createModuleContext<sup>(deprecated)</sup>
75
76createModuleContext(bundleName: string, moduleName: string): Context
77
78根据Bundle名称和模块名称创建上下文。
79
80> **说明:**
81>
82> 从 API Version 12 开始废弃,建议使用[application.createModuleContext](./js-apis-app-ability-application-sys.md#applicationcreatemodulecontext12)替代。
83
84**系统接口**:此接口为系统接口。
85
86**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
87
88**参数:**
89
90| 参数名       | 类型                     | 必填   | 说明            |
91| -------- | ---------------------- | ---- | ------------- |
92| bundleName | string | 是    | Bundle名称。 |
93| moduleName | string | 是    | 模块名。 |
94
95**返回值:**
96
97| 类型 | 说明 |
98| -------- | -------- |
99| Context | 模块的上下文。 |
100
101**错误码**:
102
103以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
104
105| 错误码ID | 错误信息 |
106| ------- | -------------------------------- |
107| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
108
109**示例:**
110
111```ts
112import { common, UIAbility } from '@kit.AbilityKit';
113import { BusinessError } from '@kit.BasicServicesKit';
114
115export default class EntryAbility extends UIAbility {
116  onCreate() {
117    console.log('MyAbility onCreate');
118    let moduleContext: common.Context;
119    try {
120      moduleContext = this.context.createModuleContext('com.example.test', 'entry');
121    } catch (error) {
122      console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
123    }
124  }
125}
126```
127
128## Context.createModuleResourceManager<sup>11+</sup>
129
130createModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager
131
132为指定Moudle创建资源管理对象。
133
134**系统接口**:此接口为系统接口。
135
136**需要权限**:ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
137
138**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
139
140**参数:**
141
142| 参数名       | 类型                     | 必填   | 说明            |
143| -------- | ---------------------- | ---- | ------------- |
144| bundleName | string | 是    | Bundle名称。 |
145| moduleName | string | 是    | 模块名。 |
146
147**返回值:**
148
149| 类型 | 说明 |
150| -------- | -------- |
151| resmgr.ResourceManager | 资源管理对象。 |
152
153**错误码**:
154
155以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
156
157| 错误码ID | 错误信息 |
158| ------- | -------------------------------- |
159| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
160
161**示例:**
162
163```ts
164import { UIAbility } from '@kit.AbilityKit';
165import { resourceManager } from '@kit.LocalizationKit';
166import { BusinessError } from '@kit.BasicServicesKit';
167
168export default class EntryAbility extends UIAbility {
169  onCreate() {
170    console.log('MyAbility onCreate');
171    let ModuleResourceManager: resourceManager.ResourceManager;
172    try {
173      ModuleResourceManager = this.context.createModuleResourceManager('com.example.test', 'entry');
174    } catch (error) {
175      console.error(`createModuleResourceManager failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
176    }
177  }
178}
179```
180## Context.createSystemHspModuleResourceManager<sup>12+</sup>
181
182createSystemHspModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager
183
184创建系统级HSP的某个模块的资源管理对象。
185
186**系统接口**:此接口为系统接口。
187
188**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
189
190**参数:**
191
192| 参数名       | 类型     | 必填   | 说明   |
193| -------- |--------| ---- |------|
194| bundleName | string | 是    | 包名。  |
195| moduleName | string | 是    | 模块名。 |
196
197**错误码**:
198
199以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
200
201| 错误码ID | 错误信息 |
202| ------- | -------------------------------- |
203| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
204| 16400001 | The specified ability does not exist. |
205
206**示例:**
207
208```ts
209import { UIAbility } from '@kit.AbilityKit';
210
211export default class EntryAbility extends UIAbility {
212  onCreate() {
213    console.log('MyAbility onCreate');
214    this.context.createSystemHspModuleResourceManager("com.example.myapplication", "library");
215  }
216}
217```
218