1# @ohos.application.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 APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.appManager](js-apis-app-ability-appManager.md) instead. 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.application.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 result. The value **true** means that the application is undergoing a stability test, and **false** means the opposite.| 28 29**Example** 30 31 ```ts 32 import appManager from '@ohos.application.appManager'; 33 34 appManager.isRunningInStabilityTest((error, flag) => { 35 if (error && error.code !== 0) { 36 console.error(`isRunningInStabilityTest fail, error: ${JSON.stringify(error)}`); 37 } else { 38 console.log(`isRunningInStabilityTest success, the result is: ${JSON.stringify(flag)}`); 39 } 40 }); 41 ``` 42 43 44## appManager.isRunningInStabilityTest 45 46isRunningInStabilityTest(): Promise<boolean> 47 48Checks whether this application is undergoing a stability test. This API uses a promise to return the result. 49 50**System capability**: SystemCapability.Ability.AbilityRuntime.Core 51 52**Return value** 53 54 | Type| Description| 55 | -------- | -------- | 56 | Promise<boolean> | Promise used to return the result. The value **true** means that the application is undergoing a stability test, and **false** means the opposite.| 57 58**Example** 59 60 ```ts 61 import appManager from '@ohos.application.appManager'; 62 import { BusinessError } from '@ohos.base'; 63 64 appManager.isRunningInStabilityTest().then((flag) => { 65 console.log(`The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}`); 66 }).catch((error: BusinessError) => { 67 console.error(`error: ${JSON.stringify(error)}`); 68 }); 69 ``` 70 71 72## appManager.isRamConstrainedDevice 73 74isRamConstrainedDevice(): Promise\<boolean> 75 76Checks whether this application is running on a RAM constrained device. This API uses a promise to return the result. 77 78**System capability**: SystemCapability.Ability.AbilityRuntime.Core 79 80**Return value** 81 82 | Type| Description| 83 | -------- | -------- | 84 | Promise<boolean> | Promise used to return the result. The value **true** means that the application is running on a RAM constrained device, and **false** means the opposite.| 85 86**Example** 87 88 ```ts 89 import appManager from '@ohos.application.appManager'; 90 import { BusinessError } from '@ohos.base'; 91 92 appManager.isRamConstrainedDevice().then((data) => { 93 console.log(`The result of isRamConstrainedDevice is: ${JSON.stringify(data)}`); 94 }).catch((error: BusinessError) => { 95 console.error(`error: ${JSON.stringify(error)}`); 96 }); 97 ``` 98 99## appManager.isRamConstrainedDevice 100 101isRamConstrainedDevice(callback: AsyncCallback\<boolean>): void 102 103Checks whether this application is running on a RAM constrained device. This API uses an asynchronous callback to return the result. 104 105**System capability**: SystemCapability.Ability.AbilityRuntime.Core 106 107**Parameters** 108 109 | Name| Type| Mandatory| Description| 110 | -------- | -------- | -------- | -------- | 111 | callback | AsyncCallback<boolean> | Yes| Callback used to return the result. The value **true** means that the application is running on a RAM constrained device, and **false** means the opposite.| 112 113**Example** 114 115 ```ts 116 import appManager from '@ohos.application.appManager'; 117 118 appManager.isRamConstrainedDevice((error, data) => { 119 if (error && error.code !== 0) { 120 console.error(`isRamConstrainedDevice fail, error: ${JSON.stringify(error)}`); 121 } else { 122 console.log(`The result of isRamConstrainedDevice is: ${JSON.stringify(data)}`); 123 } 124 }); 125 ``` 126 127## appManager.getAppMemorySize 128 129getAppMemorySize(): Promise\<number> 130 131Obtains the memory size of this application. This API uses a promise to return the result. 132 133**System capability**: SystemCapability.Ability.AbilityRuntime.Core 134 135**Return value** 136 137 | Type| Description| 138 | -------- | -------- | 139 | Promise<number> | Promise used to return the memory size, in MB.| 140 141**Example** 142 143 ```ts 144 import appManager from '@ohos.application.appManager'; 145 import { BusinessError } from '@ohos.base'; 146 147 appManager.getAppMemorySize().then((data) => { 148 console.log(`The size of app memory is: ${JSON.stringify(data)}`); 149 }).catch((error: BusinessError) => { 150 console.error(`error: ${JSON.stringify(error)}`); 151 }); 152 ``` 153 154## appManager.getAppMemorySize 155 156getAppMemorySize(callback: AsyncCallback\<number>): void 157 158Obtains the memory size of this application. This API uses an asynchronous callback to return the result. 159 160**System capability**: SystemCapability.Ability.AbilityRuntime.Core 161 162**Parameters** 163 164 | Name| Type| Mandatory| Description| 165 | -------- | -------- | -------- | -------- | 166 | callback | AsyncCallback<number> | Yes| Callback used to return the memory size, in MB.| 167 168**Example** 169 170 ```ts 171 import appManager from '@ohos.application.appManager'; 172 173 appManager.getAppMemorySize((error, data) => { 174 if (error && error.code !== 0) { 175 console.error(`getAppMemorySize fail, error: ${JSON.stringify(error)}`); 176 } else { 177 console.log(`The size of app memory is: ${JSON.stringify(data)}`); 178 } 179 }); 180 ``` 181## appManager.getProcessRunningInfos<sup>(deprecated)</sup> 182 183getProcessRunningInfos(): Promise\<Array\<ProcessRunningInfo>> 184 185Obtains information about the running processes. This API uses a promise to return the result. 186 187> This API is deprecated since API version 9. You are advised to use [appManager.getRunningProcessInformation](js-apis-app-ability-appManager.md#appmanagergetrunningprocessinformation) instead. 188 189**Required permissions**: ohos.permission.GET_RUNNING_INFO 190 191**System capability**: SystemCapability.Ability.AbilityRuntime.Core 192 193**Return value** 194 195| Type| Description| 196| -------- | -------- | 197| Promise\<Array\<[ProcessRunningInfo](js-apis-inner-application-processRunningInfo.md)>> | Promise used to return the information about the running processes.| 198 199**Example** 200 201 ```ts 202 import appManager from '@ohos.application.appManager'; 203 import { BusinessError } from '@ohos.base'; 204 205 appManager.getProcessRunningInfos().then((data) => { 206 console.log(`The process running infos is: ${JSON.stringify(data)}`); 207 }).catch((error: BusinessError) => { 208 console.error(`error: ${JSON.stringify(error)}`); 209 }); 210 ``` 211 212## appManager.getProcessRunningInfos<sup>(deprecated)</sup> 213 214getProcessRunningInfos(callback: AsyncCallback\<Array\<ProcessRunningInfo>>): void 215 216Obtains information about the running processes. This API uses an asynchronous callback to return the result. 217 218> This API is deprecated since API version 9. You are advised to use [appManager.getRunningProcessInformation](js-apis-app-ability-appManager.md#appmanagergetrunningprocessinformation) instead. 219 220**Required permissions**: ohos.permission.GET_RUNNING_INFO 221 222**System capability**: SystemCapability.Ability.AbilityRuntime.Core 223 224**Parameters** 225 226| Name| Type| Mandatory| Description| 227| -------- | -------- | -------- | -------- | 228| callback | AsyncCallback\<Array\<[ProcessRunningInfo](js-apis-inner-application-processRunningInfo.md)>> | Yes| Callback used to return the information about the running processes.| 229 230**Example** 231 232 ```ts 233 import appManager from '@ohos.application.appManager'; 234 235 appManager.getProcessRunningInfos((error, data) => { 236 if (error && error.code !== 0) { 237 console.error(`getProcessRunningInfos fail, error: ${JSON.stringify(error)}`); 238 } else { 239 console.log(`getProcessRunningInfos success, data: ${JSON.stringify(data)}`); 240 } 241 }); 242 ``` 243 244## appManager.registerApplicationStateObserver 245 246registerApplicationStateObserver(observer: ApplicationStateObserver): number 247 248Registers an observer to listen for the state changes of all applications. 249 250**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 251 252**System capability**: SystemCapability.Ability.AbilityRuntime.Core 253 254**System API**: This is a system API. 255 256**Parameters** 257 258| Name| Type| Mandatory| Description| 259| -------- | -------- | -------- | -------- | 260| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.| 261 262**Return value** 263 264| Type| Description| 265| --- | --- | 266| number | Digital code of the observer.| 267 268**Example** 269 270 ```ts 271 import appManager from '@ohos.application.appManager'; 272 273 const observerCode = appManager.registerApplicationStateObserver({ 274 onForegroundApplicationChanged(appStateData) { 275 console.log('------------ onForegroundApplicationChanged -----------', appStateData); 276 }, 277 onAbilityStateChanged(abilityStateData) { 278 console.log('------------ onAbilityStateChanged -----------', abilityStateData); 279 }, 280 onProcessCreated(processData) { 281 console.log('------------ onProcessCreated -----------', processData); 282 }, 283 onProcessDied(processData) { 284 console.log('------------ onProcessDied -----------', processData); 285 }, 286 onProcessStateChanged(processData) { 287 console.log('------------ onProcessStateChanged -----------', processData); 288 } 289 }); 290 console.log('-------- observerCode: ---------', observerCode); 291 ``` 292 293## appManager.unregisterApplicationStateObserver 294 295unregisterApplicationStateObserver(observerId: number, callback: AsyncCallback\<void>): void 296 297Deregisters the application state observer. This API uses an asynchronous callback to return the result. 298 299**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 300 301**System capability**: SystemCapability.Ability.AbilityRuntime.Core 302 303**System API**: This is a system API. 304 305**Parameters** 306 307| Name| Type| Mandatory| Description| 308| -------- | -------- | -------- | -------- | 309| observerId | number | Yes| Numeric code of the observer.| 310| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 311 312**Example** 313 314 ```ts 315 import appManager from '@ohos.application.appManager'; 316 import { BusinessError } from '@ohos.base'; 317 318 let observerId = 100; 319 320 function unregisterApplicationStateObserverCallback(err: BusinessError) { 321 if (err) { 322 console.error('------------ unregisterApplicationStateObserverCallback ------------', err); 323 } 324 } 325 appManager.unregisterApplicationStateObserver(observerId, unregisterApplicationStateObserverCallback); 326 ``` 327 328## appManager.unregisterApplicationStateObserver 329 330unregisterApplicationStateObserver(observerId: number): Promise\<void> 331 332Deregisters the application state observer. This API uses a promise to return the result. 333 334**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER 335 336**System capability**: SystemCapability.Ability.AbilityRuntime.Core 337 338**System API**: This is a system API. 339 340**Parameters** 341 342| Name| Type| Mandatory| Description| 343| -------- | -------- | -------- | -------- | 344| observerId | number | Yes| Numeric code of the observer.| 345 346**Return value** 347 348| Type| Description| 349| -------- | -------- | 350| Promise\<void> | Promise that returns no value.| 351 352**Example** 353 354 ```ts 355 import appManager from '@ohos.application.appManager'; 356 import { BusinessError } from '@ohos.base'; 357 358 let observerId = 100; 359 360 appManager.unregisterApplicationStateObserver(observerId) 361 .then((data) => { 362 console.log('----------- unregisterApplicationStateObserver success ----------', data); 363 }) 364 .catch((err: BusinessError) => { 365 console.error('----------- unregisterApplicationStateObserver fail ----------', err); 366 }); 367 ``` 368 369## appManager.getForegroundApplications 370 371getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void 372 373Obtains information about the applications that are running in the foreground. The application information is defined by [AppStateData](js-apis-inner-application-appStateData.md). This API uses an asynchronous callback to return the result. 374 375**Required permissions**: ohos.permission.GET_RUNNING_INFO 376 377**System capability**: SystemCapability.Ability.AbilityRuntime.Core 378 379**System API**: This is a system API. 380 381**Parameters** 382 383| Name| Type| Mandatory| Description| 384| -------- | -------- | -------- | -------- | 385| callback | AsyncCallback\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Yes| Callback used to return the application information.| 386 387**Example** 388 389 ```ts 390 import appManager from '@ohos.application.appManager'; 391 392 appManager.getForegroundApplications((err, data) => { 393 if (err) { 394 console.error('--------- getForegroundApplicationsCallback fail ---------', err); 395 } else { 396 console.log('--------- getForegroundApplicationsCallback success ---------', data); 397 } 398 }); 399 ``` 400 401## appManager.getForegroundApplications 402 403getForegroundApplications(): Promise\<Array\<AppStateData>> 404 405Obtains information about the applications that are running in the foreground. The application information is defined by [AppStateData](js-apis-inner-application-appStateData.md). This API uses a promise to return the result. 406 407**Required permissions**: ohos.permission.GET_RUNNING_INFO 408 409**System capability**: SystemCapability.Ability.AbilityRuntime.Core 410 411**System API**: This is a system API. 412 413**Return value** 414 415| Type| Description| 416| -------- | -------- | 417| Promise\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Promise used to return the application information.| 418 419**Example** 420 421 ```ts 422 import appManager from '@ohos.application.appManager'; 423 import { BusinessError } from '@ohos.base'; 424 425 appManager.getForegroundApplications() 426 .then((data) => { 427 console.log('--------- getForegroundApplications success -------', data); 428 }) 429 .catch((err: BusinessError) => { 430 console.error('--------- getForegroundApplications fail -------', err); 431 }); 432 ``` 433 434## appManager.killProcessWithAccount 435 436killProcessWithAccount(bundleName: string, accountId: number): Promise\<void\> 437 438Kills a process by bundle name and account ID. This API uses a promise to return the result. 439 440> **NOTE** 441> 442> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 443 444**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.CLEAN_BACKGROUND_PROCESSES 445 446**System capability**: SystemCapability.Ability.AbilityRuntime.Core 447 448**System API**: This is a system API. 449 450**Parameters** 451 452| Name| Type| Mandatory| Description| 453| -------- | -------- | -------- | -------- | 454| bundleName | string | Yes| Bundle name.| 455| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 456 457**Return value** 458 459| Type| Description| 460| -------- | -------- | 461| Promise\<void> | Promise that returns no value.| 462 463**Example** 464 465```ts 466import appManager from '@ohos.application.appManager'; 467import { BusinessError } from '@ohos.base'; 468 469let bundleName = 'bundleName'; 470let accountId = 0; 471appManager.killProcessWithAccount(bundleName, accountId) 472 .then((data) => { 473 console.log('------------ killProcessWithAccount success ------------', data); 474 }) 475 .catch((err: BusinessError) => { 476 console.error('------------ killProcessWithAccount fail ------------', err); 477 }); 478``` 479 480 481## appManager.killProcessWithAccount 482 483killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCallback\<void\>): void 484 485Kills a process by bundle name and account ID. This API uses an asynchronous callback to return the result. 486 487> **NOTE** 488> 489> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 490 491**System capability**: SystemCapability.Ability.AbilityRuntime.Core 492 493**System API**: This is a system API. 494 495**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.CLEAN_BACKGROUND_PROCESSES 496 497**Parameters** 498 499| Name| Type| Mandatory| Description| 500| -------- | -------- | -------- | -------- | 501| bundleName | string | Yes| Bundle name.| 502| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 503| callback | AsyncCallback\<void\> | Yes| Callback used to return the result. If the process is killed, **err** is **undefined**; otherwise, **err** is an error object.| 504 505**Example** 506 507```ts 508import appManager from '@ohos.application.appManager'; 509import { BusinessError } from '@ohos.base'; 510 511let bundleName = 'bundleName'; 512let accountId = 0; 513function killProcessWithAccountCallback(err: BusinessError, data: void) { 514 if (err) { 515 console.error('------------- killProcessWithAccountCallback fail, err: --------------', err); 516 } else { 517 console.log('------------- killProcessWithAccountCallback success, data: --------------', data); 518 } 519} 520appManager.killProcessWithAccount(bundleName, accountId, killProcessWithAccountCallback); 521``` 522 523## appManager.killProcessesByBundleName 524 525killProcessesByBundleName(bundleName: string, callback: AsyncCallback\<void>) 526 527Kills a process by bundle name. This API uses an asynchronous callback to return the result. 528 529**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES 530 531**System capability**: SystemCapability.Ability.AbilityRuntime.Core 532 533**System API**: This is a system API. 534 535**Parameters** 536 537| Name| Type| Mandatory| Description| 538| -------- | -------- | -------- | -------- | 539| bundleName | string | Yes| Bundle name.| 540| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the process is killed, **err** is **undefined**; otherwise, **err** is an error object.| 541 542**Example** 543 544 ```ts 545 import appManager from '@ohos.application.appManager'; 546import { BusinessError } from '@ohos.base'; 547 548 let bundleName = 'bundleName'; 549 function killProcessesByBundleNameCallback(err: BusinessError, data: void) { 550 if (err) { 551 console.error('------------- killProcessesByBundleNameCallback fail, err: --------------', err); 552 } else { 553 console.log('------------- killProcessesByBundleNameCallback success, data: --------------', data); 554 } 555 } 556 appManager.killProcessesByBundleName(bundleName, killProcessesByBundleNameCallback); 557 ``` 558 559## appManager.killProcessesByBundleName 560 561killProcessesByBundleName(bundleName: string): Promise\<void> 562 563Kills a process by bundle name. This API uses a promise to return the result. 564 565**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES 566 567**System capability**: SystemCapability.Ability.AbilityRuntime.Core 568 569**System API**: This is a system API. 570 571**Parameters** 572 573| Name| Type| Mandatory| Description| 574| -------- | -------- | -------- | -------- | 575| bundleName | string | Yes| Bundle name.| 576 577**Return value** 578 579| Type| Description| 580| -------- | -------- | 581| Promise\<void> | Promise that returns no value.| 582 583**Example** 584 585 ```ts 586 import appManager from '@ohos.application.appManager'; 587 import { BusinessError } from '@ohos.base'; 588 589 let bundleName = 'com.example.myapplication'; 590 appManager.killProcessesByBundleName(bundleName) 591 .then((data) => { 592 console.log('------------ killProcessesByBundleName success ------------', data); 593 }) 594 .catch((err: BusinessError) => { 595 console.error('------------ killProcessesByBundleName fail ------------', err); 596 }); 597 ``` 598 599## appManager.clearUpApplicationData 600 601clearUpApplicationData(bundleName: string, callback: AsyncCallback\<void>) 602 603Clears application data by bundle name. This API uses an asynchronous callback to return the result. 604 605**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA 606 607**System capability**: SystemCapability.Ability.AbilityRuntime.Core 608 609**System API**: This is a system API. 610 611**Parameters** 612 613| Name| Type| Mandatory| Description| 614| -------- | -------- | -------- | -------- | 615| bundleName | string | Yes| Bundle name.| 616| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the application data is cleared, **err** is **undefined**; otherwise, **err** is an error object.| 617 618**Example** 619 620 ```ts 621 import appManager from '@ohos.application.appManager'; 622 import { BusinessError } from '@ohos.base'; 623 624 let bundleName = 'bundleName'; 625 function clearUpApplicationDataCallback(err: BusinessError, data: void) { 626 if (err) { 627 console.error('------------- clearUpApplicationDataCallback fail, err: --------------', err); 628 } else { 629 console.log('------------- clearUpApplicationDataCallback success, data: --------------', data); 630 } 631 } 632 appManager.clearUpApplicationData(bundleName, clearUpApplicationDataCallback); 633 ``` 634 635## appManager.clearUpApplicationData 636 637clearUpApplicationData(bundleName: string): Promise\<void> 638 639Clears application data by bundle name. This API uses a promise to return the result. 640 641**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA 642 643**System capability**: SystemCapability.Ability.AbilityRuntime.Core 644 645**System API**: This is a system API. 646 647**Parameters** 648 649| Name| Type| Mandatory| Description| 650| -------- | -------- | -------- | -------- | 651| bundleName | string | Yes| Bundle name.| 652 653**Return value** 654 655| Type| Description| 656| -------- | -------- | 657| Promise\<void> | Promise that returns no value.| 658 659**Example** 660 661 ```ts 662 import appManager from '@ohos.application.appManager'; 663 import { BusinessError } from '@ohos.base'; 664 665 let bundleName = 'bundleName'; 666 appManager.clearUpApplicationData(bundleName) 667 .then((data) => { 668 console.log('------------ clearUpApplicationData success ------------', data); 669 }) 670 .catch((err: BusinessError) => { 671 console.error('------------ clearUpApplicationData fail ------------', err); 672 }); 673 ``` 674