js-apis-inner-application-uiAbilityContext.md 118.9 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
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
W
VOD  
wangkailong 已提交
40
 - 跨任务链启动时,如果需要跨任务链进行返回,需要参考[Want](js-apis-app-ability-want.md)中的parameter参数用法。
M
m00512953 已提交
41

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
55 56 57 58 59 60 61
| 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. |
Y
yuyaozhi 已提交
62 63 64 65
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
66 67 68 69
| 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 已提交
70

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

M
m00512953 已提交
73 74 75
**示例:**

  ```ts
L
liuliu 已提交
76 77 78 79
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
80 81 82
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
83

84
try {
L
liuliu 已提交
85
  this.context.startAbility(want, (err: BusinessError) => {
86 87 88 89 90 91 92 93 94 95
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
96 97 98
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbility failed, code is ${code}, message is ${message}`);
99
}
M
m00512953 已提交
100 101
  ```

Z
zhongjianfei 已提交
102
## UIAbilityContext.startAbility
M
m00512953 已提交
103 104 105 106 107

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

启动Ability(callback形式)。

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

M
m00512953 已提交
113 114 115 116 117 118 119 120 121 122 123 124
**系统能力**: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 已提交
125
| 错误码ID | 错误信息 |
M
m00512953 已提交
126
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
127 128 129 130 131 132 133
| 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. |
Y
yuyaozhi 已提交
134 135 136 137
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
138 139 140 141
| 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 已提交
142

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

M
m00512953 已提交
145 146 147
**示例:**

  ```ts
L
liuliu 已提交
148 149 150 151 152
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
153 154 155 156
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
157
let options: StartOptions = {
158 159
  windowMode: 0
};
M
m00512953 已提交
160

161
try {
L
liuliu 已提交
162
  this.context.startAbility(want, options, (err: BusinessError) => {
163 164 165 166 167 168 169 170 171 172
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
173 174 175
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbility failed, code is ${code}, message is ${message}`);
176
}
M
m00512953 已提交
177 178
  ```

Z
zhongjianfei 已提交
179
## UIAbilityContext.startAbility
M
m00512953 已提交
180 181 182 183 184

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

启动Ability(promise形式)。

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

M
m00512953 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
**系统能力**: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 已提交
209 210 211 212 213 214 215
| 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. |
Y
yuyaozhi 已提交
216 217 218 219
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
220 221 222 223
| 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 已提交
224

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

M
m00512953 已提交
227 228 229
**示例:**

  ```ts
L
liuliu 已提交
230 231 232 233 234
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
235 236 237
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
238
let options: StartOptions = {
239 240
  windowMode: 0,
};
M
m00512953 已提交
241

242 243 244 245 246 247
try {
  this.context.startAbility(want, options)
    .then(() => {
      // 执行正常业务
      console.info('startAbility succeed');
    })
L
liuliu 已提交
248
    .catch((err: BusinessError) => {
249 250 251 252 253
      // 处理业务逻辑错误
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
254 255 256
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbility failed, code is ${code}, message is ${message}`);
257
}
M
m00512953 已提交
258 259
  ```

Z
zhongjianfei 已提交
260
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
261 262 263

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

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

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

M
m00512953 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
**系统能力**: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 已提交
287 288 289 290 291 292 293 294 295
| 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 已提交
296 297
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
298 299 300 301
| 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 已提交
302

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

M
m00512953 已提交
305 306 307
**示例:**

  ```ts
L
liuliu 已提交
308 309 310 311 312 313 314
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
315 316 317 318
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
319

320
try {
L
liuliu 已提交
321
  this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => {
322 323 324 325 326 327 328 329 330
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
331 332 333
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
334
}
M
m00512953 已提交
335 336
  ```

Z
zhongjianfei 已提交
337
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
338 339 340

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

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

M
m00512953 已提交
346 347
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
348
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
349
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
M
m00512953 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

**系统能力**: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 已提交
365 366 367 368 369 370 371 372 373
| 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 已提交
374 375
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
376 377 378 379
| 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 已提交
380

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

M
m00512953 已提交
383 384 385
**示例:**

  ```ts
L
liuliu 已提交
386 387 388 389 390 391 392 393
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
394 395 396 397
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
398
let options: StartOptions = {
399 400
  windowMode: 0,
};
M
m00512953 已提交
401

402
try {
L
liuliu 已提交
403
  this.context.startAbilityForResult(want, options, (err: BusinessError, result: common.AbilityResult) => {
404 405
    if (err.code) {
      // 处理业务逻辑错误
L
liuliu 已提交
406
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);  
407 408 409 410 411
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
412
} catch (err) {
413
  // 处理入参错误异常
L
liuliu 已提交
414 415 416
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
417
}
M
m00512953 已提交
418 419 420
  ```


Z
zhongjianfei 已提交
421
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
422 423 424

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

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

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

M
m00512953 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
**系统能力**: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 已提交
455 456 457 458 459 460 461 462 463
| 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 已提交
464 465
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
466 467 468 469
| 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 已提交
470

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

M
m00512953 已提交
473 474 475
**示例:**

  ```ts
L
liuliu 已提交
476 477 478 479 480 481 482 483
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
484 485
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
486
};
L
liuliu 已提交
487
let options: StartOptions = {
488
  windowMode: 0,
489
};
M
m00512953 已提交
490

491
try {
492
  this.context.startAbilityForResult(want, options)
L
liuliu 已提交
493
    .then((result: common.AbilityResult) => {
494 495
      // 执行正常业务
      console.info('startAbilityForResult succeed');
496
    })
L
liuliu 已提交
497
    .catch((err: BusinessError) => {
498 499
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
500
    });
501 502
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
503 504 505
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
506
}
M
m00512953 已提交
507 508
  ```

Z
zhongjianfei 已提交
509
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
510 511 512

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

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

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

Y
yuyaozhi 已提交
520 521 522 523 524
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
525 526 527 528 529 530 531 532 533 534

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
542 543 544 545 546 547 548 549 550
| 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 已提交
551 552
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
553 554 555 556
| 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 已提交
557

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

M
m00512953 已提交
560 561 562
**示例:**

  ```ts
L
liuliu 已提交
563 564 565 566 567 568 569
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
570 571 572 573 574
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
575

576
try {
L
liuliu 已提交
577
  this.context.startAbilityForResultWithAccount(want, accountId, (err: BusinessError, result: common.AbilityResult) => {
578 579 580 581 582 583 584 585 586 587
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
588 589 590
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`);
591
}
M
m00512953 已提交
592 593 594
  ```


Z
zhongjianfei 已提交
595
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
596 597 598

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

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

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

Y
yuyaozhi 已提交
606 607 608 609 610
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
611 612 613 614 615 616 617 618 619 620

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
629 630 631 632 633 634 635 636 637
| 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 已提交
638 639
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
640 641 642 643
| 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 已提交
644

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

M
m00512953 已提交
647 648 649
**示例:**

  ```ts
L
liuliu 已提交
650 651 652 653 654
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
655 656 657 658 659
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
L
liuliu 已提交
660
let options: StartOptions = {
661 662
  windowMode: 0
};
M
m00512953 已提交
663

664
try {
L
liuliu 已提交
665
  this.context.startAbilityForResultWithAccount(want, accountId, options, (err: BusinessError) => {
666 667 668 669 670 671 672 673 674 675
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
676 677 678
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`);
679
}
M
m00512953 已提交
680 681 682
  ```


Z
zhongjianfei 已提交
683
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
684 685 686

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

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

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

Y
yuyaozhi 已提交
694 695 696 697 698
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
699 700 701 702 703 704 705 706 707 708

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

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

**参数:**

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
716
| Promise&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Ability被销毁时的回调函数,包含AbilityResult参数。 |
M
m00512953 已提交
717 718 719 720 721

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
722 723 724 725 726 727 728 729 730
| 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 已提交
731 732
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
733 734 735 736
| 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 已提交
737

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

M
m00512953 已提交
740 741 742
**示例:**

  ```ts
L
liuliu 已提交
743 744 745 746 747 748 749 750
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
751 752 753 754 755
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
L
liuliu 已提交
756
let options: StartOptions = {
757 758
  windowMode: 0
};
M
m00512953 已提交
759

760 761
try {
  this.context.startAbilityForResultWithAccount(want, accountId, options)
L
liuliu 已提交
762
    .then((result: common.AbilityResult) => {
763 764 765
      // 执行正常业务
      console.info('startAbilityForResultWithAccount succeed');
    })
L
liuliu 已提交
766
    .catch((err: BusinessError) => {
767 768 769 770 771
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
772 773 774
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`);
775
}
M
m00512953 已提交
776
  ```
Z
zhongjianfei 已提交
777
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
791 792
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
793 794 795 796 797

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
798 799
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
800
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
801 802 803 804
| 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. |
Y
yuyaozhi 已提交
805 806
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
807 808
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
809

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

M
m00512953 已提交
812 813 814
**示例:**

  ```ts
L
liuliu 已提交
815 816 817 818
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
819 820 821 822
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
823

824 825 826
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
M
m00512953 已提交
827
      // 执行正常业务
828 829
      console.info('startServiceExtensionAbility succeed');
    })
L
liuliu 已提交
830
    .catch((err: BusinessError) => {
831 832
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
833
    });
834 835
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
836 837 838
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`);
839
}
M
m00512953 已提交
840 841
  ```

Z
zhongjianfei 已提交
842
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
856
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
857 858 859 860 861

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
862 863
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
864
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
865 866 867 868
| 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. |
Y
yuyaozhi 已提交
869 870
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
871 872
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
873

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

M
m00512953 已提交
876 877 878
**示例:**

  ```ts
L
liuliu 已提交
879 880 881 882
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
883 884 885 886
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
887

888 889 890 891 892 893
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
      // 执行正常业务
      console.info('startServiceExtensionAbility succeed');
    })
L
liuliu 已提交
894
    .catch((err: BusinessError) => {
895 896 897
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
898
} catch (err) {
899
  // 处理入参错误异常
L
liuliu 已提交
900 901 902
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`);
903
}
M
m00512953 已提交
904 905
  ```

Z
zhongjianfei 已提交
906
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
907 908 909 910 911

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

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

Y
yuyaozhi 已提交
912 913 914 915 916
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
917 918 919 920 921 922 923 924 925

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
926
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
927
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
928
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
929 930 931 932 933

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
934 935
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
936
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
937 938 939 940
| 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. |
Y
yuyaozhi 已提交
941 942
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
943 944
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
945

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

M
m00512953 已提交
948 949 950
**示例:**

  ```ts
L
liuliu 已提交
951 952 953 954
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
955 956 957 958 959
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
960

961
try {
L
liuliu 已提交
962
  this.context.startServiceExtensionAbilityWithAccount(want, accountId, (err: BusinessError) => {
963 964 965 966 967 968 969 970 971 972
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
973 974 975
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`);
976
}
M
m00512953 已提交
977 978
  ```

Z
zhongjianfei 已提交
979
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
980 981 982 983 984

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

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

Y
yuyaozhi 已提交
985 986 987 988 989
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
990 991 992 993 994 995 996 997 998 999

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1006 1007
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1008
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1009 1010 1011 1012
| 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. |
Y
yuyaozhi 已提交
1013 1014
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1015 1016
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1017

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

M
m00512953 已提交
1020 1021 1022
**示例:**

  ```ts
L
liuliu 已提交
1023 1024 1025 1026
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1027 1028 1029 1030 1031
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1032

1033 1034 1035 1036 1037 1038
try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // 执行正常业务
      console.info('startServiceExtensionAbilityWithAccount succeed');
    })
L
liuliu 已提交
1039
    .catch((err: BusinessError) => {
1040 1041 1042 1043 1044
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1045 1046 1047
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`);
1048
}
M
m00512953 已提交
1049
  ```
Z
zhongjianfei 已提交
1050
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1064 1065
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
1066 1067 1068 1069 1070

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1071 1072
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1073
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1074 1075 1076
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
1077 1078
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1079 1080
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1081

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

M
m00512953 已提交
1084 1085 1086
**示例:**

  ```ts
L
liuliu 已提交
1087 1088 1089 1090
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1091 1092 1093 1094
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
1095

1096
try {
L
liuliu 已提交
1097
  this.context.stopServiceExtensionAbility(want, (err: BusinessError) => {
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('stopServiceExtensionAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1108 1109 1110
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`stopServiceExtensionAbility failed, code is ${code}, message is ${message}`);
1111
}
M
m00512953 已提交
1112 1113
  ```

Z
zhongjianfei 已提交
1114
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1128
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
1129 1130 1131 1132 1133

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1134 1135
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1136
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1137 1138 1139 1140 1141
| 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 已提交
1142

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

M
m00512953 已提交
1145 1146 1147
**示例:**

  ```ts
L
liuliu 已提交
1148 1149 1150 1151
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1152 1153 1154 1155
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
1156

1157 1158 1159 1160 1161 1162
try {
  this.context.stopServiceExtensionAbility(want)
    .then(() => {
      // 执行正常业务
      console.info('stopServiceExtensionAbility succeed');
    })
L
liuliu 已提交
1163
    .catch((err: BusinessError) => {
1164 1165 1166 1167 1168
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1169 1170 1171
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`stopServiceExtensionAbility failed, code is ${code}, message is ${message}`);
1172
}
M
m00512953 已提交
1173 1174
  ```

Z
zhongjianfei 已提交
1175
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1176 1177 1178

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

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

Y
yuyaozhi 已提交
1181 1182 1183 1184 1185
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1195 1196 1197
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
1198 1199 1200 1201 1202

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1203 1204
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1205
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1206 1207 1208 1209 1210
| 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 已提交
1211

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

M
m00512953 已提交
1214 1215 1216
**示例:**

  ```ts
L
liuliu 已提交
1217 1218 1219 1220
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1221 1222 1223 1224 1225
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1226

1227
try {
L
liuliu 已提交
1228
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (err: BusinessError) => {
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('stopServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1239 1240 1241
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`);
1242
}
M
m00512953 已提交
1243 1244
  ```

Z
zhongjianfei 已提交
1245
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1246 1247 1248

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

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

Y
yuyaozhi 已提交
1251 1252 1253 1254 1255
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1265 1266
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1267 1268 1269 1270 1271

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1272 1273
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1274
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1275 1276 1277 1278 1279
| 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 已提交
1280

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

M
m00512953 已提交
1283 1284 1285
**示例:**

  ```ts
L
liuliu 已提交
1286 1287 1288 1289
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1290 1291 1292 1293 1294
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1295

1296 1297 1298 1299 1300 1301
try {
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // 执行正常业务
      console.info('stopServiceExtensionAbilityWithAccount succeed');
    })
L
liuliu 已提交
1302
    .catch((err: BusinessError) => {
1303 1304 1305 1306 1307
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1308 1309 1310
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`);
1311
}
M
m00512953 已提交
1312 1313
  ```

Z
zhongjianfei 已提交
1314
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325

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

停止Ability自身(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1326
| callback | AsyncCallback&lt;void&gt; | 是 | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1327 1328 1329 1330 1331

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1332 1333 1334 1335 1336 1337
| 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 已提交
1338

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

M
m00512953 已提交
1341 1342 1343
**示例:**

  ```ts
L
liuliu 已提交
1344 1345
  import { BusinessError } from '@ohos.base';

1346
  try {
L
liuliu 已提交
1347
    this.context.terminateSelf((err: BusinessError) => {
1348
      if (err.code) {
1349
        // 处理业务逻辑错误
1350
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1351 1352 1353
        return;
      }
      // 执行正常业务
1354
      console.info('terminateSelf succeed');
1355
    });
1356
  } catch (err) {
1357
    // 捕获同步的参数错误
L
liuliu 已提交
1358 1359 1360
    let code = (err as BusinessError).code;
    let message = (err as BusinessError).message;
    console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
1361
  }
M
m00512953 已提交
1362 1363 1364
  ```


Z
zhongjianfei 已提交
1365
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

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

停止Ability自身(promise形式)。

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
1377
| Promise&lt;void&gt; | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1378 1379 1380 1381 1382

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1383 1384 1385 1386 1387 1388
| 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 已提交
1389

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

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

  ```ts
L
liuliu 已提交
1395 1396
  import { BusinessError } from '@ohos.base';

1397 1398 1399 1400
  try {
    this.context.terminateSelf()
      .then(() => {
        // 执行正常业务
1401
        console.info('terminateSelf succeed');
1402
      })
L
liuliu 已提交
1403
      .catch((err: BusinessError) => {
1404
        // 处理业务逻辑错误
1405
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1406
      });
Y
yuyaozhi 已提交
1407
  } catch (err) {
1408
    // 捕获同步的参数错误
L
liuliu 已提交
1409 1410 1411
    let code = (err as BusinessError).code;
    let message = (err as BusinessError).message;
    console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
1412
  }
M
m00512953 已提交
1413 1414 1415
  ```


Z
zhongjianfei 已提交
1416
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1417 1418 1419

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

M
m00512953 已提交
1420
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(callback形式)。
M
m00512953 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1435 1436 1437 1438 1439 1440
| 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 已提交
1441

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

M
m00512953 已提交
1444 1445 1446
**示例:**

  ```ts
L
liuliu 已提交
1447 1448 1449 1450 1451 1452
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common'; 
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1453 1454 1455 1456 1457
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
L
liuliu 已提交
1458
let abilityResult: common.AbilityResult = {
1459 1460 1461
  want,
  resultCode
};
M
m00512953 已提交
1462

1463
try {
L
liuliu 已提交
1464
  this.context.terminateSelfWithResult(abilityResult, (err: BusinessError) => {
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('terminateSelfWithResult succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1475 1476 1477
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`);
1478
}
M
m00512953 已提交
1479 1480 1481
  ```


Z
zhongjianfei 已提交
1482
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1483 1484 1485

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

M
m00512953 已提交
1486
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(promise形式)。
M
m00512953 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

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

**参数:**

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

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1506 1507 1508 1509 1510 1511
| 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 已提交
1512

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

M
m00512953 已提交
1515 1516 1517
**示例:**

  ```ts
L
liuliu 已提交
1518 1519 1520 1521 1522 1523
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
1524 1525 1526 1527 1528
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
L
liuliu 已提交
1529
let abilityResult: common.AbilityResult = {
1530 1531 1532
  want,
  resultCode
};
M
m00512953 已提交
1533

1534 1535 1536 1537 1538 1539
try {
  this.context.terminateSelfWithResult(abilityResult)
    .then(() => {
      // 执行正常业务
      console.info('terminateSelfWithResult succeed');
    })
L
liuliu 已提交
1540
    .catch((err: BusinessError) => {
1541 1542 1543 1544 1545
      // 处理业务逻辑错误
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1546 1547 1548
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`);
1549
}
M
m00512953 已提交
1550 1551
  ```

Z
zhongjianfei 已提交
1552
## UIAbilityContext.connectServiceExtensionAbility
M
m00512953 已提交
1553 1554 1555

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

1556
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的Ability。
M
m00512953 已提交
1557 1558 1559 1560 1561 1562 1563

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1564 1565
| want | [Want](js-apis-application-want.md) | 是 | 连接ServiceExtensionAbility的want信息。 |
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 |
M
m00512953 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1577
| 16000001 | The specified ability does not exist. |
Y
yuyaozhi 已提交
1578 1579
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
Y
yuyaozhi 已提交
1580
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1581 1582 1583 1584 1585
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16000011 | The context does not exist.        |
Z
zhangyafei.echo 已提交
1586
| 16000050 | Internal error. |
M
m00512953 已提交
1587

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

M
m00512953 已提交
1590 1591 1592
**示例:**

  ```ts
L
liuliu 已提交
1593 1594 1595 1596 1597 1598 1599
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import rpc from '@ohos.rpc';

let want: Want = {
1600 1601 1602 1603
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
L
liuliu 已提交
1604 1605
let commRemote: rpc.IRemoteObject;
let options: common.ConnectOptions = {
1606
  onConnect(elementName, remote) {
M
mingxihua 已提交
1607
    commRemote = remote;
1608 1609 1610 1611 1612 1613 1614
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1615
  }
1616
};
L
liuliu 已提交
1617
let connection: number;
1618 1619 1620 1621
try {
  connection = this.context.connectServiceExtensionAbility(want, options);
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1622 1623 1624
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
1625
}
M
m00512953 已提交
1626 1627 1628
  ```


Z
zhongjianfei 已提交
1629
## UIAbilityContext.connectServiceExtensionAbilityWithAccount
M
m00512953 已提交
1630 1631 1632

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

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

Y
yuyaozhi 已提交
1635 1636 1637 1638 1639
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1650
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
1651
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。。 |
M
m00512953 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1663
| 16000001 | The specified ability does not exist. |
Y
yuyaozhi 已提交
1664 1665
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
1
Bugfix  
18870373690 已提交
1666
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1667 1668 1669 1670 1671
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16000011 | The context does not exist.        |
Z
zhangyafei.echo 已提交
1672
| 16000050 | Internal error. |
M
m00512953 已提交
1673

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

M
m00512953 已提交
1676 1677 1678
**示例:**

  ```ts
L
liuliu 已提交
1679 1680 1681 1682 1683 1684 1685
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import rpc from '@ohos.rpc';

let want: Want = {
1686 1687 1688 1689 1690
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
L
liuliu 已提交
1691 1692
let commRemote: rpc.IRemoteObject;
let options: common.ConnectOptions = {
1693
  onConnect(elementName, remote) {
M
mingxihua 已提交
1694
    commRemote = remote;
1695 1696 1697 1698 1699 1700 1701
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1702
  }
1703
};
L
liuliu 已提交
1704
let connection: number;
1705 1706 1707 1708
try {
  connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1709 1710 1711
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
1712
}
M
m00512953 已提交
1713 1714
  ```

Z
zhongjianfei 已提交
1715
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1716 1717 1718

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

M
mingxihua 已提交
1719
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(promise形式)。
M
m00512953 已提交
1720 1721 1722 1723 1724 1725 1726

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1727
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
M
m00512953 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

**返回值:**

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

**错误码:**

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

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

M
m00512953 已提交
1744 1745 1746
**示例:**

  ```ts
L
liuliu 已提交
1747 1748
import { BusinessError } from '@ohos.base';

1749 1750
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
L
liuliu 已提交
1751
let commRemote: rpc.IRemoteObject | null;
M
m00512953 已提交
1752

1753
try {
L
liuliu 已提交
1754
  this.context.disconnectServiceExtensionAbility(connection).then(() => {
M
mingxihua 已提交
1755
    commRemote = null;
1756 1757
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
L
liuliu 已提交
1758 1759 1760 1761
  }).catch((err: BusinessError) => {
    // 处理业务逻辑错误
    console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
  })
1762
} catch (err) {
M
mingxihua 已提交
1763
  commRemote = null;
1764
  // 处理入参错误异常
L
liuliu 已提交
1765 1766 1767
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
1768
}
M
m00512953 已提交
1769 1770
  ```

Z
zhongjianfei 已提交
1771
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1772 1773 1774

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

M
mingxihua 已提交
1775
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(callback形式)。
M
m00512953 已提交
1776 1777 1778 1779 1780 1781 1782

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1783
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
1784
| callback | AsyncCallback\<void> | 是 | callback形式返回断开连接的结果。 |
M
m00512953 已提交
1785 1786 1787 1788 1789

**错误码:**

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

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

M
m00512953 已提交
1795 1796 1797
**示例:**

  ```ts
L
liuliu 已提交
1798 1799
import { BusinessError } from '@ohos.base';

1800 1801
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
L
liuliu 已提交
1802
let commRemote: rpc.IRemoteObject | null;
M
m00512953 已提交
1803

1804
try {
L
liuliu 已提交
1805
  this.context.disconnectServiceExtensionAbility(connection, (err: BusinessError) => {
M
mingxihua 已提交
1806
    commRemote = null;
1807 1808 1809 1810 1811 1812 1813 1814 1815
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
M
mingxihua 已提交
1816
  commRemote = null;
1817
  // 处理入参错误异常
L
liuliu 已提交
1818 1819 1820
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
1821
}
M
m00512953 已提交
1822 1823
  ```

Z
zhongjianfei 已提交
1824
## UIAbilityContext.startAbilityByCall
M
m00512953 已提交
1825 1826 1827 1828 1829

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

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

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

Y
yuyaozhi 已提交
1835 1836
**需要权限:** ohos.permission.ABILITY_BACKGROUND_COMMUNICATION

M
m00512953 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
**系统能力**: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 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
**错误码:**

| 错误码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. |
Y
yuyaozhi 已提交
1862 1863
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1864 1865 1866
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

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

M
m00512953 已提交
1869 1870 1871 1872 1873
**示例:**

  后台启动:

  ```ts
L
liuliu 已提交
1874 1875 1876 1877 1878
import { Caller } from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let caller: Caller;
1879 1880

// 后台启动Ability,不配置parameters
L
liuliu 已提交
1881
let wantBackground: Want = {
1882 1883 1884 1885 1886
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: ''
};
M
m00512953 已提交
1887

1888 1889
try {
  this.context.startAbilityByCall(wantBackground)
L
liuliu 已提交
1890
    .then((obj: Caller) => {
1891 1892 1893
      // 执行正常业务
      caller = obj;
      console.info('startAbilityByCall succeed');
L
liuliu 已提交
1894
    }).catch((err: BusinessError) => {
1895 1896 1897 1898 1899
    // 处理业务逻辑错误
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1900 1901 1902
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityByCall failed, code is ${code}, message is ${message}`);
1903
}
M
m00512953 已提交
1904 1905
  ```

1906
前台启动:
M
m00512953 已提交
1907 1908

  ```ts
L
liuliu 已提交
1909 1910 1911 1912 1913
import { Caller } from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let caller: Caller;
1914 1915

// 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
L
liuliu 已提交
1916
let wantForeground: Want = {
1917 1918 1919 1920 1921 1922
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: '',
  parameters: {
    'ohos.aafwk.param.callAbilityToForeground': true
M
m00512953 已提交
1923
  }
1924 1925 1926 1927
};

try {
  this.context.startAbilityByCall(wantForeground)
L
liuliu 已提交
1928
    .then((obj: Caller) => {
1929 1930 1931
      // 执行正常业务
      caller = obj;
      console.info('startAbilityByCall succeed');
L
liuliu 已提交
1932
    }).catch((err: BusinessError) => {
1933 1934 1935
    // 处理业务逻辑错误
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
1936
} catch (err) {
1937
  // 处理入参错误异常
L
liuliu 已提交
1938 1939 1940
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityByCall failed, code is ${code}, message is ${message}`);
1941
}
M
m00512953 已提交
1942 1943
  ```

Z
zhongjianfei 已提交
1944
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1945 1946 1947

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

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

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

Y
yuyaozhi 已提交
1955 1956 1957 1958 1959
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985
| 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 已提交
1986 1987
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1988 1989 1990 1991
| 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 已提交
1992

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

M
m00512953 已提交
1995 1996 1997
**示例:**

  ```ts
L
liuliu 已提交
1998 1999 2000 2001
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
2002 2003 2004 2005 2006
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
2007

2008
try {
L
liuliu 已提交
2009
  this.context.startAbilityWithAccount(want, accountId, (err: BusinessError) => {
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2020 2021 2022
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`);
2023
}
M
m00512953 已提交
2024 2025 2026
  ```


Z
zhongjianfei 已提交
2027
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
2028 2029 2030

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

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

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

Y
yuyaozhi 已提交
2038 2039 2040 2041 2042
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069
| 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 已提交
2070 2071
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
2072 2073 2074 2075
| 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 已提交
2076

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

M
m00512953 已提交
2079 2080 2081
**示例:**

  ```ts
L
liuliu 已提交
2082 2083 2084 2085 2086
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
2087 2088 2089 2090 2091
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
L
liuliu 已提交
2092
let options: StartOptions = {
2093 2094
  windowMode: 0
};
M
m00512953 已提交
2095

2096
try {
L
liuliu 已提交
2097
  this.context.startAbilityWithAccount(want, accountId, options, (err: BusinessError) => {
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2108 2109 2110
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`);
2111
}
M
m00512953 已提交
2112 2113 2114
  ```


Z
zhongjianfei 已提交
2115
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
2116 2117 2118

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

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

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

Y
yuyaozhi 已提交
2126 2127 2128 2129 2130
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
2148 2149 2150 2151 2152 2153 2154 2155 2156
| 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 已提交
2157 2158
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
2159 2160 2161 2162
| 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 已提交
2163

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

M
m00512953 已提交
2166 2167 2168
**示例:**

  ```ts
L
liuliu 已提交
2169 2170 2171 2172 2173
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
2174 2175 2176 2177 2178
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
L
liuliu 已提交
2179
let options: StartOptions = {
2180 2181
  windowMode: 0
};
M
m00512953 已提交
2182

2183 2184 2185 2186 2187 2188
try {
  this.context.startAbilityWithAccount(want, accountId, options)
    .then(() => {
      // 执行正常业务
      console.info('startAbilityWithAccount succeed');
    })
L
liuliu 已提交
2189
    .catch((err: BusinessError) => {
2190 2191 2192 2193 2194
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2195 2196 2197
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`);
2198
}
M
m00512953 已提交
2199 2200
  ```

Z
zhongjianfei 已提交
2201
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
2202 2203 2204

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

2205
设置UIAbility在任务中显示的名称(callback形式)。
M
m00512953 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215

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

**参数:**

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

H
huangshiwei 已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224
**错误码:**

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

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

M
m00512953 已提交
2225 2226 2227
**示例:**

  ```ts
L
liuliu 已提交
2228 2229 2230
  import { BusinessError } from '@ohos.base';

  this.context.setMissionLabel('test', (result: BusinessError) => {
2231
    console.info(`setMissionLabel: ${JSON.stringify(result)}`);
M
m00512953 已提交
2232 2233 2234
  });
  ```

Z
zhongjianfei 已提交
2235
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
2236 2237 2238

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

2239
设置UIAbility在任务中显示的名称(promise形式)。
M
m00512953 已提交
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254

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

**参数:**

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2255 2256 2257 2258 2259 2260 2261
**错误码:**

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

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

M
m00512953 已提交
2264 2265 2266
**示例:**

  ```ts
L
liuliu 已提交
2267 2268
  import { BusinessError } from '@ohos.base';

M
mingxihua 已提交
2269
  this.context.setMissionLabel('test').then(() => {
2270
    console.info('success');
L
liuliu 已提交
2271 2272 2273 2274
  }).catch((err: BusinessError) => {
    let code = (err as BusinessError).code;
    let message = (err as BusinessError).message;
    console.error(`setMissionLabel failed, code is ${code}, message is ${message}`);
M
m00512953 已提交
2275 2276
  });
  ```
Z
zhongjianfei 已提交
2277
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2278 2279 2280

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

M
mingxihua 已提交
2281
设置当前ability在任务中显示的图标, 图标大小最大为600M(callback形式)。
M
m00512953 已提交
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293

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

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

**参数:**

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

Z
zhangyafei.echo 已提交
2294 2295 2296 2297 2298 2299 2300
**错误码:**

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

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

M
m00512953 已提交
2303 2304 2305
**示例:**

  ```ts
L
liuliu 已提交
2306 2307
  import UIAbility from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
2308
  import image from '@ohos.multimedia.image';
2309
  
L
liuliu 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
  export default class EntryAbility extends UIAbility {
    onForeground() {
      let imagePixelMap: image.PixelMap;
      let color = new ArrayBuffer(0);
      image.createPixelMap(color, {
        size: {
          height: 100,
          width: 100
        }
      }).then((data) => {
        imagePixelMap = data;
        this.context.setMissionIcon(imagePixelMap, (err: BusinessError) => {
          console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
        })
      })
        .catch((err: BusinessError) => {
          console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
        });
2328
    }
L
liuliu 已提交
2329
  }
M
m00512953 已提交
2330 2331 2332
  ```


Z
zhongjianfei 已提交
2333
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2334 2335 2336

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

M
mingxihua 已提交
2337
设置当前ability在任务中显示的图标, 图标大小最大为600M(promise形式)。
M
m00512953 已提交
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354

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

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

**参数:**

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2355 2356 2357 2358 2359 2360 2361
**错误码:**

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

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

M
m00512953 已提交
2364 2365 2366
**示例:**

  ```ts
L
liuliu 已提交
2367 2368
  import UIAbility from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
2369 2370
  import image from '@ohos.multimedia.image';

L
liuliu 已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
  export default class EntryAbility extends UIAbility {
    onForeground() {
      let imagePixelMap: image.PixelMap;
      let color = new ArrayBuffer(0);
      image.createPixelMap(color, {
        size: {
          height: 100,
          width: 100
        }
      }).then((data) => {
          imagePixelMap = data;
          this.context.setMissionIcon(imagePixelMap)
            .then(() => {
              console.info('setMissionIcon succeed');
            })
            .catch((err: BusinessError) => {
              console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
            });
        })
        .catch((err: BusinessError) => {
          console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
        });
2393
    }
L
liuliu 已提交
2394
  }
M
m00512953 已提交
2395
  ```
H
hobbycao 已提交
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408

## UIAbilityContext.setMissionContinueState<sup>10+</sup>

setMissionContinueState(state: AbilityConstant.ContinueState, callback:AsyncCallback&lt;void&gt;): void;

设置UIAbility任务中流转状态(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
Y
yuyaozhi 已提交
2409
| state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#abilityconstantcontinuestate10) | 是 | 流转状态。 |
H
hobbycao 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |

**错误码:**

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

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

**示例:**

  ```ts
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
L
liuliu 已提交
2425
  import { BusinessError } from '@ohos.base';
H
hobbycao 已提交
2426

L
liuliu 已提交
2427
  this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
H
hobbycao 已提交
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
    console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
  });
  ```

## UIAbilityContext.setMissionContinueState<sup>10+</sup>

setMissionContinueState(state: AbilityConstant.ContinueState): Promise&lt;void&gt;;

设置UIAbility任务中流转状态(promise形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
Y
yuyaozhi 已提交
2444
| state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#abilityconstantcontinuestate10) | 是 | 流转状态。 |
H
hobbycao 已提交
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464

**返回值:**

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

**错误码:**

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

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

**示例:**

  ```ts
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
L
liuliu 已提交
2465
  import { BusinessError } from '@ohos.base';
H
hobbycao 已提交
2466 2467 2468

  this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE).then(() => {
    console.info('success');
L
liuliu 已提交
2469
  }).catch((err: BusinessError) => {
H
hobbycao 已提交
2470 2471 2472 2473
    console.error(`setMissionContinueState failed, code is ${err.code}, message is ${err.message}`);
  });
  ```

Z
zhongjianfei 已提交
2474
## UIAbilityContext.restoreWindowStage
M
m00512953 已提交
2475 2476 2477

restoreWindowStage(localStorage: LocalStorage) : void;

2478
恢复UIAbility中的WindowStage数据。
M
m00512953 已提交
2479 2480 2481 2482 2483 2484 2485

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
Y
yuyaozhi 已提交
2486
| localStorage | LocalStorage | 是 | 用于恢复window stage的存储数据。 |
M
m00512953 已提交
2487

Z
zhangyafei.echo 已提交
2488 2489 2490 2491 2492 2493 2494
**错误码:**

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

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

M
m00512953 已提交
2497 2498 2499
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2500
  let storage = new LocalStorage();
2501
  this.context.restoreWindowStage(storage);
M
m00512953 已提交
2502 2503
  ```

Z
zhongjianfei 已提交
2504
## UIAbilityContext.isTerminating
M
m00512953 已提交
2505 2506 2507

isTerminating(): boolean;

2508
查询UIAbility是否在terminating状态。
M
m00512953 已提交
2509 2510 2511 2512 2513 2514 2515 2516 2517

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2518 2519 2520 2521 2522
**错误码:**

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

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

M
m00512953 已提交
2526 2527 2528
**示例:**

  ```ts
L
liuliu 已提交
2529
  let isTerminating: boolean = this.context.isTerminating();
2530
  console.info(`ability state is ${isTerminating}`);
2531
  ```
C
caochunlei 已提交
2532 2533 2534 2535 2536 2537 2538 2539 2540

## 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`权限。
2541
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
C
caochunlei 已提交
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
 - 组件启动规则详见:[组件启动规则(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 已提交
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
**错误码:**

| 错误码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 已提交
2566 2567
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
2568 2569 2570 2571 2572 2573 2574
| 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 已提交
2575 2576 2577
**示例:**

  ```ts
2578
import dialogRequest from '@ohos.app.ability.dialogRequest';
L
liuliu 已提交
2579 2580
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
C
caochunlei 已提交
2581

L
liuliu 已提交
2582
let want: Want = {
2583 2584 2585 2586
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
};
C
caochunlei 已提交
2587

2588
try {
L
liuliu 已提交
2589
  this.context.requestDialogService(want, (err: BusinessError, result: dialogRequest.RequestResult) => {
2590 2591 2592 2593 2594 2595
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
M
mingxihua 已提交
2596
    console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
2597 2598 2599
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2600 2601 2602
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`requestDialogService failed, code is ${code}, message is ${message}`);
2603
}
C
caochunlei 已提交
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
  ```

  ## 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`权限。
2614
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
C
caochunlei 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
 - 组件启动规则详见:[组件启动规则(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 已提交
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
**错误码:**

| 错误码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 已提交
2645 2646
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
2647 2648 2649 2650 2651 2652 2653
| 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 已提交
2654 2655 2656 2657
**示例:**

  ```ts
import dialogRequest from '@ohos.app.ability.dialogRequest';
L
liuliu 已提交
2658 2659
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
C
caochunlei 已提交
2660

L
liuliu 已提交
2661
let want: Want = {
2662 2663
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
C
caochunlei 已提交
2664 2665 2666
};

try {
2667
  this.context.requestDialogService(want)
L
liuliu 已提交
2668
    .then((result: dialogRequest.RequestResult) => {
2669
      // 执行正常业务
M
mingxihua 已提交
2670
      console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
C
caochunlei 已提交
2671
    })
L
liuliu 已提交
2672
    .catch((err: BusinessError) => {
2673 2674
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2675
    });
2676 2677
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2678 2679 2680
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`requestDialogService failed, code is ${code}, message is ${message}`);
C
caochunlei 已提交
2681 2682
}
  ```
U
unknown 已提交
2683
  ## UIAbilityContext.startRecentAbility
U
unknown 已提交
2684

Y
yuyaozhi 已提交
2685
startRecentAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void;
U
unknown 已提交
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706

启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 需要启动Ability的want信息。 |
| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |

**错误码:**

U
unknown 已提交
2707 2708
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
| 错误码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 已提交
2720 2721
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
U
unknown 已提交
2722 2723 2724 2725 2726 2727 2728 2729
| 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. |

**示例:**

  ```ts
L
liuliu 已提交
2730 2731 2732 2733
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
U
unknown 已提交
2734 2735 2736 2737 2738
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

try {
L
liuliu 已提交
2739
  this.context.startRecentAbility(want, (err: BusinessError) => {
U
unknown 已提交
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2750 2751 2752
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
U
unknown 已提交
2753 2754
}
  ```
U
unknown 已提交
2755
## UIAbilityContext.startRecentAbility
U
unknown 已提交
2756

Y
yuyaozhi 已提交
2757
startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void;
U
unknown 已提交
2758 2759

启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。
U
unknown 已提交
2760
当开发者需要携带启动参数时可以选择此API。
U
unknown 已提交
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780

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

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

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

**参数:**

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

**错误码:**

U
unknown 已提交
2781 2782
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
| 错误码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 已提交
2794 2795
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
U
unknown 已提交
2796 2797 2798 2799 2800 2801 2802 2803
| 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. |

**示例:**

  ```ts
L
liuliu 已提交
2804 2805 2806 2807 2808
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
U
unknown 已提交
2809 2810 2811 2812
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
2813
let options: StartOptions = {
U
unknown 已提交
2814 2815 2816 2817
  windowMode: 0
};

try {
L
liuliu 已提交
2818
  this.context.startRecentAbility(want, options, (err: BusinessError) => {
U
unknown 已提交
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2829 2830 2831
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
U
unknown 已提交
2832 2833
}
  ```
U
unknown 已提交
2834
## UIAbilityContext.startRecentAbility
U
unknown 已提交
2835

Y
yuyaozhi 已提交
2836
startRecentAbility(want: Want, options?: StartOptions): Promise&lt;void&gt;;
U
unknown 已提交
2837

U
unknown 已提交
2838 2839
启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。
当开发者期望启动结果以Promise形式返回时可以选择此API。
U
unknown 已提交
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858

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

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

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

**参数:**

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

**错误码:**

U
unknown 已提交
2859 2860
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
| 错误码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 已提交
2872 2873
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
U
unknown 已提交
2874 2875 2876 2877 2878 2879 2880 2881
| 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. |

**示例:**

  ```ts
L
liuliu 已提交
2882 2883 2884 2885 2886
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
U
unknown 已提交
2887 2888 2889
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
2890
let options: StartOptions = {
U
unknown 已提交
2891 2892 2893 2894 2895 2896 2897 2898 2899
  windowMode: 0,
};

try {
  this.context.startRecentAbility(want, options)
    .then(() => {
      // 执行正常业务
      console.info('startRecentAbility succeed');
    })
L
liuliu 已提交
2900
    .catch((err: BusinessError) => {
U
unknown 已提交
2901 2902 2903 2904 2905
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
2906 2907 2908
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
C
caochunlei 已提交
2909 2910
}
  ```
X
add doc  
xinking129 已提交
2911

Y
yuyaozhi 已提交
2912
## UIAbilityContext.startAbilityByCallWithAccount<sup>10+</sup>
X
add doc  
xinking129 已提交
2913 2914 2915 2916 2917 2918 2919

startAbilityByCallWithAccount(want: Want, accountId: number): Promise&lt;Caller&gt;;

根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。

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

Y
yuyaozhi 已提交
2924 2925
**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

X
add doc  
xinking129 已提交
2926 2927 2928 2929 2930 2931 2932 2933
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
X
xinking129 已提交
2934
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
X
xinking129 已提交
2935
| accountId | number | 是 | 系统帐号的帐号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
X
add doc  
xinking129 已提交
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
2947
| 16000001 | The specified ability does not exist. |
X
add doc  
xinking129 已提交
2948
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
2949
| 16000004 | Can not start invisible component. |
X
add doc  
xinking129 已提交
2950 2951
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
Y
yuyaozhi 已提交
2952
| 16000008 | The crowdtesting application expires. |
X
add doc  
xinking129 已提交
2953
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
2954 2955 2956
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 16000050 | Internal error. |
X
add doc  
xinking129 已提交
2957 2958 2959 2960 2961 2962 2963
| 16200001 | The caller has been released.        |

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

**示例:**

  ```ts
L
liuliu 已提交
2964 2965 2966 2967 2968
  import { Caller } from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let caller: Caller;
X
add doc  
xinking129 已提交
2969 2970 2971 2972 2973

  // 系统账号的账号ID, -1表示当前激活用户
  let accountId = -1;

  // 指定启动的Ability
L
liuliu 已提交
2974
  let want: Want = {
X
add doc  
xinking129 已提交
2975 2976
      bundleName: 'com.acts.actscalleeabilityrely',
      moduleName: 'entry',
Y
yuyaozhi 已提交
2977 2978
      abilityName: 'EntryAbility',
      deviceId: '',
X
xinking129 已提交
2979 2980 2981 2982
      parameters: {
        // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动
        'ohos.aafwk.param.callAbilityToForeground': true
      }
X
add doc  
xinking129 已提交
2983 2984 2985 2986
  };

  try {
    this.context.startAbilityByCallWithAccount(want, accountId)
L
liuliu 已提交
2987
      .then((obj: Caller) => {
X
add doc  
xinking129 已提交
2988 2989 2990
        // 执行正常业务
        caller = obj;
        console.log('startAbilityByCallWithAccount succeed');
L
liuliu 已提交
2991
      }).catch((error: BusinessError) => {
X
add doc  
xinking129 已提交
2992 2993 2994 2995 2996 2997 2998
        // 处理业务逻辑错误
        console.error('startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
  }
2999 3000
  ```

D
donglin 已提交
3001 3002
## UIAbilityContext.startAbilityAsCaller<sup>10+<sup>

Y
yuyaozhi 已提交
3003
startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void;
D
donglin 已提交
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013

使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。

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

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

1
Bugfix  
18870373690 已提交
3014 3015
**系统API**:此接口为系统接口,三方应用不支持调用。

D
donglin 已提交
3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码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.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 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)

**示例:**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
3049
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
D
donglin 已提交
3050 3051 3052
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends UIAbility {
L
liuliu 已提交
3053
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
D
donglin 已提交
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.log('startAbilityAsCaller success.');
      }
    })
  }
}

```

## UIAbilityContext.startAbilityAsCaller<sup>10+<sup>

1
Bugfix  
18870373690 已提交
3075
startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
D
donglin 已提交
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085

使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。

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

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

1
Bugfix  
18870373690 已提交
3086 3087
**系统API**:此接口为系统接口,三方应用不支持调用。

D
donglin 已提交
3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000001 | The specified ability does not exist. |
| 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. |
| 16000011 | The context does not exist.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 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)

**示例:**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
3120 3121
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import StartOptions from '@ohos.app.ability.StartOptions';
D
donglin 已提交
3122 3123 3124
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends UIAbility {
L
liuliu 已提交
3125
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
D
donglin 已提交
3126 3127 3128 3129 3130 3131
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

L
liuliu 已提交
3132
    let option: StartOptions = {
D
donglin 已提交
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150
      displayId: 0
    }

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, option, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.log('startAbilityAsCaller success.');
      }
    })
  }
}

```

## UIAbilityContext.startAbilityAsCaller<sup>10+<sup>

1
Bugfix  
18870373690 已提交
3151
startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>;
D
donglin 已提交
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161

使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。

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

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

1
Bugfix  
18870373690 已提交
3162 3163
**系统API**:此接口为系统接口,三方应用不支持调用。

D
donglin 已提交
3164 3165 3166 3167 3168
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
1
Bugfix  
18870373690 已提交
3169
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
D
donglin 已提交
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |

**错误码:**

| 错误码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.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 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)

**示例:**

```ts
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
3203 3204
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import StartOptions from '@ohos.app.ability.StartOptions';
D
donglin 已提交
3205
import Want from '@ohos.app.ability.Want';
L
liuliu 已提交
3206
import { BusinessError } from '@ohos.base';
D
donglin 已提交
3207 3208

export default class EntryAbility extends UIAbility {
L
liuliu 已提交
3209
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
D
donglin 已提交
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    let option: StartOptions = {
      displayId: 0
    }

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, option)
      .then(() => {
        console.log('startAbilityAsCaller success.');
      })
L
liuliu 已提交
3225
      .catch((err: BusinessError) => {
D
donglin 已提交
3226 3227 3228 3229 3230 3231 3232
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      })
  }
}

```

Y
yuyaozhi 已提交
3233
## UIAbilityContext.reportDrawnCompleted<sup>10+</sup>
3234

Y
yuyaozhi 已提交
3235
reportDrawnCompleted(callback: AsyncCallback\<void>): void;
3236

X
xinking129 已提交
3237
当页面加载完成(loadContent成功)时,为开发者提供打点功能(callback形式)。
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 页面加载完成打点的回调函数。 |

**错误码:**

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

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

**示例:**

  ```ts
Y
yuyaozhi 已提交
3258 3259
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
L
liuliu 已提交
3260
import { BusinessError } from '@ohos.base';
Y
yuyaozhi 已提交
3261 3262 3263

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
X
xinking129 已提交
3264
    windowStage.loadContent('pages/Index', (err, data) => {
Y
yuyaozhi 已提交
3265 3266 3267 3268 3269 3270 3271
      if (err.code) {
        return;
      }
      try {
        this.context.reportDrawnCompleted((err) => {
          if (err.code) {
            // 处理业务逻辑错误
X
xinking129 已提交
3272
            console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`);
Y
yuyaozhi 已提交
3273 3274 3275 3276 3277 3278 3279
            return;
          }
          // 执行正常业务
          console.info('reportDrawnCompleted succeed');
        });
      } catch (err) {
        // 捕获同步的参数错误
L
liuliu 已提交
3280 3281 3282
        let code = (err as BusinessError).code;
        let message = (err as BusinessError).message;
        console.error(`reportDrawnCompleted failed, code is ${code}, message is ${message}`);
Y
yuyaozhi 已提交
3283
      }
3284
    });
X
xinking129 已提交
3285
    console.log("MainAbility onWindowStageCreate")
Y
yuyaozhi 已提交
3286 3287
  }
};
U
unknown 已提交
3288
  ```