js-apis-inner-application-abilityDelegator.md 38.7 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.

9
## Usage
W
wusongqing 已提交
10

11
An **AbilityDelegator** object is obtained by calling [getAbilityDelegator](js-apis-app-ability-abilityDelegatorRegistry.md#abilitydelegatorregistrygetabilitydelegator) in **AbilityDelegatorRegistry**.
12
```ts
G
Gloria 已提交
13
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
W
wusongqing 已提交
14 15 16 17 18 19
```

## AbilityDelegator

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

20
addAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
21 22 23 24 25 26 27 28 29

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                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
30
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes      | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
31 32 33 34
| callback | AsyncCallback\<void>                                         | Yes      | Callback used to return the result.                                          |

**Example**

35
```ts
G
Gloria 已提交
36
let abilityDelegator;
W
wusongqing 已提交
37

W
wusongqing 已提交
38
function onAbilityCreateCallback(data) {
39
    console.info('onAbilityCreateCallback, data: ${JSON.stringify(data)}');
W
wusongqing 已提交
40 41
}

G
Gloria 已提交
42
let monitor = {
43
    abilityName: 'abilityname',
W
wusongqing 已提交
44
    onAbilityCreate: onAbilityCreateCallback
45
};
W
wusongqing 已提交
46 47

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
48 49
abilityDelegator.addAbilityMonitor(monitor, (error : any) => {
    console.error('addAbilityMonitor fail, error: ${JSON.stringify(error)}');
W
wusongqing 已提交
50 51 52 53 54
});
```

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

55
addAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
W
wusongqing 已提交
56 57 58 59 60 61 62 63 64

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

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
65
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
66 67 68 69 70 71 72 73 74

**Return value**

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

**Example**

75
```ts
G
Gloria 已提交
76
let abilityDelegator;
W
wusongqing 已提交
77

W
wusongqing 已提交
78
function onAbilityCreateCallback(data) {
79
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
80 81
}

G
Gloria 已提交
82
let monitor = {
83
    abilityName: 'abilityname',
W
wusongqing 已提交
84
    onAbilityCreate: onAbilityCreateCallback
85
};
W
wusongqing 已提交
86 87 88

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor).then(() => {
89
    console.info('addAbilityMonitor promise');
W
wusongqing 已提交
90 91 92 93 94
});
```

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

95
removeAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
96 97 98 99 100 101 102 103 104

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                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
105
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
106 107 108 109
| callback | AsyncCallback\<void>                                         | Yes  | Callback used to return the result.                                          |

**Example**

110
```ts
G
Gloria 已提交
111
let abilityDelegator;
W
wusongqing 已提交
112

W
wusongqing 已提交
113
function onAbilityCreateCallback(data) {
114
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
115 116
}

G
Gloria 已提交
117
let monitor = {
118
    abilityName: 'abilityname',
W
wusongqing 已提交
119
    onAbilityCreate: onAbilityCreateCallback
120
};
W
wusongqing 已提交
121 122

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
123 124
abilityDelegator.removeAbilityMonitor(monitor, (error : any) => {
    console.error('removeAbilityMonitor fail, error: ${JSON.stringify(error)}');
W
wusongqing 已提交
125 126 127 128 129
});
```

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

130
removeAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
W
wusongqing 已提交
131 132 133 134 135 136 137 138 139

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

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

**Parameters**

| Name   | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
140
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
141 142 143 144 145 146 147 148 149

**Return value**

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

- Example

150
```ts
G
Gloria 已提交
151
let abilityDelegator;
W
wusongqing 已提交
152

W
wusongqing 已提交
153
function onAbilityCreateCallback(data) {
154
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
155 156
}

G
Gloria 已提交
157
let monitor = {
158
    abilityName: 'abilityname',
W
wusongqing 已提交
159
    onAbilityCreate: onAbilityCreateCallback
160
};
W
wusongqing 已提交
161 162 163

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityMonitor(monitor).then(() => {
164
    console.info('removeAbilityMonitor promise');
W
wusongqing 已提交
165 166 167 168 169
});
```

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

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

172
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 已提交
173 174 175 176 177 178 179

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
180 181
| 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 已提交
182 183 184

**Example**

185
```ts
G
Gloria 已提交
186
let abilityDelegator;
W
wusongqing 已提交
187

W
wusongqing 已提交
188
function onAbilityCreateCallback(data) {
189
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
190 191
}

G
Gloria 已提交
192
let monitor = {
193
    abilityName: 'abilityname',
W
wusongqing 已提交
194
    onAbilityCreate: onAbilityCreateCallback
195
};
W
wusongqing 已提交
196 197

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
198 199 200 201 202 203
abilityDelegator.waitAbilityMonitor(monitor, (error : any, data : any) => {
    if (error && error.code !== 0) {
        console.error('waitAbilityMonitor fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('waitAbilityMonitor success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
204 205 206 207 208
});
```

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

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

211
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 已提交
212 213 214 215 216 217 218

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
219 220 221
| 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 已提交
222 223 224

**Example**

225
```ts
G
Gloria 已提交
226 227
let abilityDelegator;
let timeout = 100;
W
wusongqing 已提交
228

W
wusongqing 已提交
229
function onAbilityCreateCallback(data) {
230
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
231 232
}

G
Gloria 已提交
233
let monitor = {
234
    abilityName: 'abilityname',
W
wusongqing 已提交
235
    onAbilityCreate: onAbilityCreateCallback
236
};
W
wusongqing 已提交
237 238

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
239 240 241 242 243 244
abilityDelegator.waitAbilityMonitor(monitor, timeout, (error : any, data : any) => {
    if (error && error.code !== 0) {
        console.error('waitAbilityMonitor fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('waitAbilityMonitor success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
245 246 247 248 249 250 251
});
```



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

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

254
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 已提交
255 256 257 258 259 260 261

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

**Parameters**

| Name | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
262
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | Yes  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) instance.|
W
wusongqing 已提交
263 264 265 266 267 268
| timeout | number                                                       | No  | Maximum waiting time, in milliseconds.                                |

**Return value**

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

**Example**

273
```ts
G
Gloria 已提交
274
let abilityDelegator;
W
wusongqing 已提交
275

W
wusongqing 已提交
276
function onAbilityCreateCallback(data) {
277
    console.info('onAbilityCreateCallback');
W
wusongqing 已提交
278 279
}

G
Gloria 已提交
280
let monitor = {
281
    abilityName: 'abilityname',
W
wusongqing 已提交
282
    onAbilityCreate: onAbilityCreateCallback
283
};
W
wusongqing 已提交
284 285 286

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor).then((data : any) => {
287
    console.info('waitAbilityMonitor promise');
W
wusongqing 已提交
288 289 290 291 292
});
```

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

293
getAppContext(): Context;
W
wusongqing 已提交
294 295 296 297 298 299 300 301 302

Obtains the application context.

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

**Return value**

| Type                                 | Description                                       |
| ------------------------------------- | ------------------------------------------- |
303
| [Context](js-apis-inner-application-context.md) | Application [context](js-apis-inner-application-context.md).|
W
wusongqing 已提交
304 305 306

**Example**

307
```ts
G
Gloria 已提交
308
let abilityDelegator;
W
wusongqing 已提交
309 310

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
G
Gloria 已提交
311
let context = abilityDelegator.getAppContext();
W
wusongqing 已提交
312 313 314 315
```

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

316
getAbilityState(ability: UIAbility): number;
W
wusongqing 已提交
317 318 319 320 321 322 323 324 325

Obtains the lifecycle state of an ability.

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

**Parameters**

| Name | Type                                             | Mandatory| Description           |
| ------- | ------------------------------------------------- | ---- | --------------- |
326
| ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes  | Target ability.|
W
wusongqing 已提交
327 328 329 330 331

**Return value**

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

**Example**

336
```ts
G
Gloria 已提交
337 338
let abilityDelegator;
let ability;
W
wusongqing 已提交
339 340

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
341
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
342
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
343
    ability = data;
G
Gloria 已提交
344
    let state = abilityDelegator.getAbilityState(ability);
345
    console.info('getAbilityState ${state}');
W
wusongqing 已提交
346 347 348 349 350
});
```

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

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

353
Obtains the top ability of this application. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
354 355 356 357 358 359 360

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

**Parameters**

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

**Example**

365
```ts
G
Gloria 已提交
366 367
let abilityDelegator;
let ability;
W
wusongqing 已提交
368 369

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
370
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
371
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
372 373 374 375 376 377
    ability = data;
});
```

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

378
getCurrentTopAbility(): Promise\<UIAbility>;
W
wusongqing 已提交
379

380
Obtains the top ability of this application. This API uses a promise to return the result.
W
wusongqing 已提交
381 382 383 384 385 386 387

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

**Return value**

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

**Example**

392
```ts
G
Gloria 已提交
393 394
let abilityDelegator;
let ability;
W
wusongqing 已提交
395 396 397

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility().then((data : any) => {
398
    console.info('getCurrentTopAbility promise');
W
wusongqing 已提交
399 400 401 402
    ability = data;
});
```

W
wusongqing 已提交
403
### startAbility<sup>9+</sup>
W
wusongqing 已提交
404

405
startAbility(want: Want, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
406 407 408 409 410 411 412 413 414

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

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

**Parameters**

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

**Example**

420
```ts
G
Gloria 已提交
421 422
let abilityDelegator;
let want = {
423 424
    bundleName: 'bundleName',
    abilityName: 'abilityName'
W
wusongqing 已提交
425 426 427
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
428
abilityDelegator.startAbility(want, (err : any, data : any) => {
429
    console.info('startAbility callback');
W
wusongqing 已提交
430 431 432
});
```

W
wusongqing 已提交
433
### startAbility<sup>9+</sup>
W
wusongqing 已提交
434

435
startAbility(want: Want): Promise\<void>;
W
wusongqing 已提交
436 437 438 439 440 441 442 443 444

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

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

**Parameters**

| Name| Type                                  | Mandatory| Description           |
| ------ | -------------------------------------- | ---- | --------------- |
445
| want   | [Want](js-apis-application-want.md) | Yes  | **Want** parameter for starting the ability.|
W
wusongqing 已提交
446 447 448 449 450 451 452 453 454

**Return value**

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

**Example**

455
```ts
G
Gloria 已提交
456 457
let abilityDelegator;
let want = {
458 459
    bundleName: 'bundleName',
    abilityName: 'abilityName'
W
wusongqing 已提交
460 461 462 463
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.startAbility(want).then((data: any) => {
464
    console.info('startAbility promise');
W
wusongqing 已提交
465 466 467 468 469
});
```

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

470
doAbilityForeground(ability: UIAbility, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
471 472 473 474 475 476 477 478 479

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                                                   |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
480 481
| 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 已提交
482 483 484

**Example**

485
```ts
G
Gloria 已提交
486 487
let abilityDelegator;
let ability;
W
wusongqing 已提交
488 489

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
490
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
491
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
492
    ability = data;
G
Gloria 已提交
493 494
    abilityDelegator.doAbilityForeground(ability, (err : any) => {
        console.info("doAbilityForeground callback");
W
wusongqing 已提交
495 496 497 498 499 500
    });
});
```

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

501
doAbilityForeground(ability: UIAbility): Promise\<void>;
W
wusongqing 已提交
502 503 504 505 506 507 508 509 510

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           |
| ------- | ------- | ---- | --------------- |
511
| ability | UIAbility | Yes  | Target ability.|
W
wusongqing 已提交
512 513 514 515 516

**Return value**

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

**Example**

521
```ts
G
Gloria 已提交
522 523
let abilityDelegator;
let ability;
W
wusongqing 已提交
524 525

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
526
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
527
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
528
    ability = data;
G
Gloria 已提交
529 530
    abilityDelegator.doAbilityForeground(ability).then(() => {
        console.info("doAbilityForeground promise");
W
wusongqing 已提交
531 532 533 534 535 536
    });
});
```

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

537
doAbilityBackground(ability: UIAbility, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
538 539 540 541 542 543 544 545 546

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                                                   |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
547 548
| 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 已提交
549 550 551

**Example**

552
```ts
G
Gloria 已提交
553 554
let abilityDelegator;
let ability;
W
wusongqing 已提交
555 556

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
557
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
558
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
559
    ability = data;
G
Gloria 已提交
560 561
    abilityDelegator.doAbilityBackground(ability, (err : any) => {
        console.info("doAbilityBackground callback");
W
wusongqing 已提交
562 563 564 565 566 567
    });
});
```

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

568
doAbilityBackground(ability: UIAbility): Promise\<void>;
W
wusongqing 已提交
569 570 571 572 573 574 575 576 577

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           |
| ------- | ------- | ---- | --------------- |
578
| ability | UIAbility | Yes  | Target ability.|
W
wusongqing 已提交
579 580 581 582 583

**Return value**

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

**Example**

588
```ts
G
Gloria 已提交
589 590
let abilityDelegator;
let ability;
W
wusongqing 已提交
591 592

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
593
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
594
    console.info('getCurrentTopAbility callback');
W
wusongqing 已提交
595
    ability = data;
G
Gloria 已提交
596 597
    abilityDelegator.doAbilityBackground(ability).then(() => {
        console.info("doAbilityBackground promise");
W
wusongqing 已提交
598 599 600 601
    });
});
```

602 603
### printSync<sup>9+</sup>

604
printSync(msg: string): void;
605 606 607 608 609 610 611 612 613 614 615 616 617

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

618
```ts
G
Gloria 已提交
619
let abilityDelegator;
620
let msg = 'msg';
621 622 623 624 625

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

W
wusongqing 已提交
626 627
### print

628
print(msg: string, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642

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

643
```ts
G
Gloria 已提交
644
let abilityDelegator;
645
let msg = 'msg';
W
wusongqing 已提交
646 647

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
648
abilityDelegator.print(msg, (err : any) => {
649
    console.info('print callback');
W
wusongqing 已提交
650 651 652 653 654
});
```

### print

655
print(msg: string): Promise\<void>;
W
wusongqing 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

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

675
```ts
G
Gloria 已提交
676
let abilityDelegator;
677
let msg = 'msg';
W
wusongqing 已提交
678 679 680

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg).then(() => {
681
    console.info('print promise');
W
wusongqing 已提交
682 683 684 685 686
});
```

### executeShellCommand

687
executeShellCommand(cmd: string, callback: AsyncCallback\<ShellCmdResult>): void;
W
wusongqing 已提交
688 689 690 691 692 693 694 695 696 697

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.   |
698
| callback | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
699 700 701

**Example**

702
```ts
G
Gloria 已提交
703
let abilityDelegator;
704
let cmd = 'cmd';
W
wusongqing 已提交
705 706

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
707
abilityDelegator.executeShellCommand(cmd, (err : any, data : any) => {
708
    console.info('executeShellCommand callback');
W
wusongqing 已提交
709 710 711 712 713
});
```

### executeShellCommand

714
executeShellCommand(cmd: string, timeoutSecs: number, callback: AsyncCallback\<ShellCmdResult>): void;
W
wusongqing 已提交
715 716 717 718 719 720 721 722 723 724

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.              |
725 726
| 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 已提交
727 728 729

**Example**

730
```ts
G
Gloria 已提交
731
let abilityDelegator;
732
let cmd = 'cmd';
G
Gloria 已提交
733
let timeout = 100;
W
wusongqing 已提交
734 735

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
W
wusongqing 已提交
736
abilityDelegator.executeShellCommand(cmd, timeout, (err : any, data : any) => {
737
    console.info('executeShellCommand callback');
W
wusongqing 已提交
738 739 740 741 742
});
```

### executeShellCommand

743
executeShellCommand(cmd: string, timeoutSecs?: number): Promise\<ShellCmdResult>;
W
wusongqing 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

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                                                        |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
760
| 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 已提交
761 762 763

**Example**

764
```ts
G
Gloria 已提交
765
let abilityDelegator;
766
let cmd = 'cmd';
G
Gloria 已提交
767
let timeout = 100;
W
wusongqing 已提交
768 769 770

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, timeout).then((data : any) => {
771
    console.info('executeShellCommand promise');
W
wusongqing 已提交
772 773
});
```
W
wusongqing 已提交
774 775 776

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

777
finishTest(msg: string, code: number, callback: AsyncCallback\<void>): void;
W
wusongqing 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

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

**Example**

793
```ts
G
Gloria 已提交
794
let abilityDelegator;
795
let msg = 'msg';
W
wusongqing 已提交
796 797 798

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0, (err : any) => {
799
    console.info('finishTest callback');
W
wusongqing 已提交
800 801 802 803 804
});
```

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

805
finishTest(msg: string, code: number): Promise\<void>;
W
wusongqing 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825

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

**Example**

826
```ts
G
Gloria 已提交
827
let abilityDelegator;
828
let msg = 'msg';
W
wusongqing 已提交
829 830 831

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0).then(() => {
832
    console.info('finishTest promise');
W
wusongqing 已提交
833 834
});
```
835 836 837 838 839 840 841 842 843 844 845 846 847

### 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                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
848
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes      | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
849 850 851 852
| callback | AsyncCallback\<void>                                         | Yes      | Callback used to return the result.                                          |

**Example**

853
```ts
G
Gloria 已提交
854
let abilityDelegator;
855

G
Gloria 已提交
856
let monitor = {
857 858 859
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
860 861 862

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor, (err : any) => {
863
    console.info('addAbilityStageMonitor callback');
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
});
```

### 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                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
879
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
880 881 882 883 884 885 886 887 888

**Return value**

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

**Example**

889
```ts
G
Gloria 已提交
890
let abilityDelegator;
891

G
Gloria 已提交
892
let monitor = {
893 894 895
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
896 897 898

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor).then(() => {
899
    console.info('addAbilityStageMonitor promise');
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
});
```

### 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                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
915
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes      | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
916 917 918 919
| callback | AsyncCallback\<void>                                         | Yes      | Callback used to return the result.                                          |

**Example**

920
```ts
G
Gloria 已提交
921
let abilityDelegator;
922

G
Gloria 已提交
923
let monitor = {
924 925 926
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
927 928 929

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor, (err : any) => {
930
    console.info('removeAbilityStageMonitor callback');
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
});
```

### 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                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
946
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
947 948 949 950 951 952 953 954 955

**Return value**

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

**Example**

956
```ts
G
Gloria 已提交
957
let abilityDelegator;
958

G
Gloria 已提交
959
let monitor = {
960 961 962
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
963 964 965

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor).then(() => {
966
    console.info('removeAbilityStageMonitor promise');
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
});
```

### 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                                                        |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
982
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes      | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
983 984 985 986
| 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.            |

**Example**

987
```ts
G
Gloria 已提交
988
let abilityDelegator;
989 990

function onAbilityCreateCallback(data) {
991
    console.info('onAbilityCreateCallback');
992 993
}

G
Gloria 已提交
994
let monitor = {
995 996 997
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
998 999 1000

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor, (err : any, data : any) => {
1001
    console.info('waitAbilityStageMonitor callback');
1002 1003
});
```
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
### 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                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1017
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
| 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.|

**Example**

1028
```ts
G
Gloria 已提交
1029
let abilityDelegator;
1030 1031

function onAbilityCreateCallback(data) {
1032
    console.info('onAbilityCreateCallback');
1033 1034
}

G
Gloria 已提交
1035
let monitor = {
1036 1037 1038
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1039 1040 1041

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor).then((data : any) => {
1042
    console.info('waitAbilityStageMonitor promise');
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
});
```

### 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                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1058
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | Yes  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) instance.|
1059 1060 1061 1062 1063
| 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.                    |

**Example**

1064
```ts
G
Gloria 已提交
1065 1066
let abilityDelegator;
let timeout = 100;
1067 1068

function onAbilityCreateCallback(data) {
1069
    console.info('onAbilityCreateCallback');
1070 1071
}

G
Gloria 已提交
1072
let monitor = {
1073 1074 1075
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
1076 1077 1078

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