1# ApplicationContext 2 3The **ApplicationContext** module provides application-level context. You can use the APIs of this module to register and deregister the ability lifecycle listener in an application. 4 5> **NOTE** 6> 7> 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. 8> The APIs of this module can be used only in the stage model. 9 10## Usage 11 12Before calling any APIs in **ApplicationContext**, obtain an **ApplicationContext** instance through the **context** instance. 13 14```ts 15let applicationContext = this.context.getApplicationContext(); 16``` 17 18## ApplicationContext.on(type: 'abilityLifecycle', callback: AbilityLifecycleCallback) 19 20on(type: 'abilityLifecycle', callback: AbilityLifecycleCallback): **number**; 21 22Registers a listener to monitor the ability lifecycle of the application. 23 24**System capability**: SystemCapability.Ability.AbilityRuntime.Core 25 26**Parameters** 27 28| Name | Type | Mandatory| Description | 29| ------------------------ | -------- | ---- | ------------------------------ | 30| type | 'abilityLifecycle' | Yes | Event type.| 31| callback | [AbilityLifecycleCallback](js-apis-app-ability-abilityLifecycleCallback.md) | Yes | Callback used to return the ID of the registered listener.| 32 33**Return value** 34 35| Type | Description | 36| ------ | ------------------------------ | 37| 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.| 38 39**Example** 40 41```ts 42import UIAbility from '@ohos.app.ability.UIAbility'; 43 44let lifecycleId; 45 46export default class EntryAbility extends UIAbility { 47 onCreate() { 48 console.log('MyAbility onCreate'); 49 let AbilityLifecycleCallback = { 50 onAbilityCreate(ability) { 51 console.log('AbilityLifecycleCallback onAbilityCreate ability:' + ability); 52 }, 53 onWindowStageCreate(ability, windowStage) { 54 console.log('AbilityLifecycleCallback onWindowStageCreate ability:' + ability); 55 console.log('AbilityLifecycleCallback onWindowStageCreate windowStage:' + windowStage); 56 }, 57 onWindowStageActive(ability, windowStage) { 58 console.log('AbilityLifecycleCallback onWindowStageActive ability:' + ability); 59 console.log('AbilityLifecycleCallback onWindowStageActive windowStage:' + windowStage); 60 }, 61 onWindowStageInactive(ability, windowStage) { 62 console.log('AbilityLifecycleCallback onWindowStageInactive ability:' + ability); 63 console.log('AbilityLifecycleCallback onWindowStageInactive windowStage:' + windowStage); 64 }, 65 onWindowStageDestroy(ability, windowStage) { 66 console.log('AbilityLifecycleCallback onWindowStageDestroy ability:' + ability); 67 console.log('AbilityLifecycleCallback onWindowStageDestroy windowStage:' + windowStage); 68 }, 69 onAbilityDestroy(ability) { 70 console.log('AbilityLifecycleCallback onAbilityDestroy ability:' + ability); 71 }, 72 onAbilityForeground(ability) { 73 console.log('AbilityLifecycleCallback onAbilityForeground ability:' + ability); 74 }, 75 onAbilityBackground(ability) { 76 console.log('AbilityLifecycleCallback onAbilityBackground ability:' + ability); 77 }, 78 onAbilityContinue(ability) { 79 console.log('AbilityLifecycleCallback onAbilityContinue ability:' + ability); 80 } 81 } 82 // 1. Obtain applicationContext through the context attribute. 83 let applicationContext = this.context.getApplicationContext(); 84 // 2. Use applicationContext to register a listener for the ability lifecycle in the application. 85 lifecycleId = applicationContext.on('abilityLifecycle', AbilityLifecycleCallback); 86 console.log('registerAbilityLifecycleCallback number: ' + JSON.stringify(lifecycleId)); 87 } 88} 89``` 90 91## ApplicationContext.off(type: 'abilityLifecycle', callbackId: number, callback: AsyncCallback\<void>) 92 93off(type: 'abilityLifecycle', callbackId: **number**, callback: AsyncCallback<**void**>): **void**; 94 95Deregisters the listener that monitors the ability lifecycle of the application. 96 97**System capability**: SystemCapability.Ability.AbilityRuntime.Core 98 99**Parameters** 100 101| Name | Type | Mandatory| Description | 102| ------------- | -------- | ---- | -------------------------- | 103| type | 'abilityLifecycle' | Yes | Event type.| 104| callbackId | number | Yes | ID of the listener to deregister.| 105| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 106 107**Example** 108 109```ts 110import UIAbility from '@ohos.app.ability.UIAbility'; 111 112let lifecycleId; 113 114export default class EntryAbility extends UIAbility { 115 onDestroy() { 116 let applicationContext = this.context.getApplicationContext(); 117 console.log('stage applicationContext: ' + applicationContext); 118 applicationContext.off('abilityLifecycle', lifecycleId, (error, data) => { 119 console.log('unregisterAbilityLifecycleCallback success, err: ' + JSON.stringify(error)); 120 }); 121 } 122} 123``` 124 125## ApplicationContext.off(type: 'abilityLifecycle', callbackId: number) 126 127off(type: 'abilityLifecycle', callbackId: **number**): **void**; 128 129Deregisters the listener that monitors the ability lifecycle of the application. 130 131**System capability**: SystemCapability.Ability.AbilityRuntime.Core 132 133**Parameters** 134 135| Name | Type | Mandatory| Description | 136| ------------- | -------- | ---- | -------------------------- | 137| type | 'abilityLifecycle' | Yes | Event type.| 138| callbackId | number | Yes | ID of the listener to deregister.| 139 140**Example** 141 142```ts 143import Ability from '@ohos.app.ability.UIAbility'; 144 145let lifecycleId; 146 147export default class MyAbility extends Ability { 148 onDestroy() { 149 let applicationContext = this.context.getApplicationContext(); 150 console.log('stage applicationContext: ' + applicationContext); 151 applicationContext.off('abilityLifecycle', lifecycleId); 152 } 153} 154``` 155 156## ApplicationContext.on(type: 'environment', callback: EnvironmentCallback) 157 158on(type: 'environment', callback: EnvironmentCallback): **number**; 159 160Registers a listener for system environment changes. This API uses an asynchronous callback to return the result. 161 162**System capability**: SystemCapability.Ability.AbilityRuntime.Core 163 164**Parameters** 165 166| Name | Type | Mandatory| Description | 167| ------------------------ | -------- | ---- | ------------------------------ | 168| type | 'environment' | Yes | Event type.| 169| callback | [EnvironmentCallback](js-apis-app-ability-environmentCallback.md) | Yes | Callback used to return the ID of the registered listener.| 170 171**Return value** 172 173| Type | Description | 174| ------ | ------------------------------ | 175| 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.| 176 177**Example** 178 179```ts 180import UIAbility from '@ohos.app.ability.UIAbility'; 181 182let callbackId; 183 184export default class EntryAbility extends UIAbility { 185 onCreate() { 186 console.log('MyAbility onCreate') 187 globalThis.applicationContext = this.context.getApplicationContext(); 188 let environmentCallback = { 189 onConfigurationUpdated(config){ 190 console.log('onConfigurationUpdated config:' + JSON.stringify(config)); 191 }, 192 onMemoryLevel(level){ 193 console.log('onMemoryLevel level:' + level); 194 } 195 } 196 // 1. Obtain an applicationContext object. 197 let applicationContext = globalThis.applicationContext; 198 // 2. Use applicationContext to register a listener for the ability lifecycle in the application. 199 callbackId = applicationContext.on('environment', environmentCallback); 200 console.log('registerEnvironmentCallback callbackId: ${callbackId}'); 201 } 202} 203``` 204 205## ApplicationContext.off(type: 'environment', callbackId: number, callback: AsyncCallback\<void>) 206 207off(type: 'environment', callbackId: **number**, callback: AsyncCallback<**void**>): **void**; 208 209Deregisters the listener for system environment changes. This API uses an asynchronous callback to return the result. 210 211**System capability**: SystemCapability.Ability.AbilityRuntime.Core 212 213**Parameters** 214 215| Name | Type | Mandatory| Description | 216| ------------- | -------- | ---- | -------------------------- | 217| type | 'environment' | Yes | Event type.| 218| callbackId | number | Yes | ID of the listener to deregister. | 219| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 220 221**Example** 222 223```ts 224import UIAbility from '@ohos.app.ability.UIAbility'; 225 226let callbackId; 227 228export default class EntryAbility extends UIAbility { 229 onDestroy() { 230 let applicationContext = this.context.getApplicationContext(); 231 applicationContext.off('environment', callbackId, (error, data) => { 232 console.log('unregisterEnvironmentCallback success, err: ' + JSON.stringify(error)); 233 }); 234 } 235} 236``` 237 238## ApplicationContext.off(type: 'environment', callbackId: number) 239 240off(type: 'environment', callbackId: **number**, callback: AsyncCallback<**void**>): **void**; 241 242Deregisters the listener for system environment changes. This API uses an asynchronous callback to return the result. 243 244**System capability**: SystemCapability.Ability.AbilityRuntime.Core 245 246**Parameters** 247 248| Name | Type | Mandatory| Description | 249| ------------- | -------- | ---- | -------------------------- | 250| type | 'environment' | Yes | Event type.| 251| callbackId | number | Yes | ID of the listener to deregister. | 252 253**Example** 254 255```ts 256import Ability from '@ohos.app.ability.UIAbility'; 257 258let callbackId; 259 260export default class MyAbility extends Ability { 261 onDestroy() { 262 let applicationContext = this.context.getApplicationContext(); 263 applicationContext.off('environment', callbackId); 264 } 265} 266``` 267 268## ApplicationContext.getRunningProcessInformation<sup>9+</sup> 269 270getRunningProcessInformation(): Promise\<Array\<ProcessInformation>>; 271 272Obtains information about the running processes. This API uses a promise to return the result. 273 274**Required permissions**: ohos.permission.GET_RUNNING_INFO 275 276**System capability**: SystemCapability.Ability.AbilityRuntime.Core 277 278**System API**: This is a system API and cannot be called by third-party applications. 279 280**Return value** 281 282| Type| Description| 283| -------- | -------- | 284| 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.| 285 286**Example** 287 288```ts 289let applicationContext = this.context.getApplicationContext(); 290applicationContext.getRunningProcessInformation().then((data) => { 291 console.log('The process running information is:' + JSON.stringify(data)); 292}).catch((error) => { 293 console.log('error:' + JSON.stringify(error)); 294}); 295``` 296 297## ApplicationContext.getRunningProcessInformation<sup>9+</sup> 298 299getRunningProcessInformation(callback: AsyncCallback\<Array\<ProcessInformation>>): void; 300 301Obtains information about the running processes. This API uses an asynchronous callback to return the result. 302 303**Required permissions**: ohos.permission.GET_RUNNING_INFO 304 305**System capability**: SystemCapability.Ability.AbilityRuntime.Core 306 307**System API**: This is a system API and cannot be called by third-party applications. 308 309**Return value** 310 311| Type| Description| 312| -------- | -------- | 313|AsyncCallback\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Callback used to return the API call result and the process running information. You can perform error handling or custom processing in this callback.| 314 315**Example** 316 317```ts 318let applicationContext = this.context.getApplicationContext(); 319applicationContext.getRunningProcessInformation((err, data) => { 320 if (err.code !== 0) { 321 console.error('getRunningProcessInformation faile, err: ' + JSON.stringify(err)); 322 } else { 323 console.log('The process running information is:' + JSON.stringify(data)); 324 } 325}) 326``` 327 328## ApplicationContext.killProcessesBySelf<sup>9+</sup> 329 330killProcessesBySelf(): Promise\<void>; 331 332Kills all the processes where the application is located. This API uses a promise to return the result. 333 334**System capability**: SystemCapability.Ability.AbilityRuntime.Core 335 336**Return value** 337 338| Type| Description| 339| -------- | -------- | 340| Promise\<void>> | Promise used to return the result.| 341 342**Example** 343 344```ts 345let applicationContext = this.context.getApplicationContext(); 346applicationContext.killProcessesBySelf().then((data) => { 347 console.log('The process running information is:' + JSON.stringify(data)); 348}).catch((error) => { 349 console.error('error:' + JSON.stringify(error)); 350}); 351``` 352 353## ApplicationContext.killProcessesBySelf<sup>9+</sup> 354 355killProcessesBySelf(callback: AsyncCallback\<void>); 356 357Kills all the processes where the application is located. This API uses an asynchronous callback to return the result. 358 359**System capability**: SystemCapability.Ability.AbilityRuntime.Core 360 361**Return value** 362 363| Type| Description| 364| -------- | -------- | 365|AsyncCallback\<void\> | Callback used to return the result.| 366 367**Example** 368 369```ts 370let applicationContext = this.context.getApplicationContext(); 371applicationContext.killProcessesBySelf(err => { 372 if (err.code !== 0) { 373 console.error('killProcessesBySelf faile, err: ' + JSON.stringify(err)); 374 } 375}) 376``` 377