js-apis-application-abilityDelegator.md 25.6 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 { AbilityDelegator } from './application/abilityDelegator'
N
njupthan 已提交
11 12 13 14 15 16
```



## AbilityDelegator

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

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

添加AbilityMonitor实例(callback形式)

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

**参数:**

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

**示例:**

```js
var abilityDelegator;

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName: "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor, (err) => {
    console.info("==========================>AddAbilityMonitorCallback=======================>");
});
```



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

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

添加AbilityMonitor实例(promise形式)

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

**参数:**

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

**返回值:**

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

**示例:**

```js
var abilityDelegator;

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName : "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor).then((void) => {
	console.info("==========================>AddAbilityMonitor Promise=======================>");
});
```



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

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

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

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

**参数:**

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

**示例:**

```js
var abilityDelegator;

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName : "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityMonitor(monitor, (err) => {
    console.info("==========================>removeAbilityMonitorCallback=======================>");
});
```



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

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

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

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

**参数:**

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

**返回值:**

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

- 示例

```js
var abilityDelegator;

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName : "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityMonitor(monitor).then((void) => {
	console.info("==========================>removeAbilityMonitor Promise=======================>");
});
```



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

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

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

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

**参数:**

N
njupthan 已提交
185 186 187 188
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor  | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
| callback | AsyncCallback\<Ability>                                      | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

**示例:**

```js
var abilityDelegator;

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName: "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor, (err, data) => {
    console.info("==========================>waitAbilityMonitorCallback=======================>");
});
```



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

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

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

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

**参数:**

N
njupthan 已提交
222 223 224 225 226
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor  | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
| timeout  | number                                                       | 是   | 最大等待时间,单位毫秒                                       |
| callback | AsyncCallback\<Ability>                                      | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

**示例:**

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

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName : "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor, timeout, (err, data) => {
    console.info("==========================>waitAbilityMonitorCallback=======================>");
});
```



N
njupthan 已提交
251
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
252 253 254 255 256 257 258 259 260

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

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

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

**参数:**

N
njupthan 已提交
261 262 263 264
| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| monitor | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-application-abilityMonitor.md#AbilityMonitor)实例 |
| timeout | number                                                       | 否   | 最大等待时间,单位毫秒                                       |
N
njupthan 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

**返回值:**

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

**示例:**

```js
var abilityDelegator;

function onAbilityCreateCallback(err) {
    console.info("==========================>onAbilityCreateCallback=======================>");
}

var monitor = {
	abilityName : "abilityname",
	onAbilityCreate: onAbilityCreateCallback
}

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor).then((data) => {
	console.info("==========================>waitAbilityMonitor Promise=======================>");
});
```



N
njupthan 已提交
294
### getAppContext<sup>9+</sup>
N
njupthan 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

getAppContext(): Context

获取应用Context

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

**返回值:**

| 类型                                  | 说明                                        |
| ------------------------------------- | ------------------------------------------- |
| [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 325 326 327 328 329 330 331 332 333 334 335 336

getAbilityState(ability: Ability): number

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err, data) => {
    console.info("==========================>GetCurrentTopCallBack=======================>");
    ability = data;
    var state = abilityDelegator.getAbilityState(ability);
    console.info("==========================>getAbilityState=======================>" + state);
});
```



N
njupthan 已提交
356
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

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

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

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

**参数:**

| 参数名   | 类型                    | 必填 | 说明               |
| -------- | ----------------------- | ---- | ------------------ |
| callback | AsyncCallback\<Ability> | 是   | 表示指定的回调方法 |

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err, data) => {
    console.info("==========================>GetCurrentTopCallBack=======================>");
    ability = data;
});
```



N
njupthan 已提交
385
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

getCurrentTopAbility(): Promise\<Ability>

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

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

**返回值:**

| 类型              | 说明                                   |
| ----------------- | -------------------------------------- |
| Promise\<Ability> | 以Promise形式返回当前应用顶部ability。 |

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility().then((data) => {
	console.info("==========================>getCurrentTopAbility Promise=======================>");
	ability = data;
});
```



### startAbility

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

启动指定ability(callback形式)

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

**参数:**

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

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.startAbility(want, (err, data) => {
    console.info("==========================>StartAbilityCallBack=======================>");
});
```



### startAbility

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

启动指定ability(promise形式)

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

**参数:**

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

**返回值:**

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

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.startAbility(want).then((data) => {
	console.info("==========================>startAbility Promise=======================>");
});
```



N
njupthan 已提交
483
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

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

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

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

**参数:**

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

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err, data) => {
    console.info("==========================>GetCurrentTopCallBack=======================>");
    ability = data;
    abilityDelegator.doAbilityForeground(ability, (err, data) => {
    	console.info("==========================>DoAbilityForegroundCallBack=======================>");
	});
});
```



N
njupthan 已提交
516
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err, data) => {
    console.info("==========================>GetCurrentTopCallBack=======================>");
    ability = data;
    abilityDelegator.doAbilityForeground(ability).then((data) => {
        console.info("==========================>doAbilityForeground Promise=======================>");
    });
});
```



N
njupthan 已提交
554
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

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

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

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

**参数:**

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

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err, data) => {
    console.info("==========================>GetCurrentTopCallBack=======================>");
    ability = data;
    abilityDelegator.doAbilityBackground(ability, (err, data) => {
        console.info("==========================>DoAbilityBackgroundCallBack=======================>");
    });
});
```



N
njupthan 已提交
587
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
var abilityDelegator;
var ability;

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err, data) => {
    console.info("==========================>GetCurrentTopCallBack=======================>");
    ability = data;
    abilityDelegator.doAbilityBackground(ability).then((data) => {
        console.info("==========================>doAbilityBackground Promise=======================>");
    });
});
```



### print

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

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

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

**参数:**

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

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg, (err) => {
    console.info("==========================>printCallBack=======================>");
});
```



### print

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg).then(() => {
	console.info("==========================>print Promise=======================>");
});
```



### executeShellCommand

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

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

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

**参数:**

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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, (err,data) => {
    console.info("==========================>executeShellCommandCallBack=======================>");
});
```



### executeShellCommand

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

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

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

**参数:**

N
njupthan 已提交
727 728 729 730 731
| 参数名      | 类型                                                         | 必填 | 说明               |
| ----------- | ------------------------------------------------------------ | ---- | ------------------ |
| cmd         | string                                                       | 是   | shell命令字符串    |
| timeoutSecs | number                                                       | 是   | 设定时间,单位秒   |
| callback    | AsyncCallback\<[ShellCmdResult](js-apis-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法 |
N
njupthan 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, timeout, (err,data) => {
    console.info("==========================>executeShellCommandCallBack=======================>");
});
```



### executeShellCommand

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

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

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

**参数:**

| 参数名      | 类型   | 必填 | 说明             |
| ----------- | ------ | ---- | ---------------- |
| cmd         | string | 是   | shell命令字符串  |
| timeoutSecs | number | 否   | 设定时间,单位秒 |

**返回值:**

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 777 778 779 780

**示例:**

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

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, timeout).then((data) => {
	console.info("==========================>executeShellCommand Promise=======================>");
});
```