js-apis-application-abilityDelegator.md 26.7 KB
Newer Older
N
njupthan 已提交
1
# AbilityDelegator
N
njupthan 已提交
2 3 4 5 6 7 8 9

> **说明**
>
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

```js
N
njupthan 已提交
10
import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
N
njupthan 已提交
11 12 13 14 15 16
```



## AbilityDelegator

N
njupthan 已提交
17
### addAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
18 19 20 21 22

addAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void

添加AbilityMonitor实例(callback形式)

23
**系统能力:**SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
24 25 26

**参数:**

N
njupthan 已提交
27 28 29 30
| 参数名   | 类型                                                         | 必填说明 | 说明                                                         |
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
| monitor  | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是       | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
| callback | AsyncCallback\<void>                                         | 是       | 表示指定的回调方法                                           |
N
njupthan 已提交
31 32 33 34 35 36

**示例:**

```js
var abilityDelegator;

N
njupthan 已提交
37
function onAbilityCreateCallback(data) {
N
njupthan 已提交
38
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
39 40 41
}

var monitor = {
N
njupthan 已提交
42 43
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
44 45 46
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
47
abilityDelegator.addAbilityMonitor(monitor, (err : any) => {
N
njupthan 已提交
48
    console.info("addAbilityMonitor callback");
N
njupthan 已提交
49 50 51 52 53
});
```



N
njupthan 已提交
54
### addAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
55 56 57 58 59

addAbilityMonitor(monitor: AbilityMonitor): Promise\<void>

添加AbilityMonitor实例(promise形式)

N
njupthan 已提交
60
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
61 62 63

**参数:**

N
njupthan 已提交
64 65 66
| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
67 68 69 70 71 72 73 74 75 76 77 78

**返回值:**

| 类型           | 说明                |
| -------------- | ------------------- |
| Promise\<void> | 以Promise形式返回。 |

**示例:**

```js
var abilityDelegator;

N
njupthan 已提交
79
function onAbilityCreateCallback(data) {
N
njupthan 已提交
80
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
81 82 83
}

var monitor = {
N
njupthan 已提交
84 85
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
86 87 88
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
89
abilityDelegator.addAbilityMonitor(monitor).then(() => {
N
njupthan 已提交
90
    console.info("addAbilityMonitor promise");
N
njupthan 已提交
91 92 93 94 95
});
```



N
njupthan 已提交
96
### removeAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
97 98 99 100 101

removeAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void

删除已经添加的AbilityMonitor实例(callback形式)

N
njupthan 已提交
102
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
103 104 105

**参数:**

N
njupthan 已提交
106 107 108 109
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor  | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
| callback | AsyncCallback\<void>                                         | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
110 111 112 113 114 115

**示例:**

```js
var abilityDelegator;

N
njupthan 已提交
116
function onAbilityCreateCallback(data) {
N
njupthan 已提交
117
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
118 119 120
}

var monitor = {
N
njupthan 已提交
121 122
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
123 124 125
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
126
abilityDelegator.removeAbilityMonitor(monitor, (err : any) => {
N
njupthan 已提交
127
    console.info("removeAbilityMonitor callback");
N
njupthan 已提交
128 129 130 131 132
});
```



N
njupthan 已提交
133
### removeAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
134 135 136 137 138

removeAbilityMonitor(monitor: AbilityMonitor): Promise\<void>

删除已经添加的AbilityMonitor实例(promise形式)

N
njupthan 已提交
139
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
140 141 142

**参数:**

N
njupthan 已提交
143 144 145
| 名称    | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
146 147 148 149 150 151 152 153 154 155 156 157

**返回值:**

| 类型           | 说明                |
| -------------- | ------------------- |
| Promise\<void> | 以Promise形式返回。 |

- 示例

```js
var abilityDelegator;

N
njupthan 已提交
158
function onAbilityCreateCallback(data) {
N
njupthan 已提交
159
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
160 161 162
}

var monitor = {
N
njupthan 已提交
163 164
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
165 166 167
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
168
abilityDelegator.removeAbilityMonitor(monitor).then(() => {
N
njupthan 已提交
169
    console.info("removeAbilityMonitor promise");
N
njupthan 已提交
170 171 172 173 174
});
```



N
njupthan 已提交
175
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
176 177 178 179 180

waitAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<Ability>): void

等待与AbilityMonitor实例匹配的ability到达OnCreate生命周期,并返回ability实例(callback形式)

N
njupthan 已提交
181
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
182 183 184

**参数:**

N
njupthan 已提交
185 186 187
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor  | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
188
| callback | AsyncCallback\<[Ability](js-apis-application-ability.md#Ability)> | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
189 190 191 192 193 194

**示例:**

```js
var abilityDelegator;

N
njupthan 已提交
195
function onAbilityCreateCallback(data) {
N
njupthan 已提交
196
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
197 198 199
}

var monitor = {
N
njupthan 已提交
200 201
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
202 203 204
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
205
abilityDelegator.waitAbilityMonitor(monitor, (err : any, data : any) => {
N
njupthan 已提交
206
    console.info("waitAbilityMonitor callback");
N
njupthan 已提交
207 208 209 210 211
});
```



N
njupthan 已提交
212
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
213 214 215 216 217

waitAbilityMonitor(monitor: AbilityMonitor, timeout: number, callback: AsyncCallback\<Ability>): void

设置等待时间,等待与AbilityMonitor实例匹配的ability到达OnCreate生命周期,并返回ability实例(callback形式)

N
njupthan 已提交
218
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
219 220 221

**参数:**

N
njupthan 已提交
222 223 224
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor  | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
225
| timeout  | number                                                       | 是   | 最大等待时间,单位毫秒(ms)                                 |
N
njupthan 已提交
226
| callback | AsyncCallback\<[Ability](js-apis-application-ability.md#Ability)> | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
227 228 229 230 231 232 233

**示例:**

```js
var abilityDelegator;
var timeout = 100;

N
njupthan 已提交
234
function onAbilityCreateCallback(data) {
N
njupthan 已提交
235
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
236 237 238
}

var monitor = {
N
njupthan 已提交
239 240
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
241 242 243
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
244
abilityDelegator.waitAbilityMonitor(monitor, timeout, (err : any, data : any) => {
N
njupthan 已提交
245
    console.info("waitAbilityMonitor callback");
N
njupthan 已提交
246 247 248 249 250
});
```



N
njupthan 已提交
251
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
252 253 254

waitAbilityMonitor(monitor: AbilityMonitor, timeout?: number): Promise\<Ability>

N
njupthan 已提交
255
设置等待时间,等待与AbilityMonitor实例匹配的ability到达OnCreate生命周期,并返回ability实例(promise形式)
N
njupthan 已提交
256

N
njupthan 已提交
257
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
258 259 260

**参数:**

N
njupthan 已提交
261 262 263
| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
264
| timeout | number                                                       | 否   | 最大等待时间,单位毫秒(ms)                                 |
N
njupthan 已提交
265 266 267

**返回值:**

N
njupthan 已提交
268 269 270
| 类型                                                        | 说明                       |
| ----------------------------------------------------------- | -------------------------- |
| Promise\<[Ability](js-apis-application-ability.md#Ability)> | 以Promise形式返回Ability。 |
N
njupthan 已提交
271 272 273 274 275 276

**示例:**

```js
var abilityDelegator;

N
njupthan 已提交
277
function onAbilityCreateCallback(data) {
N
njupthan 已提交
278
    console.info("onAbilityCreateCallback");
N
njupthan 已提交
279 280 281
}

var monitor = {
N
njupthan 已提交
282 283
    abilityName: "abilityname",
    onAbilityCreate: onAbilityCreateCallback
N
njupthan 已提交
284 285 286
}

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



N
njupthan 已提交
294
### getAppContext<sup>9+</sup>
N
njupthan 已提交
295 296 297 298 299

getAppContext(): Context

获取应用Context

N
njupthan 已提交
300
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

**返回值:**

| 类型                                  | 说明                                        |
| ------------------------------------- | ------------------------------------------- |
| [Context](js-apis-Context.md#Context) | 应用[Context](js-apis-Context.md#Context)。 |

**示例:**

```js
var abilityDelegator;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
var context = abilityDelegator.getAppContext();
```



N
njupthan 已提交
319
### getAbilityState<sup>9+</sup>
N
njupthan 已提交
320 321 322 323 324

getAbilityState(ability: Ability): number

获取指定ability的生命周期状态

N
njupthan 已提交
325
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
326 327 328

**参数:**

N
njupthan 已提交
329 330 331
| 参数名  | 类型                                              | 必填 | 说明            |
| ------- | ------------------------------------------------- | ---- | --------------- |
| ability | [Ability](js-apis-application-ability.md#Ability) | 是   | 指定Ability对象 |
N
njupthan 已提交
332 333 334 335 336

**返回值:**

| 类型   | 说明                                                         |
| ------ | ------------------------------------------------------------ |
N
njupthan 已提交
337
| number | 指定ability的生命周期状态。状态枚举值使用[AbilityLifecycleState](js-apis-abilityDelegatorRegistry.md#AbilityLifecycleState)。 |
N
njupthan 已提交
338 339 340 341 342 343 344 345

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
346
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
N
njupthan 已提交
347
    console.info("getCurrentTopAbility callback");
N
njupthan 已提交
348 349
    ability = data;
    var state = abilityDelegator.getAbilityState(ability);
N
njupthan 已提交
350
    console.info("getAbilityState" + state);
N
njupthan 已提交
351 352 353 354 355
});
```



N
njupthan 已提交
356
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
357 358 359 360 361

getCurrentTopAbility(callback: AsyncCallback\<Ability>): void

获取当前应用顶部ability(callback形式)

N
njupthan 已提交
362
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
363 364 365

**参数:**

N
njupthan 已提交
366 367 368
| 参数名   | 类型                                                         | 必填 | 说明               |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
| callback | AsyncCallback\<[Ability](js-apis-application-ability.md#Ability)> | 是   | 表示指定的回调方法 |
N
njupthan 已提交
369 370 371 372 373 374 375 376

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
377
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
N
njupthan 已提交
378
    console.info("getCurrentTopAbility callback");
N
njupthan 已提交
379 380 381 382 383 384
    ability = data;
});
```



N
njupthan 已提交
385
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
386 387 388 389 390

getCurrentTopAbility(): Promise\<Ability>

获取当前应用顶部ability(promise形式)

N
njupthan 已提交
391
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
392 393 394

**返回值:**

N
njupthan 已提交
395 396 397
| 类型                                                        | 说明                                   |
| ----------------------------------------------------------- | -------------------------------------- |
| Promise\<[Ability](js-apis-application-ability.md#Ability)> | 以Promise形式返回当前应用顶部ability。 |
N
njupthan 已提交
398 399 400 401 402 403 404 405

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
406
abilityDelegator.getCurrentTopAbility().then((data : any) => {
N
njupthan 已提交
407 408
    console.info("getCurrentTopAbility promise");
    ability = data;
N
njupthan 已提交
409 410 411 412 413
});
```



N
njupthan 已提交
414
### startAbility<sup>9+</sup>
N
njupthan 已提交
415 416 417 418 419

startAbility(want: Want, callback: AsyncCallback\<void>): void

启动指定ability(callback形式)

N
njupthan 已提交
420
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

**参数:**

| 参数名   | 类型                                   | 必填 | 说明               |
| -------- | -------------------------------------- | ---- | ------------------ |
| want     | [Want](js-apis-featureAbility.md#Want) | 是   | 启动Ability参数    |
| callback | AsyncCallback\<void>                   | 是   | 表示指定的回调方法 |

**示例:**

```js
var abilityDelegator;
var want = {
    bundleName: "bundleName",
    abilityName: "abilityName"
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
439
abilityDelegator.startAbility(want, (err : any, data : any) => {
N
njupthan 已提交
440
    console.info("startAbility callback");
N
njupthan 已提交
441 442 443 444 445
});
```



N
njupthan 已提交
446
### startAbility<sup>9+</sup>
N
njupthan 已提交
447 448 449 450 451

startAbility(want: Want): Promise\<void>

启动指定ability(promise形式)

N
njupthan 已提交
452
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

**参数:**

| 参数名 | 类型                                   | 必填 | 说明            |
| ------ | -------------------------------------- | ---- | --------------- |
| want   | [Want](js-apis-featureAbility.md#Want) | 是   | 启动Ability参数 |

**返回值:**

| 类型           | 说明                |
| -------------- | ------------------- |
| Promise\<void> | 以Promise形式返回。 |

**示例:**

```js
var abilityDelegator;
var want = {
    bundleName: "bundleName",
    abilityName: "abilityName"
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
476
abilityDelegator.startAbility(want).then((data: any) => {
N
njupthan 已提交
477
    console.info("startAbility promise");
N
njupthan 已提交
478 479 480 481 482
});
```



N
njupthan 已提交
483
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
484 485 486 487 488

doAbilityForeground(ability: Ability, callback: AsyncCallback\<boolean>): void

调度指定ability生命周期状态到Foreground状态(callback形式)

N
njupthan 已提交
489
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                                    |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
| ability  | Ability                 | 是   | 指定Ability对象                                         |
| callback | AsyncCallback\<boolean> | 是   | 表示指定的回调方法<br/>\- true:成功<br/>\- false:失败 |

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
505
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
N
njupthan 已提交
506
    console.info("getCurrentTopAbility callback");
N
njupthan 已提交
507
    ability = data;
508
    abilityDelegator.doAbilityForeground(ability, (err : any, data : any) => {
N
njupthan 已提交
509 510
        console.info("doAbilityForeground callback");
    });
N
njupthan 已提交
511 512 513 514 515
});
```



N
njupthan 已提交
516
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
517 518 519 520 521

doAbilityForeground(ability: Ability): Promise\<boolean>

调度指定ability生命周期状态到Foreground状态(promise形式)

N
njupthan 已提交
522
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

**参数:**

| 参数名  | 类型    | 必填 | 说明            |
| ------- | ------- | ---- | --------------- |
| ability | Ability | 是   | 指定Ability对象 |

**返回值:**

| 类型              | 说明                                                         |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | 以Promise形式返回执行结果。<br/>\- true:成功<br/>\- false:失败 |

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
543
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
N
njupthan 已提交
544
    console.info("getCurrentTopAbility callback");
N
njupthan 已提交
545
    ability = data;
N
njupthan 已提交
546
    abilityDelegator.doAbilityForeground(ability).then((data : any) => {
N
njupthan 已提交
547
        console.info("doAbilityForeground promise");
N
njupthan 已提交
548 549 550 551 552 553
    });
});
```



N
njupthan 已提交
554
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
555 556 557 558 559

doAbilityBackground(ability: Ability, callback: AsyncCallback\<boolean>): void

调度指定ability生命周期状态到Background状态(callback形式)

N
njupthan 已提交
560
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                                    |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
| ability  | Ability                 | 是   | 指定Ability对象                                         |
| callback | AsyncCallback\<boolean> | 是   | 表示指定的回调方法<br/>\- true:成功<br/>\- false:失败 |

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
576
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
N
njupthan 已提交
577
    console.info("getCurrentTopAbility callback");
N
njupthan 已提交
578
    ability = data;
579
    abilityDelegator.doAbilityBackground(ability, (err : any, data : any) => {
N
njupthan 已提交
580
        console.info("doAbilityBackground callback");
N
njupthan 已提交
581 582 583 584 585 586
    });
});
```



N
njupthan 已提交
587
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
588 589 590 591 592

doAbilityBackground(ability: Ability): Promise\<boolean>

调度指定ability生命周期状态到Background状态(promise形式)

N
njupthan 已提交
593
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

**参数:**

| 参数名  | 类型    | 必填 | 说明            |
| ------- | ------- | ---- | --------------- |
| ability | Ability | 是   | 指定Ability对象 |

**返回值:**

| 类型              | 说明                                                         |
| ----------------- | ------------------------------------------------------------ |
| Promise\<boolean> | 以Promise形式返回执行结果。<br/>\- true:成功<br/>\- false:失败 |

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
614
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
N
njupthan 已提交
615
    console.info("getCurrentTopAbility callback");
N
njupthan 已提交
616
    ability = data;
617
    abilityDelegator.doAbilityBackground(ability).then((data : any) => {
N
njupthan 已提交
618
        console.info("doAbilityBackground promise");
N
njupthan 已提交
619 620 621 622 623 624 625 626 627 628 629 630
    });
});
```



### print

print(msg: string, callback: AsyncCallback\<void>): void

打印日志信息到单元测试终端控制台(callback形式)

N
njupthan 已提交
631
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

**参数:**

| 参数名   | 类型                 | 必填 | 说明               |
| -------- | -------------------- | ---- | ------------------ |
| msg      | string               | 是   | 日志字符串         |
| callback | AsyncCallback\<void> | 是   | 表示指定的回调方法 |

**示例:**

```js
var abilityDelegator;
var msg = "msg";

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
647
abilityDelegator.print(msg, (err : any) => {
N
njupthan 已提交
648
    console.info("print callback");
N
njupthan 已提交
649 650 651 652 653 654 655 656 657 658 659
});
```



### print

print(msg: string): Promise\<void>

打印日志信息到单元测试终端控制台(promise形式)

N
njupthan 已提交
660
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

**参数:**

| 参数名 | 类型   | 必填 | 说明       |
| ------ | ------ | ---- | ---------- |
| msg    | string | 是   | 日志字符串 |

**返回值:**

| 类型           | 说明                |
| -------------- | ------------------- |
| Promise\<void> | 以Promise形式返回。 |

**示例:**

```js
var abilityDelegator;
var msg = "msg";

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg).then(() => {
N
njupthan 已提交
682
    console.info("print promise");
N
njupthan 已提交
683 684 685 686 687 688 689 690 691 692 693
});
```



### executeShellCommand

executeShellCommand(cmd: string, callback: AsyncCallback\<ShellCmdResult>): void

执行指定的shell命令(callback形式)

N
njupthan 已提交
694
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
695 696 697

**参数:**

N
njupthan 已提交
698 699 700 701
| 参数名   | 类型                                                         | 必填 | 说明               |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
| cmd      | string                                                       | 是   | shell命令字符串    |
| callback | AsyncCallback\<[ShellCmdResult](js-apis-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法 |
N
njupthan 已提交
702 703 704 705 706 707 708 709

**示例:**

```js
var abilityDelegator;
var cmd = "cmd";

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
710
abilityDelegator.executeShellCommand(cmd, (err : any, data : any) => {
N
njupthan 已提交
711
    console.info("executeShellCommand callback");
N
njupthan 已提交
712 713 714 715 716 717 718 719 720
});
```



### executeShellCommand

executeShellCommand(cmd: string, timeoutSecs: number, callback: AsyncCallback\<ShellCmdResult>): void

N
njupthan 已提交
721
指定超时时间,并执行指定的shell命令(callback形式)
N
njupthan 已提交
722

N
njupthan 已提交
723
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
724 725 726

**参数:**

N
njupthan 已提交
727 728 729 730 731
| 参数名      | 类型                                                         | 必填 | 说明                          |
| ----------- | ------------------------------------------------------------ | ---- | ----------------------------- |
| cmd         | string                                                       | 是   | shell命令字符串               |
| timeoutSecs | number                                                       | 是   | 设定命令超时时间,单位秒(s) |
| callback    | AsyncCallback\<[ShellCmdResult](js-apis-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法            |
N
njupthan 已提交
732 733 734 735 736 737 738 739 740

**示例:**

```js
var abilityDelegator;
var cmd = "cmd";
var timeout = 100;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
741
abilityDelegator.executeShellCommand(cmd, timeout, (err : any, data : any) => {
N
njupthan 已提交
742
    console.info("executeShellCommand callback");
N
njupthan 已提交
743 744 745 746 747 748 749 750 751
});
```



### executeShellCommand

executeShellCommand(cmd: string, timeoutSecs: number): Promise\<ShellCmdResult>

N
njupthan 已提交
752
指定超时时间,并执行指定的shell命令(promise形式)
N
njupthan 已提交
753

N
njupthan 已提交
754
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
755 756 757

**参数:**

N
njupthan 已提交
758 759 760 761
| 参数名      | 类型   | 必填 | 说明                          |
| ----------- | ------ | ---- | ----------------------------- |
| cmd         | string | 是   | shell命令字符串               |
| timeoutSecs | number | 否   | 设定命令超时时间,单位秒(s) |
N
njupthan 已提交
762 763 764

**返回值:**

N
njupthan 已提交
765 766 767
| 类型                                                         | 说明                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| Promise\<[ShellCmdResult](js-apis-application-shellCmdResult.md#ShellCmdResult)> | 以Promise形式返回Shell命令执行结果[ShellCmdResult](js-apis-application-shellCmdResult.md#ShellCmdResult)对象。 |
N
njupthan 已提交
768 769 770 771 772 773 774 775 776

**示例:**

```js
var abilityDelegator;
var cmd = "cmd";
var timeout = 100;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
777
abilityDelegator.executeShellCommand(cmd, timeout).then((data : any) => {
N
njupthan 已提交
778
    console.info("executeShellCommand promise");
N
njupthan 已提交
779 780
});
```
781 782 783



N
njupthan 已提交
784
### finishTest<sup>9+</sup>
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

finishTest(msg: string, code: number, callback: AsyncCallback\<void>): void

结束测试并打印日志信息到单元测试终端控制台(callback形式)

**系统能力:** SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名   | 类型                 | 必填 | 说明               |
| -------- | -------------------- | ---- | ------------------ |
| msg      | string               | 是   | 日志字符串         |
| code     | number               | 是   | 日志码             |
| callback | AsyncCallback\<void> | 是   | 表示指定的回调方法 |

**示例:**

```js
var abilityDelegator;
var msg = "msg";

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0, (err : any) => {
    console.info("finishTest callback");
});
```



N
njupthan 已提交
814
### finishTest<sup>9+</sup>
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

finishTest(msg: string, code: number): Promise\<void>

结束测试并打印日志信息到单元测试终端控制台(promise形式)

**系统能力:** SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明       |
| ------ | ------ | ---- | ---------- |
| msg    | string | 是   | 日志字符串 |
| code   | number | 是   | 日志码     |

**返回值:**

| 类型           | 说明                |
| -------------- | ------------------- |
| Promise\<void> | 以Promise形式返回。 |

**示例:**

```js
var abilityDelegator;
var msg = "msg";

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0).then(() => {
    console.info("finishTest promise");
});
```