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