js-apis-inner-application-uiAbilityContext.md 75.4 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 10 11 12 13

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

## 属性

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

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

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

Z
zhongjianfei 已提交
24
## UIAbilityContext.startAbility
M
m00512953 已提交
25 26 27 28 29

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

启动Ability(callback形式)。

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

M
m00512953 已提交
35 36 37 38 39 40 41
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
42
| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 |
M
m00512953 已提交
43 44 45 46 47

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
61 62 63 64

**示例:**

  ```ts
65 66 67 68
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83
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 已提交
84 85
  ```

Z
zhongjianfei 已提交
86
## UIAbilityContext.startAbility
M
m00512953 已提交
87 88 89 90 91

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

启动Ability(callback形式)。

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

M
m00512953 已提交
97 98 99 100 101 102 103 104 105 106 107 108
**系统能力**: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 已提交
109
| 错误码ID | 错误信息 |
M
m00512953 已提交
110
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
124 125 126 127

**示例:**

  ```ts
128 129 130 131 132 133 134 135
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};
M
m00512953 已提交
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150
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 已提交
151 152
  ```

Z
zhongjianfei 已提交
153
## UIAbilityContext.startAbility
M
m00512953 已提交
154 155 156 157 158

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

启动Ability(promise形式)。

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

M
m00512953 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
**系统能力**: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 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
196 197 198 199

**示例:**

  ```ts
200 201 202 203 204 205 206
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};
M
m00512953 已提交
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221
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 已提交
222 223
  ```

Z
zhongjianfei 已提交
224
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
225 226 227

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

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

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

M
m00512953 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250
**系统能力**: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 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
264 265 266 267

**示例:**

  ```ts
268 269 270 271 272
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
M
m00512953 已提交
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287
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 已提交
288 289
  ```

Z
zhongjianfei 已提交
290
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
291 292 293

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

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

M
m00512953 已提交
299 300
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
M
m00512953 已提交
301
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
302
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
M
m00512953 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

**系统能力**: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 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
331 332 333 334

**示例:**

  ```ts
335 336 337 338 339 340 341 342
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};
M
m00512953 已提交
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357
try {
  this.context.startAbilityForResult(want, options, (err, result) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startAbilityForResult succeed');
  });
} catch (paramError) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
358 359 360
  ```


Z
zhongjianfei 已提交
361
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
362 363 364

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

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

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

M
m00512953 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
**系统能力**: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 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
408 409 410 411

**示例:**

  ```ts
Z
zhangyafei.echo 已提交
412
let want = {
413 414
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
415
};
Z
zhangyafei.echo 已提交
416
let options = {
417
  windowMode: 0,
418
};
M
m00512953 已提交
419

420
try {
421 422 423 424
  this.context.startAbilityForResult(want, options)
    .then((result) => {
      // 执行正常业务
      console.info('startAbilityForResult succeed');
425
    })
426 427 428
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
429
    });
430 431 432
} catch (err) {
  // 处理入参错误异常
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
433
}
M
m00512953 已提交
434 435
  ```

Z
zhongjianfei 已提交
436
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
437 438 439

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

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

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

D
donglin 已提交
447
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
448 449 450 451 452 453 454 455 456 457

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
478 479 480 481

**示例:**

  ```ts
482 483 484 485 486 487
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502
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 已提交
503 504 505
  ```


Z
zhongjianfei 已提交
506
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
507 508 509

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

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

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

D
donglin 已提交
517
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
518 519 520 521 522 523 524 525 526 527

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
549 550 551 552

**示例:**

  ```ts
553 554 555 556 557 558 559 560 561
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
562

563 564 565 566 567 568 569 570 571 572 573 574 575 576
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 已提交
577 578 579
  ```


Z
zhongjianfei 已提交
580
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
581 582 583

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

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

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

D
donglin 已提交
591
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
592 593 594 595 596 597 598 599 600 601

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

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

**参数:**

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
609
| Promise&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Ability被销毁时的回调函数,包含AbilityResult参数。 |
M
m00512953 已提交
610 611 612 613 614

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
615 616 617 618 619 620 621 622 623 624 625 626 627
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
628 629 630 631

**示例:**

  ```ts
632 633 634 635 636 637 638 639 640
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
641

642 643 644 645 646 647 648 649 650 651 652 653 654 655
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 已提交
656
  ```
Z
zhongjianfei 已提交
657
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
671 672
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
673 674 675 676 677

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
678 679 680 681 682 683 684 685
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
686 687 688 689

**示例:**

  ```ts
690 691 692 693 694
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
695

696 697 698
try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
M
m00512953 已提交
699
      // 执行正常业务
700 701 702 703 704
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
705
    });
706 707 708 709
} catch (err) {
  // 处理入参错误异常
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
710 711
  ```

Z
zhongjianfei 已提交
712
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725

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

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

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
732 733 734 735 736 737 738 739
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
740 741 742 743

**示例:**

  ```ts
744 745 746 747 748
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
749

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

Z
zhongjianfei 已提交
766
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
767 768 769 770 771

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

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

D
donglin 已提交
772
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
773 774 775 776 777 778 779 780 781

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
782
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
783
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
784
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
785 786 787 788 789

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
790 791 792 793 794 795 796 797
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
798 799 800 801

**示例:**

  ```ts
802 803 804 805 806 807
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
808

809 810 811 812 813 814 815 816 817 818 819 820 821 822
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 已提交
823 824
  ```

Z
zhongjianfei 已提交
825
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
826 827 828 829 830

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

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

D
donglin 已提交
831
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
832 833 834 835 836 837 838 839 840 841

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
848 849 850 851 852 853 854 855
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
856 857 858 859

**示例:**

  ```ts
860 861 862 863 864 865
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880
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 已提交
881
  ```
Z
zhongjianfei 已提交
882
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
896 897
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
898 899 900 901 902

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
903 904 905 906 907 908 909
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
910 911 912 913

**示例:**

  ```ts
914 915 916 917 918
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
919

920 921 922 923 924 925 926 927 928 929 930 931 932 933
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 已提交
934 935
  ```

Z
zhongjianfei 已提交
936
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
950
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
951 952 953 954 955

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
956 957 958 959 960 961 962
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
963 964 965 966

**示例:**

  ```ts
967 968 969 970 971
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
M
m00512953 已提交
972

973 974 975 976 977 978 979 980 981 982 983 984 985 986
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 已提交
987 988
  ```

Z
zhongjianfei 已提交
989
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
990 991 992

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

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

D
donglin 已提交
995
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
996 997 998 999 1000 1001 1002 1003 1004

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1005 1006 1007
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
1008 1009 1010 1011 1012

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1013 1014 1015 1016 1017 1018 1019
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1020 1021 1022 1023

**示例:**

  ```ts
1024 1025 1026 1027 1028 1029
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1030

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
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 已提交
1045 1046
  ```

Z
zhongjianfei 已提交
1047
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1048 1049 1050

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

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

D
donglin 已提交
1053
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1063 1064
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1065 1066 1067 1068 1069

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1070 1071 1072 1073 1074 1075 1076
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1077 1078 1079 1080

**示例:**

  ```ts
1081 1082 1083 1084 1085 1086
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
M
m00512953 已提交
1087

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
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 已提交
1102 1103
  ```

Z
zhongjianfei 已提交
1104
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

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

停止Ability自身(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1116
| callback | AsyncCallback&lt;void&gt; | 是 | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1117 1118 1119 1120 1121

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1122 1123 1124 1125 1126 1127
| 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 已提交
1128 1129 1130 1131

**示例:**

  ```ts
1132
  try {
1133 1134
    this.context.terminateSelf((err) => {
      if (err.code) {
1135
        // 处理业务逻辑错误
1136
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1137 1138 1139
        return;
      }
      // 执行正常业务
1140
      console.info('terminateSelf succeed');
1141
    });
1142
  } catch (err) {
1143
    // 捕获同步的参数错误
1144
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1145
  }
M
m00512953 已提交
1146 1147 1148
  ```


Z
zhongjianfei 已提交
1149
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

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

停止Ability自身(promise形式)。

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
1161
| Promise&lt;void&gt; | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1162 1163 1164 1165 1166

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1167 1168 1169 1170 1171 1172
| 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 已提交
1173 1174 1175 1176

**示例:**

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


Z
zhongjianfei 已提交
1194
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1195 1196 1197

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

M
m00512953 已提交
1198
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(callback形式)。
M
m00512953 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1213 1214 1215 1216 1217 1218
| 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 已提交
1219 1220 1221 1222

**示例:**

  ```ts
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
let abilityResult = {
  want,
  resultCode
};
M
m00512953 已提交
1233

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
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 已提交
1248 1249 1250
  ```


Z
zhongjianfei 已提交
1251
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1252 1253 1254

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

M
m00512953 已提交
1255
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(promise形式)。
M
m00512953 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

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

**参数:**

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

**返回值:**

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

**错误码:**

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

**示例:**

  ```ts
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// 返回给接口调用方AbilityResult信息
let abilityResult = {
  want,
  resultCode
};
M
m00512953 已提交
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
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 已提交
1310 1311
  ```

Z
zhongjianfei 已提交
1312
## UIAbilityContext.connectServiceExtensionAbility
M
m00512953 已提交
1313 1314 1315

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

1316
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的Ability。
M
m00512953 已提交
1317 1318 1319 1320 1321 1322 1323

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1324 1325
| want | [Want](js-apis-application-want.md) | 是 | 连接ServiceExtensionAbility的want信息。 |
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 |
M
m00512953 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1337 1338 1339 1340
| 16000001 | The specified ability does not exist. |
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1341 1342 1343 1344

**示例:**

  ```ts
1345 1346 1347 1348 1349 1350 1351
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let options = {
  onConnect(elementName, remote) {
M
mingxihua 已提交
1352
    commRemote = remote;
1353 1354 1355 1356 1357 1358 1359
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1360
  }
1361 1362 1363 1364 1365 1366 1367 1368 1369
};

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 已提交
1370 1371 1372
  ```


Z
zhongjianfei 已提交
1373
## UIAbilityContext.connectServiceExtensionAbilityWithAccount
M
m00512953 已提交
1374 1375 1376

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

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

D
donglin 已提交
1379
**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1390
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
1391
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。。 |
M
m00512953 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1403 1404 1405 1406
| 16000001 | The specified ability does not exist. |
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1407 1408 1409 1410

**示例:**

  ```ts
1411 1412 1413 1414 1415 1416 1417 1418
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;
let options = {
  onConnect(elementName, remote) {
M
mingxihua 已提交
1419
    commRemote = remote;
1420 1421 1422 1423 1424 1425 1426
    console.info('onConnect...')
  },
  onDisconnect(elementName) {
    console.info('onDisconnect...')
  },
  onFailed(code) {
    console.info('onFailed...')
M
m00512953 已提交
1427
  }
1428 1429 1430 1431 1432 1433 1434 1435 1436
};

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 已提交
1437 1438
  ```

Z
zhongjianfei 已提交
1439
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1440 1441 1442

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

M
mingxihua 已提交
1443
断开与ServiceExtensionAbility的连接,断开连接之后需要将连接成功时返回的remote对象置空(promise形式)。
M
m00512953 已提交
1444 1445 1446 1447 1448 1449 1450

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1451
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
M
m00512953 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1463 1464 1465 1466
| 16000001 | The specified ability does not exist. |
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1467 1468 1469 1470

**示例:**

  ```ts
1471 1472
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
M
m00512953 已提交
1473

1474 1475
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
M
mingxihua 已提交
1476
    commRemote = null;
1477 1478 1479 1480 1481 1482 1483 1484 1485
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
M
mingxihua 已提交
1486
  commRemote = null;
1487 1488 1489
  // 处理入参错误异常
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
M
m00512953 已提交
1490 1491
  ```

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1504
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
1505
| callback | AsyncCallback\<void> | 是 | callback形式返回断开连接的结果。 |
M
m00512953 已提交
1506 1507 1508 1509 1510

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1511 1512 1513 1514
| 16000001 | The specified ability does not exist. |
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
M
m00512953 已提交
1515 1516 1517 1518

**示例:**

  ```ts
1519 1520
// connection为connectServiceExtensionAbility中的返回值
let connection = 1;
M
m00512953 已提交
1521

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

Z
zhongjianfei 已提交
1540
## UIAbilityContext.startAbilityByCall
M
m00512953 已提交
1541 1542 1543 1544 1545

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

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

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

M
m00512953 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
**系统能力**: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 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

M
m00512953 已提交
1583 1584 1585 1586 1587
**示例:**

  后台启动:

  ```ts
1588 1589 1590 1591 1592 1593 1594 1595 1596
let caller;

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

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
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 已提交
1612 1613
  ```

1614
前台启动:
M
m00512953 已提交
1615 1616

  ```ts
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
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 已提交
1627
  }
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
};

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

Z
zhongjianfei 已提交
1646
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1647 1648 1649

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

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

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

D
donglin 已提交
1657
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1688 1689 1690 1691

**示例:**

  ```ts
1692 1693 1694 1695 1696 1697
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
M
m00512953 已提交
1698

1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
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 已提交
1713 1714 1715
  ```


Z
zhongjianfei 已提交
1716
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1717 1718 1719

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

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

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

D
donglin 已提交
1727
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1759 1760 1761 1762

**示例:**

  ```ts
1763 1764 1765 1766 1767 1768 1769 1770 1771
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
1772

1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
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 已提交
1787 1788 1789
  ```


Z
zhongjianfei 已提交
1790
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1791 1792 1793

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

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

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

D
donglin 已提交
1801
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811

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

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Z
zhangyafei.echo 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |
M
m00512953 已提交
1832 1833 1834 1835

**示例:**

  ```ts
1836 1837 1838 1839 1840 1841 1842 1843 1844
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};
M
m00512953 已提交
1845

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
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 已提交
1860 1861
  ```

Z
zhongjianfei 已提交
1862
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
1863 1864 1865

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

1866
设置UIAbility在任务中显示的名称(callback形式)。
M
m00512953 已提交
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879

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

**参数:**

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

**示例:**

  ```ts
M
mingxihua 已提交
1880
  this.context.setMissionLabel('test', (result) => {
1881
    console.info(`setMissionLabel: ${JSON.stringify(result)}`);
M
m00512953 已提交
1882 1883 1884
  });
  ```

Z
zhongjianfei 已提交
1885
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
1886 1887 1888

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

1889
设置UIAbility在任务中显示的名称(promise形式)。
M
m00512953 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904

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

**参数:**

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

**返回值:**

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

Z
zhangyafei.echo 已提交
1905 1906 1907 1908 1909 1910 1911
**错误码:**

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

M
m00512953 已提交
1912 1913 1914
**示例:**

  ```ts
M
mingxihua 已提交
1915
  this.context.setMissionLabel('test').then(() => {
1916 1917 1918
    console.info('success');
  }).catch((err) => {
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
M
m00512953 已提交
1919 1920
  });
  ```
Z
zhongjianfei 已提交
1921
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
1922 1923 1924

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

M
mingxihua 已提交
1925
设置当前ability在任务中显示的图标, 图标大小最大为600M(callback形式)。
M
m00512953 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937

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

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

**参数:**

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

Z
zhangyafei.echo 已提交
1938 1939 1940 1941 1942 1943 1944
**错误码:**

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

M
m00512953 已提交
1945 1946 1947
**示例:**

  ```ts
1948
  import image from '@ohos.multimedia.image';
1949
  
Z
zhangyafei.echo 已提交
1950 1951 1952
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
1953 1954 1955 1956 1957 1958 1959 1960
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
M
m00512953 已提交
1961
    })
1962
    .catch((err) => {
1963
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
1964 1965
    });
  this.context.setMissionIcon(imagePixelMap, (err) => {
1966
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
1967
  })
M
m00512953 已提交
1968 1969 1970
  ```


Z
zhongjianfei 已提交
1971
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
1972 1973 1974

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

M
mingxihua 已提交
1975
设置当前ability在任务中显示的图标, 图标大小最大为600M(promise形式)。
M
m00512953 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992

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

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

**参数:**

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

**返回值:**

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

Z
zhangyafei.echo 已提交
1993 1994 1995 1996 1997 1998 1999
**错误码:**

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

M
m00512953 已提交
2000 2001 2002
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2003 2004 2005
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
    })
    .catch((err) => {
2016
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
2017 2018 2019
    });
  this.context.setMissionIcon(imagePixelMap)
    .then(() => {
2020
      console.info('setMissionIcon succeed');
2021 2022
    })
    .catch((err) => {
2023
      console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
2024
    });
M
m00512953 已提交
2025
  ```
Z
zhongjianfei 已提交
2026
## UIAbilityContext.restoreWindowStage
M
m00512953 已提交
2027 2028 2029

restoreWindowStage(localStorage: LocalStorage) : void;

2030
恢复UIAbility中的WindowStage数据。
M
m00512953 已提交
2031 2032 2033 2034 2035 2036 2037 2038 2039

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

**参数:**

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

Z
zhangyafei.echo 已提交
2040 2041 2042 2043 2044 2045 2046
**错误码:**

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

M
m00512953 已提交
2047 2048 2049
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2050
  let storage = new LocalStorage();
2051
  this.context.restoreWindowStage(storage);
M
m00512953 已提交
2052 2053
  ```

Z
zhongjianfei 已提交
2054
## UIAbilityContext.isTerminating
M
m00512953 已提交
2055 2056 2057

isTerminating(): boolean;

2058
查询UIAbility是否在terminating状态。
M
m00512953 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067

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

**返回值:**

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

Z
zhangyafei.echo 已提交
2068 2069 2070 2071 2072 2073 2074
**错误码:**

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

M
m00512953 已提交
2075 2076 2077
**示例:**

  ```ts
Z
zhangyafei.echo 已提交
2078
  let isTerminating = this.context.isTerminating();
2079
  console.info(`ability state is ${isTerminating}`);
2080
  ```
C
caochunlei 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104

## 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`权限。
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
 - 组件启动规则详见:[组件启动规则(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; | 是 | 执行结果回调函数。 |

**示例:**

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

2107 2108 2109 2110 2111
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
};
C
caochunlei 已提交
2112

2113 2114 2115 2116 2117 2118 2119 2120
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 已提交
2121
    console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
2122 2123 2124 2125 2126
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
}
C
caochunlei 已提交
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
  ```

  ## 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`权限。
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。
 - 组件启动规则详见:[组件启动规则(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形式返回执行结果。

**示例:**

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

let want = {
2161 2162
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
C
caochunlei 已提交
2163 2164 2165
};

try {
2166 2167 2168
  this.context.requestDialogService(want)
    .then((result) => {
      // 执行正常业务
M
mingxihua 已提交
2169
      console.info('requestDialogService succeed, result = ${JSON.stringify(result)}');
C
caochunlei 已提交
2170
    })
2171 2172 2173
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2174
    });
2175 2176 2177
} catch (err) {
  // 处理入参错误异常
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
C
caochunlei 已提交
2178 2179
}
  ```