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