js-apis-inner-application-uiAbilityContext.md 79.7 KB
Newer Older
M
m00512953 已提交
1 2
# UIAbilityContext

3
UIAbilityContext是[UIAbility](js-apis-app-ability-uiAbility.md)的上下文环境,继承自[Context](js-apis-inner-application-context.md),提供UIAbility的相关配置信息以及操作UIAbility和ServiceExtensionAbility的方法,如启动UIAbility,停止当前UIAbilityContext所属的UIAbility,启动、停止、连接、断开连接ServiceExtensionAbility等。
M
m00512953 已提交
4 5 6 7 8 9

> **说明:**
>
>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>  - 本模块接口仅可在Stage模型下使用。

10 11 12 13 14 15
## 导入模块

```ts
import common from '@ohos.app.ability.common';
```

M
m00512953 已提交
16 17 18 19
## 属性

**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

D
merge  
donglin 已提交
20
| 名称 | 类型 | 可读 | 可写 | 说明 |
M
m00512953 已提交
21
| -------- | -------- | -------- | -------- | -------- |
22
| abilityInfo | [AbilityInfo](js-apis-bundleManager-abilityInfo.md) | 是 | 否 | UIAbility的相关信息。 |
zyjhandsome's avatar
zyjhandsome 已提交
23
| currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | 是 | 否 | 当前HAP的信息。 |
24 25 26
| config | [Configuration](js-apis-app-ability-configuration.md) | 是 | 否 | 与UIAbility相关的配置信息,如语言、颜色模式等。 |

> **关于示例代码的说明:**
27 28
>
> 在本文档的示例中,通过`this.context`来获取`UIAbilityContext`,其中`this`代表继承自`UIAbility`的`UIAbility`实例。如需要在页面中使用`UIAbilityContext`提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。
M
m00512953 已提交
29

Z
zhongjianfei 已提交
30
## UIAbilityContext.startAbility
M
m00512953 已提交
31 32 33 34 35

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

启动Ability(callback形式)。

M
m00512953 已提交
36 37
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
38
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
39 40
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

M
m00512953 已提交
41 42 43 44 45 46 47
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
48
| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 |
M
m00512953 已提交
49 50 51 52 53

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66
| 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. |
| 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. |
M
m00512953 已提交
67

H
huangshiwei 已提交
68 69
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
70 71 72
**示例:**

  ```ts
73 74 75 76
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91
try {
  this.context.startAbility(want, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
92 93
  ```

Z
zhongjianfei 已提交
94
## UIAbilityContext.startAbility
M
m00512953 已提交
95 96 97 98 99

startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void;

启动Ability(callback形式)。

M
m00512953 已提交
100 101
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
102
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
103 104
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

M
m00512953 已提交
105 106 107 108 109 110 111 112 113 114 115 116
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 |

**错误码:**

Z
zhongjianfei 已提交
117
| 错误码ID | 错误信息 |
M
m00512953 已提交
118
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131
| 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. |
| 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. |
M
m00512953 已提交
132

H
huangshiwei 已提交
133 134
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
135 136 137
**示例:**

  ```ts
138 139 140 141 142 143 144 145
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};
M
m00512953 已提交
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160
try {
  this.context.startAbility(want, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
161 162
  ```

Z
zhongjianfei 已提交
163
## UIAbilityContext.startAbility
M
m00512953 已提交
164 165 166 167 168

startAbility(want: Want, options?: StartOptions): Promise<void>;

启动Ability(promise形式)。

M
m00512953 已提交
169 170
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
171
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
172 173
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

M
m00512953 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise<void> | Promise形式返回启动结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
| 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. |
| 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. |
M
m00512953 已提交
206

H
huangshiwei 已提交
207 208
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
209 210 211
**示例:**

  ```ts
212 213 214 215 216 217 218
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};
M
m00512953 已提交
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233
try {
  this.context.startAbility(want, options)
    .then(() => {
      // 执行正常业务
      console.info('startAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
234 235
  ```

Z
zhongjianfei 已提交
236
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
237 238 239

startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void;

M
mingxihua 已提交
240
启动一个Ability。Ability被启动后,有如下情况(callback形式):
M
mingxihua 已提交
241
 - 正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
M
mingxihua 已提交
242
 - 异常情况下比如杀死Ability会返回异常信息给调用方, 异常信息中resultCode为-1。
M
mingxihua 已提交
243
 - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方, 其它调用方返回异常信息, 异常信息中resultCode为-1。
M
m00512953 已提交
244

M
m00512953 已提交
245 246
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
247
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
248 249
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

M
m00512953 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 执行结果回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275
| 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. |
| 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. |
M
m00512953 已提交
276

H
huangshiwei 已提交
277 278
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
279 280 281
**示例:**

  ```ts
282 283 284 285 286
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301
try {
  this.context.startAbilityForResult(want, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
302 303
  ```

Z
zhongjianfei 已提交
304
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
305 306 307

startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void;

M
mingxihua 已提交
308
启动一个Ability。Ability被启动后,有如下情况(callback形式):
M
mingxihua 已提交
309
 - 正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
310 311
 - 异常情况下比如杀死Ability会返回异常信息给调用方,异常信息中resultCode为-1。
 - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方,其它调用方返回异常信息, 异常信息中resultCode为-1。
M
m00512953 已提交
312

M
m00512953 已提交
313 314
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
315
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
316
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
M
m00512953 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 执行结果回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344
| 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. |
| 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. |
M
m00512953 已提交
345

H
huangshiwei 已提交
346 347
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
348 349 350
**示例:**

  ```ts
351 352 353 354 355 356 357 358
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};
M
m00512953 已提交
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373
try {
  this.context.startAbilityForResult(want, options, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
} catch (paramError) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
374 375 376
  ```


Z
zhongjianfei 已提交
377
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
378 379 380

startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>;

M
mingxihua 已提交
381
启动一个Ability。Ability被启动后,有如下情况(promise形式):
M
mingxihua 已提交
382
 - 正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
M
mingxihua 已提交
383
 - 异常情况下比如杀死Ability会返回异常信息给调用方, 异常信息中resultCode为-1。
M
mingxihua 已提交
384
 - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方, 其它调用方返回异常信息, 异常信息中resultCode为-1。
M
m00512953 已提交
385

M
m00512953 已提交
386 387
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
388
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
389 390
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

M
m00512953 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |


**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise形式返回执行结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423
| 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. |
| 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. |
M
m00512953 已提交
424

H
huangshiwei 已提交
425 426
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
427 428 429
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
430
let want = {
431 432
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
433
};
Z
zhangyafei.echo 已提交
434
let options = {
435
  windowMode: 0,
436
};
M
m00512953 已提交
437

438
try {
439 440 441 442
  this.context.startAbilityForResult(want, options)
    .then((result) => {
      // 执行正常业务
      console.info('startAbilityForResult succeed');
443
    })
444 445 446
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
447
    });
448 449 450
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
451
}
M
m00512953 已提交
452 453
  ```

Z
zhongjianfei 已提交
454
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
455 456 457

startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void;

458
启动一个Ability并在该Ability销毁时返回执行结果(callback形式)。
M
m00512953 已提交
459

M
m00512953 已提交
460 461
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
462
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
463 464
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

D
donglin 已提交
465
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
466 467 468 469 470 471 472 473 474 475

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
476 477
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
| callback | AsyncCallback&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | 是 | 启动Ability的回调函数,返回Ability结果。 |
M
m00512953 已提交
478 479 480 481 482

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
| 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. |
| 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. |
M
m00512953 已提交
496

H
huangshiwei 已提交
497 498
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
499 500 501
**示例:**

  ```ts
502 503 504 505 506 507
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522
try {
  this.context.startAbilityForResultWithAccount(want, accountId, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
523 524 525
  ```


Z
zhongjianfei 已提交
526
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
527 528 529

startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;

530
启动一个Ability并在该Ability销毁时返回执行结果(callback形式)。
M
m00512953 已提交
531

M
m00512953 已提交
532 533
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
534
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
535 536
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

D
donglin 已提交
537
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
538 539 540 541 542 543 544 545 546 547

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
548
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
549
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
550
| callback | AsyncCallback\<void\> | 是 | 启动Ability后,Ability被销毁时的回调函数。 |
M
m00512953 已提交
551 552 553 554 555

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568
| 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. |
| 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. |
M
m00512953 已提交
569

H
huangshiwei 已提交
570 571
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
572 573 574
**示例:**

  ```ts
575 576 577 578 579 580 581 582 583
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598
try {
  this.context.startAbilityForResultWithAccount(want, accountId, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
599 600 601
  ```


Z
zhongjianfei 已提交
602
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
603 604 605

startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult\>;

606
启动一个Ability并在该Ability销毁时返回执行结果(promise形式)。
M
m00512953 已提交
607

M
m00512953 已提交
608 609
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
610
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
611 612
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

D
donglin 已提交
613
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
614 615 616 617 618 619 620 621 622 623

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
624
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
625 626 627 628 629 630
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
631
| Promise&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Ability被销毁时的回调函数,包含AbilityResult参数。 |
M
m00512953 已提交
632 633 634 635 636

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649
| 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. |
| 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. |
M
m00512953 已提交
650

H
huangshiwei 已提交
651 652
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
653 654 655
**示例:**

  ```ts
656 657 658 659 660 661 662 663 664
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
665

666 667 668 669 670 671 672 673 674 675 676 677 678 679
try {
  this.context.startAbilityForResultWithAccount(want, accountId, options)
    .then((result) => {
      // 执行正常业务
      console.info('startAbilityForResultWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
680
  ```
Z
zhongjianfei 已提交
681
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694

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

启动一个新的ServiceExtensionAbility(callback形式)。

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
695 696
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
697 698 699 700 701

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
702 703 704 705 706 707 708 709
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
710

H
huangshiwei 已提交
711 712
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
713 714 715
**示例:**

  ```ts
716 717 718 719 720
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
721

722 723 724
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
M
m00512953 已提交
725
      // 执行正常业务
726 727 728 729 730
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
731
    });
732 733 734 735
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
736 737
  ```

Z
zhongjianfei 已提交
738
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751

startServiceExtensionAbility(want: Want): Promise\<void>;

启动一个新的ServiceExtensionAbility(Promise形式)。

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
752
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
753 754 755 756 757

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
758 759 760 761 762 763 764 765
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
766

H
huangshiwei 已提交
767 768
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
769 770 771
**示例:**

  ```ts
772 773 774 775 776
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
777

778 779 780 781 782 783 784 785 786 787 788 789 790 791
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
      // 执行正常业务
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (paramError) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
792 793
  ```

Z
zhongjianfei 已提交
794
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
795 796 797 798 799

startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;

启动一个新的ServiceExtensionAbility(callback形式)。

D
donglin 已提交
800
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
801 802 803 804 805 806 807 808 809

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
810
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
811
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
812
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
813 814 815 816 817

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
818 819 820 821 822 823 824 825
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
826

H
huangshiwei 已提交
827 828
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
829 830 831
**示例:**

  ```ts
832 833 834 835 836 837
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
838

839 840 841 842 843 844 845 846 847 848 849 850 851 852
try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
853 854
  ```

Z
zhongjianfei 已提交
855
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
856 857 858 859 860

startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;

启动一个新的ServiceExtensionAbility(Promise形式)。

D
donglin 已提交
861
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
862 863 864 865 866 867 868 869 870 871

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
872
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
873 874 875 876 877

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
878 879 880 881 882 883 884 885
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
886

H
huangshiwei 已提交
887 888
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
889 890 891
**示例:**

  ```ts
892 893 894 895 896 897
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
898

899 900 901 902 903 904 905 906 907 908 909 910 911 912
try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // 执行正常业务
      console.info('startServiceExtensionAbilityWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
913
  ```
Z
zhongjianfei 已提交
914
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927

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

停止同一应用程序内的服务(callback形式)。

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
928 929
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
930 931 932 933 934

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
935 936 937 938 939 940 941
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
942

H
huangshiwei 已提交
943 944
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
945 946 947
**示例:**

  ```ts
948 949 950 951 952
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967
try {
  this.context.stopServiceExtensionAbility(want, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('stopServiceExtensionAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
968 969
  ```

Z
zhongjianfei 已提交
970
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
971 972 973 974 975 976 977 978 979 980 981 982 983

stopServiceExtensionAbility(want: Want): Promise\<void>;

停止同一应用程序内的服务(Promise形式)。

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
984
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
985 986 987 988 989

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
990 991 992 993 994 995 996
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
997

H
huangshiwei 已提交
998 999
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1000 1001 1002
**示例:**

  ```ts
1003 1004 1005 1006 1007
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
try {
  this.context.stopServiceExtensionAbility(want)
    .then(() => {
      // 执行正常业务
      console.info('stopServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1023 1024
  ```

Z
zhongjianfei 已提交
1025
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1026 1027 1028

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;

1029
停止同一应用程序内指定账户的服务(callback形式)。
M
m00512953 已提交
1030

D
donglin 已提交
1031
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1041 1042 1043
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
1044 1045 1046 1047 1048

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1049 1050 1051 1052 1053 1054 1055
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1056

H
huangshiwei 已提交
1057 1058
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1059 1060 1061
**示例:**

  ```ts
1062 1063 1064 1065 1066 1067
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1068

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
try {
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('stopServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1083 1084
  ```

Z
zhongjianfei 已提交
1085
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1086 1087 1088

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;

1089
停止同一应用程序内指定账户的服务(Promise形式)。
M
m00512953 已提交
1090

D
donglin 已提交
1091
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1101 1102
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1103 1104 1105 1106 1107

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1108 1109 1110 1111 1112 1113 1114
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1115

H
huangshiwei 已提交
1116 1117
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1118 1119 1120
**示例:**

  ```ts
1121 1122 1123 1124 1125 1126
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1127

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
try {
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // 执行正常业务
      console.info('stopServiceExtensionAbilityWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1142 1143
  ```

Z
zhongjianfei 已提交
1144
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

terminateSelf(callback: AsyncCallback&lt;void&gt;): void;

停止Ability自身(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1156
| callback | AsyncCallback&lt;void&gt; | 是 | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1157 1158 1159 1160 1161

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1162 1163 1164 1165 1166 1167
| 16000001 | The specified ability does not exist. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1168

H
huangshiwei 已提交
1169 1170
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1171 1172 1173
**示例:**

  ```ts
1174
  try {
1175 1176
    this.context.terminateSelf((err) => {
      if (err.code) {
1177
        // 处理业务逻辑错误
1178
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1179 1180 1181
        return;
      }
      // 执行正常业务
1182
      console.info('terminateSelf succeed');
1183
    });
1184
  } catch (err) {
1185
    // 捕获同步的参数错误
1186
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1187
  }
M
m00512953 已提交
1188 1189 1190
  ```


Z
zhongjianfei 已提交
1191
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

terminateSelf(): Promise&lt;void&gt;;

停止Ability自身(promise形式)。

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
1203
| Promise&lt;void&gt; | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1204 1205 1206 1207 1208

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1209 1210 1211 1212 1213 1214
| 16000001 | The specified ability does not exist. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1215

H
huangshiwei 已提交
1216 1217
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1218 1219 1220
**示例:**

  ```ts
1221 1222 1223 1224
  try {
    this.context.terminateSelf()
      .then(() => {
        // 执行正常业务
1225
        console.info('terminateSelf succeed');
1226
      })
1227
      .catch((err) => {
1228
        // 处理业务逻辑错误
1229
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1230 1231 1232
      });
  } catch (error) {
    // 捕获同步的参数错误
1233
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1234
  }
M
m00512953 已提交
1235 1236 1237
  ```


Z
zhongjianfei 已提交
1238
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1239 1240 1241

terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void;

M
m00512953 已提交
1242
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(callback形式)。
M
m00512953 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给调用startAbilityForResult&nbsp;接口调用方的相关信息。 |
| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回停止结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1257 1258 1259 1260 1261 1262
| 16000001 | The specified ability does not exist. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1263

H
huangshiwei 已提交
1264 1265
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1266 1267 1268
**示例:**

  ```ts
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
let abilityResult = {
  want,
  resultCode
};
M
m00512953 已提交
1279

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
try {
  this.context.terminateSelfWithResult(abilityResult, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('terminateSelfWithResult succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1294 1295 1296
  ```


Z
zhongjianfei 已提交
1297
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1298 1299 1300

terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;;

M
m00512953 已提交
1301
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(promise形式)。
M
m00512953 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给startAbilityForResult&nbsp;调用方的信息。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | promise形式返回停止结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1321 1322 1323 1324 1325 1326
| 16000001 | The specified ability does not exist. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1327

H
huangshiwei 已提交
1328 1329
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1330 1331 1332
**示例:**

  ```ts
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
let abilityResult = {
  want,
  resultCode
};
M
m00512953 已提交
1343

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
try {
  this.context.terminateSelfWithResult(abilityResult)
    .then(() => {
      // 执行正常业务
      console.info('terminateSelfWithResult succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1358 1359
  ```

Z
zhongjianfei 已提交
1360
## UIAbilityContext.connectServiceExtensionAbility
M
m00512953 已提交
1361 1362 1363

connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;

1364
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的Ability。
M
m00512953 已提交
1365 1366 1367 1368 1369 1370 1371

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1372 1373
| want | [Want](js-apis-application-want.md) | 是 | 连接ServiceExtensionAbility的want信息。 |
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 |
M
m00512953 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| number | 返回Ability连接的结果code。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1385 1386 1387 1388
| 16000001 | The specified ability does not exist. |
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1389

H
huangshiwei 已提交
1390 1391
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1392 1393 1394
**示例:**

  ```ts
1395 1396 1397 1398 1399 1400 1401
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let options = {
  onConnect(elementName, remote) {
M
mingxihua 已提交
1402
    commRemote = remote;
1403 1404 1405 1406 1407 1408 1409
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1410
  }
1411 1412 1413 1414 1415 1416 1417 1418 1419
};

let connection = null;
try {
  connection = this.context.connectServiceExtensionAbility(want, options);
} catch (err) {
  // 处理入参错误异常
  console.error(`connectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1420 1421 1422
  ```


Z
zhongjianfei 已提交
1423
## UIAbilityContext.connectServiceExtensionAbilityWithAccount
M
m00512953 已提交
1424 1425 1426

connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;

1427
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的指定account的Ability。
M
m00512953 已提交
1428

D
donglin 已提交
1429
**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1440
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
1441
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。。 |
M
m00512953 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| number | 返回Ability连接的结果code。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1453 1454 1455 1456
| 16000001 | The specified ability does not exist. |
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1457

H
huangshiwei 已提交
1458 1459
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1460 1461 1462
**示例:**

  ```ts
1463 1464 1465 1466 1467 1468 1469 1470
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
let options = {
  onConnect(elementName, remote) {
M
mingxihua 已提交
1471
    commRemote = remote;
1472 1473 1474 1475 1476 1477 1478
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1479
  }
1480 1481 1482 1483 1484 1485 1486 1487 1488
};

let connection = null;
try {
  connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
} catch (err) {
  // 处理入参错误异常
  console.error(`connectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1489 1490
  ```

Z
zhongjianfei 已提交
1491
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1492 1493 1494

disconnectServiceExtensionAbility(connection: number): Promise\<void>;

M
mingxihua 已提交
1495
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(promise形式)。
M
m00512953 已提交
1496 1497 1498 1499 1500 1501 1502

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1503
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
M
m00512953 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise\<void> | 返回执行结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1515 1516
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1517

H
huangshiwei 已提交
1518 1519
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1520 1521 1522
**示例:**

  ```ts
1523 1524
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
M
m00512953 已提交
1525

1526 1527
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
M
mingxihua 已提交
1528
    commRemote = null;
1529 1530 1531 1532 1533 1534 1535 1536 1537
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
M
mingxihua 已提交
1538
  commRemote = null;
1539 1540 1541
  // 处理入参错误异常
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1542 1543
  ```

Z
zhongjianfei 已提交
1544
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1545 1546 1547

disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\<void>): void;

M
mingxihua 已提交
1548
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(callback形式)。
M
m00512953 已提交
1549 1550 1551 1552 1553 1554 1555

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1556
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
1557
| callback | AsyncCallback\<void> | 是 | callback形式返回断开连接的结果。 |
M
m00512953 已提交
1558 1559 1560 1561 1562

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1563 1564
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1565

H
huangshiwei 已提交
1566 1567
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1568 1569 1570
**示例:**

  ```ts
1571 1572
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
M
m00512953 已提交
1573

1574 1575
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
M
mingxihua 已提交
1576
    commRemote = null;
1577 1578 1579 1580 1581 1582 1583 1584 1585
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
M
mingxihua 已提交
1586
  commRemote = null;
1587 1588 1589
  // 处理入参错误异常
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1590 1591
  ```

Z
zhongjianfei 已提交
1592
## UIAbilityContext.startAbilityByCall
M
m00512953 已提交
1593 1594 1595 1596 1597

startAbilityByCall(want: Want): Promise&lt;Caller&gt;;

启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。

M
m00512953 已提交
1598 1599
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1600
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
1601
 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
M
m00512953 已提交
1602

M
m00512953 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;Caller&gt; | 获取要通讯的caller对象。 |

Z
zhangyafei.echo 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
**错误码:**

| 错误码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. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

H
huangshiwei 已提交
1631 1632
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1633 1634 1635 1636 1637
**示例:**

  后台启动:

  ```ts
1638 1639 1640 1641 1642 1643 1644 1645 1646
let caller;

// 后台启动Ability,不配置parameters
let wantBackground = {
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: ''
};
M
m00512953 已提交
1647

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
try {
  this.context.startAbilityByCall(wantBackground)
    .then((obj) => {
      // 执行正常业务
      caller = obj;
      console.info('startAbilityByCall succeed');
    }).catch((err) => {
    // 处理业务逻辑错误
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1662 1663
  ```

1664
前台启动:
M
m00512953 已提交
1665 1666

  ```ts
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
let caller;

// 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
let wantForeground = {
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: '',
  parameters: {
    'ohos.aafwk.param.callAbilityToForeground': true
M
m00512953 已提交
1677
  }
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
};

try {
  this.context.startAbilityByCall(wantForeground)
    .then((obj) => {
      // 执行正常业务
      caller = obj;
      console.info('startAbilityByCall succeed');
    }).catch((error) => {
    // 处理业务逻辑错误
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (paramError) {
  // 处理入参错误异常
  console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1694 1695
  ```

Z
zhongjianfei 已提交
1696
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1697 1698 1699

startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void\>): void;

1700
根据want和accountId启动Ability(callback形式)。
M
m00512953 已提交
1701

M
m00512953 已提交
1702 1703
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1704
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
1705 1706
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

D
donglin 已提交
1707
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1718
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1719 1720 1721 1722 1723 1724
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
| 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. |
| 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. |
M
m00512953 已提交
1738

H
huangshiwei 已提交
1739 1740
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1741 1742 1743
**示例:**

  ```ts
1744 1745 1746 1747 1748 1749
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
1750

1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
try {
  this.context.startAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1765 1766 1767
  ```


Z
zhongjianfei 已提交
1768
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1769 1770 1771

startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;

1772
根据want、accountId及startOptions启动Ability(callback形式)。
M
m00512953 已提交
1773

M
m00512953 已提交
1774 1775
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1776
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
1777 1778
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

D
donglin 已提交
1779
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1790
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。|
1791
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
M
m00512953 已提交
1792 1793 1794 1795 1796 1797
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
| 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. |
| 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. |
M
m00512953 已提交
1811

H
huangshiwei 已提交
1812 1813
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1814 1815 1816
**示例:**

  ```ts
1817 1818 1819 1820 1821 1822 1823 1824 1825
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
1826

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
try {
  this.context.startAbilityWithAccount(want, accountId, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1841 1842 1843
  ```


Z
zhongjianfei 已提交
1844
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1845 1846 1847

startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void\>;

1848
根据want、accountId和startOptions启动Ability(Promise形式)。
M
m00512953 已提交
1849

M
m00512953 已提交
1850 1851
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1852
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
1853 1854
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

D
donglin 已提交
1855
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1866
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1867 1868 1869 1870 1871 1872
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
| 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. |
| 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. |
M
m00512953 已提交
1886

H
huangshiwei 已提交
1887 1888
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1889 1890 1891
**示例:**

  ```ts
1892 1893 1894 1895 1896 1897 1898 1899 1900
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
1901

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
try {
  this.context.startAbilityWithAccount(want, accountId, options)
    .then(() => {
      // 执行正常业务
      console.info('startAbilityWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1916 1917
  ```

Z
zhongjianfei 已提交
1918
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
1919 1920 1921

setMissionLabel(label: string, callback:AsyncCallback&lt;void&gt;): void;

1922
设置UIAbility在任务中显示的名称(callback形式)。
M
m00512953 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| label | string | 是 | 显示名称。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |

H
huangshiwei 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

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

M
m00512953 已提交
1942 1943 1944
**示例:**

  ```ts
M
mingxihua 已提交
1945
  this.context.setMissionLabel('test', (result) => {
1946
    console.info(`setMissionLabel: ${JSON.stringify(result)}`);
M
m00512953 已提交
1947 1948 1949
  });
  ```

Z
zhongjianfei 已提交
1950
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
1951 1952 1953

setMissionLabel(label: string): Promise&lt;void&gt;;

1954
设置UIAbility在任务中显示的名称(promise形式)。
M
m00512953 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| label | string | 是 | 显示名称。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |

Z
zhangyafei.echo 已提交
1970 1971 1972 1973 1974 1975 1976
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

H
huangshiwei 已提交
1977 1978
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
1979 1980 1981
**示例:**

  ```ts
M
mingxihua 已提交
1982
  this.context.setMissionLabel('test').then(() => {
1983 1984 1985
    console.info('success');
  }).catch((err) => {
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
1986 1987
  });
  ```
Z
zhongjianfei 已提交
1988
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
1989 1990 1991

setMissionIcon(icon: image.PixelMap, callback:AsyncCallback\<void>): void;

M
mingxihua 已提交
1992
设置当前ability在任务中显示的图标, 图标大小最大为600M(callback形式)。
M
m00512953 已提交
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 |
| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |

Z
zhangyafei.echo 已提交
2005 2006 2007 2008 2009 2010 2011
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

H
huangshiwei 已提交
2012 2013
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
2014 2015 2016
**示例:**

  ```ts
2017
  import image from '@ohos.multimedia.image';
2018
  
Z
zhangyafei.echo 已提交
2019 2020 2021
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
2022 2023 2024 2025 2026 2027 2028 2029
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
M
m00512953 已提交
2030
    })
2031
    .catch((err) => {
2032
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
2033 2034
    });
  this.context.setMissionIcon(imagePixelMap, (err) => {
2035
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
2036
  })
M
m00512953 已提交
2037 2038 2039
  ```


Z
zhongjianfei 已提交
2040
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2041 2042 2043

setMissionIcon(icon: image.PixelMap): Promise\<void>;

M
mingxihua 已提交
2044
设置当前ability在任务中显示的图标, 图标大小最大为600M(promise形式)。
M
m00512953 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

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

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |

Z
zhangyafei.echo 已提交
2062 2063 2064 2065 2066 2067 2068
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

H
huangshiwei 已提交
2069 2070
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
2071 2072 2073
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2074 2075 2076
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
    })
    .catch((err) => {
2087
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
2088 2089 2090
    });
  this.context.setMissionIcon(imagePixelMap)
    .then(() => {
2091
      console.info('setMissionIcon succeed');
2092 2093
    })
    .catch((err) => {
2094
      console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
2095
    });
M
m00512953 已提交
2096
  ```
Z
zhongjianfei 已提交
2097
## UIAbilityContext.restoreWindowStage
M
m00512953 已提交
2098 2099 2100

restoreWindowStage(localStorage: LocalStorage) : void;

2101
恢复UIAbility中的WindowStage数据。
M
m00512953 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| localStorage | image.LocalStorage | 是 | 用于恢复window stage的存储数据。 |

Z
zhangyafei.echo 已提交
2111 2112 2113 2114 2115 2116 2117
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

H
huangshiwei 已提交
2118 2119
错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

M
m00512953 已提交
2120 2121 2122
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2123
  let storage = new LocalStorage();
2124
  this.context.restoreWindowStage(storage);
M
m00512953 已提交
2125 2126
  ```

Z
zhongjianfei 已提交
2127
## UIAbilityContext.isTerminating
M
m00512953 已提交
2128 2129 2130

isTerminating(): boolean;

2131
查询UIAbility是否在terminating状态。
M
m00512953 已提交
2132 2133 2134 2135 2136 2137 2138 2139 2140

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| bool | true:ability当前处于terminating状态;false:不处于terminating状态。 |

Z
zhangyafei.echo 已提交
2141 2142 2143 2144 2145
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
H
huangshiwei 已提交
2146 2147

错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
Z
zhangyafei.echo 已提交
2148

M
m00512953 已提交
2149 2150 2151
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2152
  let isTerminating = this.context.isTerminating();
2153
  console.info(`ability state is ${isTerminating}`);
2154
  ```
C
caochunlei 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163

## UIAbilityContext.requestDialogService

requestDialogService(want: Want, result: AsyncCallback&lt;dialogRequest.RequestResult&gt;): void;

启动一个支持模态弹框的ServiceExtensionAbility。ServiceExtensionAbility被启动后,应用弹出模态弹框,通过调用[setRequestResult](js-apis-app-ability-dialogRequest.md#requestcallbacksetrequestresult)接口返回结果给调用者。

使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限。
2164
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
C
caochunlei 已提交
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
| result | AsyncCallback&lt;[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md)&gt; | 是 | 执行结果回调函数。 |

H
huangshiwei 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
**错误码:**

| 错误码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. |
| 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)

C
caochunlei 已提交
2196 2197 2198
**示例:**

  ```ts
2199
import dialogRequest from '@ohos.app.ability.dialogRequest';
C
caochunlei 已提交
2200

2201 2202 2203 2204 2205
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
};
C
caochunlei 已提交
2206

2207 2208 2209 2210 2211 2212 2213 2214
try {
  this.context.requestDialogService(want, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
M
mingxihua 已提交
2215
    console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
2216 2217 2218 2219 2220
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
}
C
caochunlei 已提交
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
  ```

  ## UIAbilityContext.requestDialogService

requestDialogService(want: Want): Promise&lt;dialogRequest.RequestResult&gt;;

启动一个支持模态弹框的ServiceExtensionAbility。ServiceExtensionAbility被启动后,应用弹出模态弹框,通过调用[setRequestResult](js-apis-app-ability-dialogRequest.md#requestcallbacksetrequestresult)接口返回结果给调用者(promise形式)。

使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限。
2231
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
C
caochunlei 已提交
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |


**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md)&gt; | Promise形式返回执行结果。

H
huangshiwei 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
**错误码:**

| 错误码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. |
| 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)

C
caochunlei 已提交
2269 2270 2271 2272 2273 2274
**示例:**

  ```ts
import dialogRequest from '@ohos.app.ability.dialogRequest';

let want = {
2275 2276
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
C
caochunlei 已提交
2277 2278 2279
};

try {
2280 2281 2282
  this.context.requestDialogService(want)
    .then((result) => {
      // 执行正常业务
M
mingxihua 已提交
2283
      console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
C
caochunlei 已提交
2284
    })
2285 2286 2287
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2288
    });
2289 2290 2291
} catch (err) {
  // 处理入参错误异常
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2292 2293
}
  ```