js-apis-inner-application-abilityDelegator.md 37.7 KB
Newer Older
N
njupthan 已提交
1
# AbilityDelegator
N
njupthan 已提交
2

3
AbilityDelegator提供添加用于监视指定ability的生命周期状态更改的AbilityMonitor对象的能力,包括对AbilityMonitor实例的添加、删除、等待ability到达OnCreate生命周期、设置等待时间等、获取指定ability的生命周期状态、获取当前应用顶部ability、启动指定ability等。
Y
yuyaozhi 已提交
4

Y
yuyaozhi 已提交
5 6
> **说明:**
> 
M
m00512953 已提交
7
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 
N
njupthan 已提交
8

9
## 使用说明
N
njupthan 已提交
10

Z
zhongjianfei 已提交
11
通过AbilityDelegatorRegistry中[getAbilityDelegator](js-apis-app-ability-abilityDelegatorRegistry.md#abilitydelegatorregistrygetabilitydelegator)方法获取。
M
m00512953 已提交
12
```ts
13
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
N
njupthan 已提交
14 15 16 17
```

## AbilityDelegator

N
njupthan 已提交
18
### addAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
19

Y
yuyaozhi 已提交
20
addAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
21 22 23

添加AbilityMonitor实例(callback形式)

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

**参数:**

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

**示例:**

M
m00512953 已提交
35
```ts
36
let abilityDelegator;
N
njupthan 已提交
37

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

42
let monitor = {
M
mingxihua 已提交
43
    abilityName: 'abilityname',
N
njupthan 已提交
44
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
45
};
N
njupthan 已提交
46 47

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

N
njupthan 已提交
53
### addAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
54

Y
yuyaozhi 已提交
55
addAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
N
njupthan 已提交
56 57 58

添加AbilityMonitor实例(promise形式)

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

**参数:**

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

**返回值:**

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

**示例:**

M
m00512953 已提交
75
```ts
76
let abilityDelegator;
N
njupthan 已提交
77

N
njupthan 已提交
78
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
79
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
80 81
}

82
let monitor = {
M
mingxihua 已提交
83
    abilityName: 'abilityname',
N
njupthan 已提交
84
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
85
};
N
njupthan 已提交
86 87

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
88
abilityDelegator.addAbilityMonitor(monitor).then(() => {
M
mingxihua 已提交
89
    console.info('addAbilityMonitor promise');
N
njupthan 已提交
90 91 92
});
```

N
njupthan 已提交
93
### removeAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
94

Y
yuyaozhi 已提交
95
removeAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
96 97 98

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

N
njupthan 已提交
99
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
100 101 102

**参数:**

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

**示例:**

M
m00512953 已提交
110
```ts
111
let abilityDelegator;
N
njupthan 已提交
112

N
njupthan 已提交
113
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
114
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
115 116
}

117
let monitor = {
M
mingxihua 已提交
118
    abilityName: 'abilityname',
N
njupthan 已提交
119
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
120
};
N
njupthan 已提交
121 122

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

N
njupthan 已提交
128
### removeAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
129

Y
yuyaozhi 已提交
130
removeAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
N
njupthan 已提交
131 132 133

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

N
njupthan 已提交
134
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
135 136 137

**参数:**

Y
yuyaozhi 已提交
138
| 参数名    | 类型                                                         | 必填 | 说明                                                         |
N
njupthan 已提交
139
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
140
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
141 142 143 144 145 146 147 148 149

**返回值:**

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

- 示例

M
m00512953 已提交
150
```ts
151
let abilityDelegator;
N
njupthan 已提交
152

N
njupthan 已提交
153
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
154
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
155 156
}

157
let monitor = {
M
mingxihua 已提交
158
    abilityName: 'abilityname',
N
njupthan 已提交
159
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
160
};
N
njupthan 已提交
161 162

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
163
abilityDelegator.removeAbilityMonitor(monitor).then(() => {
M
mingxihua 已提交
164
    console.info('removeAbilityMonitor promise');
N
njupthan 已提交
165 166 167
});
```

N
njupthan 已提交
168
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
169

Y
yuyaozhi 已提交
170
waitAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<UIAbility>): void;
N
njupthan 已提交
171 172 173

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

N
njupthan 已提交
174
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
175 176 177

**参数:**

N
njupthan 已提交
178 179
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
180 181
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor)实例 |
| callback | AsyncCallback\<[UIAbility](js-apis-app-ability-uiAbility.md)> | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
182 183 184

**示例:**

M
m00512953 已提交
185
```ts
186
let abilityDelegator;
N
njupthan 已提交
187

N
njupthan 已提交
188
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
189
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
190 191
}

192
let monitor = {
M
mingxihua 已提交
193
    abilityName: 'abilityname',
N
njupthan 已提交
194
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
195
};
N
njupthan 已提交
196 197

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
M
mingxihua 已提交
198
abilityDelegator.waitAbilityMonitor(monitor, (error : any, data : any) => {
X
xieqiongyang 已提交
199
    if (error) {
M
mingxihua 已提交
200 201 202 203
        console.error('waitAbilityMonitor fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('waitAbilityMonitor success, data: ${JSON.stringify(data)}');
    }
N
njupthan 已提交
204 205 206
});
```

N
njupthan 已提交
207
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
208

Y
yuyaozhi 已提交
209
waitAbilityMonitor(monitor: AbilityMonitor, timeout: number, callback: AsyncCallback\<UIAbility>): void;
N
njupthan 已提交
210 211 212

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

N
njupthan 已提交
213
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
214 215 216

**参数:**

N
njupthan 已提交
217 218
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
219
| monitor  | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor)实例 |
Y
yuyaozhi 已提交
220
| timeout  | number                                                       | 否   | 最大等待时间,单位毫秒(ms)                                 |
M
m00512953 已提交
221
| callback | AsyncCallback\<[UIAbility](js-apis-app-ability-uiAbility.md)> | 是   | 表示指定的回调方法                                           |
N
njupthan 已提交
222 223 224

**示例:**

M
m00512953 已提交
225
```ts
226 227
let abilityDelegator;
let timeout = 100;
N
njupthan 已提交
228

N
njupthan 已提交
229
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
230
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
231 232
}

233
let monitor = {
M
mingxihua 已提交
234
    abilityName: 'abilityname',
N
njupthan 已提交
235
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
236
};
N
njupthan 已提交
237 238

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
M
mingxihua 已提交
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)}');
    }
N
njupthan 已提交
245 246 247 248 249
});
```



N
njupthan 已提交
250
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
251

Y
yuyaozhi 已提交
252
waitAbilityMonitor(monitor: AbilityMonitor, timeout?: number): Promise\<UIAbility>;
N
njupthan 已提交
253

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

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

**参数:**

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

**返回值:**

N
njupthan 已提交
267 268
| 类型                                                        | 说明                       |
| ----------------------------------------------------------- | -------------------------- |
M
m00512953 已提交
269
| Promise\<[UIAbility](js-apis-app-ability-uiAbility.md)> | 以Promise形式返回Ability。 |
N
njupthan 已提交
270 271 272

**示例:**

M
m00512953 已提交
273
```ts
274
let abilityDelegator;
N
njupthan 已提交
275

N
njupthan 已提交
276
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
277
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
278 279
}

280
let monitor = {
M
mingxihua 已提交
281
    abilityName: 'abilityname',
N
njupthan 已提交
282
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
283
};
N
njupthan 已提交
284 285

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
286
abilityDelegator.waitAbilityMonitor(monitor).then((data : any) => {
M
mingxihua 已提交
287
    console.info('waitAbilityMonitor promise');
N
njupthan 已提交
288 289 290
});
```

N
njupthan 已提交
291
### getAppContext<sup>9+</sup>
N
njupthan 已提交
292

Y
yuyaozhi 已提交
293
getAppContext(): Context;
N
njupthan 已提交
294 295 296

获取应用Context

N
njupthan 已提交
297
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
298 299 300 301 302

**返回值:**

| 类型                                  | 说明                                        |
| ------------------------------------- | ------------------------------------------- |
M
m00512953 已提交
303
| [Context](js-apis-inner-application-context.md) | 应用[Context](js-apis-inner-application-context.md)。 |
N
njupthan 已提交
304 305 306

**示例:**

M
m00512953 已提交
307
```ts
308
let abilityDelegator;
N
njupthan 已提交
309 310

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
311
let context = abilityDelegator.getAppContext();
N
njupthan 已提交
312 313
```

N
njupthan 已提交
314
### getAbilityState<sup>9+</sup>
N
njupthan 已提交
315

Y
yuyaozhi 已提交
316
getAbilityState(ability: UIAbility): number;
N
njupthan 已提交
317 318 319

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

N
njupthan 已提交
320
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
321 322 323

**参数:**

N
njupthan 已提交
324 325
| 参数名  | 类型                                              | 必填 | 说明            |
| ------- | ------------------------------------------------- | ---- | --------------- |
M
m00512953 已提交
326
| ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是   | 指定Ability对象 |
N
njupthan 已提交
327 328 329 330 331

**返回值:**

| 类型   | 说明                                                         |
| ------ | ------------------------------------------------------------ |
M
m00512953 已提交
332
| number | 指定ability的生命周期状态。状态枚举值使用[AbilityLifecycleState](js-apis-application-abilityDelegatorRegistry.md#AbilityLifecycleState)。 |
N
njupthan 已提交
333 334 335

**示例:**

M
m00512953 已提交
336
```ts
337 338
let abilityDelegator;
let ability;
N
njupthan 已提交
339 340

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
341
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
342
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
343
    ability = data;
344
    let state = abilityDelegator.getAbilityState(ability);
M
mingxihua 已提交
345
    console.info('getAbilityState ${state}');
N
njupthan 已提交
346 347 348
});
```

N
njupthan 已提交
349
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
350

Y
yuyaozhi 已提交
351
getCurrentTopAbility(callback: AsyncCallback\<UIAbility>): void;
N
njupthan 已提交
352 353 354

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

N
njupthan 已提交
355
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
356 357 358

**参数:**

N
njupthan 已提交
359 360
| 参数名   | 类型                                                         | 必填 | 说明               |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
M
m00512953 已提交
361
| callback | AsyncCallback\<[UIAbility](js-apis-app-ability-uiAbility.md)> | 是   | 表示指定的回调方法 |
N
njupthan 已提交
362 363 364

**示例:**

M
m00512953 已提交
365
```ts
366 367
let abilityDelegator;
let ability;
N
njupthan 已提交
368 369

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

N
njupthan 已提交
376
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
377

Y
yuyaozhi 已提交
378
getCurrentTopAbility(): Promise\<UIAbility>;
N
njupthan 已提交
379 380 381

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

N
njupthan 已提交
382
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
383 384 385

**返回值:**

N
njupthan 已提交
386 387
| 类型                                                        | 说明                                   |
| ----------------------------------------------------------- | -------------------------------------- |
M
m00512953 已提交
388
| Promise\<[UIAbility](js-apis-app-ability-uiAbility.md)> | 以Promise形式返回当前应用顶部ability。 |
N
njupthan 已提交
389 390 391

**示例:**

M
m00512953 已提交
392
```ts
393 394
let abilityDelegator;
let ability;
N
njupthan 已提交
395 396

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

N
njupthan 已提交
403
### startAbility<sup>9+</sup>
N
njupthan 已提交
404

Y
yuyaozhi 已提交
405
startAbility(want: Want, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
406 407 408

启动指定ability(callback形式)

N
njupthan 已提交
409
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
410 411 412 413 414

**参数:**

| 参数名   | 类型                                   | 必填 | 说明               |
| -------- | -------------------------------------- | ---- | ------------------ |
M
m00512953 已提交
415
| want     | [Want](js-apis-application-want.md) | 是   | 启动Ability参数    |
N
njupthan 已提交
416 417 418 419
| callback | AsyncCallback\<void>                   | 是   | 表示指定的回调方法 |

**示例:**

M
m00512953 已提交
420
```ts
421 422
let abilityDelegator;
let want = {
M
mingxihua 已提交
423 424
    bundleName: 'bundleName',
    abilityName: 'abilityName'
N
njupthan 已提交
425 426 427
};

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

N
njupthan 已提交
433
### startAbility<sup>9+</sup>
N
njupthan 已提交
434

Y
yuyaozhi 已提交
435
startAbility(want: Want): Promise\<void>;
N
njupthan 已提交
436 437 438

启动指定ability(promise形式)

N
njupthan 已提交
439
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
440 441 442 443 444

**参数:**

| 参数名 | 类型                                   | 必填 | 说明            |
| ------ | -------------------------------------- | ---- | --------------- |
M
m00512953 已提交
445
| want   | [Want](js-apis-application-want.md) | 是   | 启动Ability参数 |
N
njupthan 已提交
446 447 448 449 450 451 452 453 454

**返回值:**

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

**示例:**

M
m00512953 已提交
455
```ts
456 457
let abilityDelegator;
let want = {
M
mingxihua 已提交
458 459
    bundleName: 'bundleName',
    abilityName: 'abilityName'
N
njupthan 已提交
460 461 462
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
463
abilityDelegator.startAbility(want).then((data: any) => {
M
mingxihua 已提交
464
    console.info('startAbility promise');
N
njupthan 已提交
465 466 467
});
```

N
njupthan 已提交
468
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
469

Y
yuyaozhi 已提交
470
doAbilityForeground(ability: UIAbility, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
471 472 473

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

N
njupthan 已提交
474
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
475 476 477 478 479

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                                    |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
Y
yuyaozhi 已提交
480 481
| ability  | UIAbility               | 是   | 指定Ability对象                                         |
| callback | AsyncCallback\<void>    | 是   | 表示指定的回调方法<br/>\- true:成功<br/>\- false:失败 |
N
njupthan 已提交
482 483 484

**示例:**

M
m00512953 已提交
485
```ts
486 487
let abilityDelegator;
let ability;
N
njupthan 已提交
488 489

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
490
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
491
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
492
    ability = data;
杨亮 已提交
493 494
    abilityDelegator.doAbilityForeground(ability, (err : any) => {
        console.info("doAbilityForeground callback");
N
njupthan 已提交
495
    });
N
njupthan 已提交
496 497 498
});
```

N
njupthan 已提交
499
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
500

Y
yuyaozhi 已提交
501
doAbilityForeground(ability: UIAbility): Promise\<void>;
N
njupthan 已提交
502 503 504

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

N
njupthan 已提交
505
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
506 507 508 509 510

**参数:**

| 参数名  | 类型    | 必填 | 说明            |
| ------- | ------- | ---- | --------------- |
Y
yuyaozhi 已提交
511
| ability | UIAbility | 是   | 指定Ability对象 |
N
njupthan 已提交
512 513 514 515 516 517 518 519 520

**返回值:**

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

**示例:**

M
m00512953 已提交
521
```ts
522 523
let abilityDelegator;
let ability;
N
njupthan 已提交
524 525

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
526
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
527
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
528
    ability = data;
杨亮 已提交
529 530
    abilityDelegator.doAbilityForeground(ability).then(() => {
        console.info("doAbilityForeground promise");
N
njupthan 已提交
531 532 533 534
    });
});
```

N
njupthan 已提交
535
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
536

Y
yuyaozhi 已提交
537
doAbilityBackground(ability: UIAbility, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
538 539 540

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

N
njupthan 已提交
541
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
542 543 544 545 546

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                                    |
| -------- | ----------------------- | ---- | ------------------------------------------------------- |
Y
yuyaozhi 已提交
547 548
| ability  | UIAbility                 | 是   | 指定Ability对象                                         |
| callback | AsyncCallback\<void> | 是   | 表示指定的回调方法<br/>\- true:成功<br/>\- false:失败 |
N
njupthan 已提交
549 550 551

**示例:**

M
m00512953 已提交
552
```ts
553 554
let abilityDelegator;
let ability;
N
njupthan 已提交
555 556

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
557
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
558
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
559
    ability = data;
杨亮 已提交
560 561
    abilityDelegator.doAbilityBackground(ability, (err : any) => {
        console.info("doAbilityBackground callback");
N
njupthan 已提交
562 563 564 565
    });
});
```

N
njupthan 已提交
566
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
567

Y
yuyaozhi 已提交
568
doAbilityBackground(ability: UIAbility): Promise\<void>;
N
njupthan 已提交
569 570 571

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

N
njupthan 已提交
572
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
573 574 575 576 577

**参数:**

| 参数名  | 类型    | 必填 | 说明            |
| ------- | ------- | ---- | --------------- |
Y
yuyaozhi 已提交
578
| ability | UIAbility | 是   | 指定Ability对象 |
N
njupthan 已提交
579 580 581 582 583 584 585 586 587

**返回值:**

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

**示例:**

M
m00512953 已提交
588
```ts
589 590
let abilityDelegator;
let ability;
N
njupthan 已提交
591 592

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
593
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
594
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
595
    ability = data;
杨亮 已提交
596 597
    abilityDelegator.doAbilityBackground(ability).then(() => {
        console.info("doAbilityBackground promise");
N
njupthan 已提交
598 599 600 601
    });
});
```

602
### printSync<sup>9+</sup>
X
update  
xuchenghua09 已提交
603

Y
yuyaozhi 已提交
604
printSync(msg: string): void;
X
update  
xuchenghua09 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617

打印日志信息到单元测试终端控制台

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

**参数:**

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

**示例:**

M
m00512953 已提交
618
```ts
619
let abilityDelegator;
M
mingxihua 已提交
620
let msg = 'msg';
X
update  
xuchenghua09 已提交
621 622 623 624 625

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

626
### print
N
njupthan 已提交
627

Y
yuyaozhi 已提交
628
print(msg: string, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
629 630 631

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

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

**参数:**

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

**示例:**

M
m00512953 已提交
643
```ts
644
let abilityDelegator;
M
mingxihua 已提交
645
let msg = 'msg';
N
njupthan 已提交
646 647

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
648
abilityDelegator.print(msg, (err : any) => {
M
mingxihua 已提交
649
    console.info('print callback');
N
njupthan 已提交
650 651 652
});
```

653
### print
N
njupthan 已提交
654

Y
yuyaozhi 已提交
655
print(msg: string): Promise\<void>;
N
njupthan 已提交
656 657 658

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

N
njupthan 已提交
659
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

**参数:**

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

**返回值:**

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

**示例:**

M
m00512953 已提交
675
```ts
676
let abilityDelegator;
M
mingxihua 已提交
677
let msg = 'msg';
N
njupthan 已提交
678 679 680

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

### executeShellCommand

Y
yuyaozhi 已提交
687
executeShellCommand(cmd: string, callback: AsyncCallback\<ShellCmdResult>): void;
N
njupthan 已提交
688 689 690

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

N
njupthan 已提交
691
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
692 693 694

**参数:**

N
njupthan 已提交
695 696 697
| 参数名   | 类型                                                         | 必填 | 说明               |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
| cmd      | string                                                       | 是   | shell命令字符串    |
M
m00512953 已提交
698
| callback | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法 |
N
njupthan 已提交
699 700 701

**示例:**

M
m00512953 已提交
702
```ts
703
let abilityDelegator;
M
mingxihua 已提交
704
let cmd = 'cmd';
N
njupthan 已提交
705 706

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

### executeShellCommand

Y
yuyaozhi 已提交
714
executeShellCommand(cmd: string, timeoutSecs: number, callback: AsyncCallback\<ShellCmdResult>): void;
N
njupthan 已提交
715

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

N
njupthan 已提交
718
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
719 720 721

**参数:**

N
njupthan 已提交
722 723 724
| 参数名      | 类型                                                         | 必填 | 说明                          |
| ----------- | ------------------------------------------------------------ | ---- | ----------------------------- |
| cmd         | string                                                       | 是   | shell命令字符串               |
Y
yuyaozhi 已提交
725
| timeoutSecs | number                                                       | 否   | 设定命令超时时间,单位秒(s) |
M
m00512953 已提交
726
| callback    | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法            |
N
njupthan 已提交
727 728 729

**示例:**

M
m00512953 已提交
730
```ts
731
let abilityDelegator;
M
mingxihua 已提交
732
let cmd = 'cmd';
733
let timeout = 100;
N
njupthan 已提交
734 735

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

### executeShellCommand

Y
yuyaozhi 已提交
743
executeShellCommand(cmd: string, timeoutSecs?: number): Promise\<ShellCmdResult>;
N
njupthan 已提交
744

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

N
njupthan 已提交
747
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
748 749 750

**参数:**

N
njupthan 已提交
751 752 753 754
| 参数名      | 类型   | 必填 | 说明                          |
| ----------- | ------ | ---- | ----------------------------- |
| cmd         | string | 是   | shell命令字符串               |
| timeoutSecs | number | 否   | 设定命令超时时间,单位秒(s) |
N
njupthan 已提交
755 756 757

**返回值:**

N
njupthan 已提交
758 759
| 类型                                                         | 说明                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
M
m00512953 已提交
760
| Promise\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | 以Promise形式返回Shell命令执行结果[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)对象。 |
N
njupthan 已提交
761 762 763

**示例:**

M
m00512953 已提交
764
```ts
765
let abilityDelegator;
M
mingxihua 已提交
766
let cmd = 'cmd';
767
let timeout = 100;
N
njupthan 已提交
768 769

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
770
abilityDelegator.executeShellCommand(cmd, timeout).then((data : any) => {
M
mingxihua 已提交
771
    console.info('executeShellCommand promise');
N
njupthan 已提交
772 773
});
```
774

N
njupthan 已提交
775
### finishTest<sup>9+</sup>
776

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

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

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

**参数:**

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

**示例:**

M
m00512953 已提交
793
```ts
794
let abilityDelegator;
M
mingxihua 已提交
795
let msg = 'msg';
796 797 798

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

N
njupthan 已提交
803
### finishTest<sup>9+</sup>
804

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

M
m00512953 已提交
826
```ts
827
let abilityDelegator;
M
mingxihua 已提交
828
let msg = 'msg';
829 830 831

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0).then(() => {
M
mingxihua 已提交
832
    console.info('finishTest promise');
833 834 835
});
```

Y
yuyaozhi 已提交
836 837 838 839 840 841 842 843 844 845
### addAbilityStageMonitor<sup>9+</sup>

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

添加一个AbilityStageMonitor对象,用于监视指定abilityStage的生命周期状态更改。(callback形式)

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

**参数:**

Y
yuyaozhi 已提交
846
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
Y
yuyaozhi 已提交
847
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
M
m00512953 已提交
848
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是       | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
849 850 851 852
| callback | AsyncCallback\<void>                                         | 是       | 表示指定的回调方法                                           |

**示例:**

M
m00512953 已提交
853
```ts
854
let abilityDelegator;
Y
yuyaozhi 已提交
855

856
let monitor = {
M
mingxihua 已提交
857 858 859
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
860 861 862

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

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

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

添加一个AbilityStageMonitor对象,用于监视指定abilityStage的生命周期状态更改。(promise形式)

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

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
879
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
880 881 882 883 884 885 886 887 888

**返回值:**

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

**示例:**

M
m00512953 已提交
889
```ts
890
let abilityDelegator;
Y
yuyaozhi 已提交
891

892
let monitor = {
M
mingxihua 已提交
893 894 895
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
896 897 898

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor).then(() => {
M
mingxihua 已提交
899
    console.info('addAbilityStageMonitor promise');
Y
yuyaozhi 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912
});
```

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

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

从应用程序内存中删除指定的AbilityStageMonitor对象。(callback形式)

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

**参数:**

Y
yuyaozhi 已提交
913
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
Y
yuyaozhi 已提交
914
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
M
m00512953 已提交
915
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是       | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
916 917 918 919
| callback | AsyncCallback\<void>                                         | 是       | 表示指定的回调方法                                           |

**示例:**

M
m00512953 已提交
920
```ts
921
let abilityDelegator;
Y
yuyaozhi 已提交
922

923
let monitor = {
M
mingxihua 已提交
924 925 926
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
927 928 929

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

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

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

从应用程序内存中删除指定的AbilityStageMonitor对象。(promise形式)

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

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
946
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
947 948 949 950 951 952 953 954 955

**返回值:**

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

**示例:**

M
m00512953 已提交
956
```ts
957
let abilityDelegator;
Y
yuyaozhi 已提交
958

959
let monitor = {
M
mingxihua 已提交
960 961 962
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
963 964 965

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor).then(() => {
M
mingxihua 已提交
966
    console.info('removeAbilityStageMonitor promise');
Y
yuyaozhi 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979
});
```

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

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

等待并返回与给定AbilityStageMonitor中设置的条件匹配的AbilityStage对象。(callback形式)

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

**参数:**

Y
yuyaozhi 已提交
980
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
Y
yuyaozhi 已提交
981
| -------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
M
m00512953 已提交
982
| monitor  | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是       | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
983 984 985 986
| callback | AsyncCallback\<AbilityStage>                                         | 是       | 成功返回AbilityStage对象,失败返回空。             |

**示例:**

M
m00512953 已提交
987
```ts
988
let abilityDelegator;
Y
yuyaozhi 已提交
989 990

function onAbilityCreateCallback(data) {
M
mingxihua 已提交
991
    console.info('onAbilityCreateCallback');
Y
yuyaozhi 已提交
992 993
}

994
let monitor = {
M
mingxihua 已提交
995 996 997
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
998 999 1000

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor, (err : any, data : any) => {
M
mingxihua 已提交
1001
    console.info('waitAbilityStageMonitor callback');
Y
yuyaozhi 已提交
1002 1003
});
```
Z
zhongjianfei 已提交
1004

Y
yuyaozhi 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
### waitAbilityStageMonitor<sup>9+</sup>

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

等待并返回与给定AbilityStageMonitor中设置的条件匹配的AbilityStage对象。(promise形式)

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

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
1017
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
1018
| timeout | number | 否   | 超时最大等待时间,以毫秒为单位。 |
Y
yuyaozhi 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027

**返回值:**

| 类型           | 说明                |
| -------------- | ------------------- |
| Promise\<AbilityStage> | 成功返回AbilityStage对象,失败返回空。 |

**示例:**

M
m00512953 已提交
1028
```ts
1029
let abilityDelegator;
Y
yuyaozhi 已提交
1030 1031

function onAbilityCreateCallback(data) {
M
mingxihua 已提交
1032
    console.info('onAbilityCreateCallback');
Y
yuyaozhi 已提交
1033 1034
}

1035
let monitor = {
M
mingxihua 已提交
1036 1037 1038
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1039 1040 1041

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor).then((data : any) => {
M
mingxihua 已提交
1042
    console.info('waitAbilityStageMonitor promise');
Y
yuyaozhi 已提交
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;

等待并返回与给定AbilityStageMonitor中设置的条件匹配的AbilityStage对象。(callback形式)

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

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
1058
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
1059 1060 1061 1062 1063
| timeout | number | 否   | 超时最大等待时间,以毫秒为单位。 |
| callback | AsyncCallback\<AbilityStage>                                         | 是       | 成功返回AbilityStage对象,失败返回空。                     |

**示例:**

M
m00512953 已提交
1064
```ts
1065 1066
let abilityDelegator;
let timeout = 100;
Y
yuyaozhi 已提交
1067 1068

function onAbilityCreateCallback(data) {
M
mingxihua 已提交
1069
    console.info('onAbilityCreateCallback');
Y
yuyaozhi 已提交
1070 1071
}

1072
let monitor = {
M
mingxihua 已提交
1073 1074 1075
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1076 1077 1078

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor, timeout, (err : any, data : any) => {
M
mingxihua 已提交
1079
    console.info('waitAbilityStageMonitor callback');
Y
yuyaozhi 已提交
1080
});
M
m00512953 已提交
1081
```