js-apis-inner-application-uiAbilityContext.md 108.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
76 77 78 79
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
80

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

Z
zhongjianfei 已提交
97
## UIAbilityContext.startAbility
M
m00512953 已提交
98 99 100 101 102

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

启动Ability(callback形式)。

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

M
m00512953 已提交
108 109 110 111 112 113 114 115 116 117 118 119
**系统能力**: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 已提交
120
| 错误码ID | 错误信息 |
M
m00512953 已提交
121
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
122 123 124 125 126 127 128
| 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 已提交
129 130 131 132
| 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 已提交
133 134 135 136
| 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 已提交
137

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

M
m00512953 已提交
140 141 142
**示例:**

  ```ts
143 144 145 146 147 148 149 150
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};
M
m00512953 已提交
151

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

Z
zhongjianfei 已提交
168
## UIAbilityContext.startAbility
M
m00512953 已提交
169 170 171 172 173

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

启动Ability(promise形式)。

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

M
m00512953 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
**系统能力**: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 已提交
198 199 200 201 202 203 204
| 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 已提交
205 206 207 208
| 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 已提交
209 210 211 212
| 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 已提交
213

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

M
m00512953 已提交
216 217 218
**示例:**

  ```ts
219 220 221 222 223 224 225
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};
M
m00512953 已提交
226

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

Z
zhongjianfei 已提交
243
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
244 245 246

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

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

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

M
m00512953 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269
**系统能力**: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 已提交
270 271 272 273 274 275 276 277 278
| 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 已提交
279 280
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
281 282 283 284
| 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 已提交
285

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

M
m00512953 已提交
288 289 290
**示例:**

  ```ts
291 292 293 294 295
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
296

297 298 299 300 301 302 303 304 305 306 307 308 309 310
try {
  this.context.startAbilityForResult(want, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
311 312
  ```

Z
zhongjianfei 已提交
313
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
314 315 316

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

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

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

**系统能力**: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 已提交
341 342 343 344 345 346 347 348 349
| 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 已提交
350 351
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
352 353 354 355
| 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 已提交
356

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

M
m00512953 已提交
359 360 361
**示例:**

  ```ts
362 363 364 365 366 367 368 369
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};
M
m00512953 已提交
370

371 372 373 374 375 376 377 378 379 380
try {
  this.context.startAbilityForResult(want, options, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
381
} catch (err) {
382 383 384
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
385 386 387
  ```


Z
zhongjianfei 已提交
388
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
389 390 391

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

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

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

M
m00512953 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
**系统能力**: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 已提交
422 423 424 425 426 427 428 429 430
| 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 已提交
431 432
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
433 434 435 436
| 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 已提交
437

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

M
m00512953 已提交
440 441 442
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
443
let want = {
444 445
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
446
};
Z
zhangyafei.echo 已提交
447
let options = {
448
  windowMode: 0,
449
};
M
m00512953 已提交
450

451
try {
452 453 454 455
  this.context.startAbilityForResult(want, options)
    .then((result) => {
      // 执行正常业务
      console.info('startAbilityForResult succeed');
456
    })
457 458 459
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
460
    });
461 462 463
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
464
}
M
m00512953 已提交
465 466
  ```

Z
zhongjianfei 已提交
467
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
468 469 470

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

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

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

Y
yuyaozhi 已提交
478 479 480 481 482
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
483 484 485 486 487 488 489 490 491 492

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
500 501 502 503 504 505 506 507 508
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
509 510
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
511 512 513 514
| 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 已提交
515

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

M
m00512953 已提交
518 519 520
**示例:**

  ```ts
521 522 523 524 525 526
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541
try {
  this.context.startAbilityForResultWithAccount(want, accountId, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
542 543 544
  ```


Z
zhongjianfei 已提交
545
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
546 547 548

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

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

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

Y
yuyaozhi 已提交
556 557 558 559 560
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
561 562 563 564 565 566 567 568 569 570

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
579 580 581 582 583 584 585 586 587
| 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 已提交
588 589
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
590 591 592 593
| 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 已提交
594

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

M
m00512953 已提交
597 598 599
**示例:**

  ```ts
600 601 602 603 604 605 606 607 608
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
609

610 611 612 613 614 615 616 617 618 619 620 621 622 623
try {
  this.context.startAbilityForResultWithAccount(want, accountId, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
624 625 626
  ```


Z
zhongjianfei 已提交
627
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
628 629 630

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

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

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

Y
yuyaozhi 已提交
638 639 640 641 642
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
643 644 645 646 647 648 649 650 651 652

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

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

**参数:**

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
660
| Promise&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Ability被销毁时的回调函数,包含AbilityResult参数。 |
M
m00512953 已提交
661 662 663 664 665

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
666 667 668 669 670 671 672 673 674
| 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 已提交
675 676
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
677 678 679 680
| 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 已提交
681

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

M
m00512953 已提交
684 685 686
**示例:**

  ```ts
687 688 689 690 691 692 693 694 695
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
696

697 698 699 700 701 702 703 704 705 706 707 708 709 710
try {
  this.context.startAbilityForResultWithAccount(want, accountId, options)
    .then((result) => {
      // 执行正常业务
      console.info('startAbilityForResultWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
711
  ```
Z
zhongjianfei 已提交
712
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
726 727
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
728 729 730 731 732

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
733 734
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
735
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
736 737 738 739
| 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 已提交
740 741
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
742 743
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
744

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

M
m00512953 已提交
747 748 749
**示例:**

  ```ts
750 751 752 753 754
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
755

756 757 758
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
M
m00512953 已提交
759
      // 执行正常业务
760 761 762 763 764
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
765
    });
766 767 768 769
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
770 771
  ```

Z
zhongjianfei 已提交
772
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
786
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
787 788 789 790 791

**错误码:**

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

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

M
m00512953 已提交
806 807 808
**示例:**

  ```ts
809 810 811 812 813
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
814

815 816 817 818 819 820 821 822 823 824
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
      // 执行正常业务
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
825
} catch (err) {
826 827 828
  // 处理入参错误异常
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
829 830
  ```

Z
zhongjianfei 已提交
831
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
832 833 834 835 836

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

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

Y
yuyaozhi 已提交
837 838 839 840 841
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
842 843 844 845 846 847 848 849 850

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
851
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
852
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
853
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
854 855 856 857 858

**错误码:**

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

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

M
m00512953 已提交
873 874 875
**示例:**

  ```ts
876 877 878 879 880 881
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
882

883 884 885 886 887 888 889 890 891 892 893 894 895 896
try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
897 898
  ```

Z
zhongjianfei 已提交
899
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
900 901 902 903 904

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

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

Y
yuyaozhi 已提交
905 906 907 908 909
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
910 911 912 913 914 915 916 917 918 919

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
926 927
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
928
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
929 930 931 932
| 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 已提交
933 934
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
935 936
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
937

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

M
m00512953 已提交
940 941 942
**示例:**

  ```ts
943 944 945 946 947 948
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
949

950 951 952 953 954 955 956 957 958 959 960 961 962 963
try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // 执行正常业务
      console.info('startServiceExtensionAbilityWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
964
  ```
Z
zhongjianfei 已提交
965
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
979 980
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
981 982 983 984 985

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
986 987
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
988
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
989 990 991
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
992 993
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
994 995
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
996

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

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

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

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

Z
zhongjianfei 已提交
1024
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1038
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
1039 1040 1041 1042 1043

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1044 1045
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1046
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1047 1048 1049 1050 1051
| 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 已提交
1052

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

M
m00512953 已提交
1055 1056 1057
**示例:**

  ```ts
1058 1059 1060 1061 1062
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
try {
  this.context.stopServiceExtensionAbility(want)
    .then(() => {
      // 执行正常业务
      console.info('stopServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1078 1079
  ```

Z
zhongjianfei 已提交
1080
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1081 1082 1083

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

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

Y
yuyaozhi 已提交
1086 1087 1088 1089 1090
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1108 1109
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1110
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1111 1112 1113 1114 1115
| 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 已提交
1116

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

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

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

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

Z
zhongjianfei 已提交
1145
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1146 1147 1148

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

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

Y
yuyaozhi 已提交
1151 1152 1153 1154 1155
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1165 1166
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1167 1168 1169 1170 1171

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1172 1173
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1174
| 16000004 | Can not start invisible component. |
Z
zhangyafei.echo 已提交
1175 1176 1177 1178 1179
| 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 已提交
1180

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

M
m00512953 已提交
1183 1184 1185
**示例:**

  ```ts
1186 1187 1188 1189 1190 1191
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
try {
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // 执行正常业务
      console.info('stopServiceExtensionAbilityWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1207 1208
  ```

Z
zhongjianfei 已提交
1209
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

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

停止Ability自身(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1221
| callback | AsyncCallback&lt;void&gt; | 是 | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1222 1223 1224 1225 1226

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1227 1228 1229 1230 1231 1232
| 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 已提交
1233

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

M
m00512953 已提交
1236 1237 1238
**示例:**

  ```ts
1239
  try {
1240 1241
    this.context.terminateSelf((err) => {
      if (err.code) {
1242
        // 处理业务逻辑错误
1243
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1244 1245 1246
        return;
      }
      // 执行正常业务
1247
      console.info('terminateSelf succeed');
1248
    });
1249
  } catch (err) {
1250
    // 捕获同步的参数错误
1251
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1252
  }
M
m00512953 已提交
1253 1254 1255
  ```


Z
zhongjianfei 已提交
1256
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

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

停止Ability自身(promise形式)。

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
1268
| Promise&lt;void&gt; | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1269 1270 1271 1272 1273

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1274 1275 1276 1277 1278 1279
| 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 已提交
1280

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

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

  ```ts
1286 1287 1288 1289
  try {
    this.context.terminateSelf()
      .then(() => {
        // 执行正常业务
1290
        console.info('terminateSelf succeed');
1291
      })
1292
      .catch((err) => {
1293
        // 处理业务逻辑错误
1294
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1295
      });
Y
yuyaozhi 已提交
1296
  } catch (err) {
1297
    // 捕获同步的参数错误
1298
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1299
  }
M
m00512953 已提交
1300 1301 1302
  ```


Z
zhongjianfei 已提交
1303
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1304 1305 1306

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

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1322 1323 1324 1325 1326 1327
| 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 已提交
1328

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

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

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

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


Z
zhongjianfei 已提交
1362
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1363 1364 1365

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

M
m00512953 已提交
1366
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(promise形式)。
M
m00512953 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

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

**参数:**

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

**返回值:**

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

**错误码:**

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

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

M
m00512953 已提交
1395 1396 1397
**示例:**

  ```ts
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
let abilityResult = {
  want,
  resultCode
};
M
m00512953 已提交
1408

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
try {
  this.context.terminateSelfWithResult(abilityResult)
    .then(() => {
      // 执行正常业务
      console.info('terminateSelfWithResult succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1423 1424
  ```

Z
zhongjianfei 已提交
1425
## UIAbilityContext.connectServiceExtensionAbility
M
m00512953 已提交
1426 1427 1428

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

1429
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的Ability。
M
m00512953 已提交
1430 1431 1432 1433 1434 1435 1436

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1437 1438
| want | [Want](js-apis-application-want.md) | 是 | 连接ServiceExtensionAbility的want信息。 |
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 |
M
m00512953 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1450
| 16000001 | The specified ability does not exist. |
Y
yuyaozhi 已提交
1451 1452
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
Y
yuyaozhi 已提交
1453
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1454 1455 1456 1457 1458
| 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 已提交
1459
| 16000050 | Internal error. |
M
m00512953 已提交
1460

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

M
m00512953 已提交
1463 1464 1465
**示例:**

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

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


Z
zhongjianfei 已提交
1495
## UIAbilityContext.connectServiceExtensionAbilityWithAccount
M
m00512953 已提交
1496 1497 1498

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

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

Y
yuyaozhi 已提交
1501 1502 1503 1504 1505
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1516
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
1517
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。。 |
M
m00512953 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1529
| 16000001 | The specified ability does not exist. |
Y
yuyaozhi 已提交
1530 1531 1532 1533 1534 1535 1536
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 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 已提交
1537
| 16000050 | Internal error. |
M
m00512953 已提交
1538

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

M
m00512953 已提交
1541 1542 1543
**示例:**

  ```ts
1544 1545 1546 1547 1548 1549
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
1550
let commRemote;
1551 1552
let options = {
  onConnect(elementName, remote) {
M
mingxihua 已提交
1553
    commRemote = remote;
1554 1555 1556 1557 1558 1559 1560
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1561
  }
1562 1563 1564 1565 1566 1567 1568 1569 1570
};

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

Z
zhongjianfei 已提交
1573
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1574 1575 1576

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

M
mingxihua 已提交
1577
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(promise形式)。
M
m00512953 已提交
1578 1579 1580 1581 1582 1583 1584

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1585
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
M
m00512953 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596

**返回值:**

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

**错误码:**

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

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

M
m00512953 已提交
1602 1603 1604
**示例:**

  ```ts
1605 1606
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
1607
let commRemote;
M
m00512953 已提交
1608

1609 1610
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
M
mingxihua 已提交
1611
    commRemote = null;
1612 1613 1614 1615 1616 1617 1618 1619 1620
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
M
mingxihua 已提交
1621
  commRemote = null;
1622 1623 1624
  // 处理入参错误异常
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1625 1626
  ```

Z
zhongjianfei 已提交
1627
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1628 1629 1630

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

M
mingxihua 已提交
1631
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(callback形式)。
M
m00512953 已提交
1632 1633 1634 1635 1636 1637 1638

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1639
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
1640
| callback | AsyncCallback\<void> | 是 | callback形式返回断开连接的结果。 |
M
m00512953 已提交
1641 1642 1643 1644 1645

**错误码:**

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

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

M
m00512953 已提交
1651 1652 1653
**示例:**

  ```ts
1654 1655
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
let commRemote;

try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
    commRemote = null;
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
  commRemote = null;
  // 处理入参错误异常
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1674

1675 1676
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
M
mingxihua 已提交
1677
    commRemote = null;
1678 1679 1680 1681 1682 1683 1684 1685 1686
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
M
mingxihua 已提交
1687
  commRemote = null;
1688 1689 1690
  // 处理入参错误异常
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1691 1692
  ```

Z
zhongjianfei 已提交
1693
## UIAbilityContext.startAbilityByCall
M
m00512953 已提交
1694 1695 1696 1697 1698

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

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

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

Y
yuyaozhi 已提交
1704 1705
**需要权限:** ohos.permission.ABILITY_BACKGROUND_COMMUNICATION

M
m00512953 已提交
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
**系统能力**: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 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
**错误码:**

| 错误码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 已提交
1731 1732
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1733 1734 1735
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

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

M
m00512953 已提交
1738 1739 1740 1741 1742
**示例:**

  后台启动:

  ```ts
1743 1744 1745 1746 1747 1748 1749 1750 1751
let caller;

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

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

1769
前台启动:
M
m00512953 已提交
1770 1771

  ```ts
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
let caller;

// 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
let wantForeground = {
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: '',
  parameters: {
    'ohos.aafwk.param.callAbilityToForeground': true
M
m00512953 已提交
1782
  }
1783 1784 1785 1786 1787 1788 1789 1790
};

try {
  this.context.startAbilityByCall(wantForeground)
    .then((obj) => {
      // 执行正常业务
      caller = obj;
      console.info('startAbilityByCall succeed');
1791
    }).catch((err) => {
1792 1793 1794
    // 处理业务逻辑错误
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
1795
} catch (err) {
1796 1797 1798
  // 处理入参错误异常
  console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1799 1800
  ```

Z
zhongjianfei 已提交
1801
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1802 1803 1804

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

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

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

Y
yuyaozhi 已提交
1812 1813 1814 1815 1816
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1834 1835 1836 1837 1838 1839 1840 1841 1842
| 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 已提交
1843 1844
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1845 1846 1847 1848
| 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 已提交
1849

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

M
m00512953 已提交
1852 1853 1854
**示例:**

  ```ts
1855 1856 1857 1858 1859 1860
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
1861

1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
try {
  this.context.startAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1876 1877 1878
  ```


Z
zhongjianfei 已提交
1879
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1880 1881 1882

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

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

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

Y
yuyaozhi 已提交
1890 1891 1892 1893 1894
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921
| 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 已提交
1922 1923
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
1924 1925 1926 1927
| 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 已提交
1928

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

M
m00512953 已提交
1931 1932 1933
**示例:**

  ```ts
1934 1935 1936 1937 1938 1939 1940 1941 1942
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
1943

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
try {
  this.context.startAbilityWithAccount(want, accountId, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1958 1959 1960
  ```


Z
zhongjianfei 已提交
1961
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1962 1963 1964

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

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

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

Y
yuyaozhi 已提交
1972 1973 1974 1975 1976
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
M
m00512953 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002
| 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 已提交
2003 2004
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
Z
zhangyafei.echo 已提交
2005 2006 2007 2008
| 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 已提交
2009

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

M
m00512953 已提交
2012 2013 2014
**示例:**

  ```ts
2015 2016 2017 2018 2019 2020 2021 2022 2023
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
2024

2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
try {
  this.context.startAbilityWithAccount(want, accountId, options)
    .then(() => {
      // 执行正常业务
      console.info('startAbilityWithAccount succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
2039 2040
  ```

Z
zhongjianfei 已提交
2041
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
2042 2043 2044

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

2045
设置UIAbility在任务中显示的名称(callback形式)。
M
m00512953 已提交
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055

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

**参数:**

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

H
huangshiwei 已提交
2056 2057 2058 2059 2060 2061 2062 2063 2064
**错误码:**

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

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

M
m00512953 已提交
2065 2066 2067
**示例:**

  ```ts
M
mingxihua 已提交
2068
  this.context.setMissionLabel('test', (result) => {
2069
    console.info(`setMissionLabel: ${JSON.stringify(result)}`);
M
m00512953 已提交
2070 2071 2072
  });
  ```

Z
zhongjianfei 已提交
2073
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
2074 2075 2076

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

2077
设置UIAbility在任务中显示的名称(promise形式)。
M
m00512953 已提交
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092

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

**参数:**

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2093 2094 2095 2096 2097 2098 2099
**错误码:**

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

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

M
m00512953 已提交
2102 2103 2104
**示例:**

  ```ts
M
mingxihua 已提交
2105
  this.context.setMissionLabel('test').then(() => {
2106 2107 2108
    console.info('success');
  }).catch((err) => {
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
2109 2110
  });
  ```
Z
zhongjianfei 已提交
2111
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2112 2113 2114

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

M
mingxihua 已提交
2115
设置当前ability在任务中显示的图标, 图标大小最大为600M(callback形式)。
M
m00512953 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127

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

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

**参数:**

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

Z
zhangyafei.echo 已提交
2128 2129 2130 2131 2132 2133 2134
**错误码:**

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

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

M
m00512953 已提交
2137 2138 2139
**示例:**

  ```ts
2140
  import image from '@ohos.multimedia.image';
2141
  
Z
zhangyafei.echo 已提交
2142 2143 2144
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
2145 2146 2147 2148 2149 2150 2151 2152
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
M
m00512953 已提交
2153
    })
2154
    .catch((err) => {
2155
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
2156 2157
    });
  this.context.setMissionIcon(imagePixelMap, (err) => {
2158
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
2159
  })
M
m00512953 已提交
2160 2161 2162
  ```


Z
zhongjianfei 已提交
2163
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2164 2165 2166

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

M
mingxihua 已提交
2167
设置当前ability在任务中显示的图标, 图标大小最大为600M(promise形式)。
M
m00512953 已提交
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184

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

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

**参数:**

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2185 2186 2187 2188 2189 2190 2191
**错误码:**

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

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

M
m00512953 已提交
2194 2195 2196
**示例:**

  ```ts
2197 2198
  import image from '@ohos.multimedia.image';

Z
zhangyafei.echo 已提交
2199 2200 2201
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
    })
    .catch((err) => {
2212
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
2213 2214 2215
    });
  this.context.setMissionIcon(imagePixelMap)
    .then(() => {
2216
      console.info('setMissionIcon succeed');
2217 2218
    })
    .catch((err) => {
2219
      console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
2220
    });
M
m00512953 已提交
2221
  ```
H
hobbycao 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
Y
yuyaozhi 已提交
2235
| state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#abilityconstantcontinuestate10) | 是 | 流转状态。 |
H
hobbycao 已提交
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
| 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';

  this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result) => {
    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 已提交
2269
| state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#abilityconstantcontinuestate10) | 是 | 流转状态。 |
H
hobbycao 已提交
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| 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';

  this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE).then(() => {
    console.info('success');
  }).catch((err) => {
    console.error(`setMissionContinueState failed, code is ${err.code}, message is ${err.message}`);
  });
  ```

Z
zhongjianfei 已提交
2298
## UIAbilityContext.restoreWindowStage
M
m00512953 已提交
2299 2300 2301

restoreWindowStage(localStorage: LocalStorage) : void;

2302
恢复UIAbility中的WindowStage数据。
M
m00512953 已提交
2303 2304 2305 2306 2307 2308 2309

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

**参数:**

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

Z
zhangyafei.echo 已提交
2312 2313 2314 2315 2316 2317 2318
**错误码:**

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

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

M
m00512953 已提交
2321 2322 2323
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2324
  let storage = new LocalStorage();
2325
  this.context.restoreWindowStage(storage);
M
m00512953 已提交
2326 2327
  ```

Z
zhongjianfei 已提交
2328
## UIAbilityContext.isTerminating
M
m00512953 已提交
2329 2330 2331

isTerminating(): boolean;

2332
查询UIAbility是否在terminating状态。
M
m00512953 已提交
2333 2334 2335 2336 2337 2338 2339 2340 2341

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2342 2343 2344 2345 2346
**错误码:**

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

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

M
m00512953 已提交
2350 2351 2352
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2353
  let isTerminating = this.context.isTerminating();
2354
  console.info(`ability state is ${isTerminating}`);
2355
  ```
C
caochunlei 已提交
2356 2357 2358 2359 2360 2361 2362 2363 2364

## 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`权限。
2365
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
C
caochunlei 已提交
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
 - 组件启动规则详见:[组件启动规则(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 已提交
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
**错误码:**

| 错误码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 已提交
2390 2391
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
2392 2393 2394 2395 2396 2397 2398
| 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 已提交
2399 2400 2401
**示例:**

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

2404 2405 2406 2407 2408
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
};
C
caochunlei 已提交
2409

2410 2411 2412 2413 2414 2415 2416 2417
try {
  this.context.requestDialogService(want, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
M
mingxihua 已提交
2418
    console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
2419 2420 2421 2422 2423
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
}
C
caochunlei 已提交
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
  ```

  ## 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`权限。
2434
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
C
caochunlei 已提交
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
 - 组件启动规则详见:[组件启动规则(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 已提交
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
**错误码:**

| 错误码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 已提交
2465 2466
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
2467 2468 2469 2470 2471 2472 2473
| 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 已提交
2474 2475 2476 2477 2478 2479
**示例:**

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

let want = {
2480 2481
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
C
caochunlei 已提交
2482 2483 2484
};

try {
2485 2486 2487
  this.context.requestDialogService(want)
    .then((result) => {
      // 执行正常业务
M
mingxihua 已提交
2488
      console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
C
caochunlei 已提交
2489
    })
2490 2491 2492
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2493
    });
2494 2495 2496
} catch (err) {
  // 处理入参错误异常
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2497 2498
}
  ```
U
unknown 已提交
2499
  ## UIAbilityContext.startRecentAbility
U
unknown 已提交
2500

Y
yuyaozhi 已提交
2501
startRecentAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void;
U
unknown 已提交
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522

启动一个指定的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 已提交
2523 2524
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
| 错误码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 已提交
2536 2537
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
U
unknown 已提交
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
| 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
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

try {
  this.context.startRecentAbility(want, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startRecentAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
  ```
U
unknown 已提交
2566
## UIAbilityContext.startRecentAbility
U
unknown 已提交
2567

Y
yuyaozhi 已提交
2568
startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void;
U
unknown 已提交
2569 2570

启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。
U
unknown 已提交
2571
当开发者需要携带启动参数时可以选择此API。
U
unknown 已提交
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591

使用规则:
 - 调用方应用位于后台时,使用该接口启动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 已提交
2592 2593
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
| 错误码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 已提交
2605 2606
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
U
unknown 已提交
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
| 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
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};

try {
  this.context.startRecentAbility(want, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startRecentAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
  ```
U
unknown 已提交
2639
## UIAbilityContext.startRecentAbility
U
unknown 已提交
2640

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

U
unknown 已提交
2643 2644
启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。
当开发者期望启动结果以Promise形式返回时可以选择此API。
U
unknown 已提交
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663

使用规则:
 - 调用方应用位于后台时,使用该接口启动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 已提交
2664 2665
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
| 错误码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 已提交
2677 2678
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
U
unknown 已提交
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
| 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
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};

try {
  this.context.startRecentAbility(want, options)
    .then(() => {
      // 执行正常业务
      console.info('startRecentAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2708 2709
}
  ```
X
add doc  
xinking129 已提交
2710

Y
yuyaozhi 已提交
2711
## UIAbilityContext.startAbilityByCallWithAccount<sup>10+</sup>
X
add doc  
xinking129 已提交
2712 2713 2714 2715 2716 2717 2718

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 已提交
2719 2720 2721
 - 调用方应用位于后台时,使用该接口启动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 已提交
2722

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

X
add doc  
xinking129 已提交
2725 2726 2727 2728 2729 2730 2731 2732
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
X
xinking129 已提交
2733
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
X
xinking129 已提交
2734
| accountId | number | 是 | 系统帐号的帐号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
X
add doc  
xinking129 已提交
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
2746
| 16000001 | The specified ability does not exist. |
X
add doc  
xinking129 已提交
2747
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
2748
| 16000004 | Can not start invisible component. |
X
add doc  
xinking129 已提交
2749 2750
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
Y
yuyaozhi 已提交
2751
| 16000008 | The crowdtesting application expires. |
X
add doc  
xinking129 已提交
2752
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
2753 2754 2755
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 16000050 | Internal error. |
X
add doc  
xinking129 已提交
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
| 16200001 | The caller has been released.        |

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

**示例:**

  ```ts
  let caller;

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

  // 指定启动的Ability
  let want = {
      bundleName: 'com.acts.actscalleeabilityrely',
      moduleName: 'entry',
Y
yuyaozhi 已提交
2772 2773
      abilityName: 'EntryAbility',
      deviceId: '',
X
xinking129 已提交
2774 2775 2776 2777
      parameters: {
        // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动
        'ohos.aafwk.param.callAbilityToForeground': true
      }
X
add doc  
xinking129 已提交
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
  };

  try {
    this.context.startAbilityByCallWithAccount(want, accountId)
      .then((obj) => {
        // 执行正常业务
        caller = obj;
        console.log('startAbilityByCallWithAccount succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
        console.error('startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
  }
2794 2795
  ```

D
donglin 已提交
2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
## UIAbilityContext.startAbilityAsCaller<sup>10+<sup>

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

使用设置的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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| 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';
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    // 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>

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

使用设置的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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| 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. |
| 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';
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    // 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, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.log('startAbilityAsCaller success.');
      }
    })
  }
}

```

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

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

使用设置的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

**参数:**

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| 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';
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    // 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.');
      })
      .catch((err) => {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      })
  }
}

```

Y
yuyaozhi 已提交
3018
## UIAbilityContext.reportDrawnCompleted<sup>10+</sup>
3019

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

X
xinking129 已提交
3022
当页面加载完成(loadContent成功)时,为开发者提供打点功能(callback形式)。
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
 **系统能力**: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 已提交
3043 3044 3045 3046 3047
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
X
xinking129 已提交
3048
    windowStage.loadContent('pages/Index', (err, data) => {
Y
yuyaozhi 已提交
3049 3050 3051 3052 3053 3054 3055
      if (err.code) {
        return;
      }
      try {
        this.context.reportDrawnCompleted((err) => {
          if (err.code) {
            // 处理业务逻辑错误
X
xinking129 已提交
3056
            console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`);
Y
yuyaozhi 已提交
3057 3058 3059 3060 3061 3062 3063 3064 3065
            return;
          }
          // 执行正常业务
          console.info('reportDrawnCompleted succeed');
        });
      } catch (err) {
        // 捕获同步的参数错误
        console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`);
      }
3066
    });
X
xinking129 已提交
3067
    console.log("MainAbility onWindowStageCreate")
Y
yuyaozhi 已提交
3068 3069
  }
};
U
unknown 已提交
3070
  ```