• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.appManager (Application Management)
2<!--Kit: Ability Kit-->
3<!--Subsystem: Ability-->
4<!--Owner: @SKY2001-->
5<!--Designer: @yzkp-->
6<!--Tester: @lixueqing513-->
7<!--Adviser: @huipeizi-->
8
9The 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.
10
11> **NOTE**
12>
13> 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.
14
15## Modules to Import
16
17```ts
18import { appManager } from '@kit.AbilityKit';
19```
20
21## ProcessState<sup>10+</sup>
22
23Enumerates the processes states.
24
25**Atomic service API**: This API can be used in atomic services since API version 11.
26
27**System capability**: SystemCapability.Ability.AbilityRuntime.Core
28
29| Name                | Value | Description                              |
30| -------------------- | --- | --------------------------------- |
31| STATE_CREATE    | 0   |    The process is being created.      |
32| STATE_FOREGROUND          | 1   |    The process is running in the foreground.     |
33| STATE_ACTIVE  | 2   |     The process is active.  |
34| STATE_BACKGROUND        | 3   |    The process is running in the background.          |
35| STATE_DESTROY        | 4   |    The process is being destroyed.        |
36
37## appManager.isRunningInStabilityTest
38
39isRunningInStabilityTest(callback: AsyncCallback&lt;boolean&gt;): void
40
41Checks whether this application is undergoing a stability test. This API uses an asynchronous callback to return the result.
42
43**Atomic service API**: This API can be used in atomic services since API version 11.
44
45**System capability**: SystemCapability.Ability.AbilityRuntime.Core
46
47**Parameters**
48
49  | Name| Type| Mandatory| Description|
50  | -------- | -------- | -------- | -------- |
51  | callback | AsyncCallback&lt;boolean&gt; | 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. **true** if undergoing a stability test, **false** otherwise. |
52
53**Error codes**
54
55For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
56
57| ID| Error Message|
58| ------- | -------- |
59| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
60| 16000050 | Internal error. |
61
62**Example**
63
64```ts
65import { appManager } from '@kit.AbilityKit';
66
67appManager.isRunningInStabilityTest((err, flag) => {
68  if (err) {
69    console.error(`isRunningInStabilityTest fail, err: ${JSON.stringify(err)}`);
70  } else {
71    console.log(`The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}`);
72  }
73});
74```
75
76
77## appManager.isRunningInStabilityTest
78
79isRunningInStabilityTest(): Promise&lt;boolean&gt;
80
81Checks whether this application is undergoing a stability test. This API uses a promise to return the result.
82
83**Atomic service API**: This API can be used in atomic services since API version 11.
84
85**System capability**: SystemCapability.Ability.AbilityRuntime.Core
86
87**Return value**
88
89  | Type| Description|
90  | -------- | -------- |
91  | 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. **true** if undergoing a stability test, **false** otherwise.|
92
93**Error codes**
94
95For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
96
97| ID| Error Message|
98| ------- | -------- |
99| 16000050 | Internal error. |
100
101**Example**
102
103```ts
104import { appManager } from '@kit.AbilityKit';
105import { BusinessError } from '@kit.BasicServicesKit';
106
107appManager.isRunningInStabilityTest().then((flag) => {
108  console.log(`The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}`);
109}).catch((error: BusinessError) => {
110  console.error(`error: ${JSON.stringify(error)}`);
111});
112```
113
114
115## appManager.isRamConstrainedDevice
116
117isRamConstrainedDevice(): Promise\<boolean>
118
119Checks whether this application is running on a RAM constrained device. This API uses a promise to return the result.
120
121**Atomic service API**: This API can be used in atomic services since API version 11.
122
123**System capability**: SystemCapability.Ability.AbilityRuntime.Core
124
125**Return value**
126
127  | Type| Description|
128  | -------- | -------- |
129  | 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. **true** if running on a RAM constrained device, **false** otherwise.|
130
131**Error codes**
132
133For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
134
135| ID| Error Message|
136| ------- | -------- |
137| 16000050 | Internal error. |
138
139**Example**
140
141```ts
142import { appManager } from '@kit.AbilityKit';
143import { BusinessError } from '@kit.BasicServicesKit';
144
145appManager.isRamConstrainedDevice().then((data) => {
146  console.log(`The result of isRamConstrainedDevice is: ${JSON.stringify(data)}`);
147}).catch((error: BusinessError) => {
148  console.error(`error: ${JSON.stringify(error)}`);
149});
150```
151
152## appManager.isRamConstrainedDevice
153
154isRamConstrainedDevice(callback: AsyncCallback\<boolean>): void
155
156Checks whether this application is running on a RAM constrained device. This API uses an asynchronous callback to return the result.
157
158**Atomic service API**: This API can be used in atomic services since API version 11.
159
160**System capability**: SystemCapability.Ability.AbilityRuntime.Core
161
162**Parameters**
163
164  | Name| Type| Mandatory| Description|
165  | -------- | -------- | -------- | -------- |
166  | callback | AsyncCallback&lt;boolean&gt; | 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. **true** if running on a RAM constrained device, **false** otherwise. |
167
168**Error codes**
169
170For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
171
172| ID| Error Message|
173| ------- | -------- |
174| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
175| 16000050 | Internal error. |
176
177**Example**
178
179```ts
180import { appManager } from '@kit.AbilityKit';
181
182appManager.isRamConstrainedDevice((err, data) => {
183  if (err) {
184    console.error(`isRamConstrainedDevice fail, err: ${JSON.stringify(err)}`);
185  } else {
186    console.log(`The result of isRamConstrainedDevice is: ${JSON.stringify(data)}`);
187  }
188});
189```
190
191## appManager.getAppMemorySize
192
193getAppMemorySize(): Promise\<number>
194
195Obtains the memory size of this application. This API uses a promise to return the result.
196
197**Atomic service API**: This API can be used in atomic services since API version 11.
198
199**System capability**: SystemCapability.Ability.AbilityRuntime.Core
200
201**Return value**
202
203  | Type| Description|
204  | -------- | -------- |
205  | Promise&lt;number&gt; | Promise used to return the memory size, in MB. You can perform error processing or other custom processing based on the size.  |
206
207**Error codes**
208
209For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
210
211| ID| Error Message|
212| ------- | -------- |
213| 16000050 | Internal error. |
214
215**Example**
216
217```ts
218import { appManager } from '@kit.AbilityKit';
219import { BusinessError } from '@kit.BasicServicesKit';
220
221appManager.getAppMemorySize().then((data) => {
222  console.log(`The size of app memory is: ${JSON.stringify(data)}`);
223}).catch((error: BusinessError) => {
224  console.error(`error: ${JSON.stringify(error)}`);
225});
226```
227
228## appManager.getAppMemorySize
229
230getAppMemorySize(callback: AsyncCallback\<number>): void
231
232Obtains the memory size of this application. This API uses an asynchronous callback to return the result.
233
234**Atomic service API**: This API can be used in atomic services since API version 11.
235
236**System capability**: SystemCapability.Ability.AbilityRuntime.Core
237
238**Parameters**
239
240  | Name| Type| Mandatory| Description|
241  | -------- | -------- | -------- | -------- |
242  | callback | AsyncCallback&lt;number&gt; | Yes|Callback used to return the memory size, in MB. You can perform error processing or other custom processing based on the size.  |
243
244**Error codes**
245
246For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
247
248| ID| Error Message|
249| ------- | -------- |
250| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
251| 16000050 | Internal error. |
252
253**Example**
254
255```ts
256import { appManager } from '@kit.AbilityKit';
257
258appManager.getAppMemorySize((err, data) => {
259  if (err) {
260    console.error(`getAppMemorySize fail, err: ${JSON.stringify(err)}`);
261  } else {
262    console.log(`The size of app memory is: ${JSON.stringify(data)}`);
263  }
264});
265```
266
267## appManager.getRunningProcessInformation
268
269getRunningProcessInformation(): Promise\<Array\<ProcessInformation>>
270
271Obtains information about the running processes of this application. This API uses a promise to return the result.
272
273> **NOTE**
274>
275> - In versions earlier than API version 11, this API requires the ohos.permission.GET_RUNNING_INFO permission, which is available only for system applications.
276> - Starting from API version 11, this API is used only to obtain the process information of the caller. No permission is required.
277
278**Atomic service API**: This API can be used in atomic services since API version 11.
279
280**System capability**: SystemCapability.Ability.AbilityRuntime.Core
281
282**Return value**
283
284| Type| Description|
285| -------- | -------- |
286| 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.|
287
288**Error codes**
289
290For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
291
292| ID| Error Message|
293| ------- | -------- |
294| 16000050 | Internal error. |
295
296**Example**
297
298```ts
299import { appManager } from '@kit.AbilityKit';
300import { BusinessError } from '@kit.BasicServicesKit';
301
302appManager.getRunningProcessInformation().then((data) => {
303  console.log(`The running process information is: ${JSON.stringify(data)}`);
304}).catch((error: BusinessError) => {
305  console.error(`error: ${JSON.stringify(error)}`);
306});
307```
308
309## appManager.getRunningProcessInformation
310
311getRunningProcessInformation(callback: AsyncCallback\<Array\<ProcessInformation>>): void
312
313Obtains information about the running processes of this application. This API uses an asynchronous callback to return the result.
314
315> **NOTE**
316>
317> - In versions earlier than API version 11, this API requires the ohos.permission.GET_RUNNING_INFO permission, which is available only for system applications.
318> - Starting from API version 11, this API is used only to obtain the process information of the caller. No permission is required.
319
320**Atomic service API**: This API can be used in atomic services since API version 11.
321
322**System capability**: SystemCapability.Ability.AbilityRuntime.Core
323
324**Parameters**
325
326  | Name| Type| Mandatory| Description|
327  | -------- | -------- | -------- | -------- |
328  | 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.|
329
330**Error codes**
331
332For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
333
334| ID| Error Message|
335| ------- | -------- |
336| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
337| 16000050 | Internal error. |
338
339**Example**
340
341```ts
342import { appManager } from '@kit.AbilityKit';
343
344appManager.getRunningProcessInformation((err, data) => {
345  if (err) {
346    console.error(`getRunningProcessInformation fail, err: ${JSON.stringify(err)}`);
347  } else {
348    console.log(`The running process information is: ${JSON.stringify(data)}`);
349  }
350});
351```
352
353## appManager.on('applicationState')<sup>14+</sup>
354
355on(type: 'applicationState', observer: ApplicationStateObserver): number
356
357Registers an observer to listen for the state changes of all applications.
358
359**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
360
361**System capability**: SystemCapability.Ability.AbilityRuntime.Core
362
363**Parameters**
364
365| Name| Type| Mandatory| Description|
366| -------- | -------- | -------- | -------- |
367| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
368| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.|
369
370**Return value**
371
372| Type| Description|
373| --- | --- |
374| number | Digital code of the observer, which will be used in **off()** to deregister the observer.|
375
376**Error codes**
377
378For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
379
380| ID| Error Message|
381| ------- | -------- |
382| 201 | Permission denied. |
383| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
384| 16000050 | Internal error. |
385
386**Example**
387
388```ts
389import { appManager } from '@kit.AbilityKit';
390import { BusinessError } from '@kit.BasicServicesKit';
391
392let applicationStateObserver: appManager.ApplicationStateObserver = {
393  onForegroundApplicationChanged(appStateData) {
394    console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
395  },
396  onAbilityStateChanged(abilityStateData) {
397    console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
398  },
399  onProcessCreated(processData) {
400    console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
401  },
402  onProcessDied(processData) {
403    console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
404  },
405  onProcessStateChanged(processData) {
406    console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
407  },
408  onAppStarted(appStateData) {
409    console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`);
410  },
411  onAppStopped(appStateData) {
412    console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`);
413  }
414};
415
416try {
417  const observerId = appManager.on('applicationState', applicationStateObserver);
418  console.log(`[appManager] observerCode: ${observerId}`);
419} catch (paramError) {
420  let code = (paramError as BusinessError).code;
421  let message = (paramError as BusinessError).message;
422  console.error(`[appManager] error: ${code}, ${message}`);
423}
424```
425
426## appManager.on('applicationState')<sup>14+</sup>
427
428on(type: 'applicationState', observer: ApplicationStateObserver, bundleNameList: Array\<string>): number
429
430Registers an observer to listen for the state changes of a specified application.
431
432**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
433
434**System capability**: SystemCapability.Ability.AbilityRuntime.Core
435
436**Parameters**
437
438| Name| Type| Mandatory| Description|
439| -------- | -------- | -------- | -------- |
440| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
441| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver.md) | Yes| Application state observer, which is used to observe the lifecycle change of an application.|
442| bundleNameList | `Array<string>` | Yes| **bundleName** array of the application. A maximum of 128 bundle names can be passed.|
443
444**Return value**
445
446| Type| Description|
447| --- | --- |
448| number | Digital code of the observer, which will be used in **off()** to deregister the observer.|
449
450**Error codes**
451
452For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
453
454| ID| Error Message|
455| ------- | -------- |
456| 201 | Permission denied. |
457| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
458| 16000050 | Internal error. |
459
460**Example**
461
462```ts
463import { appManager } from '@kit.AbilityKit';
464import { BusinessError } from '@kit.BasicServicesKit';
465
466let applicationStateObserver: appManager.ApplicationStateObserver = {
467  onForegroundApplicationChanged(appStateData) {
468    console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
469  },
470  onAbilityStateChanged(abilityStateData) {
471    console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
472  },
473  onProcessCreated(processData) {
474    console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
475  },
476  onProcessDied(processData) {
477    console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
478  },
479  onProcessStateChanged(processData) {
480    console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
481  },
482  onAppStarted(appStateData) {
483    console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`);
484  },
485  onAppStopped(appStateData) {
486    console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`);
487  }
488};
489
490let bundleNameList = ['bundleName1', 'bundleName2'];
491
492try {
493  const observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList);
494  console.log(`[appManager] observerCode: ${observerId}`);
495} catch (paramError) {
496  let code = (paramError as BusinessError).code;
497  let message = (paramError as BusinessError).message;
498  console.error(`[appManager] error: ${code}, ${message}`);
499}
500```
501
502## appManager.off('applicationState')<sup>14+</sup>
503
504off(type: 'applicationState', observerId: number): Promise\<void>
505
506Deregisters the application state observer. This API uses a promise to return the result.
507
508**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
509
510**System capability**: SystemCapability.Ability.AbilityRuntime.Core
511
512**Parameters**
513
514| Name| Type| Mandatory| Description|
515| -------- | -------- | -------- | -------- |
516| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
517| observerId | number | Yes| Digital code of the observer.|
518
519**Return value**
520
521| Type| Description|
522| -------- | -------- |
523| Promise\<void> | Promise used to return the API call result. You can perform error handling or custom processing in this callback.|
524
525**Error codes**
526
527For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
528
529| ID| Error Message|
530| ------- | -------- |
531| 201 | Permission denied. |
532| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
533| 16000050 | Internal error. |
534
535**Example**
536
537```ts
538import { appManager } from '@kit.AbilityKit';
539import { BusinessError } from '@kit.BasicServicesKit';
540
541let observerId = 0;
542
543// 1. Register an application state observer.
544let applicationStateObserver: appManager.ApplicationStateObserver = {
545  onForegroundApplicationChanged(appStateData) {
546    console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
547  },
548  onAbilityStateChanged(abilityStateData) {
549    console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
550  },
551  onProcessCreated(processData) {
552    console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
553  },
554  onProcessDied(processData) {
555    console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
556  },
557  onProcessStateChanged(processData) {
558    console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
559  },
560  onAppStarted(appStateData) {
561    console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`);
562  },
563  onAppStopped(appStateData) {
564    console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`);
565  }
566};
567let bundleNameList = ['bundleName1', 'bundleName2'];
568
569try {
570  observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList);
571} catch (paramError) {
572  let code = (paramError as BusinessError).code;
573  let message = (paramError as BusinessError).message;
574  console.error(`[appManager] error: ${code}, ${message}`);
575}
576
577// 2. Deregister the application state observer.
578try {
579  appManager.off('applicationState', observerId).then((data) => {
580    console.log(`unregisterApplicationStateObserver success, data: ${JSON.stringify(data)}`);
581  }).catch((err: BusinessError) => {
582    console.error(`unregisterApplicationStateObserver fail, err: ${JSON.stringify(err)}`);
583  });
584} catch (paramError) {
585  let code = (paramError as BusinessError).code;
586  let message = (paramError as BusinessError).message;
587  console.error(`[appManager] error: ${code}, ${message}`);
588}
589```
590
591## appManager.off('applicationState')<sup>15+</sup>
592
593off(type: 'applicationState', observerId: number, callback: AsyncCallback\<void>): void
594
595Deregisters the application state observer. This API uses an asynchronous callback to return the result.
596
597**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
598
599**System capability**: SystemCapability.Ability.AbilityRuntime.Core
600
601**Parameters**
602
603| Name| Type| Mandatory| Description|
604| -------- | -------- | -------- | -------- |
605| type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.|
606| observerId | number | Yes| Digital code of the observer.|
607| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the application state observer is deregistered, **err** is undefined; otherwise, **error** is an error object.|
608
609**Error codes**
610
611For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
612
613| ID| Error Message|
614| ------- | -------- |
615| 201 | Permission denied. |
616| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
617| 16000050 | Internal error. |
618
619**Example**
620
621```ts
622import { appManager } from '@kit.AbilityKit';
623import { BusinessError } from '@kit.BasicServicesKit';
624
625let observerId = 0;
626
627// 1. Register an application state observer.
628let applicationStateObserver: appManager.ApplicationStateObserver = {
629  onForegroundApplicationChanged(appStateData) {
630    console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
631  },
632  onAbilityStateChanged(abilityStateData) {
633    console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
634  },
635  onProcessCreated(processData) {
636    console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
637  },
638  onProcessDied(processData) {
639    console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
640  },
641  onProcessStateChanged(processData) {
642    console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
643  },
644  onAppStarted(appStateData) {
645    console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`);
646  },
647  onAppStopped(appStateData) {
648    console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`);
649  }
650};
651let bundleNameList = ['bundleName1', 'bundleName2'];
652
653try {
654  observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList);
655} catch (paramError) {
656  let code = (paramError as BusinessError).code;
657  let message = (paramError as BusinessError).message;
658  console.error(`[appManager] error: ${code}, ${message}`);
659}
660
661function offCallback(err: BusinessError) {
662  if (err) {
663    console.error(`appmanager.off failed, code: ${err.code}, msg: ${err.message}`);
664  } else {
665    console.info(`appmanager.off success.`);
666  }
667}
668
669// 2. Deregister the application state observer.
670try {
671  appManager.off('applicationState', observerId, offCallback);
672} catch (paramError) {
673  let code = (paramError as BusinessError).code;
674  let message = (paramError as BusinessError).message;
675  console.error(`[appManager] error: ${code}, ${message}`);
676}
677```
678
679## appManager.killProcessesByBundleName<sup>14+</sup>
680
681killProcessesByBundleName(bundleName: string, clearPageStack: boolean, appIndex?: number): Promise\<void>
682
683Kills a process by bundle name. This API uses an asynchronous callback to return the result. This API uses a promise to return the result.
684
685**Required permissions**: ohos.permission.KILL_APP_PROCESSES or ohos.permission.CLEAN_BACKGROUND_PROCESSES
686
687**System capability**: SystemCapability.Ability.AbilityRuntime.Core
688
689**Parameters**
690
691| Name| Type| Mandatory| Description|
692| -------- | -------- | -------- | -------- |
693| bundleName | string | Yes| Bundle name.|
694| clearPageStack | boolean | Yes| Whether to clear the page stack. **true** to clear, **false** otherwise.|
695| appIndex | number | No| ID of an application clone. The default value is **0**. If the value is **0**, all processes of the main application are terminated. If the value is greater than 0, all processes of the specified application clone are terminated.|
696
697**Return value**
698
699| Type| Description|
700| -------- | -------- |
701| Promise\<void> | Promise that returns no value.|
702
703**Error codes**
704
705For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
706
707| ID| Error Message|
708| ------- | -------- |
709| 201 | Permission denied. |
710| 401 | If the input parameter is not valid parameter. |
711| 16000050 | Internal error. |
712
713**Example**
714
715```ts
716import { appManager } from '@kit.AbilityKit';
717import { BusinessError } from '@kit.BasicServicesKit';
718
719let bundleName = 'bundleName';
720let isClearPageStack = false;
721let appIndex = 1;
722
723try {
724  appManager.killProcessesByBundleName(bundleName, isClearPageStack, appIndex).then((data) => {
725    console.log('killProcessesByBundleName success.');
726  }).catch((err: BusinessError) => {
727    console.error(`killProcessesByBundleName fail, err: ${JSON.stringify(err)}`);
728  });
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
736## appManager.isAppRunning<sup>14+</sup>
737
738isAppRunning(bundleName: string, appCloneIndex?: number): Promise\<boolean>
739
740Checks whether an application is running. This API uses a promise to return the result.
741
742**Required permissions**: ohos.permission.GET_RUNNING_INFO
743
744**System capability**: SystemCapability.Ability.AbilityRuntime.Core
745
746**Parameters**
747
748| Name| Type| Mandatory| Description|
749| -------- | -------- | -------- | -------- |
750| bundleName | string | Yes| Bundle name.|
751| appCloneIndex | number | No| Index of an application clone.|
752
753**Return value**
754
755| Type          | Description             |
756| -------------- | ---------------- |
757| Promise\<boolean> | Promise used to return the result. **true** if running, **false** otherwise.|
758
759**Error codes**
760
761  For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
762
763| ID| Error Message|
764| ------- | -------- |
765| 201 | Permission denied. |
766| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. |
767| 16000050 | Internal error. |
768| 16000073 | The app clone index is invalid. |
769
770**Example**
771
772```ts
773import { appManager } from '@kit.AbilityKit';
774import { hilog } from '@kit.PerformanceAnalysisKit';
775import { BusinessError } from '@kit.BasicServicesKit';
776
777try {
778  let bundleName = "ohos.samples.etsclock";
779  appManager.isAppRunning(bundleName).then((data: boolean) => {
780      hilog.info(0x0000, 'testTag', `data: ${JSON.stringify(data)}`);
781    }).catch((err: BusinessError) => {
782      hilog.error(0x0000, 'testTag', `isAppRunning error, code: ${err.code}, msg:${err.message}`);
783    })
784} catch (err) {
785  hilog.error(0x0000, 'testTag', `isAppRunning error, code: ${err.code}, msg:${err.message}`);
786}
787```
788
789## AbilityStateData<sup>14+</sup>
790
791type AbilityStateData = _AbilityStateData.default
792
793Defines the ability state data.
794
795**System capability**: SystemCapability.Ability.AbilityRuntime.Core
796
797| Type| Description|
798| --- | --- |
799| [_AbilityStateData.default](js-apis-inner-application-abilityStateData.md) | Ability state data.|
800
801## AppStateData<sup>14+</sup>
802
803type AppStateData = _AppStateData.default
804
805Defines the application state data.
806
807**System capability**: SystemCapability.Ability.AbilityRuntime.Core
808
809| Type| Description|
810| --- | --- |
811| [_AppStateData.default](js-apis-inner-application-appStateData.md) | Application state data.|
812
813## ApplicationStateObserver<sup>14+</sup>
814
815type ApplicationStateObserver = _ApplicationStateObserver.default
816
817Defines the ApplicationStateObserver module.
818
819**System capability**: SystemCapability.Ability.AbilityRuntime.Core
820
821| Type| Description|
822| --- | --- |
823| [_ApplicationStateObserver.default](js-apis-inner-application-applicationStateObserver.md) | ApplicationStateObserver module.|
824
825## ProcessInformation
826
827type ProcessInformation = _ProcessInformation
828
829Defines the ProcessInformation module.
830
831**Atomic service API**: This API can be used in atomic services since API version 11.
832
833**System capability**: SystemCapability.Ability.AbilityRuntime.Core
834
835| Type| Description|
836| --- | --- |
837| [_ProcessInformation](js-apis-inner-application-processInformation.md) | ProcessInformation module.|
838
839## ProcessData<sup>14+</sup>
840
841type ProcessData = _ProcessData.default
842
843Defines the process data.
844
845**System capability**: SystemCapability.Ability.AbilityRuntime.Core
846
847| Type| Description|
848| --- | --- |
849| [_ProcessData.default](js-apis-inner-application-processData.md) | Process data.|
850