js-apis-inner-application-abilityDelegator.md 45.8 KB
Newer Older
W
wusongqing 已提交
1 2
# AbilityDelegator

3 4
The **AbilityDelegator** module provides APIs for managing **AbilityMonitor** instances that are used to monitor the lifecycle state changes of a specified ability. You can use the APIs to add and remove **AbilityMonitor** instances, wait for an ability to reach the **OnCreate** lifecycle state, set the waiting time, obtain the lifecycle state of an ability, obtain the top ability of the current application, and start an ability.

W
wusongqing 已提交
5 6
> **NOTE**
> 
W
wusongqing 已提交
7 8
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

G
Gloria 已提交
9 10 11 12 13 14
## Modules to Import

```ts
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
```

15
## Usage
W
wusongqing 已提交
16

17 18
An **AbilityDelegator** object is obtained by calling [getAbilityDelegator](js-apis-app-ability-abilityDelegatorRegistry.md#abilitydelegatorregistrygetabilitydelegator) in **AbilityDelegatorRegistry**.
```ts
G
Gloria 已提交
19
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
W
wusongqing 已提交
20 21 22 23 24 25
```

## AbilityDelegator

### addAbilityMonitor<sup>9+</sup>

26
addAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
27 28 29 30 31 32 33 34 35

Adds an **AbilityMonitor** instance. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
36
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes      | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
37 38
| callback | AsyncCallback\<void>                                         | Yes      | Callback used to return the result.                                          |

G
Gloria 已提交
39 40 41 42 43 44 45 46
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | AddAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
47 48
**Example**

49
```ts
G
Gloria 已提交
50
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
51

W
wusongqing 已提交
52
function onAbilityCreateCallback(data) {
53
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
54 55
}

56 57
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
58
    onAbilityCreate: onAbilityCreateCallback
59
};
W
wusongqing 已提交
60 61

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
62
abilityDelegator.addAbilityMonitor(monitor, (err : any) => {
63
    console.info('addAbilityMonitor callback');
W
wusongqing 已提交
64 65 66 67 68
});
```

### addAbilityMonitor<sup>9+</sup>

69
addAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
W
wusongqing 已提交
70 71 72 73 74 75 76 77 78

Adds an **AbilityMonitor** instance. This API uses a promise to return the result.

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
79
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
80 81 82 83 84 85 86

**Return value**

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

G
Gloria 已提交
87 88 89 90 91 92 93 94
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | AddAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
95 96
**Example**

97
```ts
G
Gloria 已提交
98
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
99

W
wusongqing 已提交
100
function onAbilityCreateCallback(data) {
101
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
102 103
}

104 105
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
106
    onAbilityCreate: onAbilityCreateCallback
107
};
W
wusongqing 已提交
108 109 110

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor).then(() => {
111
    console.info('addAbilityMonitor promise');
W
wusongqing 已提交
112 113 114 115 116 117 118
});
```



### removeAbilityMonitor<sup>9+</sup>

119
removeAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
120 121 122 123 124 125 126 127 128

Removes an **AbilityMonitor** instance. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
129
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
130 131
| callback | AsyncCallback\<void>                                         | Yes  | Callback used to return the result.                                          |

G
Gloria 已提交
132 133 134 135 136 137 138 139
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | RemoveAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
140 141
**Example**

142
```ts
G
Gloria 已提交
143
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
144

W
wusongqing 已提交
145
function onAbilityCreateCallback(data) {
146
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
147 148
}

149 150
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
151
    onAbilityCreate: onAbilityCreateCallback
152
};
W
wusongqing 已提交
153 154

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
155
abilityDelegator.removeAbilityMonitor(monitor, (err : any) => {
156
    console.info('removeAbilityMonitor callback');
W
wusongqing 已提交
157 158 159 160 161 162 163
});
```



### removeAbilityMonitor<sup>9+</sup>

164
removeAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
W
wusongqing 已提交
165

G
Gloria 已提交
166
Removes an **AbilityMonitor** instance. This API uses a promise to return the result.
W
wusongqing 已提交
167 168 169 170 171 172 173

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

**Parameters**

| Name   | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
174
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
175 176 177 178 179 180 181

**Return value**

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

G
Gloria 已提交
182 183 184 185 186 187 188 189
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | RemoveAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
190 191
- Example

192
```ts
G
Gloria 已提交
193
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
194

W
wusongqing 已提交
195
function onAbilityCreateCallback(data) {
196
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
197 198
}

199 200
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
201
    onAbilityCreate: onAbilityCreateCallback
202
};
W
wusongqing 已提交
203 204 205

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityMonitor(monitor).then(() => {
206
    console.info('removeAbilityMonitor promise');
W
wusongqing 已提交
207 208 209 210 211 212 213
});
```



### waitAbilityMonitor<sup>9+</sup>

214
waitAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<UIAbility>): void;
W
wusongqing 已提交
215

216
Waits for the **Ability** instance that matches the **AbilityMonitor** instance to reach the **OnCreate** lifecycle state and returns the **Ability** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
217 218 219 220 221 222 223

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
224 225
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
| callback | AsyncCallback\<[UIAbility](js-apis-app-ability-uiAbility.md)> | Yes  | Callback used to return the result.                                          |
W
wusongqing 已提交
226

G
Gloria 已提交
227 228 229 230 231 232 233 234
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | WaitAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
235 236
**Example**

237
```ts
G
Gloria 已提交
238
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
239

W
wusongqing 已提交
240
function onAbilityCreateCallback(data) {
241
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
242 243
}

244 245
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
246
    onAbilityCreate: onAbilityCreateCallback
247
};
W
wusongqing 已提交
248 249

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
250
abilityDelegator.waitAbilityMonitor(monitor, (err : any, data : any) => {
251
    console.info('waitAbilityMonitor callback');
W
wusongqing 已提交
252 253 254 255 256
});
```

### waitAbilityMonitor<sup>9+</sup>

257
waitAbilityMonitor(monitor: AbilityMonitor, timeout: number, callback: AsyncCallback\<UIAbility>): void;
W
wusongqing 已提交
258

259
Waits a period of time for the **Ability** instance that matches the **AbilityMonitor** instance to reach the **OnCreate** lifecycle state and returns the **Ability** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
260 261 262 263 264 265 266

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
267 268 269
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
| timeout  | number                                                       | No  | Maximum waiting time, in milliseconds.                                |
| callback | AsyncCallback\<[UIAbility](js-apis-app-ability-uiAbility.md)> | Yes  | Callback used to return the result.                                          |
W
wusongqing 已提交
270

G
Gloria 已提交
271 272 273 274 275 276 277 278
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | WaitAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
279 280
**Example**

281
```ts
G
Gloria 已提交
282
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
283
let timeout = 100;
W
wusongqing 已提交
284

W
wusongqing 已提交
285
function onAbilityCreateCallback(data) {
286
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
287 288
}

289 290
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
291
    onAbilityCreate: onAbilityCreateCallback
292
};
W
wusongqing 已提交
293 294

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
295
abilityDelegator.waitAbilityMonitor(monitor, timeout, (err : any, data : any) => {
296
    console.info('waitAbilityMonitor callback');
W
wusongqing 已提交
297 298 299 300 301 302 303
});
```



### waitAbilityMonitor<sup>9+</sup>

304
waitAbilityMonitor(monitor: AbilityMonitor, timeout?: number): Promise\<UIAbility>;
W
wusongqing 已提交
305

306
Waits a period of time for the **Ability** instance that matches the **AbilityMonitor** instance to reach the **OnCreate** lifecycle state and returns the **Ability** instance. This API uses a promise to return the result.
W
wusongqing 已提交
307 308 309 310 311 312 313

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
314
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
315 316 317 318 319 320
| timeout | number                                                       | No  | Maximum waiting time, in milliseconds.                                |

**Return value**

| Type                                                       | Description                      |
| ----------------------------------------------------------- | -------------------------- |
321
| Promise\<[UIAbility](js-apis-app-ability-uiAbility.md)> | Promise used to return the **Ability** instance.|
W
wusongqing 已提交
322

G
Gloria 已提交
323 324 325 326 327 328 329 330
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | WaitAbilityMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
331 332
**Example**

333
```ts
G
Gloria 已提交
334
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
335

W
wusongqing 已提交
336
function onAbilityCreateCallback(data) {
337
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
338 339
}

340 341
let monitor = {
    abilityName: 'abilityname',
W
wusongqing 已提交
342
    onAbilityCreate: onAbilityCreateCallback
343
};
W
wusongqing 已提交
344 345 346

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor).then((data : any) => {
347
    console.info('waitAbilityMonitor promise');
W
wusongqing 已提交
348 349 350 351 352 353 354
});
```



### getAppContext<sup>9+</sup>

355
getAppContext(): Context;
W
wusongqing 已提交
356 357 358 359 360 361 362 363 364

Obtains the application context.

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

**Return value**

| Type                                 | Description                                       |
| ------------------------------------- | ------------------------------------------- |
365
| [Context](js-apis-inner-application-context.md) | Application [context](js-apis-inner-application-context.md).|
W
wusongqing 已提交
366 367 368

**Example**

369
```ts
G
Gloria 已提交
370
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
W
wusongqing 已提交
371 372

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
373
let context = abilityDelegator.getAppContext();
W
wusongqing 已提交
374 375 376 377 378 379
```



### getAbilityState<sup>9+</sup>

380
getAbilityState(ability: UIAbility): number;
W
wusongqing 已提交
381 382 383 384 385 386 387 388 389

Obtains the lifecycle state of an ability.

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

**Parameters**

| Name | Type                                             | Mandatory| Description           |
| ------- | ------------------------------------------------- | ---- | --------------- |
390
| ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes  | Target ability.|
W
wusongqing 已提交
391 392 393 394 395

**Return value**

| Type  | Description                                                        |
| ------ | ------------------------------------------------------------ |
396
| number | Lifecycle state of the ability. For details about the available enumerated values, see [AbilityLifecycleState](js-apis-application-abilityDelegatorRegistry.md#AbilityLifecycleState).|
W
wusongqing 已提交
397 398 399

**Example**

400
```ts
G
Gloria 已提交
401
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
402
let ability;
W
wusongqing 已提交
403 404

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
405
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
406
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
407
    ability = data;
408 409
    let state = abilityDelegator.getAbilityState(ability);
    console.info('getAbilityState' + state);
W
wusongqing 已提交
410 411 412 413 414 415 416
});
```



### getCurrentTopAbility<sup>9+</sup>

417
getCurrentTopAbility(callback: AsyncCallback\<UIAbility>): void;
W
wusongqing 已提交
418

419
Obtains the top ability of this application. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
420 421 422 423 424 425 426

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description              |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
427
| callback | AsyncCallback\<[UIAbility](js-apis-app-ability-uiAbility.md)> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
428

G
Gloria 已提交
429 430 431 432 433 434 435 436
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | GetCurrentTopAbility failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
437 438
**Example**

439
```ts
G
Gloria 已提交
440
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
441
let ability;
W
wusongqing 已提交
442 443

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
444
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
445
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
446 447 448 449 450 451 452 453
    ability = data;
});
```



### getCurrentTopAbility<sup>9+</sup>

454
getCurrentTopAbility(): Promise\<UIAbility>;
W
wusongqing 已提交
455

456
Obtains the top ability of this application. This API uses a promise to return the result.
W
wusongqing 已提交
457 458 459 460 461 462 463

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

**Return value**

| Type                                                       | Description                                  |
| ----------------------------------------------------------- | -------------------------------------- |
464
| Promise\<[UIAbility](js-apis-app-ability-uiAbility.md)> | Promise used to return the top ability.|
W
wusongqing 已提交
465

G
Gloria 已提交
466 467 468 469 470 471 472 473
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | GetCurrentTopAbility failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
474 475
**Example**

476
```ts
G
Gloria 已提交
477
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
478
let ability;
W
wusongqing 已提交
479 480 481

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility().then((data : any) => {
482
    console.info('getCurrentTopAbility promise');
W
wusongqing 已提交
483 484 485 486 487 488
    ability = data;
});
```



W
wusongqing 已提交
489
### startAbility<sup>9+</sup>
W
wusongqing 已提交
490

491
startAbility(want: Want, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
492 493 494 495 496 497 498 499 500

Starts an ability. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                  | Mandatory| Description              |
| -------- | -------------------------------------- | ---- | ------------------ |
501
| want     | [Want](js-apis-application-want.md) | Yes  | **Want** parameter for starting the ability.   |
W
wusongqing 已提交
502 503
| callback | AsyncCallback\<void>                   | Yes  | Callback used to return the result.|

G
Gloria 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
524 525
**Example**

526
```ts
G
Gloria 已提交
527
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
528 529 530
let want = {
    bundleName: 'bundleName',
    abilityName: 'abilityName'
W
wusongqing 已提交
531 532 533
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
534
abilityDelegator.startAbility(want, (err : any, data : any) => {
535
    console.info('startAbility callback');
W
wusongqing 已提交
536 537 538 539 540
});
```



W
wusongqing 已提交
541
### startAbility<sup>9+</sup>
W
wusongqing 已提交
542

543
startAbility(want: Want): Promise\<void>;
W
wusongqing 已提交
544 545 546 547 548 549 550 551 552

Starts an ability. This API uses a promise to return the result.

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

**Parameters**

| Name| Type                                  | Mandatory| Description           |
| ------ | -------------------------------------- | ---- | --------------- |
553
| want   | [Want](js-apis-application-want.md) | Yes  | **Want** parameter for starting the ability.|
W
wusongqing 已提交
554 555 556 557 558 559 560

**Return value**

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

G
Gloria 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
581 582
**Example**

583
```ts
G
Gloria 已提交
584
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
585 586 587
let want = {
    bundleName: 'bundleName',
    abilityName: 'abilityName'
W
wusongqing 已提交
588 589 590 591
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.startAbility(want).then((data: any) => {
592
    console.info('startAbility promise');
W
wusongqing 已提交
593 594 595 596 597 598 599
});
```



### doAbilityForeground<sup>9+</sup>

600
doAbilityForeground(ability: UIAbility, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
601 602 603 604 605 606 607 608 609

Schedules the lifecycle state of an ability to **Foreground**. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                   | Mandatory| Description                                                   |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
610 611
| ability  | UIAbility               | Yes  | Target ability.                                        |
| callback | AsyncCallback\<void>    | Yes  | Callback used to return the result.<br>\- **true**: The operation is successful.<br>\- **false**: The operation failed.|
W
wusongqing 已提交
612

G
Gloria 已提交
613 614 615 616 617 618 619 620
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | DoAbilityForeground failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
621 622
**Example**

623
```ts
G
Gloria 已提交
624
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
625
let ability;
W
wusongqing 已提交
626 627

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
628
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
629
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
630
    ability = data;
G
Gloria 已提交
631 632
    abilityDelegator.doAbilityForeground(ability, (err : any) => {
        console.info("doAbilityForeground callback");
W
wusongqing 已提交
633 634 635 636 637 638 639 640
    });
});
```



### doAbilityForeground<sup>9+</sup>

641
doAbilityForeground(ability: UIAbility): Promise\<void>;
W
wusongqing 已提交
642 643 644 645 646 647 648 649 650

Schedules the lifecycle state of an ability to **Foreground**. This API uses a promise to return the result.

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

**Parameters**

| Name | Type   | Mandatory| Description           |
| ------- | ------- | ---- | --------------- |
651
| ability | UIAbility | Yes  | Target ability.|
W
wusongqing 已提交
652 653 654 655 656

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
657
| Promise\<boolean> | Promise used to return the result.<br>\- **true**: The operation is successful.<br>\- **false**: The operation failed.|
W
wusongqing 已提交
658

G
Gloria 已提交
659 660 661 662 663 664 665 666
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | DoAbilityForeground failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
667 668
**Example**

669
```ts
G
Gloria 已提交
670
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
671
let ability;
W
wusongqing 已提交
672 673

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
674
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
675
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
676
    ability = data;
G
Gloria 已提交
677 678
    abilityDelegator.doAbilityForeground(ability).then(() => {
        console.info("doAbilityForeground promise");
W
wusongqing 已提交
679 680 681 682 683 684 685 686
    });
});
```



### doAbilityBackground<sup>9+</sup>

687
doAbilityBackground(ability: UIAbility, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
688 689 690 691 692 693 694 695 696

Schedules the lifecycle state of an ability to **Background**. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                   | Mandatory| Description                                                   |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
697 698
| ability  | UIAbility                 | Yes  | Target ability.                                        |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.<br>\- **true**: The operation is successful.<br>\- **false**: The operation failed.|
W
wusongqing 已提交
699

G
Gloria 已提交
700 701 702 703 704 705 706 707
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | DoAbilityBackground failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
708 709
**Example**

710
```ts
G
Gloria 已提交
711
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
712
let ability;
W
wusongqing 已提交
713 714

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
715
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
716
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
717
    ability = data;
G
Gloria 已提交
718 719
    abilityDelegator.doAbilityBackground(ability, (err : any) => {
        console.info("doAbilityBackground callback");
W
wusongqing 已提交
720 721 722 723 724 725 726 727
    });
});
```



### doAbilityBackground<sup>9+</sup>

728
doAbilityBackground(ability: UIAbility): Promise\<void>;
W
wusongqing 已提交
729 730 731 732 733 734 735 736 737

Schedules the lifecycle state of an ability to **Background**. This API uses a promise to return the result.

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

**Parameters**

| Name | Type   | Mandatory| Description           |
| ------- | ------- | ---- | --------------- |
738
| ability | UIAbility | Yes  | Target ability.|
W
wusongqing 已提交
739 740 741 742 743

**Return value**

| Type             | Description                                                        |
| ----------------- | ------------------------------------------------------------ |
744
| Promise\<boolean> | Promise used to return the result.<br>\- **true**: The operation is successful.<br>\- **false**: The operation failed.|
W
wusongqing 已提交
745

G
Gloria 已提交
746 747 748 749 750 751 752 753
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | DoAbilityBackground failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
754 755
**Example**

756
```ts
G
Gloria 已提交
757
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
758
let ability;
W
wusongqing 已提交
759 760

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
761
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
762
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
763
    ability = data;
G
Gloria 已提交
764 765
    abilityDelegator.doAbilityBackground(ability).then(() => {
        console.info("doAbilityBackground promise");
W
wusongqing 已提交
766 767 768 769 770 771
    });
});
```



772 773
### printSync<sup>9+</sup>

774
printSync(msg: string): void;
775 776 777 778 779 780 781 782 783 784 785 786 787

Prints log information to the unit test console.

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

**Parameters**

| Name| Type  | Mandatory| Description      |
| ------ | ------ | ---- | ---------- |
| msg    | string | Yes  | Log string.|

**Example**

788
```ts
G
Gloria 已提交
789
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
790
let msg = 'msg';
791 792 793 794 795 796 797

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.printSync(msg);
```



W
wusongqing 已提交
798 799
### print

800
print(msg: string, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814

Prints log information to the unit test console. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                | Mandatory| Description              |
| -------- | -------------------- | ---- | ------------------ |
| msg      | string               | Yes  | Log string.        |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

815
```ts
G
Gloria 已提交
816
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
817
let msg = 'msg';
W
wusongqing 已提交
818 819

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
820
abilityDelegator.print(msg, (err : any) => {
821
    console.info('print callback');
W
wusongqing 已提交
822 823 824 825 826 827 828
});
```



### print

829
print(msg: string): Promise\<void>;
W
wusongqing 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

Prints log information to the unit test console. This API uses a promise to return the result.

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

**Parameters**

| Name| Type  | Mandatory| Description      |
| ------ | ------ | ---- | ---------- |
| msg    | string | Yes  | Log string.|

**Return value**

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

**Example**

849
```ts
G
Gloria 已提交
850
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
851
let msg = 'msg';
W
wusongqing 已提交
852 853 854

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg).then(() => {
855
    console.info('print promise');
W
wusongqing 已提交
856 857 858 859 860 861 862
});
```



### executeShellCommand

863
executeShellCommand(cmd: string, callback: AsyncCallback\<ShellCmdResult>): void;
W
wusongqing 已提交
864 865 866 867 868 869 870 871 872 873

Executes a shell command. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description              |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
| cmd      | string                                                       | Yes  | Shell command string.   |
874
| callback | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
875 876 877

**Example**

878
```ts
G
Gloria 已提交
879
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
880
let cmd = 'cmd';
W
wusongqing 已提交
881 882

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
883
abilityDelegator.executeShellCommand(cmd, (err : any, data : any) => {
884
    console.info('executeShellCommand callback');
W
wusongqing 已提交
885 886 887 888 889 890 891
});
```



### executeShellCommand

892
executeShellCommand(cmd: string, timeoutSecs: number, callback: AsyncCallback\<ShellCmdResult>): void;
W
wusongqing 已提交
893 894 895 896 897 898 899 900 901 902

Executes a shell command with the timeout period specified. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name     | Type                                                        | Mandatory| Description                         |
| ----------- | ------------------------------------------------------------ | ---- | ----------------------------- |
| cmd         | string                                                       | Yes  | Shell command string.              |
903 904
| timeoutSecs | number                                                       | No  | Command timeout period, in seconds.|
| callback    | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | Yes  | Callback used to return the result.           |
W
wusongqing 已提交
905 906 907

**Example**

908
```ts
G
Gloria 已提交
909
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
910 911
let cmd = 'cmd';
let timeout = 100;
W
wusongqing 已提交
912 913

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
914
abilityDelegator.executeShellCommand(cmd, timeout, (err : any, data : any) => {
915
    console.info('executeShellCommand callback');
W
wusongqing 已提交
916 917 918 919 920 921 922
});
```



### executeShellCommand

923
executeShellCommand(cmd: string, timeoutSecs?: number): Promise\<ShellCmdResult>;
W
wusongqing 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

Executes a shell command with the timeout period specified. This API uses a promise to return the result.

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

**Parameters**

| Name     | Type  | Mandatory| Description                         |
| ----------- | ------ | ---- | ----------------------------- |
| cmd         | string | Yes  | Shell command string.              |
| timeoutSecs | number | No  | Command timeout period, in seconds.|

**Return value**

| Type                                                        | Description                                                        |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
940
| Promise\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | Promise used to return a [ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult) object.|
W
wusongqing 已提交
941 942 943

**Example**

944
```ts
G
Gloria 已提交
945
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
946 947
let cmd = 'cmd';
let timeout = 100;
W
wusongqing 已提交
948 949 950

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, timeout).then((data : any) => {
951
    console.info('executeShellCommand promise');
W
wusongqing 已提交
952 953
});
```
W
wusongqing 已提交
954 955 956 957 958



### finishTest<sup>9+</sup>

959
finishTest(msg: string, code: number, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972

Finishes the test and prints log information to the unit test console. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                | Mandatory| Description              |
| -------- | -------------------- | ---- | ------------------ |
| msg      | string               | Yes  | Log string.        |
| code     | number               | Yes  | Log code.            |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

G
Gloria 已提交
973 974 975 976 977 978 979 980
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | FinishTest failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
981 982
**Example**

983
```ts
G
Gloria 已提交
984
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
985
let msg = 'msg';
W
wusongqing 已提交
986 987 988

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0, (err : any) => {
989
    console.info('finishTest callback');
W
wusongqing 已提交
990 991 992 993 994 995 996
});
```



### finishTest<sup>9+</sup>

997
finishTest(msg: string, code: number): Promise\<void>;
W
wusongqing 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

Finishes the test and prints log information to the unit test console. This API uses a promise to return the result.

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

**Parameters**

| Name| Type  | Mandatory| Description      |
| ------ | ------ | ---- | ---------- |
| msg    | string | Yes  | Log string.|
| code   | number | Yes  | Log code.    |

**Return value**

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

G
Gloria 已提交
1016 1017 1018 1019 1020 1021 1022 1023
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | FinishTest failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
1024 1025
**Example**

1026
```ts
G
Gloria 已提交
1027
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1028
let msg = 'msg';
W
wusongqing 已提交
1029 1030 1031

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0).then(() => {
1032
    console.info('finishTest promise');
W
wusongqing 已提交
1033 1034
});
```
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

### addAbilityStageMonitor<sup>9+</sup>

addAbilityStageMonitor(monitor: AbilityStageMonitor, callback: AsyncCallback\<void>): void;

Adds an **AbilityStageMonitor** instance to monitor the lifecycle state changes of an ability stage. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
1048
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes      | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1049 1050
| callback | AsyncCallback\<void>                                         | Yes      | Callback used to return the result.                                          |

G
Gloria 已提交
1051 1052 1053 1054 1055 1056 1057 1058
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | AddAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1059 1060
**Example**

1061
```ts
G
Gloria 已提交
1062
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1063

1064 1065 1066 1067
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1068 1069 1070

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor, (err : any) => {
1071
    console.info('addAbilityStageMonitor callback');
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
});
```



### addAbilityStageMonitor<sup>9+</sup>

addAbilityStageMonitor(monitor: AbilityStageMonitor): Promise\<void>;

Adds an **AbilityStageMonitor** instance to monitor the lifecycle state changes of an ability stage. This API uses a promise to return the result.

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1089
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1090 1091 1092 1093 1094 1095 1096

**Return value**

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

G
Gloria 已提交
1097 1098 1099 1100 1101 1102 1103 1104
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | AddAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1105 1106
**Example**

1107
```ts
G
Gloria 已提交
1108
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1109

1110 1111 1112 1113
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1114 1115 1116

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor).then(() => {
1117
    console.info('addAbilityStageMonitor promise');
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
});
```

### removeAbilityStageMonitor<sup>9+</sup>

removeAbilityStageMonitor(monitor: AbilityStageMonitor, callback: AsyncCallback\<void>): void;

Removes an **AbilityStageMonitor** instance from the application memory. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
1133
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes      | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1134 1135
| callback | AsyncCallback\<void>                                         | Yes      | Callback used to return the result.                                          |

G
Gloria 已提交
1136 1137 1138 1139 1140 1141 1142 1143
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | RemoveAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1144 1145
**Example**

1146
```ts
G
Gloria 已提交
1147
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1148

1149 1150 1151 1152
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1153 1154 1155

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor, (err : any) => {
1156
    console.info('removeAbilityStageMonitor callback');
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
});
```



### removeAbilityStageMonitor<sup>9+</sup>

removeAbilityStageMonitor(monitor: AbilityStageMonitor): Promise\<void>;

Removes an **AbilityStageMonitor** object from the application memory. This API uses a promise to return the result.

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1174
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1175 1176 1177 1178 1179 1180 1181

**Return value**

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

G
Gloria 已提交
1182 1183 1184 1185 1186 1187 1188 1189
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | RemoveAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1190 1191
**Example**

1192
```ts
G
Gloria 已提交
1193
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1194

1195 1196 1197 1198
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1199 1200 1201

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor).then(() => {
1202
    console.info('removeAbilityStageMonitor promise');
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
});
```

### waitAbilityStageMonitor<sup>9+</sup>

waitAbilityStageMonitor(monitor: AbilityStageMonitor, callback: AsyncCallback\<AbilityStage>): void;

Waits for an **AbilityStage** instance that matches the conditions set in an **AbilityStageMonitor** instance and returns the **AbilityStage** instance. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
1218
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes      | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1219 1220
| callback | AsyncCallback\<AbilityStage>                                         | Yes      | Callback used to return the result. If the operation is successful, an **AbilityStage** instance is returned. Otherwise, no value is returned.            |

G
Gloria 已提交
1221 1222 1223 1224 1225 1226 1227 1228
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | WaitAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1229 1230
**Example**

1231
```ts
G
Gloria 已提交
1232
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1233 1234

function onAbilityCreateCallback(data) {
1235
    console.info('onAbilityCreateCallback');
1236 1237
}

1238 1239 1240 1241
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1242 1243 1244

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor, (err : any, data : any) => {
1245
    console.info('waitAbilityStageMonitor callback');
1246 1247
});
```
1248

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
### waitAbilityStageMonitor<sup>9+</sup>

waitAbilityStageMonitor(monitor: AbilityStageMonitor, timeout?: number): Promise\<AbilityStage>;

Waits for an **AbilityStage** instance that matches the conditions set in an **AbilityStageMonitor** instance and returns the **AbilityStage** instance. This API uses a promise to return the result.

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1261
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1262 1263 1264 1265 1266 1267 1268 1269
| timeout | number | No  | Maximum waiting time, in milliseconds.|

**Return value**

| Type          | Description               |
| -------------- | ------------------- |
| Promise\<AbilityStage> | Promise used to return the result. If the operation is successful, an **AbilityStage** instance is returned. Otherwise, no value is returned.|

G
Gloria 已提交
1270 1271 1272 1273 1274 1275 1276 1277
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | WaitAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1278 1279
**Example**

1280
```ts
G
Gloria 已提交
1281
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1282 1283

function onAbilityCreateCallback(data) {
1284
    console.info('onAbilityCreateCallback');
1285 1286
}

1287 1288 1289 1290
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1291 1292 1293

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor).then((data : any) => {
1294
    console.info('waitAbilityStageMonitor promise');
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
});
```

### waitAbilityStageMonitor<sup>9+</sup>

waitAbilityStageMonitor(monitor: AbilityStageMonitor, timeout: number, callback: AsyncCallback\<AbilityStage>): void;

Waits a period of time for an **AbilityStage** instance that matches the conditions set in an **AbilityStageMonitor** instance and returns the **AbilityStage** instance. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1310
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1311 1312 1313
| timeout | number | No  | Maximum waiting time, in milliseconds.|
| callback | AsyncCallback\<AbilityStage>                                         | Yes      | Callback used to return the result. If the operation is successful, an **AbilityStage** instance is returned. Otherwise, no value is returned.                    |

G
Gloria 已提交
1314 1315 1316 1317 1318 1319 1320 1321
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000100 | WaitAbilityStageMonitor failed. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1322 1323
**Example**

1324
```ts
G
Gloria 已提交
1325
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1326
let timeout = 100;
1327 1328

function onAbilityCreateCallback(data) {
1329
    console.info('onAbilityCreateCallback');
1330 1331
}

1332 1333 1334 1335
let monitor = {
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1336 1337 1338

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor, timeout, (err : any, data : any) => {
1339
    console.info('waitAbilityStageMonitor callback');
1340 1341
});
```