harmony 鸿蒙@ohos.application.appManager (appManager)

  • 2022-12-22
  • 浏览 (564)

@ohos.application.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 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 instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import appManager from '@ohos.application.appManager';

appManager.isRunningInStabilityTest8+

static isRunningInStabilityTest(callback: AsyncCallback<boolean>): void

Checks whether this application is undergoing a stability test. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<boolean> Yes Callback used to return the result. If the application is undergoing a stability test, true will be returned; otherwise, false will be returned.

Example

  import appManager from '@ohos.application.appManager';

  appManager.isRunningInStabilityTest((error, flag) => {
    if (error && error.code !== 0) {
        console.error(`isRunningInStabilityTest fail, error: ${JSON.stringify(error)}`);
    } else {
        console.log(`isRunningInStabilityTest success, the result is: ${JSON.stringify(flag)}`);
    }
  });

appManager.isRunningInStabilityTest8+

static isRunningInStabilityTest(): Promise<boolean>

Checks whether this application is undergoing a stability test. This API uses a promise to return the result.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

Type Description
Promise<boolean> Promise used to return the result. If the application is undergoing a stability test, true will be returned; otherwise, false will be returned.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  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.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

Type Description
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.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  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.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
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.

Example

  import appManager from '@ohos.application.appManager';

  appManager.isRamConstrainedDevice((error, data) => {
      if (error && error.code !== 0) {
          console.error(`isRamConstrainedDevice fail, error: ${JSON.stringify(error)}`);
      } 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.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

Type Description
Promise&lt;number&gt; Promise used to return the memory size, in MB.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  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.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback&lt;number&gt; Yes Callback used to return the memory size, in MB.

Example

  import appManager from '@ohos.application.appManager';

  appManager.getAppMemorySize((error, data) => {
      if (error && error.code !== 0) {
          console.error(`getAppMemorySize fail, error: ${JSON.stringify(error)}`);
      } else {
          console.log(`The size of app memory is: ${JSON.stringify(data)}`);
      }
  });

appManager.getProcessRunningInfos(deprecated)

getProcessRunningInfos(): Promise<Array<ProcessRunningInfo>>;

Obtains information about the running processes. This API uses a promise to return the result.

This API is deprecated since API version 9. You are advised to use appManager.getRunningProcessInformation9+ instead.

Required permissions: ohos.permission.GET_RUNNING_INFO

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

Type Description
Promise<Array<ProcessRunningInfo>> Promise used to return the process information.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  appManager.getProcessRunningInfos().then((data) => {
      console.log(`The process running infos is: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
      console.error(`error: ${JSON.stringify(error)}`);
  });

appManager.getProcessRunningInfos(deprecated)

getProcessRunningInfos(callback: AsyncCallback<Array<ProcessRunningInfo>>): void;

Obtains information about the running processes. This API uses an asynchronous callback to return the result.

This API is deprecated since API version 9. You are advised to use appManager.getRunningProcessInformation9+ instead.

Required permissions: ohos.permission.GET_RUNNING_INFO

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<Array<ProcessRunningInfo>> Yes Obtains information about the running processes. This API uses a promise to return the result.

Example

  import appManager from '@ohos.application.appManager';

  appManager.getProcessRunningInfos((error, data) => {
      if (error && error.code !== 0) {
          console.error(`getProcessRunningInfos fail, error: ${JSON.stringify(error)}`);
      } else {
          console.log(`getProcessRunningInfos success, data: ${JSON.stringify(data)}`);
      }
  });

appManager.registerApplicationStateObserver8+

registerApplicationStateObserver(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

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
observer ApplicationStateObserver Yes Numeric code of the observer.

Example

  import appManager from '@ohos.application.appManager';

  const observerCode = appManager.registerApplicationStateObserver({
    onForegroundApplicationChanged(appStateData) {
        console.log('------------ onForegroundApplicationChanged -----------', appStateData);
    },
    onAbilityStateChanged(abilityStateData) {
        console.log('------------ onAbilityStateChanged -----------', abilityStateData);
    },
    onProcessCreated(processData) {
        console.log('------------ onProcessCreated -----------', processData);
    },
    onProcessDied(processData) {
        console.log('------------ onProcessDied -----------', processData);
    },
    onProcessStateChanged(processData) {
        console.log('------------ onProcessStateChanged -----------', processData);
    }
  });
  console.log('-------- observerCode: ---------', observerCode);

appManager.unregisterApplicationStateObserver8+

unregisterApplicationStateObserver(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

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
observerId number Yes Numeric code of the observer.
callback AsyncCallback<void> Yes Callback used to return the result.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  let observerId = 100;

  function unregisterApplicationStateObserverCallback(err: BusinessError) {
    if (err) {
        console.error('------------ unregisterApplicationStateObserverCallback ------------', err);
    }
  }
  appManager.unregisterApplicationStateObserver(observerId, unregisterApplicationStateObserverCallback);

appManager.unregisterApplicationStateObserver8+

unregisterApplicationStateObserver(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

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
observerId number Yes Numeric code of the observer.

Return value

Type Description
Promise<void> Promise used to return the result.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  let observerId = 100;

  appManager.unregisterApplicationStateObserver(observerId)
  .then((data) => {
      console.log('----------- unregisterApplicationStateObserver success ----------', data);
  })
  .catch((err: BusinessError) => {
      console.error('----------- unregisterApplicationStateObserver fail ----------', err);
  });

appManager.getForegroundApplications8+

getForegroundApplications(callback: AsyncCallback<Array<AppStateData>>): void;

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

Required permissions: ohos.permission.GET_RUNNING_INFO

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<Array<AppStateData>> Yes Callback used to return the application information.

Example

  import appManager from '@ohos.application.appManager';

  appManager.getForegroundApplications((err, data) => {
    if (err) {
        console.error('--------- getForegroundApplicationsCallback fail ---------', err);
    } else {
        console.log('--------- getForegroundApplicationsCallback success ---------', data);
    }
  });

appManager.getForegroundApplications8+

getForegroundApplications(): Promise<Array<AppStateData>>;

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

Required permissions: ohos.permission.GET_RUNNING_INFO

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Return value

Type Description
Promise<Array<AppStateData>> Promise used to return the application information.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  appManager.getForegroundApplications()
  .then((data) => {
      console.log('--------- getForegroundApplications success -------', data);
  })
  .catch((err: BusinessError) => {
      console.error('--------- getForegroundApplications fail -------', err);
  });

appManager.killProcessWithAccount8+

killProcessWithAccount(bundleName: string, accountId: number): Promise<void>

Kills a process by bundle name and account ID. This API uses a promise to return the result.

NOTE

The ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS permission is not required when accountId specifies the current user.

Required permissions: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.CLEAN_BACKGROUND_PROCESSES

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name.
accountId number Yes ID of a system account. For details, see getCreatedOsAccountsCount.

Example

import appManager from '@ohos.application.appManager';
import { BusinessError } from '@ohos.base';

let bundleName = 'bundleName';
let accountId = 0;
appManager.killProcessWithAccount(bundleName, accountId)
   .then((data) => {
       console.log('------------ killProcessWithAccount success ------------', data);
   })
   .catch((err: BusinessError) => {
       console.error('------------ killProcessWithAccount fail ------------', err);
   });

appManager.killProcessWithAccount8+

killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCallback<void>): void

Kills a process by bundle name and account ID. This API uses an asynchronous callback to return the result.

NOTE

The ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS permission is not required when accountId specifies the current user.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.CLEAN_BACKGROUND_PROCESSES

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name.
accountId number Yes ID of a system account. For details, see getCreatedOsAccountsCount.
callback AsyncCallback<void> Yes Callback used to return the result.

Example

import appManager from '@ohos.application.appManager';
import { BusinessError } from '@ohos.base';

let bundleName = 'bundleName';
let accountId = 0;
function killProcessWithAccountCallback(err: BusinessError, data: void) {
   if (err) {
       console.error('------------- killProcessWithAccountCallback fail, err: --------------', err);
   } else {
       console.log('------------- killProcessWithAccountCallback success, data: --------------', data);
   }
}
appManager.killProcessWithAccount(bundleName, accountId, killProcessWithAccountCallback);

appManager.killProcessesByBundleName8+

killProcessesByBundleName(bundleName: string, callback: AsyncCallback<void>);

Kills a process by bundle name. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLEAN_BACKGROUND_PROCESSES

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name.
callback AsyncCallback<void> Yes Callback used to return the result.

Example

  import appManager from '@ohos.application.appManager';
import { BusinessError } from '@ohos.base';

  let bundleName = 'bundleName';
  function killProcessesByBundleNameCallback(err: BusinessError, data: void) {
    if (err) {
        console.error('------------- killProcessesByBundleNameCallback fail, err: --------------', err);
    } else {
        console.log('------------- killProcessesByBundleNameCallback success, data: --------------', data);
    }
  }
  appManager.killProcessesByBundleName(bundleName, killProcessesByBundleNameCallback);

appManager.killProcessesByBundleName8+

killProcessesByBundleName(bundleName: string): Promise<void>;

Kills a process by bundle name. This API uses a promise to return the result.

Required permissions: ohos.permission.CLEAN_BACKGROUND_PROCESSES

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name.

Return value

Type Description
Promise<void> Promise used to return the result.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  let bundleName = 'com.example.myapplication';
  appManager.killProcessesByBundleName(bundleName)
    .then((data) => {
        console.log('------------ killProcessesByBundleName success ------------', data);
    })
    .catch((err: BusinessError) => {
        console.error('------------ killProcessesByBundleName fail ------------', err);
    });

appManager.clearUpApplicationData8+

clearUpApplicationData(bundleName: string, callback: AsyncCallback<void>);

Clears application data by bundle name. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLEAN_APPLICATION_DATA

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name.
callback AsyncCallback<void> Yes Callback used to return the result.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  let bundleName = 'bundleName';
  function clearUpApplicationDataCallback(err: BusinessError, data: void) {
    if (err) {
        console.error('------------- clearUpApplicationDataCallback fail, err: --------------', err);
    } else {
        console.log('------------- clearUpApplicationDataCallback success, data: --------------', data);
    }
  }
  appManager.clearUpApplicationData(bundleName, clearUpApplicationDataCallback);

appManager.clearUpApplicationData8+

clearUpApplicationData(bundleName: string): Promise<void>;

Clears application data by bundle name. This API uses a promise to return the result.

Required permissions: ohos.permission.CLEAN_APPLICATION_DATA

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name.

Return value

Type Description
Promise<void> Promise used to return the result.

Example

  import appManager from '@ohos.application.appManager';
  import { BusinessError } from '@ohos.base';

  let bundleName = 'bundleName';
  appManager.clearUpApplicationData(bundleName)
    .then((data) => {
        console.log('------------ clearUpApplicationData success ------------', data);
    })
    .catch((err: BusinessError) => {
        console.error('------------ clearUpApplicationData fail ------------', err);
    });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞