# @ohos.app.ability.appManager (appManager) 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 '@kit.AbilityKit'; ``` ## ProcessState10+ Enumerates the processes states. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core | Name | Value | Description | | -------------------- | --- | --------------------------------- | | STATE_CREATE | 0 | The process is being created. | | STATE_FOREGROUND | 1 | The process is running in the foreground. | | STATE_ACTIVE | 2 | The process is active. | | STATE_BACKGROUND | 3 | The process is running in the background. | | STATE_DESTROY | 4 | The process is being destroyed. | ## appManager.isRunningInStabilityTest isRunningInStabilityTest(callback: AsyncCallback<boolean>): void Checks whether this application is undergoing a stability test. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callback | AsyncCallback<boolean> | 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. The value **true** means that the application is undergoing a stability test, and **false** means the opposite. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; appManager.isRunningInStabilityTest((err, flag) => { if (err) { console.error(`isRunningInStabilityTest fail, err: ${JSON.stringify(err)}`); } else { console.log(`The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}`); } }); ``` ## appManager.isRunningInStabilityTest isRunningInStabilityTest(): Promise<boolean> Checks whether this application is undergoing a stability test. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | 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** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; appManager.isRunningInStabilityTest().then((flag) => { console.log(`The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}`); }).catch((error: BusinessError) => { console.error(`error: ${JSON.stringify(error)}`); }); ``` ## appManager.isRamConstrainedDevice isRamConstrainedDevice(): Promise\ Checks whether this application is running on a RAM constrained device. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | 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 running on a RAM constrained device, and **false** means the opposite.| **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; appManager.isRamConstrainedDevice().then((data) => { console.log(`The result of isRamConstrainedDevice is: ${JSON.stringify(data)}`); }).catch((error: BusinessError) => { console.error(`error: ${JSON.stringify(error)}`); }); ``` ## appManager.isRamConstrainedDevice isRamConstrainedDevice(callback: AsyncCallback\): void Checks whether this application is running on a RAM constrained device. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callback | AsyncCallback<boolean> | 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. The value **true** means that the application is running on a RAM constrained device, and **false** means the opposite. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; appManager.isRamConstrainedDevice((err, data) => { if (err) { console.error(`isRamConstrainedDevice fail, err: ${JSON.stringify(err)}`); } else { console.log(`The result of isRamConstrainedDevice is: ${JSON.stringify(data)}`); } }); ``` ## appManager.getAppMemorySize getAppMemorySize(): Promise\ Obtains the memory size of this application. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | Promise<number> | Promise used to return the memory size, in MB. You can perform error processing or other custom processing based on the size. | **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; appManager.getAppMemorySize().then((data) => { console.log(`The size of app memory is: ${JSON.stringify(data)}`); }).catch((error: BusinessError) => { console.error(`error: ${JSON.stringify(error)}`); }); ``` ## appManager.getAppMemorySize getAppMemorySize(callback: AsyncCallback\): void Obtains the memory size of this application. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callback | AsyncCallback<number> | Yes|Callback used to return the memory size, in MB. You can perform error processing or other custom processing based on the size. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; appManager.getAppMemorySize((err, data) => { if (err) { console.error(`getAppMemorySize fail, err: ${JSON.stringify(err)}`); } else { console.log(`The size of app memory is: ${JSON.stringify(data)}`); } }); ``` ## appManager.getRunningProcessInformation getRunningProcessInformation(): Promise\> Obtains information about the running processes. This API uses a promise to return the result. > **NOTE** > > In versions earlier than API version 11, this API requires the **ohos.permission.GET_RUNNING_INFO** permission, which is available only for system applications. Since API version 11, no permission is required for calling this API. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | Promise\> | Promise used to return the API call result and the process running information. You can perform error handling or custom processing in this callback.| **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; appManager.getRunningProcessInformation().then((data) => { console.log(`The running process information is: ${JSON.stringify(data)}`); }).catch((error: BusinessError) => { console.error(`error: ${JSON.stringify(error)}`); }); ``` ## appManager.getRunningProcessInformation getRunningProcessInformation(callback: AsyncCallback\>): void Obtains information about the running processes. This API uses an asynchronous callback to return the result. > **NOTE** > > In versions earlier than API version 11, this API requires the **ohos.permission.GET_RUNNING_INFO** permission, which is available only for system applications. Since API version 11, no permission is required for calling this API. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callback | AsyncCallback\> | 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.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; appManager.getRunningProcessInformation((err, data) => { if (err) { console.error(`getRunningProcessInformation fail, err: ${JSON.stringify(err)}`); } else { console.log(`The running process information is: ${JSON.stringify(data)}`); } }); ``` ## appManager.on('applicationState')14+ on(type: 'applicationState', observer: ApplicationStateObserver): number 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 **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| | 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** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 201 | Permission denied. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let applicationStateObserver: appManager.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)}`); }, onAppStarted(appStateData) { console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); }, onAppStopped(appStateData) { console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); } }; try { const observerId = appManager.on('applicationState', applicationStateObserver); console.log(`[appManager] observerCode: ${observerId}`); } catch (paramError) { let code = (paramError as BusinessError).code; let message = (paramError as BusinessError).message; console.error(`[appManager] error: ${code}, ${message}`); } ``` ## appManager.on('applicationState')14+ on(type: 'applicationState', observer: ApplicationStateObserver, bundleNameList: Array\): number 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 **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| | 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` | 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** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 201 | Permission denied. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let applicationStateObserver: appManager.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)}`); }, onAppStarted(appStateData) { console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); }, onAppStopped(appStateData) { console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); } }; let bundleNameList = ['bundleName1', 'bundleName2']; try { const observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList); console.log(`[appManager] observerCode: ${observerId}`); } catch (paramError) { let code = (paramError as BusinessError).code; let message = (paramError as BusinessError).message; console.error(`[appManager] error: ${code}, ${message}`); } ``` ## appManager.off('applicationState')14+ off(type: 'applicationState', observerId: number): Promise\ Deregisters the application state observer. **Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | string | Yes| Type of the API to call. It is fixed at **'applicationState'**.| | observerId | number | Yes| Digital code of the observer.| **Return value** | Type| Description| | -------- | -------- | | Promise\ | Promise used to return the API call result. You can perform error handling or custom processing in this callback.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 201 | Permission denied. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let observerId = 0; // 1. Register an application state observer. let applicationStateObserver: appManager.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)}`); }, onAppStarted(appStateData) { console.log(`[appManager] onAppStarted: ${JSON.stringify(appStateData)}`); }, onAppStopped(appStateData) { console.log(`[appManager] onAppStopped: ${JSON.stringify(appStateData)}`); } }; let bundleNameList = ['bundleName1', 'bundleName2']; try { observerId = appManager.on('applicationState', applicationStateObserver, bundleNameList); } catch (paramError) { let code = (paramError as BusinessError).code; let message = (paramError as BusinessError).message; console.error(`[appManager] error: ${code}, ${message}`); } // 2. Deregister the application state observer. try { appManager.off('applicationState', observerId).then((data) => { console.log(`unregisterApplicationStateObserver success, data: ${JSON.stringify(data)}`); }).catch((err: BusinessError) => { console.error(`unregisterApplicationStateObserver fail, err: ${JSON.stringify(err)}`); }); } catch (paramError) { let code = (paramError as BusinessError).code; let message = (paramError as BusinessError).message; console.error(`[appManager] error: ${code}, ${message}`); } ``` ## appManager.killProcessesByBundleName14+ killProcessesByBundleName(bundleName: string, clearPageStack: boolean, appIndex?: number): Promise\ Kills a process by bundle name. This API uses an asynchronous callback to return the result. This API uses a promise to return the result. **Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | bundleName | string | Yes| Bundle name.| | clearPageStack | boolean | Yes| Whether to clear the page stack. The value **true** means to clear the page stack, and **false** means the opposite.| | appIndex | number | No| Index of an application clone.| **Return value** | Type| Description| | -------- | -------- | | Promise\ | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 201 | Permission denied. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000050 | Internal error. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let bundleName = 'bundleName'; let isClearPageStack = false; let appIndex = 1; try { appManager.killProcessesByBundleName(bundleName, isClearPageStack, appIndex).then((data) => { console.log('killProcessesByBundleName success.'); }).catch((err: BusinessError) => { console.error(`killProcessesByBundleName fail, err: ${JSON.stringify(err)}`); }); } catch (paramError) { let code = (paramError as BusinessError).code; let message = (paramError as BusinessError).message; console.error(`[appManager] error: ${code}, ${message}`); } ``` ## appManager.isAppRunning14+ isAppRunning(bundleName: string, appCloneIndex?: number): Promise\ Checks whether an application is running. This API uses a promise to return the result. **Required permissions**: ohos.permission.GET_RUNNING_INFO **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | bundleName | string | Yes| Bundle name.| | appCloneIndex | number | No| Index of an application clone.| **Return value** | Type | Description | | -------------- | ---------------- | | Promise\ | Promise used to return the result. The value **true** means that the application is running, and **false** means the opposite.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | | 16000050 | Internal error. | | 16000073 | The app clone index is invalid. | **Example** ```ts import { appManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; try { let bundleName = "ohos.samples.etsclock"; appManager.isAppRunning(bundleName).then((data: boolean) => { hilog.info(0x0000, 'testTag', `data: ${JSON.stringify(data)}`); }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', `isAppRunning error, code: ${err.code}, msg:${err.message}`); }) } catch (err) { hilog.error(0x0000, 'testTag', `isAppRunning error, code: ${err.code}, msg:${err.message}`); } ```