js-apis-app-ability-appRecovery.md 8.2 KB
Newer Older
1
# @ohos.app.ability.appRecovery (appRecovery)
2

3
The **appRecovery** module provides APIs for recovering faulty applications.
4 5 6

> **NOTE**
> 
G
Gloria 已提交
7
> 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.
8 9 10

## Modules to Import
```ts
11
import appRecovery from '@ohos.app.ability.appRecovery';
12 13 14 15 16
```


## appRecovery.RestartFlag

17
Enumerates the application restart flags. This enum is used as an input parameter of [enableAppRecovery](#apprecoveryenableapprecovery).
18 19 20

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

21 22 23 24 25 26
| Name      | Value  | Description      |
| ---------- | ---- | ---------- |
| ALWAYS_RESTART   | 0    | The application is restarted in all cases.|
| RESTART_WHEN_JS_CRASH   | 0x0001    | The application is restarted in the case of JS_CRASH.|
| RESTART_WHEN_APP_FREEZE   | 0x0002    | The application is restarted in the case of APP_FREEZE.|
| NO_RESTART           | 0xFFFF    | The application is not restarted in any case.|
27 28 29

## appRecovery.SaveOccasionFlag

30
Enumerates the scenarios for saving the application state. This enum is used as an input parameter of [enableAppRecovery](#apprecoveryenableapprecovery).
31 32 33 34 35 36 37 38 39 40

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| SAVE_WHEN_ERROR            | 0x0001    | Saving the application state when an application fault occurs.|
| SAVE_WHEN_BACKGROUND            | 0x0002    | Saving the application state when the application is switched to the background.|

## appRecovery.SaveModeFlag  

41
Enumerates the application state saving modes. This enum is used as an input parameter of [enableAppRecovery](#apprecoveryenableapprecovery).
42 43 44 45 46 47 48 49 50 51

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
| SAVE_WITH_FILE             | 0x0001    | The application state is saved and written to the local file cache.|
| SAVE_WITH_SHARED_MEMORY             | 0x0002    | The 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

52
enableAppRecovery(restart?: [RestartFlag](#apprecoveryrestartflag), saveOccasion?: [SaveOccasionFlag](#apprecoverysaveoccasionflag), saveMode?: [SaveModeFlag](#apprecoverysavemodeflag)) : void;
53

G
Gloria 已提交
54
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.
55 56 57 58 59 60 61 62 63 64 65

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| restart | [RestartFlag](#apprecoveryrestartflag) | No| Whether the application is restarted upon a fault. By default, the application is not restarted.|
| saveOccasion | [SaveOccasionFlag](#apprecoverysaveoccasionflag) | No| Scenario for saving the application state. By default, the state is saved when a fault occurs.|
| saveMode | [SaveModeFlag](#apprecoverysavemodeflag) | No| Application state saving mode. By default, the application state is written to the local file cache.|

66 67
**Example**
    
68
```ts
69 70 71
import appRecovery from '@ohos.app.ability.appRecovery';
import AbilityStage from '@ohos.app.ability.AbilityStage';

72
export default class MyAbilityStage extends AbilityStage {
73
    onCreate() {
74
        appRecovery.enableAppRecovery(
G
Gloria 已提交
75 76 77
            appRecovery.RestartFlag.ALWAYS_RESTART,
            appRecovery.SaveOccasionFlag.SAVE_WHEN_ERROR,
            appRecovery.SaveModeFlag.SAVE_WITH_FILE
78
        );
79 80 81 82 83 84 85 86
    }
}
```

## appRecovery.restartApp

restartApp(): void;

G
Gloria 已提交
87 88 89 90 91 92 93 94
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** attribute in the **want** parameter of the **OnCreate** lifecycle callback of the ability.

In API version 10, the ability specified by [setRestartWant](#apprecoverysetrestartwant) 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](js-apis-app-ability-errorManager.md).
95 96 97 98

**System capability**: SystemCapability.Ability.AbilityRuntime.Core


99 100
**Example**
    
101
```ts
102 103 104 105
import appRecovery from '@ohos.app.ability.appRecovery';
import errorManager from '@ohos.app.ability.errorManager';

let observer = {
106
    onUnhandledException(errorMsg) {
107
        console.log('onUnhandledException, errorMsg: ', errorMsg);
108 109
        appRecovery.restartApp();
    }
110
};
111

112
try {
113
    errorManager.on('error', observer);
114
} catch (paramError) {
G
Gloria 已提交
115
    console.error('error: ${paramError.code}, ${paramError.message}');
116
}
117 118 119 120 121 122
```

## appRecovery.saveAppState

saveAppState(): boolean;

G
Gloria 已提交
123
Saves the application state. This API can be used together with the APIs of [errorManager](js-apis-app-ability-errorManager.md).
124 125 126 127 128 129 130

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type| Description|
| -------- | -------- |
131
| boolean | Whether the application state is saved. The value **true** is returned if the application state is saved, and **false** is returned otherwise.|
132

133 134
**Example**
    
135
```ts
136 137 138 139
import appRecovery from '@ohos.app.ability.appRecovery';
import errorManager from '@ohos.app.ability.errorManager';

let observer = {
140
    onUnhandledException(errorMsg) {
141
        console.log('onUnhandledException, errorMsg: ', errorMsg);
142 143
        appRecovery.saveAppState();
    }
144 145 146
};

try {
147
    errorManager.on('error', observer);
148
} catch (paramError) {
G
Gloria 已提交
149
    console.error('error: ${paramError.code}, ${paramError.message}');
150 151
}
```
G
Gloria 已提交
152 153 154 155 156 157 158 159 160

## appRecovery.saveAppState<sup>10+</sup>

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](js-apis-app-ability-errorManager.md).

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

161 162 163 164 165 166
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)| No| UIAbility context.|

G
Gloria 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Whether the application state is saved. The value **true** is returned if the application state is saved, and **false** is returned otherwise.|

**Example**

```ts
import appRecovery from '@ohos.app.ability.appRecovery';
onBackground() {
    hilog.info(0x0000, '[demo]', '%{public}s', 'EntryAbility onBackground');
    appRecovery.saveAppState(this.context)
}
```

## appRecovery.setRestartWant<sup>10+</sup>

setRestartWant(want: Want): void;

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

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

191 192 193 194 195 196
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)| Yes| Want of the target ability. You can set the **bundleName** and **abilityName** fields in **Want** to specify the ability.|

G
Gloria 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
**Example**

```ts
import appRecovery from '@ohos.app.ability.appRecovery';
Button ("Start to Recover Ability")
    .fontSize(40)
    .fontWeight(FontWeight.Bold)
    .onClick(()=> {
        // set restart want
        let want = {
            bundleName: "ohos.samples.recovery",
            abilityName: "RecoveryAbility"
        };

        appRecovery.setRestartWant(want);
    })
```