1# @ohos.app.ability.application (Application Utility Class) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @li-weifeng2--> 6<!--Designer: @li-weifeng2--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10You can use this module to manage and obtain the application [context](../../application-models/application-context-stage.md) and control the application process state. 11 12> **NOTE** 13> 14> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 15> The APIs of this module can be used only in the stage model. 16 17## Modules to Import 18 19```ts 20import { application } from '@kit.AbilityKit'; 21``` 22 23## application.createModuleContext<sup>12+</sup> 24 25createModuleContext(context: Context, moduleName: string): Promise\<Context> 26 27Creates the context for a module. The [resourceManager.Configuration](../apis-localization-kit/js-apis-resource-manager.md#configuration) in the created module context inherits from the input context, making it convenient for you to access [application resources across HAP/HSP packages](../../quick-start/resource-categories-and-access.md#cross-haphsp-resources). 28 29**Atomic service API**: This API can be used in atomic services since API version 12. 30 31**System capability**: SystemCapability.Ability.AbilityRuntime.Core 32 33**Parameters** 34 35| Name | Type | Mandatory | Description | 36| --------- | ---------------------------------------- | ---- | -------------- | 37| context | [Context](js-apis-inner-application-context.md) | Yes| Application context.| 38| moduleName | string | Yes| Module name.| 39 40**Return value** 41 42| Type | Description | 43| ------------------ | ------------------- | 44| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise used to return the context created.| 45 46**Error codes** 47 48For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 49 50| ID| Error Message | 51| -------- | --------------- | 52| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 53 54**Example** 55 56```ts 57import { AbilityConstant, UIAbility, application, common, Want } from '@kit.AbilityKit'; 58import { BusinessError } from '@kit.BasicServicesKit'; 59 60export default class EntryAbility extends UIAbility { 61 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 62 let moduleContext: common.Context; 63 try { 64 application.createModuleContext(this.context, 'entry').then((data: Context) => { 65 moduleContext = data; 66 console.info('createBundleContext success!'); 67 }).catch((error: BusinessError) => { 68 let code: number = (error as BusinessError).code; 69 let message: string = (error as BusinessError).message; 70 console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`); 71 }); 72 } catch (error) { 73 let code: number = (error as BusinessError).code; 74 let message: string = (error as BusinessError).message; 75 console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`); 76 } 77 } 78} 79``` 80 81## application.getApplicationContext<sup>14+</sup> 82 83getApplicationContext(): ApplicationContext 84 85Obtains the application context. This API provides context access independent of the base class **Context**. 86 87**Atomic service API**: This API can be used in atomic services since API version 14. 88 89**System capability**: SystemCapability.Ability.AbilityRuntime.Core 90 91**Return value** 92 93| Type | Description | 94| ------------------------------------------------------------ | ------------------- | 95| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context.| 96 97**Error codes** 98 99For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 100 101| ID| Error Message | 102| -------- | --------------- | 103| 16000050 | Internal error. | 104 105**Example** 106 107```ts 108import { AbilityConstant, UIAbility, application, Want } from '@kit.AbilityKit'; 109import { BusinessError } from '@kit.BasicServicesKit'; 110 111export default class EntryAbility extends UIAbility { 112 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 113 try { 114 let applicationContext = application.getApplicationContext(); 115 } catch (error) { 116 let code: number = (error as BusinessError).code; 117 let message: string = (error as BusinessError).message; 118 console.error(`getApplicationContext failed, error.code: ${code}, error.message: ${message}`); 119 } 120 } 121} 122``` 123 124## application.createPluginModuleContext<sup>19+</sup> 125 126createPluginModuleContext(context: Context, pluginBundleName: string, pluginModuleName: string): Promise\<Context> 127 128Creates the context of a plugin under the current application based on the context, plugin bundle name, and plugin module name, so as to obtain the basic information about the plugin. This API uses a promise to return the result. 129 130**System capability**: SystemCapability.Ability.AbilityRuntime.Core 131 132**Parameters** 133 134| Name | Type | Mandatory | Description | 135| --------- | ---------------------------------------- | ---- | -------------- | 136| context | [Context](js-apis-inner-application-context.md) | Yes| Application context.| 137| pluginBundleName | string | Yes| Bundle name of the plugin.| 138| pluginModuleName | string | Yes| Module name of the plugin.| 139 140**Return value** 141 142| Type | Description | 143| ------------------ | ------------------- | 144| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise used to return the context created.| 145 146**Example** 147 148```ts 149import { AbilityConstant, UIAbility, application, common, Want } from '@kit.AbilityKit'; 150import { BusinessError } from '@kit.BasicServicesKit'; 151 152export default class EntryAbility extends UIAbility { 153 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 154 let moduleContext: common.Context; 155 try { 156 application.createPluginModuleContext(this.context, 'com.example.pluginBundleName', 'pluginModuleName') 157 .then((data: Context) => { 158 moduleContext = data; 159 console.info('createPluginModuleContext success!'); 160 }) 161 .catch((error: BusinessError) => { 162 let code: number = (error as BusinessError).code; 163 let message: string = (error as BusinessError).message; 164 console.error(`createPluginModuleContext failed, error.code: ${code}, error.message: ${message}`); 165 }); 166 } catch (error) { 167 let code: number = (error as BusinessError).code; 168 let message: string = (error as BusinessError).message; 169 console.error(`createPluginModuleContext failed, error.code: ${code}, error.message: ${message}`); 170 } 171 } 172} 173``` 174 175## application.promoteCurrentToCandidateMasterProcess<sup>20+</sup> 176 177promoteCurrentToCandidateMasterProcess(insertToHead: boolean): Promise\<void> 178 179Adds the current process into the [candidate master process](../../application-models/ability-terminology.md#candidate-master-process) list. This API uses a promise to return the result. 180 181When the [master process](../../application-models/ability-terminology.md#master-process) is destroyed and a UIAbility or UIExtensionAbility with **isolationProcess** set to **true** is restarted, the system takes corresponding actions based on whether there is a candidate master process. 182 183- If a candidate master process exists, the system sets the process at the head of the candidate master process list as the new master process and triggers the [onNewProcessRequest](js-apis-app-ability-abilityStage.md#onnewprocessrequest11) callback. 184- If no candidate master process exists, the system performs the following operations based on the component type: 185 - For a UIAbility, the system creates an empty process as the master process. 186 - For a UIExtensionAbility, the system first tries to reuse an existing UIExtensionAbility process as the new master process. If no available process exists, it creates an empty process as the master process. 187 188> **NOTE** 189> - Currently, only 2-in-1 devices and tablets are supported. 190<!--Del--> 191> 192> - The **isolationProcess** field can be set to **true** in the [module.json5](../../quick-start/module-configuration-file.md) file, but only for the UIExtensionAbility of the sys/commonUI type. 193<!--DelEnd--> 194 195**System capability**: SystemCapability.Ability.AbilityRuntime.Core 196 197**Parameters** 198 199| Name | Type | Mandatory | Description | 200| --------- | ---------------------------------------- | ---- | -------------- | 201| insertToHead | boolean | Yes| Whether to add the current process to the head of the candidate master process list. **true** to add the current process to the head of the list, **false** to add the current process to the tail of the list.| 202 203**Return value** 204 205| Type | Description | 206| ------------------ | ------------------- | 207|Promise\<void> | Promise that returns no result.| 208 209**Error codes** 210 211For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](./errorcode-ability.md). 212 213| ID| Error Message | 214| -------- | --------------- | 215| 801 | Capability not supported.| 216| 16000115 | The current process is not running a component configured with "isolationProcess" and cannot be set as a candidate master process. | 217 218 219**Example** 220 221```ts 222import { AbilityConstant, UIAbility, application, Want } from '@kit.AbilityKit'; 223import { BusinessError } from '@kit.BasicServicesKit'; 224 225export default class EntryAbility extends UIAbility { 226 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 227 try { 228 application.promoteCurrentToCandidateMasterProcess(true) 229 .then(() => { 230 console.info('promote succeed'); 231 }) 232 .catch((err: BusinessError) => { 233 console.error(`promote failed, code is ${err.code}, message is ${err.message}`); 234 }); 235 } catch (error) { 236 let code: number = (error as BusinessError).code; 237 let message: string = (error as BusinessError).message; 238 console.error(`promoteCurrentToCandidateMasterProcess failed, error.code: ${code}, error.message: ${message}`); 239 } 240 } 241} 242``` 243 244## application.demoteCurrentFromCandidateMasterProcess<sup>20+</sup> 245 246demoteCurrentFromCandidateMasterProcess(): Promise\<void> 247 248Removes the current process from the candidate master process list. This API uses a promise to return the result. 249 250> **NOTE** 251> 252> Currently, only 2-in-1 devices and tablets are supported. 253 254**System capability**: SystemCapability.Ability.AbilityRuntime.Core 255 256**Return value** 257 258| Type | Description | 259| ------------------ | ------------------- | 260|Promise\<void> | Promise that returns no result.| 261 262**Error codes** 263 264For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 265 266| ID| Error Message | 267| -------- | --------------- | 268| 801 | Capability not supported.| 269| 16000116 | The current process is already a master process and does not support cancellation. | 270| 16000117 | The current process is not a candidate master process and does not support cancellation. | 271 272**Example** 273 274```ts 275import { AbilityConstant, UIAbility, application, Want } from '@kit.AbilityKit'; 276import { BusinessError } from '@kit.BasicServicesKit'; 277 278export default class EntryAbility extends UIAbility { 279 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 280 try { 281 application.demoteCurrentFromCandidateMasterProcess() 282 .then(() => { 283 console.info('demote succeed'); 284 }) 285 .catch((err: BusinessError) => { 286 console.error(`demote failed, code is ${err.code}, message is ${err.message}`); 287 }); 288 } catch (error) { 289 let code: number = (error as BusinessError).code; 290 let message: string = (error as BusinessError).message; 291 console.error(`demoteCurrentFromCandidateMasterProcess failed, error.code: ${code}, error.message: ${message}`); 292 } 293 } 294} 295``` 296