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