js-apis-app-ability-appManager.md 32.9 KB
Newer Older
1
# @ohos.app.ability.appManager (appManager)
2 3 4 5 6 7 8 9 10 11 12 13 14

The **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.

> **NOTE**
> 
> 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.

## Modules to Import

```ts
import appManager from '@ohos.app.ability.appManager';
```

15
## appManager.isRunningInStabilityTest
16 17 18 19 20 21 22 23 24

static isRunningInStabilityTest(callback: AsyncCallback<boolean>): void

Checks whether this application is undergoing a stability test. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

25 26 27 28 29 30 31 32 33 34 35
  | Type| Description| 
  | -------- | -------- |
  |AsyncCallback<boolean> |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.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
36 37 38

**Example**

39 40
```ts
import appManager from '@ohos.app.ability.appManager';
41

42
appManager.isRunningInStabilityTest((err, flag) => {
43 44
    if (err && err.code !== 0) {
        console.log('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 54 55 56 57 58 59 60 61 62 63

static isRunningInStabilityTest(): Promise<boolean>

Checks whether this application is undergoing a stability test. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

  | Type| Description| 
  | -------- | -------- |
64 65 66 67 68 69 70 71 72
  | 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.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
73 74

**Example**
75 76 77 78 79

```ts
import appManager from '@ohos.app.ability.appManager';

appManager.isRunningInStabilityTest().then((flag) => {
80
    console.log('The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}');
81
}).catch((error) => {
82
    console.log('error: ${JSON.stringify(error)}');
83 84
});
```
85 86 87 88 89 90 91 92 93 94 95 96 97 98


## appManager.isRamConstrainedDevice

isRamConstrainedDevice(): Promise\<boolean>;

Checks whether this application is running on a RAM constrained device. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

  | Type| Description| 
  | -------- | -------- |
99 100 101 102 103 104 105 106 107
  | Promise&lt;boolean&gt; | 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.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
108 109

**Example**
110 111 112 113 114

```ts
import appManager from '@ohos.app.ability.appManager';

appManager.isRamConstrainedDevice().then((data) => {
115
    console.log('The result of isRamConstrainedDevice is: ${JSON.stringify(data)}');
116
}).catch((error) => {
117
    console.log('error: ${JSON.stringify(error)}');
118 119
});
```
120 121 122 123 124 125 126 127 128 129 130

## appManager.isRamConstrainedDevice

isRamConstrainedDevice(callback: AsyncCallback\<boolean>): void;

Checks whether this application is running on a RAM constrained device. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

131 132 133 134 135 136 137 138 139 140 141
  | Type| Description| 
  | -------- | -------- |
  | AsyncCallback&lt;boolean&gt; |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.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
142 143

**Example**
144 145 146 147 148

```ts
import appManager from '@ohos.app.ability.appManager';

appManager.isRamConstrainedDevice((err, data) => {
149 150
    if (err && err.code !== 0) {
        console.log('isRamConstrainedDevice fail, err: ${JSON.stringify(err)}');
151
    } else {
152
        console.log('The result of isRamConstrainedDevice is: ${JSON.stringify(data)}');
153
    }
154
});
155
```
156 157 158 159 160 161 162 163 164 165 166 167 168

## appManager.getAppMemorySize

getAppMemorySize(): Promise\<number>;

Obtains the memory size of this application. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

  | Type| Description| 
  | -------- | -------- |
169 170 171 172 173 174 175 176 177
  | Promise&lt;number&gt; | Promise used to return the API call result and the memory size. You can perform error handling or custom processing in this callback.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
178 179

**Example**
180 181 182 183 184

```ts
import appManager from '@ohos.app.ability.appManager';

appManager.getAppMemorySize().then((data) => {
185
    console.log('The size of app memory is: ${JSON.stringify(data)}');
186
}).catch((error) => {
187
    console.log('error: ${JSON.stringify(error)}');
188 189
});
```
190 191 192 193 194 195 196 197 198 199 200

## appManager.getAppMemorySize

getAppMemorySize(callback: AsyncCallback\<number>): void;

Obtains the memory size of this application. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

201 202 203 204 205 206 207 208 209 210 211
  | Type| Description| 
  | -------- | -------- |
  |AsyncCallback&lt;number&gt; |Callback used to return the API call result and the memory size. You can perform error handling or custom processing in this callback.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
212 213 214

**Example**

215 216 217 218
```ts
import appManager from '@ohos.app.ability.appManager';

appManager.getAppMemorySize((err, data) => {
219 220
    if (err && err.code !== 0) {
        console.log('getAppMemorySize fail, err: ${JSON.stringify(err)}');
221
    } else {
222
        console.log('The size of app memory is: ${JSON.stringify(data)}');
223
    }
224
});
225 226
```

227
## appManager.getRunningProcessInformation
228

229
getRunningProcessInformation(): Promise\<Array\<ProcessInformation>>;
230 231 232 233 234 235 236 237 238 239 240

Obtains information about the running processes. This API uses a promise to return the result.

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type| Description|
| -------- | -------- |
241
| 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.|
242 243 244 245 246 247 248 249

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
250 251

**Example**
252 253 254 255

```ts
import appManager from '@ohos.app.ability.appManager';

256 257
appManager.getRunningProcessInformation().then((data) => {
    console.log('The running process information is: ${JSON.stringify(data)}');
258
}).catch((error) => {
259
    console.log('error: ${JSON.stringify(error)}');
260 261
});
```
262

263
## appManager.getRunningProcessInformation<sup>9+</sup>
264

265
getRunningProcessInformation(callback: AsyncCallback\<Array\<ProcessInformation>>): void;
266 267 268 269 270 271 272 273 274

Obtains information about the running processes. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

275 276
| Type| Description|
| -------- | -------- |
277
|AsyncCallback\<Array\<[ProcessInformation](js-apis-inner-application-processInformation.md)>> | Callback used to return the API call result and the process running information. You can perform error handling or custom processing in this callback.|
278 279 280 281 282 283 284 285

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
286 287

**Example**
288 289 290 291

```ts
import appManager from '@ohos.app.ability.appManager';

292 293 294
appManager.getRunningProcessInformation((err, data) => {
    if (err && err.code !== 0) {
        console.log('getRunningProcessInformation fail, err: ${JSON.stringify(err)}');
295
    } else {
296
        console.log('The process running information is: ${JSON.stringify(data)}');
297
    }
298
});
299
```
300 301 302

## appManager.on

303
on(type: 'applicationState', observer: ApplicationStateObserver): number;
304 305 306 307 308 309 310 311 312 313 314 315 316

Registers an observer to listen for the state changes of all applications.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
317
| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| observer | [ApplicationStateObserver](./js-apis-inner-application-applicationStateObserver.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.|

**Return value**

| Type| Description|
| --- | --- |
| number | Digital code of the observer, which will be used in **off()** to deregister the observer.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
333 334

**Example**
335 336 337 338 339

```ts
import appManager from '@ohos.app.ability.appManager';

let applicationStateObserver = {
340
    onForegroundApplicationChanged(appStateData) {
341
        console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
342 343
    },
    onAbilityStateChanged(abilityStateData) {
344
        console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
345 346
    },
    onProcessCreated(processData) {
347
        console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
348 349
    },
    onProcessDied(processData) {
350
        console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
351 352
    },
    onProcessStateChanged(processData) {
353
        console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
354
    }
355
};
356 357 358 359 360 361 362
try {
    const observerId = appManager.on('applicationState', applicationStateObserver);
    console.log(`[appManager] observerCode: ${observerId}`);
} catch (paramError) {
    console.log(`[appManager] error: ${paramError.code}, ${paramError.message} `);
}
```
363 364 365

## appManager.on

366
on(type: 'applicationState', observer: ApplicationStateObserver, bundleNameList: Array\<string>): number;
367 368 369 370 371 372 373 374 375 376 377 378 379

Registers an observer to listen for the state changes of a specified application.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
380
| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
| observer | [ApplicationStateObserver](./js-apis-inner-application-applicationStateObserver.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.|
| bundleNameList | `Array<string>` | Yes| **bundleName** array of the application. A maximum of 128 bundle names can be passed.|

**Return value**

| Type| Description|
| --- | --- |
| number | Digital code of the observer, which will be used in **off()** to deregister the observer.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
397 398

**Example**
399 400 401 402 403

```ts
import appManager from '@ohos.app.ability.appManager';

let applicationStateObserver = {
404
    onForegroundApplicationChanged(appStateData) {
405
        console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
406 407
    },
    onAbilityStateChanged(abilityStateData) {
408
        console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
409 410
    },
    onProcessCreated(processData) {
411
        console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
412 413
    },
    onProcessDied(processData) {
414
        console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
415 416
    },
    onProcessStateChanged(processData) {
417
        console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
418
    }
419
};
420 421
let bundleNameList = ['bundleName1', 'bundleName2'];
try {
422
    const observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList);
423 424 425 426 427
    console.log(`[appManager] observerCode: ${observerId}`);
} catch (paramError) {
    console.log(`[appManager] error: ${paramError.code}, ${paramError.message} `);
}
```
428 429 430

## appManager.off

431
off(type: 'applicationState', observerId: number,  callback: AsyncCallback\<void>): void;
432 433 434 435 436 437 438 439 440 441

Deregisters the application state observer. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**
442

443 444
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
445
| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
446 447 448 449 450 451 452 453 454 455
| observerId | number | Yes| Digital code of the observer.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
456 457 458

**Example**

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
```ts
import appManager from '@ohos.app.ability.appManager';

let observeId = 0;

// 1. Register an application state observer.
let applicationStateObserver = {
    onForegroundApplicationChanged(appStateData) {
        console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
    },
    onAbilityStateChanged(abilityStateData) {
        console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
    },
    onProcessCreated(processData) {
        console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
    },
    onProcessDied(processData) {
        console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
    },
    onProcessStateChanged(processData) {
        console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
480
    }
481
};
482 483
let bundleNameList = ['bundleName1', 'bundleName2'];
try {
484
    observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList);
485 486 487 488 489 490 491
    console.log(`[appManager] observerCode: ${observerId}`);
} catch (paramError) {
    console.log(`[appManager] error: ${paramError.code}, ${paramError.message} `);
}

// 2. Deregister the application state observer.
function unregisterApplicationStateObserverCallback(err) {
492 493
    if (err && err.code !== 0) {
        console.log('unregisterApplicationStateObserverCallback fail, err: ${JSON.stringify(err)}');
494
    } else {
495
        console.log('unregisterApplicationStateObserverCallback success.');
496
    }
497 498
}
try {
499
    appManager.off('applicationState', observerId, unregisterApplicationStateObserverCallback);
500
} catch (paramError) {
501
    console.log('error: ${paramError.code}, ${paramError.message}');
502 503
}
```
504 505 506

## appManager.off

507
off(type: 'applicationState', observerId: number): Promise\<void>;
508 509 510 511 512 513 514 515 516 517 518 519 520

Deregisters the application state observer. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
521
| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
522
| observerId | number | Yes| Digital code of the observer.|
523 524 525 526 527

**Return value**

| Type| Description|
| -------- | -------- |
528 529 530 531 532 533 534 535 536
| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
537 538

**Example**
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

```ts
import appManager from '@ohos.app.ability.appManager';

let observeId = 0;

// 1. Register an application state observer.
let applicationStateObserver = {
    onForegroundApplicationChanged(appStateData) {
        console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
    },
    onAbilityStateChanged(abilityStateData) {
        console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
    },
    onProcessCreated(processData) {
        console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
    },
    onProcessDied(processData) {
        console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
    },
    onProcessStateChanged(processData) {
        console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
561
    }
562
};
563 564
let bundleNameList = ['bundleName1', 'bundleName2'];
try {
565
    observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList);
566 567 568 569 570 571 572
    console.log(`[appManager] observerCode: ${observerId}`);
} catch (paramError) {
    console.log(`[appManager] error: ${paramError.code}, ${paramError.message} `);
}
    
// 2. Deregister the application state observer.
try {
573 574
    appManager.off('applicationState', observerId).then((data) => {
        console.log('unregisterApplicationStateObserver success, data: ${JSON.stringify(data)}');
575
    }).catch((err) => {
576
        console.log('unregisterApplicationStateObserver fail, err: ${JSON.stringify(err)}');
577
    });
578
} catch (paramError) {
579
    console.log('error: ${paramError.code}, ${paramError.message}');
580 581
}
```
582 583 584 585 586

## appManager.getForegroundApplications

getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void;

587
Obtains 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).
588 589 590 591 592 593 594

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

595
**Error codes**
596

597 598 599
| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |
600

601
For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
602 603 604 605 606

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
607
| 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.|
608 609 610

**Example**

611 612
```ts
import appManager from '@ohos.app.ability.appManager';
613

614
function getForegroundApplicationsCallback(err, data) {
615 616
    if (err && err.code !== 0) {
        console.log('getForegroundApplicationsCallback fail, err: ${JSON.stringify(err)}');
617
    } else {
618
        console.log('getForegroundApplicationsCallback success, data: ${JSON.stringify(data)}');
619
    }
620 621 622 623
}
try {
    appManager.getForegroundApplications(getForegroundApplicationsCallback);
} catch (paramError) {
624
    console.log('error: ${paramError.code}, ${paramError.message}');
625 626
}
```
627

628
## appManager.getForegroundApplications
629 630 631

getForegroundApplications(): Promise\<Array\<AppStateData>>;

632
Obtains 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).
633 634 635 636 637 638 639 640 641 642 643

**Required permissions**: ohos.permission.GET_RUNNING_INFO

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Return value**

| Type| Description|
| -------- | -------- |
644 645 646 647 648 649 650 651 652
| Promise\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Promise used to return an array holding the application state data|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
653 654

**Example**
655 656 657 658 659

```ts
import appManager from '@ohos.app.ability.appManager';

appManager.getForegroundApplications().then((data) => {
660
    console.log('getForegroundApplications success, data: ${JSON.stringify(data)}');
661
}).catch((err) => {
662
    console.log('getForegroundApplications fail, err: ${JSON.stringify(err)}');
663
});
664 665 666
```

## appManager.killProcessWithAccount
667 668 669 670 671

killProcessWithAccount(bundleName: string, accountId: number): Promise\<void\>

Kills a process by bundle name and account ID. This API uses a promise to return the result.

672
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user) and ohos.permission.CLEAN_BACKGROUND_PROCESSES
673 674 675 676 677 678 679

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

680 681 682 683 684 685 686 687 688 689 690 691
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| bundleName | string | Yes| Bundle name.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
692 693 694 695

**Example**

```ts
696 697 698 699 700 701
import appManager from '@ohos.app.ability.appManager';

let bundleName = 'bundleName';
let accountId = 0;
try {
    appManager.killProcessWithAccount(bundleName, accountId).then(() => {
702
        console.log('killProcessWithAccount success');
703
    }).catch((err) => {
704
        console.error('killProcessWithAccount fail, err: ${JSON.stringify(err)}');
705
    });
706
} catch (paramError) {
707
    console.error('error: ${paramError.code}, ${paramError.message}');
708
}
709 710 711
```


712
## appManager.killProcessWithAccount
713 714 715 716 717 718 719 720 721

killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCallback\<void\>): void

Kills a process by bundle name and account ID. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

722
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user) and ohos.permission.CLEAN_BACKGROUND_PROCESSES
723 724 725 726 727

**Parameters**

  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
728
  | bundleName | string | Yes| Bundle name.| 
729
  | accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 
730 731 732 733 734 735 736 737 738
  | callback | AsyncCallback\<void\> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.| 

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
739 740 741 742

**Example**

```ts
743 744 745 746
import appManager from '@ohos.app.ability.appManager';

let bundleName = 'bundleName';
let accountId = 0;
747
function killProcessWithAccountCallback(err, data) {
748 749
    if (err && err.code !== 0) {
        console.log('killProcessWithAccountCallback fail, err: ${JSON.stringify(err)}');
750
    } else {
751
        console.log('killProcessWithAccountCallback success.');
752
    }
753
}
754
appManager.killProcessWithAccount(bundleName, accountId, killProcessWithAccountCallback);
755 756
```

757
## appManager.killProcessesByBundleName
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

killProcessesByBundleName(bundleName: string, callback: AsyncCallback\<void>);

Kills a process by bundle name. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
773 774 775 776 777 778 779 780 781 782
| bundleName | string | Yes| Bundle name.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
783 784

**Example**
785 786 787 788 789 790

```ts
import appManager from '@ohos.app.ability.appManager';

let bundleName = 'bundleName';
function killProcessesByBundleNameCallback(err, data) {
791 792
    if (err && err.code !== 0) {
        console.log('killProcessesByBundleNameCallback fail, err: ${JSON.stringify(err)}');
793
    } else {
794
        console.log('killProcessesByBundleNameCallback success.');
795
    }
796 797 798 799
}
try {
    appManager.killProcessesByBundleName(bundleName, killProcessesByBundleNameCallback);
} catch (paramError) {
800
    console.log('error: ${paramError.code}, ${paramError.message}');
801 802
}
```
803

804
## appManager.killProcessesByBundleName
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

killProcessesByBundleName(bundleName: string): Promise\<void>;

Kills a process by bundle name. This API uses a promise to return the result.

**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
820
| bundleName | string | Yes| Bundle name.|
821 822 823 824 825 826 827

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

828 829 830 831 832 833 834 835
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

836
**Example**
837 838 839 840 841 842 843

```ts
import appManager from '@ohos.app.ability.appManager';

let bundleName = 'bundleName';
try {
    appManager.killProcessesByBundleName(bundleName).then((data) => {
844
        console.log('killProcessesByBundleName success.');
845
    }).catch((err) => {
846
        console.log('killProcessesByBundleName fail, err: ${JSON.stringify(err)}');
847
    });
848
} catch (paramError) {
849
    console.log('error: ${paramError.code}, ${paramError.message}');
850 851 852 853
}
```

## appManager.clearUpApplicationData
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868

clearUpApplicationData(bundleName: string, callback: AsyncCallback\<void>);

Clears application data by bundle name. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
869 870 871 872 873 874 875 876 877 878
| bundleName | string | Yes| Bundle name.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the API call result. You can perform error handling or custom processing in this callback.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
879 880

**Example**
881 882 883 884 885 886

```ts
import appManager from '@ohos.app.ability.appManager';

let bundleName = 'bundleName';
function clearUpApplicationDataCallback(err, data) {
887 888
    if (err && err.code !== 0) {
        console.log('clearUpApplicationDataCallback fail, err: ${JSON.stringify(err)}');
889
    } else {
890
        console.log('clearUpApplicationDataCallback success.');
891
    }
892 893 894 895
}
try {
    appManager.clearUpApplicationData(bundleName, clearUpApplicationDataCallback);
} catch (paramError) {
896
    console.log('error: ${paramError.code}, ${paramError.message}');
897 898
}
```
899

900
## appManager.clearUpApplicationData
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

clearUpApplicationData(bundleName: string): Promise\<void>;

Clears application data by bundle name. This API uses a promise to return the result.

**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
916
| bundleName | string | Yes| Bundle name.|
917 918 919 920 921

**Return value**

| Type| Description|
| -------- | -------- |
922 923 924 925 926 927 928 929 930
| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.|

**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000050 | Internal error. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
931 932

**Example**
933 934 935 936 937 938 939

```ts
import appManager from '@ohos.app.ability.appManager';

let bundleName = 'bundleName';
try {
    appManager.clearUpApplicationData(bundleName).then((data) => {
940
        console.log('clearUpApplicationData success.');
941
    }).catch((err) => {
942
        console.log('clearUpApplicationData fail, err: ${JSON.stringify(err)}');
943
    });
944
} catch (paramError) {
945
    console.log('error: ${paramError.code}, ${paramError.message}');
946 947
}
```
948

949 950 951
## ApplicationState

Enumerates the application states. This enum can be used together with [AbilityStateData](js-apis-inner-application-appStateData.md) to return the application state.
952 953 954 955 956 957 958 959 960 961 962 963 964

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

| Name                | Value | Description                              |
| -------------------- | --- | --------------------------------- |
| STATE_CREATE    | 1   |   State indicating that the application is being created.        |
| STATE_FOREGROUND          | 2   |      State indicating that the application is running in the foreground.           |
| STATE_ACTIVE  | 3   |         State indicating that the application is active.    |
| STATE_BACKGROUND        | 4   |       State indicating that the application is running in the background.          |
| STATE_DESTROY        | 5   |           State indicating that the application is destroyed.      |

965 966 967
## ProcessState

Enumerates the process states. This enum can be used together with [ProcessData](js-apis-inner-application-processData.md) to return the process state.
968 969 970 971 972 973 974 975 976 977 978 979

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

| Name                | Value | Description                              |
| -------------------- | --- | --------------------------------- |
| STATE_CREATE    | 1   |      State indicating that the process is being created.      |
| STATE_FOREGROUND          | 2   |            State indicating that the process is running in the foreground.     |
| STATE_ACTIVE  | 3   |          State indicating that the process is active.  |
| STATE_BACKGROUND        | 4   |       State indicating that the process is running in the background.          |
| STATE_DESTROY        | 5   |         State indicating that the process is destroyed.        |