openharmony 鸿蒙 js-apis-app-ability-appManager

2025-06-12 浏览 (1)

@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

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

NameValueDescription
STATE_CREATE0The process is being created.
STATE_FOREGROUND1The process is running in the foreground.
STATE_ACTIVE2The process is active.
STATE_BACKGROUND3The process is running in the background.
STATE_DESTROY4The 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

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback 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 and Ability Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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

TypeDescription
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.

IDError Message
16000050Internal error.

Example

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<boolean>

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

TypeDescription
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.

IDError Message
16000050Internal error.

Example

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<boolean>): 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

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback 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 and Ability Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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<number>

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

TypeDescription
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.

IDError Message
16000050Internal error.

Example

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<number>): 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

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback 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 and Ability Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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<Array<ProcessInformation>>

Obtains information about the running processes of this application. 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, this API is used only to obtain the process information of the caller. No permission is required.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

TypeDescription
Promise<Array<ProcessInformation>>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.

IDError Message
16000050Internal error.

Example

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<Array<ProcessInformation>>): void

Obtains information about the running processes of this application. 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, this API is used only to obtain the process information of the caller. No permission is required.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<ProcessInformation>>YesCallback 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 and Ability Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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

NameTypeMandatoryDescription
typestringYesType of the API to call. It is fixed at 'applicationState'.
observerApplicationStateObserverYesApplication state observer, which is used to observe the lifecycle change of an application.

Return value

TypeDescription
numberDigital 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 and Ability Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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<string>): 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

NameTypeMandatoryDescription
typestringYesType of the API to call. It is fixed at 'applicationState'.
observerApplicationStateObserverYesApplication state observer, which is used to observe the lifecycle change of an application.
bundleNameListArray<string>YesbundleName array of the application. A maximum of 128 bundle names can be passed.

Return value

TypeDescription
numberDigital 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 and Ability Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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<void>

Deregisters the application state observer. This API uses a promise to return the result.

Required permissions: ohos.permission.RUNNING_STATE_OBSERVER

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
typestringYesType of the API to call. It is fixed at 'applicationState'.
observerIdnumberYesDigital code of the observer.

Return value

TypeDescription
Promise<void>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 and Ability Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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.off('applicationState')15+

off(type: 'applicationState', observerId: number, callback: AsyncCallback<void>): void

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

Required permissions: ohos.permission.RUNNING_STATE_OBSERVER

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
typestringYesType of the API to call. It is fixed at 'applicationState'.
observerIdnumberYesDigital code of the observer.
callbackAsyncCallback<void>YesCallback used to return the result. If the application state observer is deregistered, err is undefined; otherwise, error is an error object.

Error codes

For details about the error codes, see Universal Error Codes and Ability Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050Internal error.

Example

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}`);
}

function offCallback(err: BusinessError) {
  if (err) {
    console.error(`appmanager.off failed, code: ${err.code}, msg: ${err.message}`);
  } else {
    console.info(`appmanager.off success.`);
  }
}

// 2. Deregister the application state observer.
try {
  appManager.off('applicationState', observerId, offCallback);
} 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<void>

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.KILL_APP_PROCESSES or ohos.permission.CLEAN_BACKGROUND_PROCESSES

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.
clearPageStackbooleanYesWhether to clear the page stack. The value true means to clear the page stack, and false means the opposite.
appIndexnumberNoIndex of an application clone.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Ability Error Codes.

IDError Message
201Permission denied.
401If the input parameter is not valid parameter.
16000050Internal error.

Example

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<boolean>

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

NameTypeMandatoryDescription
bundleNamestringYesBundle name.
appCloneIndexnumberNoIndex of an application clone.

Return value

TypeDescription
Promise<boolean>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 and Ability Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16000050Internal error.
16000073The app clone index is invalid.

Example

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}`);
}

ApplicationStateObserver14+

type ApplicationStateObserver = _ApplicationStateObserver.default

Defines the ApplicationStateObserver module.

System capability: SystemCapability.Ability.AbilityRuntime.Core

TypeDescription
_ApplicationStateObserver.defaultApplicationStateObserver module.

ProcessInformation

type ProcessInformation = _ProcessInformation

Defines the ProcessInformation module.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

TypeDescription
_ProcessInformationProcessInformation module.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/qxQWHO