1# @ohos.app.ability.appManager (appManager) (System API) 2 3The **appManager** module implements application management. You can use the APIs of this module to query whether the application is undergoing a stability test, whether the application is running on a RAM constrained device, the memory size of the application, and information about the running process. 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> 9> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.app.ability.appManager (appManager)](js-apis-app-ability-appManager.md). 10 11## Modules to Import 12 13```ts 14import { appManager } from '@kit.AbilityKit'; 15``` 16 17## appManager.PreloadMode<sup>12+</sup> 18 19Enumerates the modes used for preloading an application process. 20 21**System capability**: SystemCapability.Ability.AbilityRuntime.Core 22 23**System API**: This is a system API. 24 25**Model restriction**: This API can be used only in the stage model. 26 27| Name | Value | Description | 28| ----------- | --- | --------------------------- | 29| PRESS_DOWN | 0 | The application process is preloaded when the application icon is pressed.| 30 31## appManager.isSharedBundleRunning<sup>10+</sup> 32 33isSharedBundleRunning(bundleName: string, versionCode: number): Promise\<boolean> 34 35Checks whether the shared library is in use. This API uses a promise to return the result. 36 37**Required permissions**: ohos.permission.GET_RUNNING_INFO 38 39**System capability**: SystemCapability.Ability.AbilityRuntime.Core 40 41**System API**: This is a system API. 42 43**Parameters** 44 45| Name | Type | Mandatory | Description | 46| --------- | ---------------------------------------- | ---- | -------------- | 47| bundleName | string | Yes | Bundle name of the shared library.| 48| versionCode | number | Yes | Version number of the shared library. | 49 50**Return value** 51 52| Type| Description| 53| -------- | -------- | 54| Promise\<boolean> | Promise used to return the result. The value **true** means that the shared library is in use, and **false** means the opposite.| 55 56**Error codes** 57 58For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 59 60| ID| Error Message| 61| ------- | -------- | 62| 201 | Permission denied. | 63| 202 | Not System App. Interface caller is not a system app. | 64| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 65| 16000050 | Internal error. | 66 67**Example** 68 69```ts 70import { appManager } from '@kit.AbilityKit'; 71import { BusinessError } from '@kit.BasicServicesKit'; 72 73const bundleName = "this is a bundleName"; 74const versionCode = 1; 75 76appManager.isSharedBundleRunning(bundleName, versionCode).then((data) => { 77 console.log(`The shared bundle running is: ${JSON.stringify(data)}`); 78}).catch((error: BusinessError) => { 79 console.error(`error: ${JSON.stringify(error)}`); 80}); 81``` 82 83## appManager.isSharedBundleRunning<sup>10+</sup> 84 85isSharedBundleRunning(bundleName: string, versionCode: number, callback: AsyncCallback\<boolean>): void 86 87Checks whether the shared library is in use. This API uses an asynchronous callback to return the result. 88 89**Required permissions**: ohos.permission.GET_RUNNING_INFO 90 91**System capability**: SystemCapability.Ability.AbilityRuntime.Core 92 93**System API**: This is a system API. 94 95**Parameters** 96 97| Name | Type | Mandatory | Description | 98| --------- | ---------------------------------------- | ---- | -------------- | 99| bundleName | string | Yes | Bundle name of the shared library.| 100| versionCode | number | Yes | Version number of the shared library. | 101| callback | AsyncCallback\<boolean>> | Yes | Callback used to return the result. The value **true** means that the shared library is in use, and **false** means the opposite.| 102 103**Error codes** 104 105For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 106 107| ID| Error Message| 108| ------- | -------- | 109| 201 | Permission denied. | 110| 202 | Not System App. Interface caller is not a system app. | 111| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 112| 16000050 | Internal error. | 113 114**Example** 115 116```ts 117import { appManager } from '@kit.AbilityKit'; 118 119const bundleName = "this is a bundleName"; 120const versionCode = 1; 121 122appManager.isSharedBundleRunning(bundleName, versionCode, (err, data) => { 123 if (err) { 124 console.error(`err: ${JSON.stringify(err)}`); 125 } else { 126 console.log(`The shared bundle running is: ${JSON.stringify(data)}`); 127 } 128}); 129``` 130 131## appManager.on('applicationState') 132 133on(type: 'applicationState', observer: ApplicationStateObserver): number 134 135Registers an observer to listen for the state changes of all applications. 136 137**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 138 139**System capability**: SystemCapability.Ability.AbilityRuntime.Core 140 141**System API**: This is a system API and cannot be called by third-party applications. 142 143**Parameters** 144 145| Name| Type| Mandatory| Description| 146| -------- | -------- | -------- | -------- | 147| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| 148| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver-sys.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.| 149 150**Return value** 151 152| Type| Description| 153| --- | --- | 154| number | Digital code of the observer, which will be used in **off()** to deregister the observer.| 155 156**Error codes** 157 158For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 159 160| ID| Error Message| 161| ------- | -------- | 162| 201 | Permission denied. | 163| 202 | Not System App. Interface caller is not a system app. | 164| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 165| 16000050 | Internal error. | 166 167**Example** 168 169```ts 170import { appManager } from '@kit.AbilityKit'; 171import { BusinessError } from '@kit.BasicServicesKit'; 172 173let applicationStateObserver: appManager.ApplicationStateObserver = { 174 onForegroundApplicationChanged(appStateData) { 175 console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`); 176 }, 177 onAbilityStateChanged(abilityStateData) { 178 console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`); 179 }, 180 onProcessCreated(processData) { 181 console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`); 182 }, 183 onProcessDied(processData) { 184 console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`); 185 }, 186 onProcessStateChanged(processData) { 187 console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`); 188 }, 189 onAppStarted(appStateData) { 190 console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); 191 }, 192 onAppStopped(appStateData) { 193 console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); 194 } 195}; 196 197try { 198 const observerId = appManager.on('applicationState', applicationStateObserver); 199 console.log(`[appManager] observerCode: ${observerId}`); 200} catch (paramError) { 201 let code = (paramError as BusinessError).code; 202 let message = (paramError as BusinessError).message; 203 console.error(`[appManager] error: ${code}, ${message}`); 204} 205``` 206 207## appManager.on('applicationState') 208 209on(type: 'applicationState', observer: ApplicationStateObserver, bundleNameList: Array\<string>): number 210 211Registers an observer to listen for the state changes of a specified application. 212 213**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 214 215**System capability**: SystemCapability.Ability.AbilityRuntime.Core 216 217**System API**: This is a system API and cannot be called by third-party applications. 218 219**Parameters** 220 221| Name| Type| Mandatory| Description| 222| -------- | -------- | -------- | -------- | 223| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| 224| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver-sys.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.| 225| bundleNameList | `Array<string>` | Yes| **bundleName** array of the application. A maximum of 128 bundle names can be passed.| 226 227**Return value** 228 229| Type| Description| 230| --- | --- | 231| number | Digital code of the observer, which will be used in **off()** to deregister the observer.| 232 233**Error codes** 234 235For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 236 237| ID| Error Message| 238| ------- | -------- | 239| 201 | Permission denied. | 240| 202 | Not System App. Interface caller is not a system app. | 241| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 242| 16000050 | Internal error. | 243 244**Example** 245 246```ts 247import { appManager } from '@kit.AbilityKit'; 248import { BusinessError } from '@kit.BasicServicesKit'; 249 250let applicationStateObserver: appManager.ApplicationStateObserver = { 251 onForegroundApplicationChanged(appStateData) { 252 console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`); 253 }, 254 onAbilityStateChanged(abilityStateData) { 255 console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`); 256 }, 257 onProcessCreated(processData) { 258 console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`); 259 }, 260 onProcessDied(processData) { 261 console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`); 262 }, 263 onProcessStateChanged(processData) { 264 console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`); 265 }, 266 onAppStarted(appStateData) { 267 console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); 268 }, 269 onAppStopped(appStateData) { 270 console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); 271 } 272}; 273 274let bundleNameList = ['bundleName1', 'bundleName2']; 275 276try { 277 const observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList); 278 console.log(`[appManager] observerCode: ${observerId}`); 279} catch (paramError) { 280 let code = (paramError as BusinessError).code; 281 let message = (paramError as BusinessError).message; 282 console.error(`[appManager] error: ${code}, ${message}`); 283} 284``` 285 286## appManager.on('appForegroundState')<sup>11+</sup> 287 288on(type: 'appForegroundState', observer: AppForegroundStateObserver): void 289 290Registers an observer to listen for application start or exit events. The observer can be used by a system application to observe the start or event events of all applications. 291 292**System API**: This is a system API. 293 294**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 295 296**System capability**: SystemCapability.Ability.AbilityRuntime.Core 297 298**Parameters** 299 300| Name| Type| Mandatory| Description| 301| -------- | -------- | -------- | -------- | 302| type | string | Yes| Event type. It is fixed at **'appForegroundState'**.| 303| observer | [AppForegroundStateObserver](js-apis-inner-application-appForegroundStateObserver-sys.md) | Yes| Observer used to listen for application start or exit events.| 304 305**Error codes** 306 307For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 308 309| ID| Error Message| 310| ------- | -------- | 311| 201 | Permission denied. | 312| 202 | Not System App. Interface caller is not a system app. | 313| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 314| 16000050 | Internal error. | 315 316**Example** 317 318```ts 319import { appManager } from '@kit.AbilityKit'; 320import { BusinessError } from '@kit.BasicServicesKit'; 321 322let observer: appManager.AppForegroundStateObserver = { 323 onAppStateChanged(appStateData) { 324 console.log(`[appManager] onAppStateChanged: ${JSON.stringify(appStateData)}`); 325 }, 326}; 327 328try { 329 appManager.on('appForegroundState', observer); 330} catch (paramError) { 331 let code = (paramError as BusinessError).code; 332 let message = (paramError as BusinessError).message; 333 console.error(`[appManager] error: ${code}, ${message}`); 334} 335``` 336 337## appManager.on('abilityFirstFrameState')<sup>12+</sup> 338 339on(type: 'abilityFirstFrameState', observer: AbilityFirstFrameStateObserver, bundleName?: string): void 340 341Registers an observer to listen for the complete of the first frame rendering of a given ability. 342 343**System API**: This is a system API. 344 345**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 346 347**System capability**: SystemCapability.Ability.AbilityRuntime.Core 348 349**Parameters** 350 351| Name | Type | Mandatory| Description | 352| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 353| type | string | Yes | Event type. It is fixed at **'abilityFirstFrameState'**. | 354| observer | [AbilityFirstFrameStateObserver](js-apis-inner-application-abilityFirstFrameStateObserver-sys.md) | Yes | Observer used to listen for the complete of the first frame rendering of the ability. | 355| bundleName | string | No | Bundle name of the ability to be listened for. If this parameter is left blank, the event is listened for all applications.| 356 357**Error codes** 358 359For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 360 361| ID| Error Message| 362| ------- | -------- | 363| 201 | Permission denied. | 364| 202 | Not System App. Interface caller is not a system app. | 365| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 366| 16000050 | Internal error. | 367 368**Example** 369 370```ts 371import { appManager } from '@kit.AbilityKit'; 372import { BusinessError } from '@kit.BasicServicesKit'; 373 374let abilityFirstFrameStateObserverForAll: appManager.AbilityFirstFrameStateObserver = { 375 onAbilityFirstFrameDrawn(abilityStateData: appManager.AbilityFirstFrameStateData) { 376 console.log("abilityFirstFrame: ", JSON.stringify(abilityStateData)); 377 } 378}; 379 380try { 381 appManager.on('abilityFirstFrameState', abilityFirstFrameStateObserverForAll); 382} catch (e) { 383 let code = (e as BusinessError).code; 384 let message = (e as BusinessError).message; 385 console.error(`[appManager] error: ${code}, ${message}`); 386} 387``` 388 389## appManager.off('applicationState') 390 391off(type: 'applicationState', observerId: number, callback: AsyncCallback\<void>): void 392 393Deregisters the application state observer. This API uses an asynchronous callback to return the result. 394 395**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 396 397**System capability**: SystemCapability.Ability.AbilityRuntime.Core 398 399**System API**: This is a system API and cannot be called by third-party applications. 400 401**Parameters** 402 403| Name| Type| Mandatory| Description| 404| -------- | -------- | -------- | -------- | 405| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| 406| observerId | number | Yes| Digital code of the observer.| 407| callback | AsyncCallback\<void> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.| 408 409**Error codes** 410 411For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 412 413| ID| Error Message| 414| ------- | -------- | 415| 201 | Permission denied. | 416| 202 | Not System App. Interface caller is not a system app. | 417| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 418| 16000050 | Internal error. | 419 420**Example** 421 422```ts 423import { appManager } from '@kit.AbilityKit'; 424import { BusinessError } from '@kit.BasicServicesKit'; 425 426let observerId = 0; 427 428let applicationStateObserver: appManager.ApplicationStateObserver = { 429 onForegroundApplicationChanged(appStateData) { 430 console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`); 431 }, 432 onAbilityStateChanged(abilityStateData) { 433 console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`); 434 }, 435 onProcessCreated(processData) { 436 console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`); 437 }, 438 onProcessDied(processData) { 439 console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`); 440 }, 441 onProcessStateChanged(processData) { 442 console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`); 443 }, 444 onAppStarted(appStateData) { 445 console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); 446 }, 447 onAppStopped(appStateData) { 448 console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); 449 } 450}; 451let bundleNameList = ['bundleName1', 'bundleName2']; 452 453try { 454 observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList); 455 console.log(`[appManager] observerCode: ${observerId}`); 456} catch (paramError) { 457 let code = (paramError as BusinessError).code; 458 let message = (paramError as BusinessError).message; 459 console.error(`[appManager] error: ${code}, ${message} `); 460} 461 462// 2. Deregister the application state observer. 463function unregisterApplicationStateObserverCallback(err: BusinessError) { 464 if (err) { 465 console.error(`unregisterApplicationStateObserverCallback fail, err: ${JSON.stringify(err)}`); 466 } else { 467 console.log('unregisterApplicationStateObserverCallback success.'); 468 } 469} 470 471try { 472 appManager.off('applicationState', observerId, unregisterApplicationStateObserverCallback); 473} catch (paramError) { 474 let code = (paramError as BusinessError).code; 475 let message = (paramError as BusinessError).message; 476 console.error(`[appManager] error: ${code}, ${message}`); 477} 478``` 479 480## appManager.off('applicationState') 481 482off(type: 'applicationState', observerId: number): Promise\<void> 483 484Deregisters the application state observer. This API uses an asynchronous callback to return the result. 485 486**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 487 488**System capability**: SystemCapability.Ability.AbilityRuntime.Core 489 490**System API**: This is a system API and cannot be called by third-party applications. 491 492**Parameters** 493 494| Name| Type| Mandatory| Description| 495| -------- | -------- | -------- | -------- | 496| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| 497| observerId | number | Yes| Digital code of the observer.| 498 499**Return value** 500 501| Type| Description| 502| -------- | -------- | 503| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.| 504 505**Error codes** 506 507For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 508 509| ID| Error Message| 510| ------- | -------- | 511| 201 | Permission denied. | 512| 202 | Not System App. Interface caller is not a system app. | 513| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 514| 16000050 | Internal error. | 515 516**Example** 517 518```ts 519import { appManager } from '@kit.AbilityKit'; 520import { BusinessError } from '@kit.BasicServicesKit'; 521 522let observerId = 0; 523 524// 1. Register an application state observer. 525let applicationStateObserver: appManager.ApplicationStateObserver = { 526 onForegroundApplicationChanged(appStateData) { 527 console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`); 528 }, 529 onAbilityStateChanged(abilityStateData) { 530 console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`); 531 }, 532 onProcessCreated(processData) { 533 console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`); 534 }, 535 onProcessDied(processData) { 536 console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`); 537 }, 538 onProcessStateChanged(processData) { 539 console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`); 540 }, 541 onAppStarted(appStateData) { 542 console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); 543 }, 544 onAppStopped(appStateData) { 545 console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); 546 } 547}; 548let bundleNameList = ['bundleName1', 'bundleName2']; 549 550try { 551 observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList); 552} catch (paramError) { 553 let code = (paramError as BusinessError).code; 554 let message = (paramError as BusinessError).message; 555 console.error(`[appManager] error: ${code}, ${message}`); 556} 557 558// 2. Deregister the application state observer. 559try { 560 appManager.off('applicationState', observerId).then((data) => { 561 console.log(`unregisterApplicationStateObserver success, data: ${JSON.stringify(data)}`); 562 }).catch((err: BusinessError) => { 563 console.error(`unregisterApplicationStateObserver fail, err: ${JSON.stringify(err)}`); 564 }); 565} catch (paramError) { 566 let code = (paramError as BusinessError).code; 567 let message = (paramError as BusinessError).message; 568 console.error(`[appManager] error: ${code}, ${message}`); 569} 570``` 571 572## appManager.off('appForegroundState')<sup>11+</sup> 573 574off(type: 'appForegroundState', observer?: AppForegroundStateObserver): void 575 576Deregisters the observer used to listen for application start or exit events. 577 578**System API**: This is a system API. 579 580**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 581 582**System capability**: SystemCapability.Ability.AbilityRuntime.Core 583 584**Parameters** 585 586| Name| Type| Mandatory| Description| 587| -------- | -------- | -------- | -------- | 588| type | string | Yes| Event type. It is fixed at **'appForegroundState'**.| 589| observer | [AppForegroundStateObserver](js-apis-inner-application-appForegroundStateObserver-sys.md) | No| Observer used to listen for application start or exit events.| 590 591**Error codes** 592 593For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 594 595| ID| Error Message| 596| ------- | -------- | 597| 201 | Permission denied. | 598| 202 | Not System App. Interface caller is not a system app. | 599| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 600| 16000050 | Internal error. | 601 602**Example** 603 604```ts 605import { appManager } from '@kit.AbilityKit'; 606import { BusinessError } from '@kit.BasicServicesKit'; 607 608let observer_: appManager.AppForegroundStateObserver | undefined; 609// 1. Register an observer to listen for application start or exit events. 610let observer: appManager.AppForegroundStateObserver = { 611 onAppStateChanged(appStateData: appManager.AppStateData) { 612 console.log(`[appManager] onAppStateChanged: ${JSON.stringify(appStateData)}`); 613 }, 614}; 615 616try { 617 appManager.on('appForegroundState', observer); 618 // Save the observer object. 619 observer_ = observer; 620} catch (paramError) { 621 let code = (paramError as BusinessError).code; 622 let message = (paramError as BusinessError).message; 623 console.error(`[appManager] error: ${code}, ${message}`); 624} 625 626// 2. Deregister the observer. 627try { 628 appManager.off('appForegroundState', observer_); 629} catch (paramError) { 630 let code = (paramError as BusinessError).code; 631 let message = (paramError as BusinessError).message; 632 console.error(`[appManager] error: ${code}, ${message}`); 633} 634``` 635 636## appManager.off('abilityFirstFrameState')<sup>12+</sup> 637 638off(type: 'abilityFirstFrameState', observer?: AbilityFirstFrameStateObserver): void 639 640Deregisters the observer used to listen for the complete of the first frame rendering of a given ability. 641 642**System API**: This is a system API. 643 644**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 645 646**System capability**: SystemCapability.Ability.AbilityRuntime.Core 647 648**Parameters** 649 650| Name | Type | Mandatory| Description | 651| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 652| type | string | Yes | Event type. It is fixed at **'abilityFirstFrameState'**. | 653| observer | [AbilityFirstFrameStateObserver](js-apis-inner-application-abilityFirstFrameStateObserver-sys.md) | No | Callback used for deregistration. If this parameter is left blank, all subscriptions to the specified event are canceled.| 654 655**Error codes** 656 657For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 658 659| ID| Error Message| 660| ------- | -------- | 661| 201 | Permission denied. | 662| 202 | Not System App. Interface caller is not a system app. | 663| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 664| 16000050 | Internal error. | 665 666**Example** 667 668```ts 669import { appManager } from '@kit.AbilityKit'; 670import { BusinessError } from '@kit.BasicServicesKit'; 671 672let abilityFirstFrameStateObserverForAll: appManager.AbilityFirstFrameStateObserver = { 673 onAbilityFirstFrameDrawn(abilityStateData: appManager.AbilityFirstFrameStateData) { 674 console.log("abilityFirstFrame: ", JSON.stringify(abilityStateData)); 675 } 676}; 677 678try { 679 appManager.on('abilityFirstFrameState', abilityFirstFrameStateObserverForAll); 680} catch (e) { 681 let code = (e as BusinessError).code; 682 let message = (e as BusinessError).message; 683 console.error(`[appManager] error: ${code}, ${message}`); 684} 685 686try { 687 appManager.off('abilityFirstFrameState', abilityFirstFrameStateObserverForAll); 688} catch (e) { 689 let code = (e as BusinessError).code; 690 let message = (e as BusinessError).message; 691 console.error(`[appManager] error: ${code}, ${message}`); 692} 693``` 694 695## appManager.getForegroundApplications 696 697getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void 698 699Obtains applications that are running in the foreground. This API uses an asynchronous callback to return the result. The application information is defined by [AppStateData](js-apis-inner-application-appStateData-sys.md). 700 701**Required permissions**: ohos.permission.GET_RUNNING_INFO 702 703**System capability**: SystemCapability.Ability.AbilityRuntime.Core 704 705**System API**: This is a system API and cannot be called by third-party applications. 706 707**Parameters** 708 709| Name| Type| Mandatory| Description| 710| -------- | -------- | -------- | -------- | 711| callback | AsyncCallback\<Array\<[AppStateData](js-apis-inner-application-appStateData-sys.md)>> | Yes| Callback used to return the API call result and an array holding the application state data. You can perform error handling or custom processing in this callback.| 712 713**Error codes** 714 715For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 716 717| ID| Error Message| 718| ------- | -------- | 719| 201 | Permission denied. | 720| 202 | Not System App. Interface caller is not a system app. | 721| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 722| 16000050 | Internal error. | 723 724**Example** 725 726```ts 727import { appManager } from '@kit.AbilityKit'; 728import { BusinessError } from '@kit.BasicServicesKit'; 729 730function getForegroundApplicationsCallback(err: BusinessError, data: Array<appManager.AppStateData>) { 731 if (err) { 732 console.error(`getForegroundApplicationsCallback fail, err: ${JSON.stringify(err)}`); 733 } else { 734 console.log(`getForegroundApplicationsCallback success, data: ${JSON.stringify(data)}`); 735 } 736} 737 738try { 739 appManager.getForegroundApplications(getForegroundApplicationsCallback); 740} catch (paramError) { 741 let code = (paramError as BusinessError).code; 742 let message = (paramError as BusinessError).message; 743 console.error(`[appManager] error: ${code}, ${message}`); 744} 745``` 746 747## appManager.getForegroundApplications 748 749getForegroundApplications(): Promise\<Array\<AppStateData>> 750 751Obtains applications that are running in the foreground. This API uses a promise to return the result. The application information is defined by [AppStateData](js-apis-inner-application-appStateData-sys.md). 752 753**Required permissions**: ohos.permission.GET_RUNNING_INFO 754 755**System capability**: SystemCapability.Ability.AbilityRuntime.Core 756 757**System API**: This is a system API and cannot be called by third-party applications. 758 759**Return value** 760 761| Type| Description| 762| -------- | -------- | 763| Promise\<Array\<[AppStateData](js-apis-inner-application-appStateData-sys.md)>> | Promise used to return an array holding the application state data.| 764 765**Error codes** 766 767For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 768 769| ID| Error Message| 770| ------- | -------- | 771| 201 | Permission denied. | 772| 202 | Not System App. Interface caller is not a system app. | 773| 16000050 | Internal error. | 774 775**Example** 776 777```ts 778import { appManager } from '@kit.AbilityKit'; 779import { BusinessError } from '@kit.BasicServicesKit'; 780 781appManager.getForegroundApplications().then((data) => { 782 console.log(`getForegroundApplications success, data: ${JSON.stringify(data)}`); 783}).catch((err: BusinessError) => { 784 console.error(`getForegroundApplications fail, err: ${JSON.stringify(err)}`); 785}); 786``` 787 788## appManager.killProcessWithAccount 789 790killProcessWithAccount(bundleName: string, accountId: number): Promise\<void\> 791 792Kills a process by bundle name and account ID. This API uses a promise to return the result. 793 794> **NOTE** 795> 796> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 797 798**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS or ohos.permission.CLEAN_BACKGROUND_PROCESSES 799 800**System capability**: SystemCapability.Ability.AbilityRuntime.Core 801 802**System API**: This is a system API and cannot be called by third-party applications. 803 804**Parameters** 805 806| Name| Type| Mandatory| Description| 807| -------- | -------- | -------- | -------- | 808| bundleName | string | Yes| Bundle name.| 809| accountId | number | Yes| ID of a system account. For details, see [getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9).| 810 811**Error codes** 812 813For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 814 815| ID| Error Message| 816| ------- | -------- | 817| 201 | Permission denied. | 818| 202 | Not System App. Interface caller is not a system app. | 819| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 820| 16000050 | Internal error. | 821 822**Example** 823 824```ts 825import { appManager } from '@kit.AbilityKit'; 826import { BusinessError } from '@kit.BasicServicesKit'; 827 828let bundleName = 'bundleName'; 829let accountId = 0; 830 831try { 832 appManager.killProcessWithAccount(bundleName, accountId).then(() => { 833 console.log('killProcessWithAccount success'); 834 }).catch((err: BusinessError) => { 835 console.error(`killProcessWithAccount fail, err: ${JSON.stringify(err)}`); 836 }); 837} catch (paramError) { 838 let code = (paramError as BusinessError).code; 839 let message = (paramError as BusinessError).message; 840 console.error(`[appManager] error: ${code}, ${message}`); 841} 842``` 843 844 845## appManager.killProcessWithAccount 846 847killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCallback\<void\>): void 848 849Kills a process by bundle name and account ID. This API uses an asynchronous callback to return the result. 850 851> **NOTE** 852> 853> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 854 855**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS or ohos.permission.CLEAN_BACKGROUND_PROCESSES 856 857**System capability**: SystemCapability.Ability.AbilityRuntime.Core 858 859**System API**: This is a system API and cannot be called by third-party applications. 860 861**Parameters** 862 863 | Name| Type| Mandatory| Description| 864 | -------- | -------- | -------- | -------- | 865 | bundleName | string | Yes| Bundle name.| 866 | accountId | number | Yes| ID of a system account. For details, see [getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9).| 867 | callback | AsyncCallback\<void\> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.| 868 869**Error codes** 870 871For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 872 873| ID| Error Message| 874| ------- | -------- | 875| 201 | Permission denied. | 876| 202 | Not System App. Interface caller is not a system app. | 877| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 878| 16000050 | Internal error. | 879 880**Example** 881 882```ts 883import { appManager } from '@kit.AbilityKit'; 884import { BusinessError } from '@kit.BasicServicesKit'; 885 886let bundleName = 'bundleName'; 887let accountId = 0; 888 889function killProcessWithAccountCallback(err: BusinessError) { 890 if (err) { 891 console.error(`killProcessWithAccountCallback fail, err: ${JSON.stringify(err)}`); 892 } else { 893 console.log('killProcessWithAccountCallback success.'); 894 } 895} 896 897appManager.killProcessWithAccount(bundleName, accountId, killProcessWithAccountCallback); 898``` 899 900## appManager.killProcessesByBundleName 901 902killProcessesByBundleName(bundleName: string, callback: AsyncCallback\<void>) 903 904Kills a process by bundle name. This API uses an asynchronous callback to return the result. 905 906**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES 907 908**System capability**: SystemCapability.Ability.AbilityRuntime.Core 909 910**System API**: This is a system API and cannot be called by third-party applications. 911 912**Parameters** 913 914| Name| Type| Mandatory| Description| 915| -------- | -------- | -------- | -------- | 916| bundleName | string | Yes| Bundle name.| 917| callback | AsyncCallback\<void> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.| 918 919**Error codes** 920 921For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 922 923| ID| Error Message| 924| ------- | -------- | 925| 201 | Permission denied. | 926| 202 | Not System App. Interface caller is not a system app. | 927| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 928| 16000050 | Internal error. | 929 930**Example** 931 932```ts 933import { appManager } from '@kit.AbilityKit'; 934import { BusinessError } from '@kit.BasicServicesKit'; 935 936let bundleName = 'bundleName'; 937 938function killProcessesByBundleNameCallback(err: BusinessError) { 939 if (err) { 940 console.error(`killProcessesByBundleNameCallback fail, err: ${JSON.stringify(err)}`); 941 } else { 942 console.log('killProcessesByBundleNameCallback success.'); 943 } 944} 945 946try { 947 appManager.killProcessesByBundleName(bundleName, killProcessesByBundleNameCallback); 948} catch (paramError) { 949 let code = (paramError as BusinessError).code; 950 let message = (paramError as BusinessError).message; 951 console.error(`[appManager] error: ${code}, ${message}`); 952} 953``` 954 955## appManager.killProcessesByBundleName 956 957killProcessesByBundleName(bundleName: string): Promise\<void> 958 959Kills a process by bundle name. This API uses a promise to return the result. 960 961**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES 962 963**System capability**: SystemCapability.Ability.AbilityRuntime.Core 964 965**System API**: This is a system API and cannot be called by third-party applications. 966 967**Parameters** 968 969| Name| Type| Mandatory| Description| 970| -------- | -------- | -------- | -------- | 971| bundleName | string | Yes| Bundle name.| 972 973**Return value** 974 975| Type| Description| 976| -------- | -------- | 977| Promise\<void> | Promise that returns no value.| 978 979**Error codes** 980 981For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 982 983| ID| Error Message| 984| ------- | -------- | 985| 201 | Permission denied. | 986| 202 | Not System App. Interface caller is not a system app. | 987| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 988| 16000050 | Internal error. | 989 990**Example** 991 992```ts 993import { appManager } from '@kit.AbilityKit'; 994import { BusinessError } from '@kit.BasicServicesKit'; 995 996let bundleName = 'bundleName'; 997 998try { 999 appManager.killProcessesByBundleName(bundleName).then((data) => { 1000 console.log('killProcessesByBundleName success.'); 1001 }).catch((err: BusinessError) => { 1002 console.error(`killProcessesByBundleName fail, err: ${JSON.stringify(err)}`); 1003 }); 1004} catch (paramError) { 1005 let code = (paramError as BusinessError).code; 1006 let message = (paramError as BusinessError).message; 1007 console.error(`[appManager] error: ${code}, ${message}`); 1008} 1009``` 1010 1011## appManager.clearUpApplicationData 1012 1013clearUpApplicationData(bundleName: string, callback: AsyncCallback\<void>) 1014 1015Clears application data by bundle name. This API uses an asynchronous callback to return the result. 1016 1017**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA 1018 1019**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1020 1021**System API**: This is a system API and cannot be called by third-party applications. 1022 1023**Parameters** 1024 1025| Name| Type| Mandatory| Description| 1026| -------- | -------- | -------- | -------- | 1027| bundleName | string | Yes| Bundle name.| 1028| callback | AsyncCallback\<void> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.| 1029 1030**Error codes** 1031 1032For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1033 1034| ID| Error Message| 1035| ------- | -------- | 1036| 201 | Permission denied. | 1037| 202 | Not System App. Interface caller is not a system app. | 1038| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1039| 16000050 | Internal error. | 1040 1041**Example** 1042 1043```ts 1044import { appManager } from '@kit.AbilityKit'; 1045import { BusinessError } from '@kit.BasicServicesKit'; 1046 1047let bundleName = 'bundleName'; 1048 1049function clearUpApplicationDataCallback(err: BusinessError) { 1050 if (err) { 1051 console.error(`clearUpApplicationDataCallback fail, err: ${JSON.stringify(err)}`); 1052 } else { 1053 console.log('clearUpApplicationDataCallback success.'); 1054 } 1055} 1056 1057try { 1058 appManager.clearUpApplicationData(bundleName, clearUpApplicationDataCallback); 1059} catch (paramError) { 1060 let code = (paramError as BusinessError).code; 1061 let message = (paramError as BusinessError).message; 1062 console.error(`[appManager] error: ${code}, ${message}`); 1063} 1064``` 1065 1066## appManager.clearUpApplicationData 1067 1068clearUpApplicationData(bundleName: string): Promise\<void> 1069 1070Clears application data by bundle name. This API uses a promise to return the result. 1071 1072**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA 1073 1074**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1075 1076**System API**: This is a system API and cannot be called by third-party applications. 1077 1078**Parameters** 1079 1080| Name| Type| Mandatory| Description| 1081| -------- | -------- | -------- | -------- | 1082| bundleName | string | Yes| Bundle name.| 1083 1084**Return value** 1085 1086| Type| Description| 1087| -------- | -------- | 1088| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.| 1089 1090**Error codes** 1091 1092For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1093 1094| ID| Error Message| 1095| ------- | -------- | 1096| 201 | Permission denied. | 1097| 202 | Not System App. Interface caller is not a system app. | 1098| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1099| 16000050 | Internal error. | 1100 1101**Example** 1102 1103```ts 1104import { appManager } from '@kit.AbilityKit'; 1105import { BusinessError } from '@kit.BasicServicesKit'; 1106 1107let bundleName = 'bundleName'; 1108 1109try { 1110 appManager.clearUpApplicationData(bundleName).then((data) => { 1111 console.log('clearUpApplicationData success.'); 1112 }).catch((err: BusinessError) => { 1113 console.error(`clearUpApplicationData fail, err: ${JSON.stringify(err)}`); 1114 }); 1115} catch (paramError) { 1116 let code = (paramError as BusinessError).code; 1117 let message = (paramError as BusinessError).message; 1118 console.error(`[appManager] error: ${code}, ${message}`); 1119} 1120``` 1121 1122## appManager.getProcessMemoryByPid<sup>10+</sup> 1123 1124getProcessMemoryByPid(pid: number, callback: AsyncCallback\<number>): void 1125 1126Obtains the memory size of a process. This API uses an asynchronous callback to return the result. 1127 1128**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1129 1130**System API**: This is a system API. 1131 1132**Parameters** 1133 1134| Name| Type| Mandatory| Description| 1135| -------- | -------- | -------- | -------- | 1136| pid | number | Yes| Process ID. For details, see [getRunningProcessInfoByBundleName](#appmanagergetrunningprocessinfobybundlename10).| 1137| callback | AsyncCallback\<number> | Yes| Callback used to return the API call result and the memory size (in KB). You can perform error handling or custom processing in this callback.| 1138 1139**Error codes** 1140 1141For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1142 1143| ID| Error Message| 1144| ------- | -------- | 1145| 202 | Not System App. Interface caller is not a system app. | 1146| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1147| 16000050 | Internal error. | 1148 1149**Example** 1150 1151```ts 1152import { appManager } from '@kit.AbilityKit'; 1153import { BusinessError } from '@kit.BasicServicesKit'; 1154 1155let pid = 0; 1156function getProcessMemoryByPidCallback(err: BusinessError, data: number) { 1157 if (err) { 1158 console.error(`getProcessMemoryByPidCallback fail, err: ${JSON.stringify(err)}`); 1159 } else { 1160 console.log('getProcessMemoryByPidCallback success.'); 1161 } 1162} 1163 1164try { 1165 appManager.getProcessMemoryByPid(pid, getProcessMemoryByPidCallback); 1166} catch (paramError) { 1167 let code = (paramError as BusinessError).code; 1168 let message = (paramError as BusinessError).message; 1169 console.error(`[appManager] error: ${code}, ${message}`); 1170} 1171``` 1172 1173## appManager.getProcessMemoryByPid<sup>10+</sup> 1174 1175getProcessMemoryByPid(pid: number): Promise\<number> 1176 1177Obtains the memory size of a process. This API uses a promise to return the result. 1178 1179**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1180 1181**System API**: This is a system API. 1182 1183**Parameters** 1184 1185| Name| Type| Mandatory| Description| 1186| -------- | -------- | -------- | -------- | 1187| pid | number | Yes| Process ID. For details, see [getRunningProcessInfoByBundleName](#appmanagergetrunningprocessinfobybundlename10). | 1188 1189**Return value** 1190 1191| Type| Description| 1192| -------- | -------- | 1193| Promise\<number> | Promise used to return the API call result and the memory size (in KB). You can perform error handling or custom processing in this callback.| 1194 1195**Error codes** 1196 1197For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1198 1199| ID| Error Message| 1200| ------- | -------- | 1201| 202 | Not System App. Interface caller is not a system app. | 1202| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1203| 16000050 | Internal error. | 1204 1205**Example** 1206 1207```ts 1208import { appManager } from '@kit.AbilityKit'; 1209import { BusinessError } from '@kit.BasicServicesKit'; 1210 1211let pid = 0; 1212 1213try { 1214 appManager.getProcessMemoryByPid(pid).then((data) => { 1215 console.log('getProcessMemoryByPid success.'); 1216 }).catch((err: BusinessError) => { 1217 console.error(`getProcessMemoryByPid fail, err: ${JSON.stringify(err)}`); 1218 }); 1219} catch (paramError) { 1220 let code = (paramError as BusinessError).code; 1221 let message = (paramError as BusinessError).message; 1222 console.error(`[appManager] error: ${code}, ${message}`); 1223} 1224``` 1225 1226## appManager.getRunningProcessInfoByBundleName<sup>10+</sup> 1227 1228getRunningProcessInfoByBundleName(bundleName: string, callback: AsyncCallback\<Array\<ProcessInformation>>): void 1229 1230Obtains information about the running processes by bundle name. This API uses an asynchronous callback to return the result. 1231 1232**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1233 1234**System API**: This is a system API. 1235 1236**Parameters** 1237 1238| Name| Type| Mandatory| Description| 1239| -------- | -------- | -------- | -------- | 1240| bundleName | string | Yes| Bundle name.| 1241| callback | AsyncCallback\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Yes| Callback used to return the API call result and the process running information. You can perform error handling or custom processing in this callback.| 1242 1243**Error codes** 1244 1245For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1246 1247| ID| Error Message| 1248| ------- | -------- | 1249| 202 | Not System App. Interface caller is not a system app. | 1250| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1251| 16000050 | Internal error. | 1252 1253**Example** 1254 1255```ts 1256import { appManager } from '@kit.AbilityKit'; 1257import { BusinessError } from '@kit.BasicServicesKit'; 1258 1259let bundleName = "bundleName"; 1260function getRunningProcessInfoByBundleNameCallback(err: BusinessError, data: Array<appManager.ProcessInformation>) { 1261 if (err) { 1262 console.error(`getRunningProcessInfoByBundleNameCallback fail, err: ${JSON.stringify(err)}`); 1263 } else { 1264 console.log('getRunningProcessInfoByBundleNameCallback success.'); 1265 } 1266} 1267 1268try { 1269 appManager.getRunningProcessInfoByBundleName(bundleName, getRunningProcessInfoByBundleNameCallback); 1270} catch (paramError) { 1271 let code = (paramError as BusinessError).code; 1272 let message = (paramError as BusinessError).message; 1273 console.error(`[appManager] error: ${code}, ${message}`); 1274} 1275``` 1276 1277## appManager.getRunningProcessInfoByBundleName<sup>10+</sup> 1278 1279getRunningProcessInfoByBundleName(bundleName: string): Promise\<Array\<ProcessInformation>> 1280 1281Obtains information about the running processes by bundle name. This API uses a promise to return the result. 1282 1283**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1284 1285**System API**: This is a system API. 1286 1287**Parameters** 1288 1289| Name| Type| Mandatory| Description| 1290| -------- | -------- | -------- | -------- | 1291| bundleName | string | Yes| Bundle name.| 1292 1293**Return value** 1294 1295| Type| Description| 1296| -------- | -------- | 1297| 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.| 1298 1299**Error codes** 1300 1301For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1302 1303| ID| Error Message| 1304| ------- | -------- | 1305| 202 | Not System App. Interface caller is not a system app. | 1306| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1307| 16000050 | Internal error. | 1308 1309**Example** 1310 1311```ts 1312import { appManager } from '@kit.AbilityKit'; 1313import { BusinessError } from '@kit.BasicServicesKit'; 1314 1315let bundleName = "bundleName"; 1316 1317try { 1318 appManager.getRunningProcessInfoByBundleName(bundleName).then((data) => { 1319 console.log('getRunningProcessInfoByBundleName success.'); 1320 }).catch((err: BusinessError) => { 1321 console.error(`getRunningProcessInfoByBundleName fail, err: ${JSON.stringify(err)}`); 1322 }); 1323} catch (paramError) { 1324 let code = (paramError as BusinessError).code; 1325 let message = (paramError as BusinessError).message; 1326 console.error(`[appManager] error: ${code}, ${message}`); 1327} 1328``` 1329 1330## appManager.getRunningProcessInfoByBundleName<sup>10+</sup> 1331 1332getRunningProcessInfoByBundleName(bundleName: string, userId: number, callback: AsyncCallback\<Array\<ProcessInformation>>): void 1333 1334Obtains information about the running processes by bundle name and user ID. This API uses an asynchronous callback to return the result. 1335 1336**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1337 1338**System API**: This is a system API. 1339 1340**Parameters** 1341 1342| Name| Type| Mandatory| Description| 1343| -------- | -------- | -------- | -------- | 1344| bundleName | string | Yes| Bundle name.| 1345| userId | number | Yes| User ID.| 1346| callback | AsyncCallback\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Yes| Callback used to return the API call result and the process running information. You can perform error handling or custom processing in this callback.| 1347 1348**Error codes** 1349 1350For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1351 1352| ID| Error Message| 1353| ------- | -------- | 1354| 202 | Not System App. Interface caller is not a system app. | 1355| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1356| 16000050 | Internal error. | 1357 1358**Example** 1359 1360```ts 1361import { appManager } from '@kit.AbilityKit'; 1362import { BusinessError } from '@kit.BasicServicesKit'; 1363 1364let bundleName = "bundleName"; 1365let userId = 0; 1366function getRunningProcessInfoByBundleNameCallback(err: BusinessError, data: Array<appManager.ProcessInformation>) { 1367 if (err) { 1368 console.error(`getRunningProcessInfoByBundleNameCallback fail, err: ${JSON.stringify(err)}`); 1369 } else { 1370 console.log('getRunningProcessInfoByBundleNameCallback success.'); 1371 } 1372} 1373 1374try { 1375 appManager.getRunningProcessInfoByBundleName(bundleName, userId, getRunningProcessInfoByBundleNameCallback); 1376} catch (paramError) { 1377 let code = (paramError as BusinessError).code; 1378 let message = (paramError as BusinessError).message; 1379 console.error(`[appManager] error: ${code}, ${message}`); 1380} 1381``` 1382 1383## appManager.getRunningProcessInfoByBundleName<sup>10+</sup> 1384 1385getRunningProcessInfoByBundleName(bundleName: string, userId: number): Promise\<Array\<ProcessInformation>> 1386 1387Obtains information about the running processes by bundle name and user ID. This API uses a promise to return the result. 1388 1389**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1390 1391**System API**: This is a system API. 1392 1393**Parameters** 1394 1395| Name| Type| Mandatory| Description| 1396| -------- | -------- | -------- | -------- | 1397| bundleName | string | Yes| Bundle name.| 1398| userId | number | Yes| User ID.| 1399 1400**Return value** 1401 1402| Type| Description| 1403| -------- | -------- | 1404| 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.| 1405 1406**Error codes** 1407 1408For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1409 1410| ID| Error Message| 1411| ------- | -------- | 1412| 202 | Not System App. Interface caller is not a system app. | 1413| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1414| 16000050 | Internal error. | 1415 1416**Example** 1417 1418```ts 1419import { appManager } from '@kit.AbilityKit'; 1420import { BusinessError } from '@kit.BasicServicesKit'; 1421 1422let bundleName = "bundleName"; 1423let userId = 0; 1424 1425try { 1426 appManager.getRunningProcessInfoByBundleName(bundleName, userId).then((data) => { 1427 console.log('getRunningProcessInfoByBundleName success.'); 1428 }).catch((err: BusinessError) => { 1429 console.error(`getRunningProcessInfoByBundleName fail, err: ${JSON.stringify(err)}`); 1430 }); 1431} catch (paramError) { 1432 let code = (paramError as BusinessError).code; 1433 let message = (paramError as BusinessError).message; 1434 console.error(`[appManager] error: ${code}, ${message}`); 1435} 1436``` 1437 1438## appManager.isApplicationRunning<sup>11+</sup> 1439 1440isApplicationRunning(bundleName: string): Promise\<boolean> 1441 1442Checks whether an application is running. This API uses a promise to return the result. 1443 1444**System API**: This is a system API. 1445 1446**Required permissions**: ohos.permission.GET_RUNNING_INFO 1447 1448**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1449 1450**Parameters** 1451 1452| Name | Type | Mandatory | Description | 1453| --------- | ---------------------------------------- | ---- | -------------- | 1454| bundleName | string | Yes | Bundle name.| 1455 1456**Return value** 1457 1458| Type| Description| 1459| -------- | -------- | 1460| Promise\<boolean> | Promise used to return the result. The value **true** means that the application is running, and **false** means the opposite.| 1461 1462**Error codes** 1463 1464For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1465 1466| ID| Error Message| 1467| ------- | -------- | 1468| 201 | Permission denied. | 1469| 202 | Not System App. Interface caller is not a system app. | 1470| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1471| 16000050 | Internal error. | 1472 1473**Example** 1474 1475```ts 1476import { appManager } from '@kit.AbilityKit'; 1477import { BusinessError } from '@kit.BasicServicesKit'; 1478 1479let bundleName = "com.example.myapplication"; 1480 1481appManager.isApplicationRunning(bundleName).then((data) => { 1482 console.log(`The application running is: ${JSON.stringify(data)}`); 1483}).catch((error: BusinessError) => { 1484 console.error(`error: ${JSON.stringify(error)}`); 1485}); 1486``` 1487 1488## appManager.isApplicationRunning<sup>11+</sup> 1489 1490isApplicationRunning(bundleName: string, callback: AsyncCallback\<boolean>): void 1491 1492Checks whether an application is running. This API uses an asynchronous callback to return the result. 1493 1494**System API**: This is a system API. 1495 1496**Required permissions**: ohos.permission.GET_RUNNING_INFO 1497 1498**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1499 1500**Parameters** 1501 1502| Name | Type | Mandatory | Description | 1503| --------- | ---------------------------------------- | ---- | -------------- | 1504| bundleName | string | Yes | Bundle name of the shared library.| 1505| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. The value **true** means that the application is running, and **false** means the opposite.| 1506 1507**Error codes** 1508 1509For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1510 1511| ID| Error Message| 1512| ------- | -------- | 1513| 201 | Permission denied. | 1514| 202 | Not System App. Interface caller is not a system app. | 1515| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1516| 16000050 | Internal error. | 1517 1518**Example** 1519 1520```ts 1521import { appManager } from '@kit.AbilityKit'; 1522import { BusinessError } from '@kit.BasicServicesKit'; 1523 1524let bundleName = "com.example.myapplication"; 1525 1526try { 1527 appManager.isApplicationRunning(bundleName, (err, data) => { 1528 if (err) { 1529 console.error(`err: ${JSON.stringify(err)}`); 1530 } else { 1531 console.log(`The application running is: ${JSON.stringify(data)}`); 1532 } 1533 }); 1534} catch (paramError) { 1535 let code = (paramError as BusinessError).code; 1536 let message = (paramError as BusinessError).message; 1537 console.error(`[appManager] error: ${code}, ${message}`); 1538} 1539``` 1540 1541## ApplicationState 1542 1543Enumerates the application states. This enum can be used together with [AbilityStateData](js-apis-inner-application-appStateData-sys.md) to return the application state. 1544 1545**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1546 1547**System API**: This is a system API and cannot be called by third-party applications. 1548 1549| Name | Value | Description | 1550| -------------------- | --- | --------------------------------- | 1551| STATE_CREATE | 0 | The application is being created. | 1552| STATE_FOREGROUND | 2 | The application is running in the foreground. | 1553| STATE_ACTIVE | 3 | The application is active. | 1554| STATE_BACKGROUND | 4 | The application is running in the background. | 1555| STATE_DESTROY | 5 | The application is being destroyed. | 1556 1557 1558## appManager.getRunningProcessInformationByBundleType<sup>12+</sup> 1559 1560getRunningProcessInformationByBundleType(bundleType: bundleManager.BundleType): Promise\<Array\<ProcessInformation>> 1561 1562Obtains the information about the running process based on the bundle type. This API uses a promise to return the result. 1563 1564**System API**: This is a system API. 1565 1566**Required permissions**: ohos.permission.GET_RUNNING_INFO 1567 1568**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1569 1570**Parameters** 1571 1572| Name | Type | Mandatory | Description | 1573| --------- | ---------------------------------------- | ---- | -------------- | 1574| bundleType | [bundleManager.BundleType](js-apis-bundleManager.md#bundletype) | Yes | Bundle type.| 1575 1576**Return value** 1577 1578| Type| Description| 1579| -------- | -------- | 1580| Promise\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Promise used to return the process information.| 1581 1582**Error codes** 1583For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1584 1585| ID| Error Message| 1586| ------- | -------- | 1587| 201 | Permission denied. | 1588| 202 | Not system application. | 1589| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1590| 16000050 | Internal error. | 1591 1592 1593**Example** 1594 1595```ts 1596import { appManager, bundleManager } from '@kit.AbilityKit'; 1597import { BusinessError } from '@kit.BasicServicesKit'; 1598 1599try { 1600 appManager.getRunningProcessInformationByBundleType(bundleManager.BundleType.ATOMIC_SERVICE) 1601 .then((data) => { 1602 console.log(`The running process information is: ${JSON.stringify(data)}`); 1603 }).catch((error: BusinessError) => { 1604 console.error(`error: ${JSON.stringify(error)}`); 1605 }); 1606} catch (paramError) { 1607 let code = (paramError as BusinessError).code; 1608 let message = (paramError as BusinessError).message; 1609 console.error(`[appManager] error: ${code}, ${message}`); 1610} 1611``` 1612 1613## appManager.preloadApplication<sup>12+</sup> 1614 1615preloadApplication(bundleName: string, userId: number, mode: PreloadMode, appIndex?: number): Promise\<void> 1616 1617Preloads an application process. A successful call does not always mean that the preloading is successful. In other words, the target application process may not be created even if the API is successfully called. This API uses a promise to return the result. 1618 1619**Required permissions**: ohos.permission.PRELOAD_APPLICATION 1620 1621**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1622 1623**System API**: This is a system API. 1624 1625**Model restriction**: This API can be used only in the stage model. 1626 1627**Parameters** 1628 1629| Name| Type| Mandatory| Description| 1630| -------- | -------- | -------- | -------- | 1631| bundleName | string | Yes| Bundle name of the application to preload.| 1632| userId | number | Yes| User ID.| 1633| mode | [PreloadMode](#appmanagerpreloadmode12) | Yes| Mode used for preloading.| 1634| appIndex | number | No| Application index of the twin application to be preloaded.| 1635 1636**Return value** 1637 1638| Type | Description | 1639| -------------- | ---------------- | 1640| Promise\<void> | Promise that returns no value.| 1641 1642**Error codes** 1643 1644 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1645 1646| ID| Error Message| 1647| ------- | -------- | 1648| 201 | The application does not have permission to call the interface. | 1649| 202 | Not system application. | 1650| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 1651| 16000050 | Internal error. | 1652| 16300005 | The target bundle does not exist. | 1653 1654**Example** 1655 1656```ts 1657import { appManager } from '@kit.AbilityKit'; 1658import { BusinessError } from '@kit.BasicServicesKit'; 1659import { hilog } from '@kit.PerformanceAnalysisKit'; 1660 1661try { 1662 let bundleName = "ohos.samples.etsclock"; 1663 let userId = 100; 1664 let mode = appManager.PreloadMode.PRESS_DOWN; 1665 let appIndex = 0; 1666 appManager.preloadApplication(bundleName, userId, mode, appIndex) 1667 .then(() => { 1668 hilog.info(0x0000, 'testTag', `preloadApplication success`); 1669 }) 1670 .catch((err: BusinessError) => { 1671 hilog.error(0x0000, 'testTag', `preloadApplication error, code: ${err.code}, msg:${err.message}`); 1672 }) 1673} catch (err) { 1674 hilog.error(0x0000, 'testTag', `preloadApplication error, code: ${(err as BusinessError).code}, msg:${(err as BusinessError).message}`); 1675} 1676``` 1677 1678## appManager.getRunningMultiAppInfo<sup>12+</sup> 1679 1680getRunningMultiAppInfo(bundleName: string): Promise\<RunningMultiAppInfo> 1681 1682Obtains the information about running applications in multi-app mode. This API uses a promise to return the result. The multi-app mode means that an application can be simultaneously logged in with different accounts on the same device. 1683 1684**Required permissions**: ohos.permission.GET_RUNNING_INFO 1685 1686**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1687 1688**System API**: This is a system API. 1689 1690**Model restriction**: This API can be used only in the stage model. 1691 1692**Parameters** 1693 1694| Name| Type| Mandatory| Description| 1695| -------- | -------- | -------- | -------- | 1696| bundleName | string | Yes| Bundle name.| 1697 1698**Return value** 1699 1700| Type | Description | 1701| -------------- | ---------------- | 1702| Promise\<[RunningMultiAppInfo](js-apis-inner-application-runningMultiAppInfo-sys.md)> | Promise used to return the information about running applications with multi-app mode.| 1703 1704**Error codes** 1705 1706 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1707 1708| ID| Error Message| 1709| ------- | -------- | 1710| 201 | The application does not have permission to call the interface. | 1711| 202 | Not system application. | 1712| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 1713| 16000072 | App clone or multi-instance is not supported. | 1714 1715**Example** 1716 1717```ts 1718import { appManager } from '@kit.AbilityKit'; 1719import { hilog } from '@kit.PerformanceAnalysisKit'; 1720import { BusinessError } from '@kit.BasicServicesKit'; 1721 1722try { 1723 let bundleName = "ohos.samples.etsclock"; 1724 appManager.getRunningMultiAppInfo(bundleName).then((info: appManager.RunningMultiAppInfo) => { 1725 hilog.info(0x0000, 'testTag', `getRunningMultiAppInfo success`); 1726 }).catch((err: BusinessError) => { 1727 hilog.error(0x0000, 'testTag', `getRunningMultiAppInfo error, code: ${err.code}, msg:${err.message}`); 1728 }) 1729} catch (err) { 1730 hilog.error(0x0000, 'testTag', `getRunningMultiAppInfo error, code: ${err.code}, msg:${err.message}`); 1731} 1732``` 1733 1734## appManager.isAppRunning<sup>12+</sup> 1735 1736isAppRunning(bundleName: string, appCloneIndex?: number): Promise\<boolean> 1737 1738Checks whether an application is running. This API uses a promise to return the result. 1739 1740**Required permissions**: ohos.permission.GET_RUNNING_INFO 1741 1742**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1743 1744**System API**: This is a system API. 1745 1746**Parameters** 1747 1748| Name| Type| Mandatory| Description| 1749| -------- | -------- | -------- | -------- | 1750| bundleName | string | Yes| Bundle name.| 1751| appCloneIndex | number | No| Index of the app clone.| 1752 1753**Return value** 1754 1755| Type | Description | 1756| -------------- | ---------------- | 1757| Promise\<boolean> | Promise used to return the result. The value **true** means that the application is running, and **false** means the opposite.| 1758 1759**Error codes** 1760 1761 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1762 1763| ID| Error Message| 1764| ------- | -------- | 1765| 201 | The application does not have permission to call the interface. | 1766| 202 | Not system application. | 1767| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 1768| 16000050 | Internal error. | 1769| 16000073 | The app clone index is invalid. | 1770 1771**Example** 1772 1773```ts 1774import { appManager } from '@kit.AbilityKit'; 1775import { hilog } from '@kit.PerformanceAnalysisKit'; 1776import { BusinessError } from '@kit.BasicServicesKit'; 1777 1778try { 1779 let bundleName = "ohos.samples.etsclock"; 1780 appManager.isAppRunning(bundleName).then((data: boolean) => { 1781 hilog.info(0x0000, 'testTag', `data: ${JSON.stringify(data)}`); 1782 }).catch((err: BusinessError) => { 1783 hilog.error(0x0000, 'testTag', `isAppRunning error, code: ${err.code}, msg:${err.message}`); 1784 }) 1785} catch (err) { 1786 hilog.error(0x0000, 'testTag', `isAppRunning error, code: ${err.code}, msg:${err.message}`); 1787} 1788``` 1789 1790## appManager.terminateMission<sup>12+</sup> 1791 1792terminateMission(missionId: number): Promise\<void> 1793 1794Terminates a mission. This API uses a promise to return the result. 1795 1796**Required permissions**: ohos.permission.KILL_APP_PROCESSES 1797 1798**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1799 1800**System API**: This is a system API. 1801 1802**Parameters** 1803 1804| Name| Type| Mandatory| Description| 1805| -------- | -------- | -------- | -------- | 1806| missionId | number | Yes| Mission ID, which can be obtained by calling [getMissionInfos](js-apis-app-ability-missionManager-sys.md#missionmanagergetmissioninfos).| 1807 1808**Return value** 1809 1810| Type| Description| 1811| -------- | -------- | 1812| Promise\<void> | Promise that returns no value.| 1813 1814**Error codes** 1815 1816For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1817 1818| ID| Error Message| 1819| ------- | -------- | 1820| 201 | Permission denied. | 1821| 202 | Not System App. Interface caller is not a system app. | 1822| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1823| 16000050 | Internal error. | 1824 1825**Example** 1826```ts 1827import { appManager } from '@kit.AbilityKit'; 1828import { BusinessError } from '@kit.BasicServicesKit'; 1829 1830@Entry 1831@Component 1832struct Index { 1833 build() { 1834 Button('start link', { type: ButtonType.Capsule, stateEffect: true }) 1835 .width('87%') 1836 .height('5%') 1837 .margin({ bottom: '12vp' }) 1838 .onClick(() => { 1839 let missionId: number = 0; 1840 try { 1841 appManager.terminateMission(missionId).then(()=>{ 1842 console.log('terminateMission success.'); 1843 }).catch((err: BusinessError)=>{ 1844 console.error('terminateMission failed. err: ' + JSON.stringify(err)); 1845 }) 1846 } catch (paramError) { 1847 let code = (paramError as BusinessError).code; 1848 let message = (paramError as BusinessError).message; 1849 console.error(`[appManager] error: ${code}, ${message}`); 1850 } 1851 }) 1852 } 1853} 1854``` 1855