js-apis-inner-application-abilityDelegator.md 46.0 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 10 11 12 13 14
## 导入模块

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

15
## 使用说明
N
njupthan 已提交
16

Z
zhongjianfei 已提交
17
通过AbilityDelegatorRegistry中[getAbilityDelegator](js-apis-app-ability-abilityDelegatorRegistry.md#abilitydelegatorregistrygetabilitydelegator)方法获取。
M
m00512953 已提交
18
```ts
19
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
N
njupthan 已提交
20 21 22 23
```

## AbilityDelegator

N
njupthan 已提交
24
### addAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
25

Y
yuyaozhi 已提交
26
addAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
27 28 29

添加AbilityMonitor实例(callback形式)

Y
yuyaozhi 已提交
30
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
31 32 33

**参数:**

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

H
huangshiwei 已提交
39 40 41 42 43 44 45 46
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | AddAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
47 48
**示例:**

M
m00512953 已提交
49
```ts
50
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
51

N
njupthan 已提交
52
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
53
    console.info('onAbilityCreateCallback, data: ${JSON.stringify(data)}');
N
njupthan 已提交
54 55
}

56
let monitor = {
M
mingxihua 已提交
57
    abilityName: 'abilityname',
N
njupthan 已提交
58
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
59
};
N
njupthan 已提交
60 61

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
M
mingxihua 已提交
62 63
abilityDelegator.addAbilityMonitor(monitor, (error : any) => {
    console.error('addAbilityMonitor fail, error: ${JSON.stringify(error)}');
N
njupthan 已提交
64 65 66
});
```

N
njupthan 已提交
67
### addAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
68

Y
yuyaozhi 已提交
69
addAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
N
njupthan 已提交
70 71 72

添加AbilityMonitor实例(promise形式)

Y
yuyaozhi 已提交
73
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
74 75 76

**参数:**

N
njupthan 已提交
77 78
| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
79
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
80 81 82 83 84 85 86

**返回值:**

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

H
huangshiwei 已提交
87 88 89 90 91 92 93 94
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | AddAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
95 96
**示例:**

M
m00512953 已提交
97
```ts
98
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
99

N
njupthan 已提交
100
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
101
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
102 103
}

104
let monitor = {
M
mingxihua 已提交
105
    abilityName: 'abilityname',
N
njupthan 已提交
106
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
107
};
N
njupthan 已提交
108 109

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
110
abilityDelegator.addAbilityMonitor(monitor).then(() => {
M
mingxihua 已提交
111
    console.info('addAbilityMonitor promise');
N
njupthan 已提交
112 113 114
});
```

N
njupthan 已提交
115
### removeAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
116

Y
yuyaozhi 已提交
117
removeAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
118 119 120

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

Y
yuyaozhi 已提交
121
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
122 123 124

**参数:**

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

H
huangshiwei 已提交
130 131 132 133 134 135 136 137
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | RemoveAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
138 139
**示例:**

M
m00512953 已提交
140
```ts
141
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
142

N
njupthan 已提交
143
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
144
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
145 146
}

147
let monitor = {
M
mingxihua 已提交
148
    abilityName: 'abilityname',
N
njupthan 已提交
149
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
150
};
N
njupthan 已提交
151 152

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
M
mingxihua 已提交
153 154
abilityDelegator.removeAbilityMonitor(monitor, (error : any) => {
    console.error('removeAbilityMonitor fail, error: ${JSON.stringify(error)}');
N
njupthan 已提交
155 156 157
});
```

N
njupthan 已提交
158
### removeAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
159

Y
yuyaozhi 已提交
160
removeAbilityMonitor(monitor: AbilityMonitor): Promise\<void>;
N
njupthan 已提交
161 162 163

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

Y
yuyaozhi 已提交
164
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
165 166 167

**参数:**

Y
yuyaozhi 已提交
168
| 参数名    | 类型                                                         | 必填 | 说明                                                         |
N
njupthan 已提交
169
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
170
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
171 172 173 174 175 176 177

**返回值:**

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

H
huangshiwei 已提交
178 179 180 181 182 183 184 185
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | RemoveAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
186 187
- 示例

M
m00512953 已提交
188
```ts
189
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
190

N
njupthan 已提交
191
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
192
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
193 194
}

195
let monitor = {
M
mingxihua 已提交
196
    abilityName: 'abilityname',
N
njupthan 已提交
197
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
198
};
N
njupthan 已提交
199 200

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
201
abilityDelegator.removeAbilityMonitor(monitor).then(() => {
M
mingxihua 已提交
202
    console.info('removeAbilityMonitor promise');
N
njupthan 已提交
203 204 205
});
```

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

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

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

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

**参数:**

N
njupthan 已提交
216 217
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
218 219
| 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 已提交
220

H
huangshiwei 已提交
221 222 223 224 225 226 227 228
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | WaitAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
229 230
**示例:**

M
m00512953 已提交
231
```ts
232
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
233

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

238
let monitor = {
M
mingxihua 已提交
239
    abilityName: 'abilityname',
N
njupthan 已提交
240
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
241
};
N
njupthan 已提交
242 243

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
M
mingxihua 已提交
244
abilityDelegator.waitAbilityMonitor(monitor, (error : any, data : any) => {
X
xieqiongyang 已提交
245
    if (error) {
M
mingxihua 已提交
246 247 248 249
        console.error('waitAbilityMonitor fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('waitAbilityMonitor success, data: ${JSON.stringify(data)}');
    }
N
njupthan 已提交
250 251 252
});
```

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

Y
yuyaozhi 已提交
255
waitAbilityMonitor(monitor: AbilityMonitor, timeout: number, callback: AsyncCallback\<UIAbility>): void;
N
njupthan 已提交
256 257 258

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

Y
yuyaozhi 已提交
259
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
260 261 262

**参数:**

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

H
huangshiwei 已提交
269 270 271 272 273 274 275 276
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | WaitAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
277 278
**示例:**

M
m00512953 已提交
279
```ts
280
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
281
let timeout = 100;
N
njupthan 已提交
282

N
njupthan 已提交
283
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
284
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
285 286
}

287
let monitor = {
M
mingxihua 已提交
288
    abilityName: 'abilityname',
N
njupthan 已提交
289
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
290
};
N
njupthan 已提交
291 292

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
M
mingxihua 已提交
293 294 295 296 297 298
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 已提交
299 300 301 302 303
});
```



N
njupthan 已提交
304
### waitAbilityMonitor<sup>9+</sup>
N
njupthan 已提交
305

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

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

Y
yuyaozhi 已提交
310
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
311 312 313

**参数:**

N
njupthan 已提交
314 315
| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
316
| monitor | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor) | 是   | [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#AbilityMonitor)实例 |
N
njupthan 已提交
317
| timeout | number                                                       | 否   | 最大等待时间,单位毫秒(ms)                                 |
N
njupthan 已提交
318 319 320

**返回值:**

N
njupthan 已提交
321 322
| 类型                                                        | 说明                       |
| ----------------------------------------------------------- | -------------------------- |
M
m00512953 已提交
323
| Promise\<[UIAbility](js-apis-app-ability-uiAbility.md)> | 以Promise形式返回Ability。 |
N
njupthan 已提交
324

H
huangshiwei 已提交
325 326 327 328 329 330 331 332
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | WaitAbilityMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
333 334
**示例:**

M
m00512953 已提交
335
```ts
336
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
337

N
njupthan 已提交
338
function onAbilityCreateCallback(data) {
M
mingxihua 已提交
339
    console.info('onAbilityCreateCallback');
N
njupthan 已提交
340 341
}

342
let monitor = {
M
mingxihua 已提交
343
    abilityName: 'abilityname',
N
njupthan 已提交
344
    onAbilityCreate: onAbilityCreateCallback
M
mingxihua 已提交
345
};
N
njupthan 已提交
346 347

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
348
abilityDelegator.waitAbilityMonitor(monitor).then((data : any) => {
M
mingxihua 已提交
349
    console.info('waitAbilityMonitor promise');
N
njupthan 已提交
350 351 352
});
```

N
njupthan 已提交
353
### getAppContext<sup>9+</sup>
N
njupthan 已提交
354

Y
yuyaozhi 已提交
355
getAppContext(): Context;
N
njupthan 已提交
356 357 358

获取应用Context

Y
yuyaozhi 已提交
359
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
360 361 362 363 364

**返回值:**

| 类型                                  | 说明                                        |
| ------------------------------------- | ------------------------------------------- |
M
m00512953 已提交
365
| [Context](js-apis-inner-application-context.md) | 应用[Context](js-apis-inner-application-context.md)。 |
N
njupthan 已提交
366 367 368

**示例:**

M
m00512953 已提交
369
```ts
370
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
N
njupthan 已提交
371 372

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
373
let context = abilityDelegator.getAppContext();
N
njupthan 已提交
374 375
```

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

Y
yuyaozhi 已提交
378
getAbilityState(ability: UIAbility): number;
N
njupthan 已提交
379 380 381

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

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

**参数:**

N
njupthan 已提交
386 387
| 参数名  | 类型                                              | 必填 | 说明            |
| ------- | ------------------------------------------------- | ---- | --------------- |
M
m00512953 已提交
388
| ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是   | 指定Ability对象 |
N
njupthan 已提交
389 390 391 392 393

**返回值:**

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

**示例:**

M
m00512953 已提交
398
```ts
399
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
400
let ability;
N
njupthan 已提交
401 402

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
403
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
404
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
405
    ability = data;
406
    let state = abilityDelegator.getAbilityState(ability);
M
mingxihua 已提交
407
    console.info('getAbilityState ${state}');
N
njupthan 已提交
408 409 410
});
```

N
njupthan 已提交
411
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
412

Y
yuyaozhi 已提交
413
getCurrentTopAbility(callback: AsyncCallback\<UIAbility>): void;
N
njupthan 已提交
414 415 416

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

Y
yuyaozhi 已提交
417
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
418 419 420

**参数:**

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

H
huangshiwei 已提交
425 426 427 428 429 430 431 432
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | GetCurrentTopAbility failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
433 434
**示例:**

M
m00512953 已提交
435
```ts
436
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
437
let ability;
N
njupthan 已提交
438 439

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
440
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
441
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
442 443 444 445
    ability = data;
});
```

N
njupthan 已提交
446
### getCurrentTopAbility<sup>9+</sup>
N
njupthan 已提交
447

Y
yuyaozhi 已提交
448
getCurrentTopAbility(): Promise\<UIAbility>;
N
njupthan 已提交
449 450 451

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

Y
yuyaozhi 已提交
452
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
453 454 455

**返回值:**

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

H
huangshiwei 已提交
460 461 462 463 464 465 466 467
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | GetCurrentTopAbility failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
468 469
**示例:**

M
m00512953 已提交
470
```ts
471
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
472
let ability;
N
njupthan 已提交
473 474

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
475
abilityDelegator.getCurrentTopAbility().then((data : any) => {
M
mingxihua 已提交
476
    console.info('getCurrentTopAbility promise');
N
njupthan 已提交
477
    ability = data;
N
njupthan 已提交
478 479 480
});
```

N
njupthan 已提交
481
### startAbility<sup>9+</sup>
N
njupthan 已提交
482

Y
yuyaozhi 已提交
483
startAbility(want: Want, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
484 485 486

启动指定ability(callback形式)

Y
yuyaozhi 已提交
487
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
488 489 490 491 492

**参数:**

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

H
huangshiwei 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
509 510
| 16000012 | The application is controlled. |
| 16000013 | The application is controlled by EDM. |
H
huangshiwei 已提交
511 512 513 514 515 516 517
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
518 519
**示例:**

M
m00512953 已提交
520
```ts
521
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
522
let want = {
M
mingxihua 已提交
523 524
    bundleName: 'bundleName',
    abilityName: 'abilityName'
N
njupthan 已提交
525 526 527
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
528
abilityDelegator.startAbility(want, (err : any, data : any) => {
M
mingxihua 已提交
529
    console.info('startAbility callback');
N
njupthan 已提交
530 531 532
});
```

N
njupthan 已提交
533
### startAbility<sup>9+</sup>
N
njupthan 已提交
534

Y
yuyaozhi 已提交
535
startAbility(want: Want): Promise\<void>;
N
njupthan 已提交
536 537 538

启动指定ability(promise形式)

Y
yuyaozhi 已提交
539
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
540 541 542 543 544

**参数:**

| 参数名 | 类型                                   | 必填 | 说明            |
| ------ | -------------------------------------- | ---- | --------------- |
M
m00512953 已提交
545
| want   | [Want](js-apis-application-want.md) | 是   | 启动Ability参数 |
N
njupthan 已提交
546 547 548 549 550 551 552

**返回值:**

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

H
huangshiwei 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
566 567
| 16000012 | The application is controlled. |
| 16000013 | The application is controlled by EDM. |
H
huangshiwei 已提交
568 569 570 571 572 573 574
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
575 576
**示例:**

M
m00512953 已提交
577
```ts
578
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
579
let want = {
M
mingxihua 已提交
580 581
    bundleName: 'bundleName',
    abilityName: 'abilityName'
N
njupthan 已提交
582 583 584
};

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
585
abilityDelegator.startAbility(want).then((data: any) => {
M
mingxihua 已提交
586
    console.info('startAbility promise');
N
njupthan 已提交
587 588 589
});
```

N
njupthan 已提交
590
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
591

Y
yuyaozhi 已提交
592
doAbilityForeground(ability: UIAbility, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
593 594 595

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

Y
yuyaozhi 已提交
596
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
597 598 599 600 601

**参数:**

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

H
huangshiwei 已提交
605 606 607 608 609 610 611 612
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | DoAbilityForeground failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
613 614
**示例:**

M
m00512953 已提交
615
```ts
616
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
617
let ability;
N
njupthan 已提交
618 619

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
620
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
621
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
622
    ability = data;
杨亮 已提交
623 624
    abilityDelegator.doAbilityForeground(ability, (err : any) => {
        console.info("doAbilityForeground callback");
N
njupthan 已提交
625
    });
N
njupthan 已提交
626 627 628
});
```

N
njupthan 已提交
629
### doAbilityForeground<sup>9+</sup>
N
njupthan 已提交
630

Y
yuyaozhi 已提交
631
doAbilityForeground(ability: UIAbility): Promise\<void>;
N
njupthan 已提交
632 633 634

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

Y
yuyaozhi 已提交
635
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
636 637 638 639 640

**参数:**

| 参数名  | 类型    | 必填 | 说明            |
| ------- | ------- | ---- | --------------- |
Y
yuyaozhi 已提交
641
| ability | UIAbility | 是   | 指定Ability对象 |
N
njupthan 已提交
642 643 644 645 646 647 648

**返回值:**

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

H
huangshiwei 已提交
649 650 651 652 653 654 655 656
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | DoAbilityForeground failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
657 658
**示例:**

M
m00512953 已提交
659
```ts
660
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
661
let ability;
N
njupthan 已提交
662 663

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
664
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
665
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
666
    ability = data;
杨亮 已提交
667 668
    abilityDelegator.doAbilityForeground(ability).then(() => {
        console.info("doAbilityForeground promise");
N
njupthan 已提交
669 670 671 672
    });
});
```

N
njupthan 已提交
673
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
674

Y
yuyaozhi 已提交
675
doAbilityBackground(ability: UIAbility, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
676 677 678

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

Y
yuyaozhi 已提交
679
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
680 681 682 683 684

**参数:**

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

H
huangshiwei 已提交
688 689 690 691 692 693 694 695
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | DoAbilityBackground failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
696 697
**示例:**

M
m00512953 已提交
698
```ts
699
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
700
let ability;
N
njupthan 已提交
701 702

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
703
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
704
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
705
    ability = data;
杨亮 已提交
706 707
    abilityDelegator.doAbilityBackground(ability, (err : any) => {
        console.info("doAbilityBackground callback");
N
njupthan 已提交
708 709 710 711
    });
});
```

N
njupthan 已提交
712
### doAbilityBackground<sup>9+</sup>
N
njupthan 已提交
713

Y
yuyaozhi 已提交
714
doAbilityBackground(ability: UIAbility): Promise\<void>;
N
njupthan 已提交
715 716 717

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

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

**参数:**

| 参数名  | 类型    | 必填 | 说明            |
| ------- | ------- | ---- | --------------- |
Y
yuyaozhi 已提交
724
| ability | UIAbility | 是   | 指定Ability对象 |
N
njupthan 已提交
725 726 727 728 729 730 731

**返回值:**

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

H
huangshiwei 已提交
732 733 734 735 736 737 738 739
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | DoAbilityBackground failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

N
njupthan 已提交
740 741
**示例:**

M
m00512953 已提交
742
```ts
743
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
744
let ability;
N
njupthan 已提交
745 746

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
747
abilityDelegator.getCurrentTopAbility((err : any, data : any) => {
M
mingxihua 已提交
748
    console.info('getCurrentTopAbility callback');
N
njupthan 已提交
749
    ability = data;
杨亮 已提交
750 751
    abilityDelegator.doAbilityBackground(ability).then(() => {
        console.info("doAbilityBackground promise");
N
njupthan 已提交
752 753 754 755
    });
});
```

756
### printSync<sup>9+</sup>
X
update  
xuchenghua09 已提交
757

Y
yuyaozhi 已提交
758
printSync(msg: string): void;
X
update  
xuchenghua09 已提交
759 760 761

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

Y
yuyaozhi 已提交
762
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
X
update  
xuchenghua09 已提交
763 764 765 766 767 768 769 770 771

**参数:**

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

**示例:**

M
m00512953 已提交
772
```ts
773
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
774
let msg = 'msg';
X
update  
xuchenghua09 已提交
775 776 777 778 779

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

780
### print
N
njupthan 已提交
781

Y
yuyaozhi 已提交
782
print(msg: string, callback: AsyncCallback\<void>): void;
N
njupthan 已提交
783 784 785

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

Y
yuyaozhi 已提交
786
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
787 788 789 790 791 792 793 794 795 796

**参数:**

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

**示例:**

M
m00512953 已提交
797
```ts
798
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
799
let msg = 'msg';
N
njupthan 已提交
800 801

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
802
abilityDelegator.print(msg, (err : any) => {
M
mingxihua 已提交
803
    console.info('print callback');
N
njupthan 已提交
804 805 806
});
```

807
### print
N
njupthan 已提交
808

Y
yuyaozhi 已提交
809
print(msg: string): Promise\<void>;
N
njupthan 已提交
810 811 812

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

Y
yuyaozhi 已提交
813
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828

**参数:**

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

**返回值:**

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

**示例:**

M
m00512953 已提交
829
```ts
830
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
831
let msg = 'msg';
N
njupthan 已提交
832 833 834

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg).then(() => {
M
mingxihua 已提交
835
    console.info('print promise');
N
njupthan 已提交
836 837 838 839 840
});
```

### executeShellCommand

Y
yuyaozhi 已提交
841
executeShellCommand(cmd: string, callback: AsyncCallback\<ShellCmdResult>): void;
N
njupthan 已提交
842 843 844

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

845 846
仅支持如下shell命令:aa, bm, cp, mkdir, rm, uinput, hilog, ppwd, echo, uitest, acm, hidumper, wukong, pkill, ps, pidof

N
njupthan 已提交
847
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
848 849 850

**参数:**

N
njupthan 已提交
851 852 853
| 参数名   | 类型                                                         | 必填 | 说明               |
| -------- | ------------------------------------------------------------ | ---- | ------------------ |
| cmd      | string                                                       | 是   | shell命令字符串    |
M
m00512953 已提交
854
| callback | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法 |
N
njupthan 已提交
855 856 857

**示例:**

M
m00512953 已提交
858
```ts
859
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
860
let cmd = 'cmd';
N
njupthan 已提交
861 862

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
863
abilityDelegator.executeShellCommand(cmd, (err : any, data : any) => {
M
mingxihua 已提交
864
    console.info('executeShellCommand callback');
N
njupthan 已提交
865 866 867 868 869
});
```

### executeShellCommand

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

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

874 875
仅支持如下shell命令:aa, bm, cp, mkdir, rm, uinput, hilog, ppwd, echo, uitest, acm, hidumper, wukong, pkill, ps, pidof

N
njupthan 已提交
876
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
877 878 879

**参数:**

N
njupthan 已提交
880 881 882
| 参数名      | 类型                                                         | 必填 | 说明                          |
| ----------- | ------------------------------------------------------------ | ---- | ----------------------------- |
| cmd         | string                                                       | 是   | shell命令字符串               |
Y
yuyaozhi 已提交
883
| timeoutSecs | number                                                       | 是   | 设定命令超时时间,单位秒(s) |
M
m00512953 已提交
884
| callback    | AsyncCallback\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | 是   | 表示指定的回调方法            |
N
njupthan 已提交
885 886 887

**示例:**

M
m00512953 已提交
888
```ts
889
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
890
let cmd = 'cmd';
891
let timeout = 100;
N
njupthan 已提交
892 893

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
894
abilityDelegator.executeShellCommand(cmd, timeout, (err : any, data : any) => {
M
mingxihua 已提交
895
    console.info('executeShellCommand callback');
N
njupthan 已提交
896 897 898 899 900
});
```

### executeShellCommand

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

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

905 906
仅支持如下shell命令:aa, bm, cp, mkdir, rm, uinput, hilog, ppwd, echo, uitest, acm, hidumper, wukong, pkill, ps, pidof

N
njupthan 已提交
907
**系统能力:** SystemCapability.Ability.AbilityRuntime.Core
N
njupthan 已提交
908 909 910

**参数:**

N
njupthan 已提交
911 912 913 914
| 参数名      | 类型   | 必填 | 说明                          |
| ----------- | ------ | ---- | ----------------------------- |
| cmd         | string | 是   | shell命令字符串               |
| timeoutSecs | number | 否   | 设定命令超时时间,单位秒(s) |
N
njupthan 已提交
915 916 917

**返回值:**

N
njupthan 已提交
918 919
| 类型                                                         | 说明                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
M
m00512953 已提交
920
| Promise\<[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)> | 以Promise形式返回Shell命令执行结果[ShellCmdResult](js-apis-inner-application-shellCmdResult.md#ShellCmdResult)对象。 |
N
njupthan 已提交
921 922 923

**示例:**

M
m00512953 已提交
924
```ts
925
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
926
let cmd = 'cmd';
927
let timeout = 100;
N
njupthan 已提交
928 929

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
N
njupthan 已提交
930
abilityDelegator.executeShellCommand(cmd, timeout).then((data : any) => {
M
mingxihua 已提交
931
    console.info('executeShellCommand promise');
N
njupthan 已提交
932 933
});
```
934

N
njupthan 已提交
935
### finishTest<sup>9+</sup>
936

Y
yuyaozhi 已提交
937
finishTest(msg: string, code: number, callback: AsyncCallback\<void>): void;
938 939 940

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

Y
yuyaozhi 已提交
941
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
942 943 944 945 946 947 948 949 950

**参数:**

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

H
huangshiwei 已提交
951 952 953 954 955 956 957 958
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | FinishTest failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

959 960
**示例:**

M
m00512953 已提交
961
```ts
962
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
963
let msg = 'msg';
964 965 966

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0, (err : any) => {
M
mingxihua 已提交
967
    console.info('finishTest callback');
968 969 970
});
```

N
njupthan 已提交
971
### finishTest<sup>9+</sup>
972

Y
yuyaozhi 已提交
973
finishTest(msg: string, code: number): Promise\<void>;
974 975 976

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

Y
yuyaozhi 已提交
977
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
978 979 980 981 982 983 984 985 986 987 988 989 990 991

**参数:**

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

**返回值:**

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

H
huangshiwei 已提交
992 993 994 995 996 997 998 999
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | FinishTest failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

1000 1001
**示例:**

M
m00512953 已提交
1002
```ts
1003
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
M
mingxihua 已提交
1004
let msg = 'msg';
1005 1006 1007

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0).then(() => {
M
mingxihua 已提交
1008
    console.info('finishTest promise');
1009 1010 1011
});
```

Y
yuyaozhi 已提交
1012 1013 1014 1015 1016 1017
### addAbilityStageMonitor<sup>9+</sup>

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

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

Y
yuyaozhi 已提交
1018
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1019 1020 1021

**参数:**

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

H
huangshiwei 已提交
1027 1028 1029 1030 1031 1032 1033 1034
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | AddAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1035 1036
**示例:**

M
m00512953 已提交
1037
```ts
1038
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
Y
yuyaozhi 已提交
1039

1040
let monitor = {
M
mingxihua 已提交
1041 1042 1043
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1044 1045 1046

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor, (err : any) => {
M
mingxihua 已提交
1047
    console.info('addAbilityStageMonitor callback');
Y
yuyaozhi 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056
});
```

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

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

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

Y
yuyaozhi 已提交
1057
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1058 1059 1060 1061 1062

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
1063
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
1064 1065 1066 1067 1068 1069 1070

**返回值:**

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

H
huangshiwei 已提交
1071 1072 1073 1074 1075 1076 1077 1078
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | AddAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1079 1080
**示例:**

M
m00512953 已提交
1081
```ts
1082
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
Y
yuyaozhi 已提交
1083

1084
let monitor = {
M
mingxihua 已提交
1085 1086 1087
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1088 1089 1090

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityStageMonitor(monitor).then(() => {
M
mingxihua 已提交
1091
    console.info('addAbilityStageMonitor promise');
Y
yuyaozhi 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100
});
```

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

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

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

Y
yuyaozhi 已提交
1101
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1102 1103 1104

**参数:**

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

H
huangshiwei 已提交
1110 1111 1112 1113 1114 1115 1116 1117
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | RemoveAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1118 1119
**示例:**

M
m00512953 已提交
1120
```ts
1121
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
Y
yuyaozhi 已提交
1122

1123
let monitor = {
M
mingxihua 已提交
1124 1125 1126
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1127 1128 1129

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor, (err : any) => {
M
mingxihua 已提交
1130
    console.info('removeAbilityStageMonitor callback');
Y
yuyaozhi 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139
});
```

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

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

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

Y
yuyaozhi 已提交
1140
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1141 1142 1143 1144 1145

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
1146
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
1147 1148 1149 1150 1151 1152 1153

**返回值:**

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

H
huangshiwei 已提交
1154 1155 1156 1157 1158 1159 1160 1161
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | RemoveAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1162 1163
**示例:**

M
m00512953 已提交
1164
```ts
1165
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
Y
yuyaozhi 已提交
1166

1167
let monitor = {
M
mingxihua 已提交
1168 1169 1170
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1171 1172 1173

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityStageMonitor(monitor).then(() => {
M
mingxihua 已提交
1174
    console.info('removeAbilityStageMonitor promise');
Y
yuyaozhi 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183
});
```

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

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

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

Y
yuyaozhi 已提交
1184
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1185 1186 1187

**参数:**

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

H
huangshiwei 已提交
1193 1194 1195 1196 1197 1198 1199 1200
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | WaitAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1201 1202
**示例:**

M
m00512953 已提交
1203
```ts
1204
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
Y
yuyaozhi 已提交
1205 1206

function onAbilityCreateCallback(data) {
M
mingxihua 已提交
1207
    console.info('onAbilityCreateCallback');
Y
yuyaozhi 已提交
1208 1209
}

1210
let monitor = {
M
mingxihua 已提交
1211 1212 1213
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1214 1215 1216

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

Y
yuyaozhi 已提交
1221 1222 1223 1224 1225 1226
### waitAbilityStageMonitor<sup>9+</sup>

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

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

Y
yuyaozhi 已提交
1227
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1228 1229 1230 1231 1232

**参数:**

| 参数名  | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
M
m00512953 已提交
1233
| monitor | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) | 是   | [AbilityStageMonitor](js-apis-inner-application-abilityStageMonitor.md) 实例 |
Y
yuyaozhi 已提交
1234
| timeout | number | 否   | 超时最大等待时间,以毫秒为单位。 |
Y
yuyaozhi 已提交
1235 1236 1237 1238 1239 1240 1241

**返回值:**

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

H
huangshiwei 已提交
1242 1243 1244 1245 1246 1247 1248 1249
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | WaitAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1250 1251
**示例:**

M
m00512953 已提交
1252
```ts
1253
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
Y
yuyaozhi 已提交
1254 1255

function onAbilityCreateCallback(data) {
M
mingxihua 已提交
1256
    console.info('onAbilityCreateCallback');
Y
yuyaozhi 已提交
1257 1258
}

1259
let monitor = {
M
mingxihua 已提交
1260 1261 1262
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1263 1264 1265

abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityStageMonitor(monitor).then((data : any) => {
M
mingxihua 已提交
1266
    console.info('waitAbilityStageMonitor promise');
Y
yuyaozhi 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275
});
```

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

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

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

Y
yuyaozhi 已提交
1276
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
Y
yuyaozhi 已提交
1277 1278 1279 1280 1281

**参数:**

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

H
huangshiwei 已提交
1286 1287 1288 1289 1290 1291 1292 1293
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000100 | WaitAbilityStageMonitor failed. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1294 1295
**示例:**

M
m00512953 已提交
1296
```ts
1297
let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
1298
let timeout = 100;
Y
yuyaozhi 已提交
1299 1300

function onAbilityCreateCallback(data) {
M
mingxihua 已提交
1301
    console.info('onAbilityCreateCallback');
Y
yuyaozhi 已提交
1302 1303
}

1304
let monitor = {
M
mingxihua 已提交
1305 1306 1307
    moduleName: 'moduleName',
    srcEntrance: 'srcEntrance',
};
Y
yuyaozhi 已提交
1308 1309 1310

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