1e41f4b71Sopenharmony_ci# Context (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Context** module provides context for abilities or applications. It allows access to application-specific resources. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> - The APIs of this module can be used only in the stage model. 9e41f4b71Sopenharmony_ci> - The APIs provided by this module are system APIs. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Modules to Import 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Context.createBundleContext 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_cicreateBundleContext(bundleName: string): Context 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciCreates the context based on the bundle name. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**NOTE** 24e41f4b71Sopenharmony_ci> 25e41f4b71Sopenharmony_ci> If there are multiple modules in the stage model, resource ID conflicts may occur. You are advised to use [Context.createModuleContext](#contextcreatemodulecontext) instead. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**System API**: This is a system API. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**Parameters** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 36e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------- | 37e41f4b71Sopenharmony_ci| bundleName | string | Yes | Bundle name. | 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**Return value** 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci| Type | Description | 42e41f4b71Sopenharmony_ci| -------- | -------- | 43e41f4b71Sopenharmony_ci| Context | Context created. | 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**Error codes** 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci| ID | Error Message | 50e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 51e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**Example** 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci```ts 56e41f4b71Sopenharmony_ciimport { common, UIAbility } from '@kit.AbilityKit'; 57e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 60e41f4b71Sopenharmony_ci onCreate() { 61e41f4b71Sopenharmony_ci console.log('MyAbility onCreate'); 62e41f4b71Sopenharmony_ci let bundleContext: common.Context; 63e41f4b71Sopenharmony_ci try { 64e41f4b71Sopenharmony_ci bundleContext = this.context.createBundleContext('com.example.test'); 65e41f4b71Sopenharmony_ci } catch (error) { 66e41f4b71Sopenharmony_ci console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci} 70e41f4b71Sopenharmony_ci``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci## Context.createModuleContext 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_cicreateModuleContext(bundleName: string, moduleName: string): Context 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciCreates the context based on the bundle name and module name. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**System API**: This is a system API. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**Parameters** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 85e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------- | 86e41f4b71Sopenharmony_ci| bundleName | string | Yes | Bundle name. | 87e41f4b71Sopenharmony_ci| moduleName | string | Yes | Module name. | 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**Return value** 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci| Type | Description | 92e41f4b71Sopenharmony_ci| -------- | -------- | 93e41f4b71Sopenharmony_ci| Context | Context created. | 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**Error codes** 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci| ID | Error Message | 100e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 101e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Example** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci```ts 106e41f4b71Sopenharmony_ciimport { common, UIAbility } from '@kit.AbilityKit'; 107e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 110e41f4b71Sopenharmony_ci onCreate() { 111e41f4b71Sopenharmony_ci console.log('MyAbility onCreate'); 112e41f4b71Sopenharmony_ci let moduleContext: common.Context; 113e41f4b71Sopenharmony_ci try { 114e41f4b71Sopenharmony_ci moduleContext = this.context.createModuleContext('com.example.test', 'entry'); 115e41f4b71Sopenharmony_ci } catch (error) { 116e41f4b71Sopenharmony_ci console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci} 120e41f4b71Sopenharmony_ci``` 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci## Context.createModuleResourceManager<sup>11+</sup> 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_cicreateModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ciCreates a resource management object for a module. 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**System API**: This is a system API. 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**Parameters** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 137e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------- | 138e41f4b71Sopenharmony_ci| bundleName | string | Yes | Bundle name. | 139e41f4b71Sopenharmony_ci| moduleName | string | Yes | Module name. | 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci**Return value** 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci| Type | Description | 144e41f4b71Sopenharmony_ci| -------- | -------- | 145e41f4b71Sopenharmony_ci| resmgr.ResourceManager | Object for resource management. | 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci**Error codes** 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci| ID | Error Message | 152e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 153e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**Example** 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci```ts 158e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 159e41f4b71Sopenharmony_ciimport { resourceManager } from '@kit.LocalizationKit'; 160e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 163e41f4b71Sopenharmony_ci onCreate() { 164e41f4b71Sopenharmony_ci console.log('MyAbility onCreate'); 165e41f4b71Sopenharmony_ci let ModuleResourceManager: resourceManager.ResourceManager; 166e41f4b71Sopenharmony_ci try { 167e41f4b71Sopenharmony_ci ModuleResourceManager = this.context.createModuleResourceManager('com.example.test', 'entry'); 168e41f4b71Sopenharmony_ci } catch (error) { 169e41f4b71Sopenharmony_ci console.error(`createModuleResourceManager failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci} 173e41f4b71Sopenharmony_ci``` 174e41f4b71Sopenharmony_ci## Context.createSystemHspModuleResourceManager<sup>12+</sup> 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_cicreateSystemHspModuleResourceManager(bundleName: string, moduleName: string): resmgr.ResourceManager 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ciCreates a resource manager object for a module of the system-level HSP. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci**System API**: This is a system API. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**Parameters** 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 187e41f4b71Sopenharmony_ci| -------- |--------| ---- |------| 188e41f4b71Sopenharmony_ci| bundleName | string | Yes | Bundle name. | 189e41f4b71Sopenharmony_ci| moduleName | string | Yes | Module name. | 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**Error codes** 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci| ID | Error Message | 196e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 197e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 198e41f4b71Sopenharmony_ci| 16400001 | The specified ability does not exist. | 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci**Example** 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci```ts 203e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 206e41f4b71Sopenharmony_ci onCreate() { 207e41f4b71Sopenharmony_ci console.log('MyAbility onCreate'); 208e41f4b71Sopenharmony_ci this.context.createSystemHspModuleResourceManager("com.example.myapplication", "library"); 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci} 211e41f4b71Sopenharmony_ci``` 212