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