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

2025-06-12 浏览 (1)

@ohos.app.ability.appRecovery (appRecovery)

The appRecovery module provides APIs for recovering faulty applications.

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. In API version 9, only applications with a single ability in a process can be recovered. In API version 10, applications with multiple abilities in a process can be recovered.

Modules to Import

import { appRecovery } from '@kit.AbilityKit';

RestartFlag

Enumerates the application restart flags. This enum is used as an input parameter of enableAppRecovery.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
ALWAYS_RESTART0The application is restarted in all cases.
RESTART_WHEN_JS_CRASH0x0001The application is restarted in the case of JS_CRASH.
RESTART_WHEN_APP_FREEZE0x0002The application is restarted in the case of APP_FREEZE.
NO_RESTART0xFFFFThe application is not restarted in any case.

SaveOccasionFlag

Enumerates the scenarios for saving the application state. This enum is used as an input parameter of enableAppRecovery.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
SAVE_WHEN_ERROR0x0001Saving the application state when an application fault occurs.
SAVE_WHEN_BACKGROUND0x0002Saving the application state when the application is switched to the background.

SaveModeFlag

Enumerates the application state saving modes. This enum is used as an input parameter of enableAppRecovery.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
SAVE_WITH_FILE0x0001The application state is saved and written to the local file cache.
SAVE_WITH_SHARED_MEMORY0x0002The application state is saved in the memory. When the application exits due to a fault, it is written to the local file cache.

appRecovery.enableAppRecovery

enableAppRecovery(restart?: RestartFlag, saveOccasion?: SaveOccasionFlag, saveMode?: SaveModeFlag) : void

Enables application recovery. After this API is called, the first ability that is displayed when the application is started from the initiator can be restored.

Model restriction: This API can be used only in the stage model.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
restartRestartFlagNoWhether the application is restarted upon a fault. By default, the application is restarted.
saveOccasionSaveOccasionFlagNoScenario for saving the application state. By default, the state is saved when a fault occurs.
saveModeSaveModeFlagNoApplication state saving mode. By default, the application state is written to the local file cache.

Example

import { appRecovery, AbilityStage } from '@kit.AbilityKit';

export default class MyAbilityStage extends AbilityStage {
  onCreate() {
    appRecovery.enableAppRecovery(
      appRecovery.RestartFlag.ALWAYS_RESTART,
      appRecovery.SaveOccasionFlag.SAVE_WHEN_ERROR,
      appRecovery.SaveModeFlag.SAVE_WITH_FILE
    );
  }
}

appRecovery.restartApp

restartApp(): void

Restarts the current process and starts the first ability that is displayed when the application is started. If the state of this ability is saved, the saved state data is passed into the wantParam property in the want parameter of the onCreate lifecycle callback of the ability.

In API version 10, the ability specified by setRestartWant is started. If no ability is specified, the following rules are used:

  • If the ability of the current application running in the foreground supports recovery, that ability is started.
  • If multiple abilities that support recovery is running in the foreground, only the last ability is started.
  • If no ability is running in the foreground, none of them is started.

This API can be used together with the APIs of errorManager. The interval between two restarts must be greater than one minute. If this API is called repeatedly within one minute, the application exits but does not restart. The behavior of automatic restart is the same as that of proactive restart.

Model restriction: This API can be used only in the stage model.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

Example

import { appRecovery, errorManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let observer: errorManager.ErrorObserver = {
  onUnhandledException(errorMsg) {
    console.log('onUnhandledException, errorMsg: ', errorMsg);
    appRecovery.restartApp();
  }
};

try {
  errorManager.on('error', observer);
} catch (paramError) {
  console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}

appRecovery.saveAppState

saveAppState(): boolean

Saves the application state. This API can be used together with the APIs of errorManager.

Model restriction: This API can be used only in the stage model.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

TypeDescription
booleanWhether the application state is saved. The value true is returned if the application state is saved, and false is returned otherwise.

Example

import { appRecovery, errorManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let observer: errorManager.ErrorObserver = {
  onUnhandledException(errorMsg) {
    console.log('onUnhandledException, errorMsg: ', errorMsg);
    appRecovery.saveAppState();
  }
};

try {
  errorManager.on('error', observer);
} catch (paramError) {
  console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}

appRecovery.saveAppState10+

saveAppState(context?: UIAbilityContext): boolean

Saves the ability state, which will be used for recovery. This API can be used together with the APIs of errorManager.

Model restriction: This API can be used only in the stage model.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
contextUIAbilityContextNoContext of the target ability.

Return value

TypeDescription
booleanWhether the application state is saved. The value true is returned if the application state is saved, and false is returned otherwise.

Example

import { appRecovery, errorManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let observer: errorManager.ErrorObserver = {
  onUnhandledException(errorMsg) {
    console.log('onUnhandledException, errorMsg: ', errorMsg);
    appRecovery.saveAppState(this.context);
  }
};

try {
  errorManager.on('error', observer);
} catch (paramError) {
  console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}

appRecovery.setRestartWant10+

setRestartWant(want: Want): void

Sets an ability that will be recovered. The ability must be a UIAbility in the current bundle.

Model restriction: This API can be used only in the stage model.

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

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
wantWantYesWant of the target ability. You can set the bundleName and abilityName fields in Want to specify the ability.

Example

import { appRecovery, Want } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  build() {
    Button("Start to Recover Ability")
      .fontSize(40)
      .fontWeight(FontWeight.Bold)
      .onClick(()=> {
        // set restart want
        let want: Want = {
          bundleName: "ohos.samples.recovery",
          abilityName: "RecoveryAbility"
        };

        appRecovery.setRestartWant(want);
      })
  }
}

你可能感兴趣的鸿蒙文章

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/fSq30K