• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 7 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<sup>8+</sup>
16
17static isRunningInStabilityTest(callback: AsyncCallback&lt;boolean&gt;): 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&lt;boolean&gt; | Yes| Callback used to return the result. If the application is undergoing a stability test, **true** will be returned; otherwise, **false** will be returned.|
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<sup>8+</sup>
45
46static isRunningInStabilityTest(): Promise&lt;boolean&gt;
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&lt;boolean&gt; | Promise used to return the result. If the application is undergoing a stability test, **true** will be returned; otherwise, **false** will be returned.|
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&lt;boolean&gt; | Promise used to return whether the application is running on a RAM constrained device. If the application is running on a RAM constrained device, **true** will be returned; otherwise, **false** will be returned.|
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&lt;boolean&gt; | Yes| Callback used to return whether the application is running on a RAM constrained device. If the application is running on a RAM constrained device, **true** will be returned; otherwise, **false** will be returned.|
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&lt;number&gt; | 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&lt;number&gt; | 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<sup>9+</sup>](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 process information.|
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<sup>9+</sup>](js-apis-app-ability-appManager.md#appmanagergetrunningprocessinformation9) 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| Obtains information about the running processes. This API uses a promise to return the result.|
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<sup>8+</sup>
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 and cannot be called by third-party applications.
255
256**Parameters**
257
258| Name| Type| Mandatory| Description|
259| -------- | -------- | -------- | -------- |
260| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver.md) | Yes| Numeric code of the observer.|
261
262**Example**
263
264  ```ts
265  import appManager from '@ohos.application.appManager';
266
267  const observerCode = appManager.registerApplicationStateObserver({
268    onForegroundApplicationChanged(appStateData) {
269        console.log('------------ onForegroundApplicationChanged -----------', appStateData);
270    },
271    onAbilityStateChanged(abilityStateData) {
272        console.log('------------ onAbilityStateChanged -----------', abilityStateData);
273    },
274    onProcessCreated(processData) {
275        console.log('------------ onProcessCreated -----------', processData);
276    },
277    onProcessDied(processData) {
278        console.log('------------ onProcessDied -----------', processData);
279    },
280    onProcessStateChanged(processData) {
281        console.log('------------ onProcessStateChanged -----------', processData);
282    }
283  });
284  console.log('-------- observerCode: ---------', observerCode);
285  ```
286
287## appManager.unregisterApplicationStateObserver<sup>8+</sup>
288
289unregisterApplicationStateObserver(observerId: number,  callback: AsyncCallback\<void>): void;
290
291Deregisters the application state observer. This API uses an asynchronous callback to return the result.
292
293**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
294
295**System capability**: SystemCapability.Ability.AbilityRuntime.Core
296
297**System API**: This is a system API and cannot be called by third-party applications.
298
299**Parameters**
300
301| Name| Type| Mandatory| Description|
302| -------- | -------- | -------- | -------- |
303| observerId | number | Yes| Numeric code of the observer.|
304| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
305
306**Example**
307
308  ```ts
309  import appManager from '@ohos.application.appManager';
310  import { BusinessError } from '@ohos.base';
311
312  let observerId = 100;
313
314  function unregisterApplicationStateObserverCallback(err: BusinessError) {
315    if (err) {
316        console.error('------------ unregisterApplicationStateObserverCallback ------------', err);
317    }
318  }
319  appManager.unregisterApplicationStateObserver(observerId, unregisterApplicationStateObserverCallback);
320  ```
321
322## appManager.unregisterApplicationStateObserver<sup>8+</sup>
323
324unregisterApplicationStateObserver(observerId: number): Promise\<void>;
325
326Deregisters the application state observer. This API uses a promise to return the result.
327
328**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER
329
330**System capability**: SystemCapability.Ability.AbilityRuntime.Core
331
332**System API**: This is a system API and cannot be called by third-party applications.
333
334**Parameters**
335
336| Name| Type| Mandatory| Description|
337| -------- | -------- | -------- | -------- |
338| observerId | number | Yes| Numeric code of the observer.|
339
340**Return value**
341
342| Type| Description|
343| -------- | -------- |
344| Promise\<void> | Promise used to return the result.|
345
346**Example**
347
348  ```ts
349  import appManager from '@ohos.application.appManager';
350  import { BusinessError } from '@ohos.base';
351
352  let observerId = 100;
353
354  appManager.unregisterApplicationStateObserver(observerId)
355  .then((data) => {
356      console.log('----------- unregisterApplicationStateObserver success ----------', data);
357  })
358  .catch((err: BusinessError) => {
359      console.error('----------- unregisterApplicationStateObserver fail ----------', err);
360  });
361  ```
362
363## appManager.getForegroundApplications<sup>8+</sup>
364
365getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void;
366
367Obtains information about the 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).
368
369**Required permissions**: ohos.permission.GET_RUNNING_INFO
370
371**System capability**: SystemCapability.Ability.AbilityRuntime.Core
372
373**System API**: This is a system API and cannot be called by third-party applications.
374
375**Parameters**
376
377| Name| Type| Mandatory| Description|
378| -------- | -------- | -------- | -------- |
379| callback | AsyncCallback\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Yes| Callback used to return the application information.|
380
381**Example**
382
383  ```ts
384  import appManager from '@ohos.application.appManager';
385
386  appManager.getForegroundApplications((err, data) => {
387    if (err) {
388        console.error('--------- getForegroundApplicationsCallback fail ---------', err);
389    } else {
390        console.log('--------- getForegroundApplicationsCallback success ---------', data);
391    }
392  });
393  ```
394
395## appManager.getForegroundApplications<sup>8+</sup>
396
397getForegroundApplications(): Promise\<Array\<AppStateData>>;
398
399Obtains information about the 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).
400
401**Required permissions**: ohos.permission.GET_RUNNING_INFO
402
403**System capability**: SystemCapability.Ability.AbilityRuntime.Core
404
405**System API**: This is a system API and cannot be called by third-party applications.
406
407**Return value**
408
409| Type| Description|
410| -------- | -------- |
411| Promise\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Promise used to return the application information.|
412
413**Example**
414
415  ```ts
416  import appManager from '@ohos.application.appManager';
417  import { BusinessError } from '@ohos.base';
418
419  appManager.getForegroundApplications()
420  .then((data) => {
421      console.log('--------- getForegroundApplications success -------', data);
422  })
423  .catch((err: BusinessError) => {
424      console.error('--------- getForegroundApplications fail -------', err);
425  });
426  ```
427
428## appManager.killProcessWithAccount<sup>8+</sup>
429
430killProcessWithAccount(bundleName: string, accountId: number): Promise\<void\>
431
432Kills a process by bundle name and account ID. This API uses a promise to return the result.
433
434> **NOTE**
435>
436> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.
437
438**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.CLEAN_BACKGROUND_PROCESSES
439
440**System capability**: SystemCapability.Ability.AbilityRuntime.Core
441
442**System API**: This is a system API and cannot be called by third-party applications.
443
444**Parameters**
445
446| Name| Type| Mandatory| Description|
447| -------- | -------- | -------- | -------- |
448| bundleName | string | Yes| Bundle name.|
449| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
450
451**Example**
452
453```ts
454import appManager from '@ohos.application.appManager';
455import { BusinessError } from '@ohos.base';
456
457let bundleName = 'bundleName';
458let accountId = 0;
459appManager.killProcessWithAccount(bundleName, accountId)
460   .then((data) => {
461       console.log('------------ killProcessWithAccount success ------------', data);
462   })
463   .catch((err: BusinessError) => {
464       console.error('------------ killProcessWithAccount fail ------------', err);
465   });
466```
467
468
469## appManager.killProcessWithAccount<sup>8+</sup>
470
471killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCallback\<void\>): void
472
473Kills a process by bundle name and account ID. This API uses an asynchronous callback to return the result.
474
475> **NOTE**
476>
477> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.
478
479**System capability**: SystemCapability.Ability.AbilityRuntime.Core
480
481**System API**: This is a system API and cannot be called by third-party applications.
482
483**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.CLEAN_BACKGROUND_PROCESSES
484
485**Parameters**
486
487| Name| Type| Mandatory| Description|
488| -------- | -------- | -------- | -------- |
489| bundleName | string | Yes| Bundle name.|
490| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
491| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
492
493**Example**
494
495```ts
496import appManager from '@ohos.application.appManager';
497import { BusinessError } from '@ohos.base';
498
499let bundleName = 'bundleName';
500let accountId = 0;
501function killProcessWithAccountCallback(err: BusinessError, data: void) {
502   if (err) {
503       console.error('------------- killProcessWithAccountCallback fail, err: --------------', err);
504   } else {
505       console.log('------------- killProcessWithAccountCallback success, data: --------------', data);
506   }
507}
508appManager.killProcessWithAccount(bundleName, accountId, killProcessWithAccountCallback);
509```
510
511## appManager.killProcessesByBundleName<sup>8+</sup>
512
513killProcessesByBundleName(bundleName: string, callback: AsyncCallback\<void>);
514
515Kills a process by bundle name. This API uses an asynchronous callback to return the result.
516
517**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES
518
519**System capability**: SystemCapability.Ability.AbilityRuntime.Core
520
521**System API**: This is a system API and cannot be called by third-party applications.
522
523**Parameters**
524
525| Name| Type| Mandatory| Description|
526| -------- | -------- | -------- | -------- |
527| bundleName | string | Yes| Bundle name.|
528| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
529
530**Example**
531
532  ```ts
533  import appManager from '@ohos.application.appManager';
534import { BusinessError } from '@ohos.base';
535
536  let bundleName = 'bundleName';
537  function killProcessesByBundleNameCallback(err: BusinessError, data: void) {
538    if (err) {
539        console.error('------------- killProcessesByBundleNameCallback fail, err: --------------', err);
540    } else {
541        console.log('------------- killProcessesByBundleNameCallback success, data: --------------', data);
542    }
543  }
544  appManager.killProcessesByBundleName(bundleName, killProcessesByBundleNameCallback);
545  ```
546
547## appManager.killProcessesByBundleName<sup>8+</sup>
548
549killProcessesByBundleName(bundleName: string): Promise\<void>;
550
551Kills a process by bundle name. This API uses a promise to return the result.
552
553**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES
554
555**System capability**: SystemCapability.Ability.AbilityRuntime.Core
556
557**System API**: This is a system API and cannot be called by third-party applications.
558
559**Parameters**
560
561| Name| Type| Mandatory| Description|
562| -------- | -------- | -------- | -------- |
563| bundleName | string | Yes| Bundle name.|
564
565**Return value**
566
567| Type| Description|
568| -------- | -------- |
569| Promise\<void> | Promise used to return the result.|
570
571**Example**
572
573  ```ts
574  import appManager from '@ohos.application.appManager';
575  import { BusinessError } from '@ohos.base';
576
577  let bundleName = 'com.example.myapplication';
578  appManager.killProcessesByBundleName(bundleName)
579    .then((data) => {
580        console.log('------------ killProcessesByBundleName success ------------', data);
581    })
582    .catch((err: BusinessError) => {
583        console.error('------------ killProcessesByBundleName fail ------------', err);
584    });
585  ```
586
587## appManager.clearUpApplicationData<sup>8+</sup>
588
589clearUpApplicationData(bundleName: string, callback: AsyncCallback\<void>);
590
591Clears application data by bundle name. This API uses an asynchronous callback to return the result.
592
593**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA
594
595**System capability**: SystemCapability.Ability.AbilityRuntime.Core
596
597**System API**: This is a system API and cannot be called by third-party applications.
598
599**Parameters**
600
601| Name| Type| Mandatory| Description|
602| -------- | -------- | -------- | -------- |
603| bundleName | string | Yes| Bundle name.|
604| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
605
606**Example**
607
608  ```ts
609  import appManager from '@ohos.application.appManager';
610  import { BusinessError } from '@ohos.base';
611
612  let bundleName = 'bundleName';
613  function clearUpApplicationDataCallback(err: BusinessError, data: void) {
614    if (err) {
615        console.error('------------- clearUpApplicationDataCallback fail, err: --------------', err);
616    } else {
617        console.log('------------- clearUpApplicationDataCallback success, data: --------------', data);
618    }
619  }
620  appManager.clearUpApplicationData(bundleName, clearUpApplicationDataCallback);
621  ```
622
623## appManager.clearUpApplicationData<sup>8+</sup>
624
625clearUpApplicationData(bundleName: string): Promise\<void>;
626
627Clears application data by bundle name. This API uses a promise to return the result.
628
629**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA
630
631**System capability**: SystemCapability.Ability.AbilityRuntime.Core
632
633**System API**: This is a system API and cannot be called by third-party applications.
634
635**Parameters**
636
637| Name| Type| Mandatory| Description|
638| -------- | -------- | -------- | -------- |
639| bundleName | string | Yes| Bundle name.|
640
641**Return value**
642
643| Type| Description|
644| -------- | -------- |
645| Promise\<void> | Promise used to return the result.|
646
647**Example**
648
649  ```ts
650  import appManager from '@ohos.application.appManager';
651  import { BusinessError } from '@ohos.base';
652
653  let bundleName = 'bundleName';
654  appManager.clearUpApplicationData(bundleName)
655    .then((data) => {
656        console.log('------------ clearUpApplicationData success ------------', data);
657    })
658    .catch((err: BusinessError) => {
659        console.error('------------ clearUpApplicationData fail ------------', err);
660    });
661  ```
662