1e41f4b71Sopenharmony_ci# ApplicationContext 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe ApplicationContext module, inherited from [Context](js-apis-inner-application-context.md), provides application-level context capabilities, including APIs for registering and deregistering the lifecycle of application components. 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 10e41f4b71Sopenharmony_ci## Modules to Import 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Usage 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciBefore calling any APIs in **ApplicationContext**, obtain an **ApplicationContext** instance through the **context** instance. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci## ApplicationContext.on('abilityLifecycle') 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_cion(type: 'abilityLifecycle', callback: AbilityLifecycleCallback): number 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciRegisters a listener to monitor the ability lifecycle of the application. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**Parameters** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 33e41f4b71Sopenharmony_ci| ------------------------ | -------- | ---- | ------------------------------ | 34e41f4b71Sopenharmony_ci| type | 'abilityLifecycle' | Yes | Event type.| 35e41f4b71Sopenharmony_ci| callback | [AbilityLifecycleCallback](js-apis-app-ability-abilityLifecycleCallback.md) | Yes | Callback used to return the ID of the registered listener.| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Return value** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci| Type | Description | 40e41f4b71Sopenharmony_ci| ------ | ------------------------------ | 41e41f4b71Sopenharmony_ci| number | ID of the registered listener. The ID is incremented by 1 each time the listener is registered. When the ID exceeds 2^63-1, **-1** is returned.| 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**Error codes** 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci| ID| Error Message| 48e41f4b71Sopenharmony_ci| ------- | -------- | 49e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**Example** 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci```ts 54e41f4b71Sopenharmony_ciimport { UIAbility, AbilityLifecycleCallback } from '@kit.AbilityKit'; 55e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_cilet lifecycleId: number; 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 60e41f4b71Sopenharmony_ci onCreate() { 61e41f4b71Sopenharmony_ci console.log('MyAbility onCreate'); 62e41f4b71Sopenharmony_ci let AbilityLifecycleCallback: AbilityLifecycleCallback = { 63e41f4b71Sopenharmony_ci onAbilityCreate(ability) { 64e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onAbilityCreate ability: ${ability}`); 65e41f4b71Sopenharmony_ci }, 66e41f4b71Sopenharmony_ci onWindowStageCreate(ability, windowStage) { 67e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageCreate ability: ${ability}`); 68e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageCreate windowStage: ${windowStage}`); 69e41f4b71Sopenharmony_ci }, 70e41f4b71Sopenharmony_ci onWindowStageActive(ability, windowStage) { 71e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageActive ability: ${ability}`); 72e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageActive windowStage: ${windowStage}`); 73e41f4b71Sopenharmony_ci }, 74e41f4b71Sopenharmony_ci onWindowStageInactive(ability, windowStage) { 75e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageInactive ability: ${ability}`); 76e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageInactive windowStage: ${windowStage}`); 77e41f4b71Sopenharmony_ci }, 78e41f4b71Sopenharmony_ci onWindowStageDestroy(ability, windowStage) { 79e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageDestroy ability: ${ability}`); 80e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onWindowStageDestroy windowStage: ${windowStage}`); 81e41f4b71Sopenharmony_ci }, 82e41f4b71Sopenharmony_ci onAbilityDestroy(ability) { 83e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onAbilityDestroy ability: ${ability}`); 84e41f4b71Sopenharmony_ci }, 85e41f4b71Sopenharmony_ci onAbilityForeground(ability) { 86e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onAbilityForeground ability: ${ability}`); 87e41f4b71Sopenharmony_ci }, 88e41f4b71Sopenharmony_ci onAbilityBackground(ability) { 89e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onAbilityBackground ability: ${ability}`); 90e41f4b71Sopenharmony_ci }, 91e41f4b71Sopenharmony_ci onAbilityContinue(ability) { 92e41f4b71Sopenharmony_ci console.log(`AbilityLifecycleCallback onAbilityContinue ability: ${ability}`); 93e41f4b71Sopenharmony_ci } 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci // 1. Obtain applicationContext through the context property. 96e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 97e41f4b71Sopenharmony_ci try { 98e41f4b71Sopenharmony_ci // 2. Use applicationContext.on() to subscribe to the 'abilityLifecycle' event. 99e41f4b71Sopenharmony_ci lifecycleId = applicationContext.on('abilityLifecycle', AbilityLifecycleCallback); 100e41f4b71Sopenharmony_ci } catch (paramError) { 101e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci console.log(`registerAbilityLifecycleCallback lifecycleId: ${lifecycleId}`); 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci} 106e41f4b71Sopenharmony_ci``` 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci## ApplicationContext.off('abilityLifecycle') 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_cioff(type: 'abilityLifecycle', callbackId: number, callback: AsyncCallback\<void>): void 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ciDeregisters the listener that monitors the ability lifecycle of the application. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Parameters** 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 121e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 122e41f4b71Sopenharmony_ci| type | 'abilityLifecycle' | Yes | Event type.| 123e41f4b71Sopenharmony_ci| callbackId | number | Yes | ID of the listener to deregister.| 124e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the deregistration is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**Error codes** 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci| ID| Error Message| 131e41f4b71Sopenharmony_ci| ------- | -------- | 132e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**Example** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci```ts 137e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 138e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_cilet lifecycleId: number; 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 143e41f4b71Sopenharmony_ci onDestroy() { 144e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 145e41f4b71Sopenharmony_ci console.log(`stage applicationContext: ${applicationContext}`); 146e41f4b71Sopenharmony_ci try { 147e41f4b71Sopenharmony_ci applicationContext.off('abilityLifecycle', lifecycleId, (error, data) => { 148e41f4b71Sopenharmony_ci if (error) { 149e41f4b71Sopenharmony_ci console.error(`unregisterAbilityLifecycleCallback fail, err: ${JSON.stringify(error)}`); 150e41f4b71Sopenharmony_ci } else { 151e41f4b71Sopenharmony_ci console.log(`unregisterAbilityLifecycleCallback success, data: ${JSON.stringify(data)}`); 152e41f4b71Sopenharmony_ci } 153e41f4b71Sopenharmony_ci }); 154e41f4b71Sopenharmony_ci } catch (paramError) { 155e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci} 159e41f4b71Sopenharmony_ci``` 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci## ApplicationContext.off('abilityLifecycle') 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_cioff(type: 'abilityLifecycle', callbackId: number): Promise\<void> 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ciDeregisters the listener that monitors the ability lifecycle of the application. This API uses a promise to return the result. It can be called only by the main thread. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**Parameters** 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 174e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 175e41f4b71Sopenharmony_ci| type | 'abilityLifecycle' | Yes | Event type.| 176e41f4b71Sopenharmony_ci| callbackId | number | Yes | ID of the listener to deregister.| 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci**Return value** 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci| Type| Description| 181e41f4b71Sopenharmony_ci| -------- | -------- | 182e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.| 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**Error codes** 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci| ID| Error Message| 189e41f4b71Sopenharmony_ci| ------- | -------- | 190e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci**Example** 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci```ts 195e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 196e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_cilet lifecycleId: number; 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 201e41f4b71Sopenharmony_ci onDestroy() { 202e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 203e41f4b71Sopenharmony_ci console.log(`stage applicationContext: ${applicationContext}`); 204e41f4b71Sopenharmony_ci try { 205e41f4b71Sopenharmony_ci applicationContext.off('abilityLifecycle', lifecycleId); 206e41f4b71Sopenharmony_ci } catch (paramError) { 207e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 208e41f4b71Sopenharmony_ci } 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci} 211e41f4b71Sopenharmony_ci``` 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci## ApplicationContext.on('environment') 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_cion(type: 'environment', callback: EnvironmentCallback): number 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ciRegisters a listener for system environment changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci**Parameters** 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 226e41f4b71Sopenharmony_ci| ------------------------ | -------- | ---- | ------------------------------ | 227e41f4b71Sopenharmony_ci| type | 'environment' | Yes | Event type.| 228e41f4b71Sopenharmony_ci| callback | [EnvironmentCallback](js-apis-app-ability-environmentCallback.md) | Yes | Callback used to return the system environment changes.| 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci**Return value** 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci| Type | Description | 233e41f4b71Sopenharmony_ci| ------ | ------------------------------ | 234e41f4b71Sopenharmony_ci| number | ID of the registered listener. The ID is incremented by 1 each time the listener is registered. When the ID exceeds 2^63-1, **-1** is returned.| 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci**Error codes** 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci| ID| Error Message| 241e41f4b71Sopenharmony_ci| ------- | -------- | 242e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**Example** 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci```ts 247e41f4b71Sopenharmony_ciimport { UIAbility, EnvironmentCallback } from '@kit.AbilityKit'; 248e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_cilet callbackId: number; 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 253e41f4b71Sopenharmony_ci onCreate() { 254e41f4b71Sopenharmony_ci console.log('MyAbility onCreate') 255e41f4b71Sopenharmony_ci let environmentCallback: EnvironmentCallback = { 256e41f4b71Sopenharmony_ci onConfigurationUpdated(config) { 257e41f4b71Sopenharmony_ci console.log(`onConfigurationUpdated config: ${JSON.stringify(config)}`); 258e41f4b71Sopenharmony_ci }, 259e41f4b71Sopenharmony_ci onMemoryLevel(level) { 260e41f4b71Sopenharmony_ci console.log(`onMemoryLevel level: ${level}`); 261e41f4b71Sopenharmony_ci } 262e41f4b71Sopenharmony_ci }; 263e41f4b71Sopenharmony_ci // 1. Obtain an applicationContext object. 264e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 265e41f4b71Sopenharmony_ci try { 266e41f4b71Sopenharmony_ci // 2. Use applicationContext.on() to subscribe to the 'environment' event. 267e41f4b71Sopenharmony_ci callbackId = applicationContext.on('environment', environmentCallback); 268e41f4b71Sopenharmony_ci } catch (paramError) { 269e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 270e41f4b71Sopenharmony_ci } 271e41f4b71Sopenharmony_ci console.log(`registerEnvironmentCallback callbackId: ${callbackId}`); 272e41f4b71Sopenharmony_ci } 273e41f4b71Sopenharmony_ci} 274e41f4b71Sopenharmony_ci``` 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci## ApplicationContext.off('environment') 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_cioff(type: 'environment', callbackId: number, callback: AsyncCallback\<void>): void 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciDeregisters the listener for system environment changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci**Parameters** 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 289e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 290e41f4b71Sopenharmony_ci| type | 'environment' | Yes | Event type.| 291e41f4b71Sopenharmony_ci| callbackId | number | Yes | ID of the listener to deregister. | 292e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the deregistration is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci**Error codes** 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci| ID| Error Message| 299e41f4b71Sopenharmony_ci| ------- | -------- | 300e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci**Example** 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci```ts 305e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 306e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_cilet callbackId: number; 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 311e41f4b71Sopenharmony_ci onDestroy() { 312e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 313e41f4b71Sopenharmony_ci try { 314e41f4b71Sopenharmony_ci applicationContext.off('environment', callbackId, (error, data) => { 315e41f4b71Sopenharmony_ci if (error) { 316e41f4b71Sopenharmony_ci console.error(`unregisterEnvironmentCallback fail, err: ${JSON.stringify(error)}`); 317e41f4b71Sopenharmony_ci } else { 318e41f4b71Sopenharmony_ci console.log(`unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`); 319e41f4b71Sopenharmony_ci } 320e41f4b71Sopenharmony_ci }); 321e41f4b71Sopenharmony_ci } catch (paramError) { 322e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 323e41f4b71Sopenharmony_ci } 324e41f4b71Sopenharmony_ci } 325e41f4b71Sopenharmony_ci} 326e41f4b71Sopenharmony_ci``` 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci## ApplicationContext.off('environment') 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_cioff(type: 'environment', callbackId: number): Promise\<void\> 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ciDeregisters the listener for system environment changes. This API uses a promise to return the result. It can be called only by the main thread. 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci**Parameters** 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 341e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 342e41f4b71Sopenharmony_ci| type | 'environment' | Yes | Event type.| 343e41f4b71Sopenharmony_ci| callbackId | number | Yes | ID of the listener to deregister. | 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci**Return value** 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci| Type| Description| 348e41f4b71Sopenharmony_ci| -------- | -------- | 349e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.| 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**Error codes** 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci| ID| Error Message| 356e41f4b71Sopenharmony_ci| ------- | -------- | 357e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci**Example** 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci```ts 362e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 363e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_cilet callbackId: number; 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 368e41f4b71Sopenharmony_ci onDestroy() { 369e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 370e41f4b71Sopenharmony_ci try { 371e41f4b71Sopenharmony_ci applicationContext.off('environment', callbackId); 372e41f4b71Sopenharmony_ci } catch (paramError) { 373e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 374e41f4b71Sopenharmony_ci } 375e41f4b71Sopenharmony_ci } 376e41f4b71Sopenharmony_ci} 377e41f4b71Sopenharmony_ci``` 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci## ApplicationContext.on('applicationStateChange')<sup>10+</sup> 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_cion(type: 'applicationStateChange', callback: ApplicationStateChangeCallback): void 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ciRegisters a listener for application foreground/background state changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci**Parameters** 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 392e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ---------------- | 393e41f4b71Sopenharmony_ci| type | 'applicationStateChange' | Yes | Event type.| 394e41f4b71Sopenharmony_ci| callback | [ApplicationStateChangeCallback](js-apis-app-ability-applicationStateChangeCallback.md) | Yes | Callback used to return the result. You can define a callback for switching from the background to the foreground and a callback for switching from the foreground to the background. | 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci**Error codes** 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci| ID| Error Message| 401e41f4b71Sopenharmony_ci| ------- | -------- | 402e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci**Example** 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci```ts 407e41f4b71Sopenharmony_ciimport { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit'; 408e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 411e41f4b71Sopenharmony_ci onCreate() { 412e41f4b71Sopenharmony_ci console.log('MyAbility onCreate'); 413e41f4b71Sopenharmony_ci let applicationStateChangeCallback: ApplicationStateChangeCallback = { 414e41f4b71Sopenharmony_ci onApplicationForeground() { 415e41f4b71Sopenharmony_ci console.info('applicationStateChangeCallback onApplicationForeground'); 416e41f4b71Sopenharmony_ci }, 417e41f4b71Sopenharmony_ci onApplicationBackground() { 418e41f4b71Sopenharmony_ci console.info('applicationStateChangeCallback onApplicationBackground'); 419e41f4b71Sopenharmony_ci } 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci // 1. Obtain an applicationContext object. 423e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 424e41f4b71Sopenharmony_ci try { 425e41f4b71Sopenharmony_ci // 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event. 426e41f4b71Sopenharmony_ci applicationContext.on('applicationStateChange', applicationStateChangeCallback); 427e41f4b71Sopenharmony_ci } catch (paramError) { 428e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 429e41f4b71Sopenharmony_ci } 430e41f4b71Sopenharmony_ci console.log('Resgiter applicationStateChangeCallback'); 431e41f4b71Sopenharmony_ci } 432e41f4b71Sopenharmony_ci} 433e41f4b71Sopenharmony_ci``` 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci## ApplicationContext.off('applicationStateChange')<sup>10+</sup> 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_cioff(type: 'applicationStateChange', callback?: ApplicationStateChangeCallback): void 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ciDeregisters all the listeners for application foreground/background state changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**Parameters** 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 448e41f4b71Sopenharmony_ci| ------ | ------------- | ---- | -------------------- | 449e41f4b71Sopenharmony_ci| type | 'applicationStateChange' | Yes | Event type.| 450e41f4b71Sopenharmony_ci| callback | [ApplicationStateChangeCallback](js-apis-app-ability-applicationStateChangeCallback.md) | No | Callback used to return the result. You can define a callback for switching from the background to the foreground and a callback for switching from the foreground to the background. | 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci**Error codes** 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci| ID| Error Message| 457e41f4b71Sopenharmony_ci| ------- | -------- | 458e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci**Example** 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ci```ts 463e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 464e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 467e41f4b71Sopenharmony_ci onDestroy() { 468e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 469e41f4b71Sopenharmony_ci try { 470e41f4b71Sopenharmony_ci applicationContext.off('applicationStateChange'); 471e41f4b71Sopenharmony_ci } catch (paramError) { 472e41f4b71Sopenharmony_ci console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 473e41f4b71Sopenharmony_ci } 474e41f4b71Sopenharmony_ci } 475e41f4b71Sopenharmony_ci} 476e41f4b71Sopenharmony_ci``` 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ci## ApplicationContext.getRunningProcessInformation 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_cigetRunningProcessInformation(): Promise\<Array\<ProcessInformation>> 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ciObtains information about the running processes. This API uses a promise to return the result. 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci**Return value** 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci| Type| Description| 491e41f4b71Sopenharmony_ci| -------- | -------- | 492e41f4b71Sopenharmony_ci| Promise\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Promise used to return the API call result and the process running information. You can perform error handling or custom processing in this callback.| 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci**Error codes** 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci| ID| Error Message| 499e41f4b71Sopenharmony_ci| ------- | -------- | 500e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 501e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 502e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci**Example** 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci```ts 507e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 508e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 511e41f4b71Sopenharmony_ci onForeground() { 512e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 513e41f4b71Sopenharmony_ci applicationContext.getRunningProcessInformation().then((data) => { 514e41f4b71Sopenharmony_ci console.log(`The process running information is: ${JSON.stringify(data)}`); 515e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 516e41f4b71Sopenharmony_ci console.error(`error: ${JSON.stringify(error)}`); 517e41f4b71Sopenharmony_ci }); 518e41f4b71Sopenharmony_ci } 519e41f4b71Sopenharmony_ci} 520e41f4b71Sopenharmony_ci``` 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci## ApplicationContext.getRunningProcessInformation 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_cigetRunningProcessInformation(callback: AsyncCallback\<Array\<ProcessInformation>>): void 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ciObtains information about the running processes. This API uses an asynchronous callback to return the result. 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ci**Parameters** 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 535e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 536e41f4b71Sopenharmony_ci| callback | AsyncCallback\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Yes | Callback used to return the information about the running processes.| 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci**Error codes** 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci| ID| Error Message| 543e41f4b71Sopenharmony_ci| ------- | -------- | 544e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 545e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 546e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci**Example** 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ci```ts 551e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 554e41f4b71Sopenharmony_ci onForeground() { 555e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 556e41f4b71Sopenharmony_ci applicationContext.getRunningProcessInformation((err, data) => { 557e41f4b71Sopenharmony_ci if (err) { 558e41f4b71Sopenharmony_ci console.error(`getRunningProcessInformation faile, err: ${JSON.stringify(err)}`); 559e41f4b71Sopenharmony_ci } else { 560e41f4b71Sopenharmony_ci console.log(`The process running information is: ${JSON.stringify(data)}`); 561e41f4b71Sopenharmony_ci } 562e41f4b71Sopenharmony_ci }) 563e41f4b71Sopenharmony_ci } 564e41f4b71Sopenharmony_ci} 565e41f4b71Sopenharmony_ci``` 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci## ApplicationContext.killAllProcesses 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_cikillAllProcesses(): Promise\<void\> 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciKills all processes of this application. The application will not go through the normal lifecycle when exiting. This API uses a promise to return the result. It can be called only by the main thread. 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci> **NOTE** 574e41f4b71Sopenharmony_ci> 575e41f4b71Sopenharmony_ci> This API is used to forcibly exit an application in abnormal scenarios. To exit an application in the normal state, call [terminateSelf()](./js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself-1). 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci**Return value** 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci| Type| Description| 584e41f4b71Sopenharmony_ci| -------- | -------- | 585e41f4b71Sopenharmony_ci| Promise\<void\> | Promise that returns no value.| 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci**Error codes** 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci| ID| Error Message| 592e41f4b71Sopenharmony_ci| ------- | -------- | 593e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 594e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci**Example** 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ci```ts 599e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 602e41f4b71Sopenharmony_ci onBackground() { 603e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 604e41f4b71Sopenharmony_ci applicationContext.killAllProcesses(); 605e41f4b71Sopenharmony_ci } 606e41f4b71Sopenharmony_ci} 607e41f4b71Sopenharmony_ci``` 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ci## ApplicationContext.killAllProcesses<sup>13+</sup> 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_cikillAllProcesses(clearPageStack: boolean): Promise\<void\> 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ciKills all processes of this application. The application will not go through the normal lifecycle when exiting. This API uses a promise to return the result. It can be called only by the main thread. 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci> **NOTE** 616e41f4b71Sopenharmony_ci> 617e41f4b71Sopenharmony_ci> This API is used to forcibly exit an application in abnormal scenarios. To exit an application in the normal state, call [terminateSelf()](./js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself-1). 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci**Parameters** 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 626e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 627e41f4b71Sopenharmony_ci| clearPageStack | boolean | Yes| Whether to clear the page stack. The value **true** means to clear the page stack, and **false** means the opposite.| 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci**Return value** 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci| Type| Description| 632e41f4b71Sopenharmony_ci| -------- | -------- | 633e41f4b71Sopenharmony_ci| Promise\<void\> | Promise that returns no value.| 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci**Error codes** 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 638e41f4b71Sopenharmony_ci 639e41f4b71Sopenharmony_ci| ID| Error Message| 640e41f4b71Sopenharmony_ci| ------- | -------- | 641e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 642e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci**Example** 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci```ts 647e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_cilet isClearPageStack = false; 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 652e41f4b71Sopenharmony_ci onBackground() { 653e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 654e41f4b71Sopenharmony_ci applicationContext.killAllProcesses(isClearPageStack); 655e41f4b71Sopenharmony_ci } 656e41f4b71Sopenharmony_ci} 657e41f4b71Sopenharmony_ci``` 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci## ApplicationContext.killAllProcesses 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_cikillAllProcesses(callback: AsyncCallback\<void\>) 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ciKills all processes of this application. The application will not go through the normal lifecycle when exiting. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci> **NOTE** 666e41f4b71Sopenharmony_ci> 667e41f4b71Sopenharmony_ci> This API is used to forcibly exit an application in abnormal scenarios. To exit an application in the normal state, call [terminateSelf()](./js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself-1). 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci**Parameters** 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 676e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 677e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If all the processes are killed, **err** is **undefined**. Otherwise, **err** is an error object.| 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci**Error codes** 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_ci| ID| Error Message| 684e41f4b71Sopenharmony_ci| ------- | -------- | 685e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 686e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci**Example** 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_ci```ts 691e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 694e41f4b71Sopenharmony_ci onBackground() { 695e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 696e41f4b71Sopenharmony_ci applicationContext.killAllProcesses(error => { 697e41f4b71Sopenharmony_ci if (error) { 698e41f4b71Sopenharmony_ci console.error(`killAllProcesses fail, error: ${JSON.stringify(error)}`); 699e41f4b71Sopenharmony_ci } 700e41f4b71Sopenharmony_ci }); 701e41f4b71Sopenharmony_ci } 702e41f4b71Sopenharmony_ci} 703e41f4b71Sopenharmony_ci``` 704e41f4b71Sopenharmony_ci## ApplicationContext.setColorMode<sup>11+</sup> 705e41f4b71Sopenharmony_ci 706e41f4b71Sopenharmony_cisetColorMode(colorMode: ConfigurationConstant.ColorMode): void 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ciSets the color mode for the application. It can be called only by the main thread. 709e41f4b71Sopenharmony_ci 710e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 711e41f4b71Sopenharmony_ci 712e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 713e41f4b71Sopenharmony_ci 714e41f4b71Sopenharmony_ci**Parameters** 715e41f4b71Sopenharmony_ci 716e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 717e41f4b71Sopenharmony_ci| ------ | ------------- | ---- | -------------------- | 718e41f4b71Sopenharmony_ci| colorMode | [ConfigurationConstant.ColorMode](js-apis-app-ability-configurationConstant.md#colormode) | Yes | Target color mode, including dark mode, light mode, and system theme mode (no setting).| 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ci**Error codes** 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci| ID| Error Message| 725e41f4b71Sopenharmony_ci| ------- | -------- | 726e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 727e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci**Example** 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ci```ts 732e41f4b71Sopenharmony_ciimport { UIAbility, ConfigurationConstant } from '@kit.AbilityKit'; 733e41f4b71Sopenharmony_ci 734e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 735e41f4b71Sopenharmony_ci onCreate() { 736e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 737e41f4b71Sopenharmony_ci applicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK); 738e41f4b71Sopenharmony_ci } 739e41f4b71Sopenharmony_ci} 740e41f4b71Sopenharmony_ci``` 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ci## ApplicationContext.setLanguage<sup>11+</sup> 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_cisetLanguage(language: string): void 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ciSets the language for the application. This API can be called only by the main thread. 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci**Parameters** 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 755e41f4b71Sopenharmony_ci| ------ | ------------- | ---- | -------------------- | 756e41f4b71Sopenharmony_ci| language | string | Yes | Target language. The list of supported languages can be obtained by calling [getSystemLanguages()](../apis-localization-kit/js-apis-i18n.md#getsystemlanguages9). | 757e41f4b71Sopenharmony_ci 758e41f4b71Sopenharmony_ci**Error codes** 759e41f4b71Sopenharmony_ci 760e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ci| ID| Error Message| 763e41f4b71Sopenharmony_ci| ------- | -------- | 764e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 765e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ci 768e41f4b71Sopenharmony_ci**Example** 769e41f4b71Sopenharmony_ci 770e41f4b71Sopenharmony_ci```ts 771e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 774e41f4b71Sopenharmony_ci onCreate() { 775e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 776e41f4b71Sopenharmony_ci applicationContext.setLanguage('zh-cn'); 777e41f4b71Sopenharmony_ci } 778e41f4b71Sopenharmony_ci} 779e41f4b71Sopenharmony_ci``` 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci## ApplicationContext.clearUpApplicationData<sup>11+</sup> 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ciclearUpApplicationData(): Promise\<void\> 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ciClears up the application data and revokes the permissions that the application has requested from users. This API uses a promise to return the result. It can be called only by the main thread. 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci> **NOTE** 788e41f4b71Sopenharmony_ci> 789e41f4b71Sopenharmony_ci> This API stops the application process. After the application process is stopped, all subsequent callbacks will not be triggered. 790e41f4b71Sopenharmony_ci 791e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 792e41f4b71Sopenharmony_ci 793e41f4b71Sopenharmony_ci**Return value** 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_ci| Type| Description| 796e41f4b71Sopenharmony_ci| -------- | -------- | 797e41f4b71Sopenharmony_ci| Promise\<void\> | Promise that returns no value.| 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_ci**Error codes** 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ciFor details about the error codes, see [Ability Error Codes](errorcode-ability.md). 802e41f4b71Sopenharmony_ci 803e41f4b71Sopenharmony_ci| ID| Error Message| 804e41f4b71Sopenharmony_ci| ------- | -------- | 805e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 806e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 807e41f4b71Sopenharmony_ci 808e41f4b71Sopenharmony_ci**Example** 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci```ts 811e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 812e41f4b71Sopenharmony_ci 813e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 814e41f4b71Sopenharmony_ci onBackground() { 815e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 816e41f4b71Sopenharmony_ci applicationContext.clearUpApplicationData(); 817e41f4b71Sopenharmony_ci } 818e41f4b71Sopenharmony_ci} 819e41f4b71Sopenharmony_ci``` 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci## ApplicationContext.clearUpApplicationData<sup>11+</sup> 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ciclearUpApplicationData(callback: AsyncCallback\<void\>): void 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ciClears up the application data and revokes the permissions that the application has requested from users. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci> **NOTE** 828e41f4b71Sopenharmony_ci> 829e41f4b71Sopenharmony_ci> This API stops the application process. After the application process is stopped, all subsequent callbacks will not be triggered. 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ci**Parameters** 834e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 835e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 836e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the application data is cleared up, **error** is **undefined**; otherwise, **error** is an error object. | 837e41f4b71Sopenharmony_ci 838e41f4b71Sopenharmony_ci**Error codes** 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 841e41f4b71Sopenharmony_ci 842e41f4b71Sopenharmony_ci| ID| Error Message| 843e41f4b71Sopenharmony_ci| ------- | -------- | 844e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 845e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 846e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci**Example** 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci```ts 851e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 852e41f4b71Sopenharmony_ci 853e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 854e41f4b71Sopenharmony_ci onBackground() { 855e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 856e41f4b71Sopenharmony_ci applicationContext.clearUpApplicationData(error => { 857e41f4b71Sopenharmony_ci if (error) { 858e41f4b71Sopenharmony_ci console.error(`clearUpApplicationData fail, error: ${JSON.stringify(error)}`); 859e41f4b71Sopenharmony_ci } 860e41f4b71Sopenharmony_ci }); 861e41f4b71Sopenharmony_ci } 862e41f4b71Sopenharmony_ci} 863e41f4b71Sopenharmony_ci``` 864e41f4b71Sopenharmony_ci 865e41f4b71Sopenharmony_ci## ApplicationContext.restartApp<sup>12+</sup> 866e41f4b71Sopenharmony_ci 867e41f4b71Sopenharmony_cirestartApp(want: Want): void 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ciRestarts the application and starts the specified UIAbility. The **onDestroy** callback is not triggered during the restart. It can be called only by the main thread, and the application to restart must be active. 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci**Parameters** 876e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 877e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 878e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the UIAbility to start. No verification is performed on the bundle name passed in.| 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ci**Error codes** 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 883e41f4b71Sopenharmony_ci 884e41f4b71Sopenharmony_ci| ID| Error Message| 885e41f4b71Sopenharmony_ci| ------- | -------- | 886e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 887e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 888e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 889e41f4b71Sopenharmony_ci| 16000063 | The target to restart does not belong to the current application or is not a UIAbility. | 890e41f4b71Sopenharmony_ci| 16000064 | Restart too frequently. Try again at least 10s later. | 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci**Example** 893e41f4b71Sopenharmony_ci 894e41f4b71Sopenharmony_ci```ts 895e41f4b71Sopenharmony_ciimport { UIAbility, Want } from '@kit.AbilityKit'; 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 898e41f4b71Sopenharmony_ci onForeground() { 899e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 900e41f4b71Sopenharmony_ci let want: Want = { 901e41f4b71Sopenharmony_ci bundleName: 'com.example.myapp', 902e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 903e41f4b71Sopenharmony_ci }; 904e41f4b71Sopenharmony_ci try { 905e41f4b71Sopenharmony_ci applicationContext.restartApp(want); 906e41f4b71Sopenharmony_ci } catch (error) { 907e41f4b71Sopenharmony_ci console.error(`restartApp fail, error: ${JSON.stringify(error)}`); 908e41f4b71Sopenharmony_ci } 909e41f4b71Sopenharmony_ci } 910e41f4b71Sopenharmony_ci} 911e41f4b71Sopenharmony_ci``` 912e41f4b71Sopenharmony_ci 913e41f4b71Sopenharmony_ci## ApplicationContext.getCurrentAppCloneIndex<sup>12+</sup> 914e41f4b71Sopenharmony_ci 915e41f4b71Sopenharmony_cigetCurrentAppCloneIndex(): number 916e41f4b71Sopenharmony_ci 917e41f4b71Sopenharmony_ciObtains the index of the current application clone. 918e41f4b71Sopenharmony_ci 919e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 922e41f4b71Sopenharmony_ci 923e41f4b71Sopenharmony_ci**Return value** 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci| Type| Description| 926e41f4b71Sopenharmony_ci| -------- | -------- | 927e41f4b71Sopenharmony_ci| number | Index of the current application clone.| 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_ci**Error codes** 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_ci| ID| Error Message| 932e41f4b71Sopenharmony_ci| ------- | -------- | 933e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 934e41f4b71Sopenharmony_ci| 16000071 | The MultiAppMode is not {@link APP_CLONE}. | 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ciFor details about the error codes, see [Ability Error Codes](errorcode-ability.md). 937e41f4b71Sopenharmony_ci 938e41f4b71Sopenharmony_ci**Example** 939e41f4b71Sopenharmony_ci 940e41f4b71Sopenharmony_ci```ts 941e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 942e41f4b71Sopenharmony_ci 943e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 944e41f4b71Sopenharmony_ci onBackground() { 945e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 946e41f4b71Sopenharmony_ci try { 947e41f4b71Sopenharmony_ci let appCloneIndex = applicationContext.getCurrentAppCloneIndex(); 948e41f4b71Sopenharmony_ci } catch (error) { 949e41f4b71Sopenharmony_ci console.error(`getCurrentAppCloneIndex fail, error: ${JSON.stringify(error)}`); 950e41f4b71Sopenharmony_ci } 951e41f4b71Sopenharmony_ci } 952e41f4b71Sopenharmony_ci} 953e41f4b71Sopenharmony_ci``` 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci## ApplicationContext.setFont<sup>12+</sup> 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_cisetFont(font: string): void 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ciSets the font for this application. This API can be called only by the main thread. 960e41f4b71Sopenharmony_ci 961e41f4b71Sopenharmony_ci> **NOTE** 962e41f4b71Sopenharmony_ci> 963e41f4b71Sopenharmony_ci> This API can be called only after a page window is created. That is, this API must be called after the lifecycle callback [onWindowStageCreate()](js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate). 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci**Parameters** 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 970e41f4b71Sopenharmony_ci| ------ | ------------- | ---- | -------------------- | 971e41f4b71Sopenharmony_ci| font | string | Yes | Font, which can be registered by calling [font.registerFont](../apis-arkui/js-apis-font.md#fontregisterfont). | 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ci**Error codes** 974e41f4b71Sopenharmony_ci 975e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 976e41f4b71Sopenharmony_ci 977e41f4b71Sopenharmony_ci| ID| Error Message| 978e41f4b71Sopenharmony_ci| ------- | -------- | 979e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 980e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 981e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 982e41f4b71Sopenharmony_ci 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci**Example** 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci```ts 987e41f4b71Sopenharmony_ciimport { font } from '@kit.ArkUI'; 988e41f4b71Sopenharmony_ci 989e41f4b71Sopenharmony_ci@Entry 990e41f4b71Sopenharmony_ci@Component 991e41f4b71Sopenharmony_cistruct Index { 992e41f4b71Sopenharmony_ci @State message: string = 'Hello World' 993e41f4b71Sopenharmony_ci 994e41f4b71Sopenharmony_ci aboutToAppear() { 995e41f4b71Sopenharmony_ci font.registerFont({ 996e41f4b71Sopenharmony_ci familyName: 'fontName', 997e41f4b71Sopenharmony_ci familySrc: $rawfile('font/medium.ttf') 998e41f4b71Sopenharmony_ci }) 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ci getContext().getApplicationContext().setFont("fontName"); 1001e41f4b71Sopenharmony_ci } 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ci build() { 1004e41f4b71Sopenharmony_ci Row() { 1005e41f4b71Sopenharmony_ci Column() { 1006e41f4b71Sopenharmony_ci Text(this.message) 1007e41f4b71Sopenharmony_ci .fontSize(50) 1008e41f4b71Sopenharmony_ci .fontWeight(50) 1009e41f4b71Sopenharmony_ci } 1010e41f4b71Sopenharmony_ci .width('100%') 1011e41f4b71Sopenharmony_ci } 1012e41f4b71Sopenharmony_ci .height('100%') 1013e41f4b71Sopenharmony_ci } 1014e41f4b71Sopenharmony_ci} 1015e41f4b71Sopenharmony_ci``` 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci## ApplicationContext.setSupportedProcessCache<sup>12+</sup> 1018e41f4b71Sopenharmony_ci 1019e41f4b71Sopenharmony_cisetSupportedProcessCache(isSupported : boolean): void 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ciSets whether the application itself supports process cache, which enables quick startup after caching. It can be called only by the main thread. 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ci> **NOTE** 1024e41f4b71Sopenharmony_ci> 1025e41f4b71Sopenharmony_ci> - This API only sets the application to be ready for quick startup after caching. It does not mean that quick startup will be triggered. Other conditions must be considered to determine whether to trigger quick startup. 1026e41f4b71Sopenharmony_ci> - The process cache support status takes effect for a single application process instance. The setting does not affect other process instances. After a process instance is destroyed, the status is not retained and can be reset. 1027e41f4b71Sopenharmony_ci> - To support process cache, you must call this API, with **true** passed in, in the **onCreate()** lifecycle of all [AbilityStages](../../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md) in the same process. 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model. 1030e41f4b71Sopenharmony_ci 1031e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1032e41f4b71Sopenharmony_ci 1033e41f4b71Sopenharmony_ci**Parameters** 1034e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1035e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | -------------------------- | 1036e41f4b71Sopenharmony_ci| isSupported | boolean | Yes| Whether process cache is supported. The value **true** means that process cache is supported, and **false** means the opposite.| 1037e41f4b71Sopenharmony_ci 1038e41f4b71Sopenharmony_ci**Error codes** 1039e41f4b71Sopenharmony_ci 1040e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1041e41f4b71Sopenharmony_ci 1042e41f4b71Sopenharmony_ci| ID| Error Message| 1043e41f4b71Sopenharmony_ci| ------- | -------- | 1044e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1045e41f4b71Sopenharmony_ci| 801 | Capability not supported.| 1046e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1047e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1048e41f4b71Sopenharmony_ci 1049e41f4b71Sopenharmony_ci**Example** 1050e41f4b71Sopenharmony_ci 1051e41f4b71Sopenharmony_ci```ts 1052e41f4b71Sopenharmony_ciimport { AbilityStage, Want } from '@kit.AbilityKit'; 1053e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1054e41f4b71Sopenharmony_ci 1055e41f4b71Sopenharmony_ciclass MyAbilityStage extends AbilityStage { 1056e41f4b71Sopenharmony_ci onCreate() { 1057e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 1058e41f4b71Sopenharmony_ci try { 1059e41f4b71Sopenharmony_ci applicationContext.setSupportedProcessCache(true); 1060e41f4b71Sopenharmony_ci } catch (error) { 1061e41f4b71Sopenharmony_ci let code = (error as BusinessError).code; 1062e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 1063e41f4b71Sopenharmony_ci console.error(`setSupportedProcessCache fail, code: ${code}, msg: ${message}`); 1064e41f4b71Sopenharmony_ci } 1065e41f4b71Sopenharmony_ci } 1066e41f4b71Sopenharmony_ci} 1067e41f4b71Sopenharmony_ci``` 1068e41f4b71Sopenharmony_ci 1069e41f4b71Sopenharmony_ci 1070e41f4b71Sopenharmony_ci## ApplicationContext.setFontSizeScale<sup>13+</sup> 1071e41f4b71Sopenharmony_ci 1072e41f4b71Sopenharmony_cisetFontSizeScale(fontSizeScale: number): void 1073e41f4b71Sopenharmony_ci 1074e41f4b71Sopenharmony_ciSets the scale ratio for the font size of this application. It can be called only by the main thread. 1075e41f4b71Sopenharmony_ci 1076e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 13. 1077e41f4b71Sopenharmony_ci 1078e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1079e41f4b71Sopenharmony_ci 1080e41f4b71Sopenharmony_ci**Parameters** 1081e41f4b71Sopenharmony_ci 1082e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1083e41f4b71Sopenharmony_ci| ------ | ------------- | ---- | -------------------- | 1084e41f4b71Sopenharmony_ci| fontSizeScale | number | Yes | Font scale ratio. The value is a non-negative number. When the application's [fontSizeScale](../../quick-start/app-configuration-file.md#configuration) is set to **followSystem** and the value set here exceeds the value of [fontSizeMaxScale](../../quick-start/app-configuration-file.md#configuration), the value of [fontSizeMaxScale](../../quick-start/app-configuration-file.md#configuration tag) takes effect.| 1085e41f4b71Sopenharmony_ci 1086e41f4b71Sopenharmony_ci**Error codes** 1087e41f4b71Sopenharmony_ci 1088e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1089e41f4b71Sopenharmony_ci 1090e41f4b71Sopenharmony_ci| ID| Error Message| 1091e41f4b71Sopenharmony_ci| ------- | -------- | 1092e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. | 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_ci**Example** 1095e41f4b71Sopenharmony_ci 1096e41f4b71Sopenharmony_ci```ts 1097e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 1098e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 1099e41f4b71Sopenharmony_ci 1100e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility { 1101e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 1102e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index', (err, data) => { 1103e41f4b71Sopenharmony_ci if (err.code) { 1104e41f4b71Sopenharmony_ci return; 1105e41f4b71Sopenharmony_ci } 1106e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 1107e41f4b71Sopenharmony_ci applicationContext.setFontSizeScale(2); 1108e41f4b71Sopenharmony_ci }); 1109e41f4b71Sopenharmony_ci } 1110e41f4b71Sopenharmony_ci} 1111e41f4b71Sopenharmony_ci``` 1112e41f4b71Sopenharmony_ci 1113e41f4b71Sopenharmony_ci 1114e41f4b71Sopenharmony_ci## ApplicationContext.getCurrentInstanceKey<sup>14+</sup> 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_cigetCurrentInstanceKey(): string 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ciObtains the unique instance ID of this application. It can be called only by the main thread. 1119e41f4b71Sopenharmony_ci 1120e41f4b71Sopenharmony_ci> **NOTE** 1121e41f4b71Sopenharmony_ci> 1122e41f4b71Sopenharmony_ci> This API is valid only for 2-in-1 devices. 1123e41f4b71Sopenharmony_ci 1124e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1125e41f4b71Sopenharmony_ci 1126e41f4b71Sopenharmony_ci**Return value** 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_ci| Type | Description | 1129e41f4b71Sopenharmony_ci| ------ | ------------------------------ | 1130e41f4b71Sopenharmony_ci| string | Unique instance ID of the application.| 1131e41f4b71Sopenharmony_ci 1132e41f4b71Sopenharmony_ci**Error codes** 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1135e41f4b71Sopenharmony_ci 1136e41f4b71Sopenharmony_ci| ID| Error Message| 1137e41f4b71Sopenharmony_ci| ------- | -------- | 1138e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1139e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 1140e41f4b71Sopenharmony_ci 1141e41f4b71Sopenharmony_ci**Example** 1142e41f4b71Sopenharmony_ci 1143e41f4b71Sopenharmony_ci```ts 1144e41f4b71Sopenharmony_ciimport { AbilityStage } from '@kit.AbilityKit'; 1145e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1146e41f4b71Sopenharmony_ci 1147e41f4b71Sopenharmony_ciclass MyAbilityStage extends AbilityStage { 1148e41f4b71Sopenharmony_ci onCreate() { 1149e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 1150e41f4b71Sopenharmony_ci let currentInstanceKey = ''; 1151e41f4b71Sopenharmony_ci try { 1152e41f4b71Sopenharmony_ci currentInstanceKey = applicationContext.getCurrentInstanceKey(); 1153e41f4b71Sopenharmony_ci } catch (error) { 1154e41f4b71Sopenharmony_ci let code = (error as BusinessError).code; 1155e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 1156e41f4b71Sopenharmony_ci console.error(`getCurrentInstanceKey fail, code: ${code}, msg: ${message}`); 1157e41f4b71Sopenharmony_ci } 1158e41f4b71Sopenharmony_ci console.log(`currentInstanceKey: ${currentInstanceKey}`); 1159e41f4b71Sopenharmony_ci } 1160e41f4b71Sopenharmony_ci} 1161e41f4b71Sopenharmony_ci``` 1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci## ApplicationContext.getAllRunningInstanceKeys<sup>14+</sup> 1164e41f4b71Sopenharmony_ci 1165e41f4b71Sopenharmony_cigetAllRunningInstanceKeys(): Promise\<Array\<string>>; 1166e41f4b71Sopenharmony_ci 1167e41f4b71Sopenharmony_ciObtains the unique instance IDs of all multi-instances of this application. This API uses a promise to return the result. It can be called only by the main thread. 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_ci> **NOTE** 1170e41f4b71Sopenharmony_ci> 1171e41f4b71Sopenharmony_ci> This API is valid only for 2-in-1 devices. 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1174e41f4b71Sopenharmony_ci 1175e41f4b71Sopenharmony_ci**Return value** 1176e41f4b71Sopenharmony_ci 1177e41f4b71Sopenharmony_ci| Type | Description | 1178e41f4b71Sopenharmony_ci| ------ | ------------------------------ | 1179e41f4b71Sopenharmony_ci| Promise\<Array\<string>> | Promise used to return the unique instance IDs of all multi-instances of the application.| 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci**Error codes** 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1184e41f4b71Sopenharmony_ci 1185e41f4b71Sopenharmony_ci| ID| Error Message| 1186e41f4b71Sopenharmony_ci| ------- | -------- | 1187e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1188e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1189e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 1190e41f4b71Sopenharmony_ci 1191e41f4b71Sopenharmony_ci**Example** 1192e41f4b71Sopenharmony_ci 1193e41f4b71Sopenharmony_ci```ts 1194e41f4b71Sopenharmony_ciimport { AbilityStage } from '@kit.AbilityKit'; 1195e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1196e41f4b71Sopenharmony_ci 1197e41f4b71Sopenharmony_ciclass MyAbilityStage extends AbilityStage { 1198e41f4b71Sopenharmony_ci onCreate() { 1199e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 1200e41f4b71Sopenharmony_ci try { 1201e41f4b71Sopenharmony_ci applicationContext.getAllRunningInstanceKeys(); 1202e41f4b71Sopenharmony_ci } catch (error) { 1203e41f4b71Sopenharmony_ci let code = (error as BusinessError).code; 1204e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 1205e41f4b71Sopenharmony_ci console.error(`getAllRunningInstanceKeys fail, code: ${code}, msg: ${message}`); 1206e41f4b71Sopenharmony_ci } 1207e41f4b71Sopenharmony_ci } 1208e41f4b71Sopenharmony_ci} 1209e41f4b71Sopenharmony_ci``` 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci <!--no_check-->