js-apis-inner-application-uiAbilityContext.md 79.9 KB
Newer Older
1 2
# UIAbilityContext

3
**UIAbilityContext**, inherited from [Context](js-apis-inner-application-context.md), provides the context environment for [UIAbility](js-apis-app-ability-uiAbility.md). **UIAbilityContext** provides UIAbility-related configuration and APIs for operating UIAbilities and ServiceExtensionAbilities. For example, you can use the APIs to start a UIAbility, terminate a UIAbility to which the UIAbilityContext belongs, and start, terminate, connect to, or disconnect from a ServiceExtensionAbility.
4 5 6 7 8 9 10 11 12 13 14 15

> **NOTE**
>
>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>  - The APIs of this module can be used only in the stage model.

## Attributes

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
16 17 18 19 20
| abilityInfo | [AbilityInfo](js-apis-bundleManager-abilityInfo.md) | Yes| No| UIAbility information.|
| currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | Yes| No| HAP information.|
| config | [Configuration](js-apis-app-ability-configuration.md) | Yes| No| UIAbility configuration, such as the language and color mode.|

> **NOTE**
21 22
>
> In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
23

24
## UIAbilityContext.startAbility
25 26 27 28 29

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

Starts an ability. This API uses an asynchronous callback to return the result.

30 31 32 33 34
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

35 36 37 38 39 40 41 42 43 44 45 46 47
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
61 62 63 64

**Example**

  ```ts
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

try {
  this.context.startAbility(want, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbility succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
84 85
  ```

86
## UIAbilityContext.startAbility
87 88 89 90 91

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

Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.

92 93 94 95 96
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

97 98 99 100 101 102 103 104 105 106 107 108
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|

**Error codes**

109
| ID| Error Message|
110
| ------- | -------------------------------- |
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. |
124 125 126 127

**Example**

  ```ts
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};

try {
  this.context.startAbility(want, options, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbility succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
151 152
  ```

153
## UIAbilityContext.startAbility
154 155 156 157 158

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

Starts an ability. This API uses a promise to return the result.

159 160 161 162 163
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
196 197 198 199

**Example**

  ```ts
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};

try {
  this.context.startAbility(want, options)
    .then(() => {
      // Carry out normal service processing.
      console.info('startAbility succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
}
222 223
  ```

224
## UIAbilityContext.startAbilityForResult
225 226 227

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

228 229 230 231
Starts an ability. This API uses an asynchronous callback to return the result when the ability is terminated. The following situations may be possible for a started ability:
 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller.
 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller.
 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others.
232 233 234 235 236

Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
237 238 239 240 241 242 243 244 245 246 247 248 249 250

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
264 265 266 267

**Example**

  ```ts
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

try {
  this.context.startAbilityForResult(want, (err, result) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbilityForResult succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
288 289
  ```

290
## UIAbilityContext.startAbilityForResult
291 292 293

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

294 295 296 297
Starts an ability with the start options specified. This API uses an asynchronous callback to return the result when the ability is terminated. The following situations may be possible for a started ability:
 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller.
 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller.
 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others.
298 299 300 301 302

Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
331 332 333 334

**Example**

  ```ts
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};

try {
  this.context.startAbilityForResult(want, options, (err, result) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbilityForResult succeed');
  });
} catch (paramError) {
  // Process input parameter errors.
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
358 359 360
  ```


361
## UIAbilityContext.startAbilityForResult
362 363 364

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

365 366 367 368
Starts an ability. This API uses a promise to return the result when the ability is terminated. The following situations may be possible to an ability after it is started:
 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller.
 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller.
 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others.
369 370 371 372 373

Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|


**Return value**

| Type| Description|
| -------- | -------- |
| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
408 409 410 411

**Example**

  ```ts
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};

try {
  this.context.startAbilityForResult(want, options)
    .then((result) => {
      // Carry out normal service processing.
      console.info('startAbilityForResult succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
}
434 435
  ```

436
## UIAbilityContext.startAbilityForResultWithAccount
437 438 439

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

440 441 442 443 444 445
Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result when the ability is terminated.

Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
446

447
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
448 449 450 451 452 453 454 455 456 457

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
458 459
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
| callback | AsyncCallback&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Yes| Callback used to return the result.|
460 461 462 463 464

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
478 479 480 481

**Example**

  ```ts
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;

try {
  this.context.startAbilityForResultWithAccount(want, accountId, (err, result) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
503 504 505
  ```


506
## UIAbilityContext.startAbilityForResultWithAccount
507 508 509

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

510
Starts an ability with the start options and account ID specified. This API uses an asynchronous callback to return the result when the ability is terminated.
511

512 513 514 515 516 517
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
518 519 520 521 522 523 524 525 526 527

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
528
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
529
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
530
| callback | AsyncCallback\<void\> | Yes| Callback invoked when the ability is terminated.|
531 532 533 534 535

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
549 550 551 552

**Example**

  ```ts
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};

try {
  this.context.startAbilityForResultWithAccount(want, accountId, options, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbilityForResultWithAccount succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
577 578 579
  ```


580
## UIAbilityContext.startAbilityForResultWithAccount
581 582 583

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

584
Starts an ability with the account ID specified. This API uses a promise to return the result when the ability is terminated.
585

586 587 588 589 590 591
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
592 593 594 595 596 597 598 599 600 601

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
602
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
603 604 605 606 607 608
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|

**Return value**

| Type| Description|
| -------- | -------- |
609
| Promise&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Promise used to return the ability result when the ability is terminated.|
610 611 612 613 614

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
628 629 630 631

**Example**

  ```ts
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};

try {
  this.context.startAbilityForResultWithAccount(want, accountId, options)
    .then((result) => {
      // Carry out normal service processing.
      console.info('startAbilityForResultWithAccount succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
656
  ```
657
## UIAbilityContext.startServiceExtensionAbility
658 659 660

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

661
Starts a ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
662 663 664 665 666 667 668 669 670

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
671
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
672 673 674 675 676 677
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
686 687 688 689

**Example**

  ```ts
690 691 692 693 694 695 696 697 698
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};

try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
699
      // Carry out normal service processing.
700 701 702 703 704
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
705
    });
706 707 708 709
} catch (err) {
  // Process input parameter errors.
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
710 711
  ```

712
## UIAbilityContext.startServiceExtensionAbility
713 714 715

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

716
Starts a ServiceExtensionAbility. This API uses a promise to return the result.
717 718 719 720 721 722 723 724 725

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
726
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
727 728 729 730 731

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
740 741 742 743

**Example**

  ```ts
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};

try {
  this.context.startServiceExtensionAbility(want)
    .then(() => {
      // Carry out normal service processing.
      console.info('startServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (paramError) {
  // Process input parameter errors.
  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
764 765
  ```

766
## UIAbilityContext.startServiceExtensionAbilityWithAccount
767 768 769

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

770
Starts a ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result.
771

772
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
773 774 775 776 777 778 779 780 781

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
782 783
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
784 785 786 787 788 789
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
798 799 800 801

**Example**

  ```ts
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;

try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
823 824
  ```

825
## UIAbilityContext.startServiceExtensionAbilityWithAccount
826 827 828

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

829
Starts a ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result.
830

831
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
832 833 834 835 836 837 838 839 840 841

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
842
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
843 844 845 846 847

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
856 857 858 859

**Example**

  ```ts
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;

try {
  this.context.startServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // Carry out normal service processing.
      console.info('startServiceExtensionAbilityWithAccount succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
881
  ```
882
## UIAbilityContext.stopServiceExtensionAbility
883 884 885

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

886
Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
887 888 889 890 891 892 893 894 895

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
896
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
897 898 899 900 901 902
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
910 911 912 913

**Example**

  ```ts
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};

try {
  this.context.stopServiceExtensionAbility(want, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('stopServiceExtensionAbility succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
934 935
  ```

936
## UIAbilityContext.stopServiceExtensionAbility
937 938 939

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

940
Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
941 942 943 944 945 946 947 948 949

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
950
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
951 952 953 954 955

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
963 964 965 966

**Example**

  ```ts
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};

try {
  this.context.stopServiceExtensionAbility(want)
    .then(() => {
      // Carry out normal service processing.
      console.info('stopServiceExtensionAbility succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
987 988
  ```

989
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
990 991 992

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

993
Stops a ServiceExtensionAbility with the account ID specified in the same application. This API uses an asynchronous callback to return the result.
994

995
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
996 997 998 999 1000 1001 1002 1003 1004

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1005 1006
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
1007 1008 1009 1010 1011 1012
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1020 1021 1022 1023

**Example**

  ```ts
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;

try {
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('stopServiceExtensionAbilityWithAccount succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
1045 1046
  ```

1047
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
1048 1049 1050

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

1051
Stops a ServiceExtensionAbility with the account ID specified in the same application. This API uses a promise to return the result.
1052

1053
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
1054 1055 1056 1057 1058 1059 1060 1061 1062

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1063 1064
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
1065 1066 1067 1068 1069

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1077 1078 1079 1080

**Example**

  ```ts
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'ServiceExtensionAbility'
};
let accountId = 100;

try {
  this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
    .then(() => {
      // Carry out normal service processing.
      console.info('stopServiceExtensionAbilityWithAccount succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
1102 1103
  ```

1104
## UIAbilityContext.terminateSelf
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

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

Terminates this ability. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1128 1129 1130 1131

**Example**

  ```ts
1132
  try {
1133 1134
    this.context.terminateSelf((err) => {
      if (err.code) {
1135
        // Process service logic errors.
1136
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1137 1138 1139
        return;
      }
      // Carry out normal service processing.
1140
      console.info('terminateSelf succeed');
1141
    });
1142
  } catch (err) {
1143
    // Capture the synchronization parameter error.
1144
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1145
  }
1146 1147 1148
  ```


1149
## UIAbilityContext.terminateSelf
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

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

Terminates this ability. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1173 1174 1175 1176

**Example**

  ```ts
1177 1178 1179 1180
  try {
    this.context.terminateSelf()
      .then(() => {
        // Carry out normal service processing.
1181
        console.info('terminateSelf succeed');
1182
      })
1183
      .catch((err) => {
1184
        // Process service logic errors.
1185
        console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1186 1187 1188
      });
  } catch (error) {
    // Capture the synchronization parameter error.
1189
    console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
1190
  }
1191 1192 1193
  ```


1194
## UIAbilityContext.terminateSelfWithResult
1195 1196 1197

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

1198
Terminates this ability. If the ability is started by calling [startAbilityForResult](#uiabilitycontextstartabilityforresult), the result is returned to the caller in the form of an asynchronous callback when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called.
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1219 1220 1221 1222

**Example**

  ```ts
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// AbilityResult information returned to the caller.
let abilityResult = {
  want,
  resultCode
};

try {
  this.context.terminateSelfWithResult(abilityResult, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('terminateSelfWithResult succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
}
1248 1249 1250
  ```


1251
## UIAbilityContext.terminateSelfWithResult
1252 1253 1254

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

1255
Terminates this ability. If the ability is started by calling [startAbilityForResult](#uiabilitycontextstartabilityforresult), the result is returned to the caller in the form of a promise when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called.
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1281 1282 1283 1284

**Example**

  ```ts
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let resultCode = 100;
// AbilityResult information returned to the caller.
let abilityResult = {
  want,
  resultCode
};

try {
  this.context.terminateSelfWithResult(abilityResult)
    .then(() => {
      // Carry out normal service processing.
      console.info('terminateSelfWithResult succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
}
1310 1311
  ```

1312
## UIAbilityContext.connectServiceExtensionAbility
1313 1314 1315

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

1316
Connects this ability to an ability that uses the **AbilityInfo.AbilityType.SERVICE** template.
1317 1318 1319 1320 1321 1322 1323

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1324 1325
| want | [Want](js-apis-application-want.md) | Yes| Want information for connecting to the ServiceExtensionAbility.|
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Instance of the callback function after the connection to the ServiceExtensionAbility is set up.|
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

**Return value**

| Type| Description|
| -------- | -------- |
| number | Result code of the ability connection.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
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. |
1341 1342 1343 1344

**Example**

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

let connection = null;
try {
  connection = this.context.connectServiceExtensionAbility(want, options);
} catch (err) {
  // Process input parameter errors.
  console.error(`connectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
1369 1370 1371
  ```


1372
## UIAbilityContext.connectServiceExtensionAbilityWithAccount
1373 1374 1375

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

1376
Connects this ability to an ability that uses the **AbilityInfo.AbilityType.SERVICE** template, with the account ID specified.
1377

1378
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1389 1390
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Instance of the callback function after the connection to the ServiceExtensionAbility is set up.|
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

**Return value**

| Type| Description|
| -------- | -------- |
| number | Result code of the ability connection.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1402 1403 1404 1405
| 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. |
1406 1407 1408 1409

**Example**

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

let connection = null;
try {
  connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
} catch (err) {
  // Process input parameter errors.
  console.error(`connectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
1435 1436
  ```

1437
## UIAbilityContext.disconnectServiceExtensionAbility
1438 1439 1440

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

1441
Disconnects from a ServiceExtensionAbility. This API uses a promise to return the result.
1442 1443 1444 1445 1446 1447 1448

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1449
| connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.|
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1461 1462 1463 1464
| 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. |
1465 1466 1467 1468

**Example**

  ```ts
1469 1470
// connection is the return value of connectServiceExtensionAbility.
let connection = 1;
1471

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
1486 1487
  ```

1488
## UIAbilityContext.disconnectServiceExtensionAbility
1489 1490 1491

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

1492
Disconnects from a ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
1493 1494 1495 1496 1497 1498 1499

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1500
| connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.|
1501 1502 1503 1504 1505 1506
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1507 1508 1509 1510
| 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. |
1511 1512 1513 1514

**Example**

  ```ts
1515 1516
// connection is the return value of connectServiceExtensionAbility.
let connection = 1;
1517

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
try {
  this.context.disconnectServiceExtensionAbility(connection, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('disconnectServiceExtensionAbility succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
}
1532 1533
  ```

1534
## UIAbilityContext.startAbilityByCall
1535 1536 1537 1538 1539

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

Starts an ability in the foreground or background and obtains the caller object for communicating with the ability.

1540 1541 1542 1543 1544
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - The rules for using this API in the same-device and cross-device scenarios are different. For details, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId** (optional), and **parameters** (optional). If **deviceId** is left blank or null, the local ability is started. If **parameters** is left blank or null, the ability is started in the background.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 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. |

1577 1578 1579 1580 1581
**Example**

  Start an ability in the background.

  ```ts
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
let caller;

// Start an ability in the background by not passing parameters.
let wantBackground = {
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: ''
};

try {
  this.context.startAbilityByCall(wantBackground)
    .then((obj) => {
      // Carry out normal service processing.
      caller = obj;
      console.info('startAbilityByCall succeed');
    }).catch((err) => {
    // Process service logic errors.
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
}
1606 1607
  ```

1608
Start an ability in the foreground.
1609 1610

  ```ts
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
let caller;

// Start an ability in the foreground with ohos.aafwk.param.callAbilityToForeground in parameters set to true.
let wantForeground = {
  bundleName: 'com.example.myapplication',
  moduleName: 'entry',
  abilityName: 'EntryAbility',
  deviceId: '',
  parameters: {
    'ohos.aafwk.param.callAbilityToForeground': true
1621
  }
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
};

try {
  this.context.startAbilityByCall(wantForeground)
    .then((obj) => {
      // Carry out normal service processing.
      caller = obj;
      console.info('startAbilityByCall succeed');
    }).catch((error) => {
    // Process service logic errors.
    console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
  });
} catch (paramError) {
  // Process input parameter errors.
  console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`);
}
1638 1639
  ```

1640
## UIAbilityContext.startAbilityWithAccount
1641 1642 1643 1644 1645

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

Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result.

1646 1647 1648 1649 1650 1651
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1662
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
1663 1664 1665 1666 1667 1668
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
| 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. |
1682 1683 1684 1685

**Example**

  ```ts
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;

try {
  this.context.startAbilityWithAccount(want, accountId, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
1707 1708 1709
  ```


1710
## UIAbilityContext.startAbilityWithAccount
1711 1712 1713 1714 1715

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

Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.

1716 1717 1718 1719 1720 1721
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1732 1733
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
1734 1735 1736 1737 1738 1739
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
| 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. |
1753 1754 1755 1756

**Example**

  ```ts
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};

try {
  this.context.startAbilityWithAccount(want, accountId, options, (err) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('startAbilityWithAccount succeed');
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
1781 1782 1783
  ```


1784
## UIAbilityContext.startAbilityWithAccount
1785 1786 1787 1788 1789

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

Starts an ability with the account ID specified. This API uses a promise to return the result.

1790 1791 1792 1793 1794 1795
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1806
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount).|
1807 1808 1809 1810 1811 1812
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
| 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. |
1826 1827 1828 1829

**Example**

  ```ts
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let accountId = 100;
let options = {
  windowMode: 0
};

try {
  this.context.startAbilityWithAccount(want, accountId, options)
    .then(() => {
      // Carry out normal service processing.
      console.info('startAbilityWithAccount succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`);
}
1854 1855
  ```

1856
## UIAbilityContext.setMissionLabel
1857 1858 1859

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

1860
Sets a label for this UIAbility in the mission. This API uses an asynchronous callback to return the result.
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Example**

  ```ts
1874 1875
  this.context.setMissionLabel('test', (result) => {
    console.info(`setMissionLabel: ${JSON.stringify(result)}`);
1876 1877 1878
  });
  ```

1879
## UIAbilityContext.setMissionLabel
1880 1881 1882

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

1883
Sets a label for this UIAbility in the mission. This API uses a promise to return the result.
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

1899 1900 1901 1902 1903 1904 1905
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

1906 1907 1908
**Example**

  ```ts
1909 1910 1911 1912
  this.context.setMissionLabel('test').then(() => {
    console.info('success');
  }).catch((err) => {
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
1913 1914
  });
  ```
1915
## UIAbilityContext.setMissionIcon
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931

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

Sets an icon for this ability in the mission. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | Yes| Icon of the ability to set.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

1932 1933 1934 1935 1936 1937 1938
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

1939 1940 1941
**Example**

  ```ts
1942
  import image from '@ohos.multimedia.image';
1943 1944 1945 1946
  
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
1947 1948 1949 1950 1951 1952 1953 1954
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
1955
    })
1956
    .catch((err) => {
1957
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
1958 1959
    });
  this.context.setMissionIcon(imagePixelMap, (err) => {
1960
    console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
1961
  })
1962 1963 1964
  ```


1965
## UIAbilityContext.setMissionIcon
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986

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

Sets an icon for this ability in the mission. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | Yes| Icon of the ability to set.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

1987 1988 1989 1990 1991 1992 1993
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

1994 1995 1996
**Example**

  ```ts
1997 1998 1999
  let imagePixelMap;
  let color = new ArrayBuffer(0);
  let initializationOptions = {
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
    })
    .catch((err) => {
2010
      console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`);
2011 2012 2013
    });
  this.context.setMissionIcon(imagePixelMap)
    .then(() => {
2014
      console.info('setMissionIcon succeed');
2015 2016
    })
    .catch((err) => {
2017
      console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`);
2018
    });
2019
  ```
2020
## UIAbilityContext.restoreWindowStage
2021 2022 2023

restoreWindowStage(localStorage: LocalStorage) : void;

2024
Restores the WindowStage data in the UIAbility.
2025 2026 2027 2028 2029 2030 2031 2032 2033

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| localStorage | image.LocalStorage | Yes| Storage used to store the restored window stage.|

2034 2035 2036 2037 2038 2039 2040
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |

2041 2042 2043
**Example**

  ```ts
2044
  let storage = new LocalStorage();
2045
  this.context.restoreWindowStage(storage);
2046 2047
  ```

2048
## UIAbilityContext.isTerminating
2049 2050 2051

isTerminating(): boolean;

2052
Checks whether this UIAbility is in the terminating state.
2053 2054 2055 2056 2057 2058 2059

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type| Description|
| -------- | -------- |
2060 2061 2062 2063 2064 2065 2066 2067
| boolean| The value **true** means that the UIAbility is in the terminating state, and **false** means the opposite.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
2068 2069 2070 2071

**Example**

  ```ts
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 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 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
  let isTerminating = this.context.isTerminating();
  console.info(`ability state is ${isTerminating}`);
  ```

## UIAbilityContext.requestDialogService

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

Starts a ServiceExtensionAbility that supports modal dialog boxes. After the ServiceExtensionAbility is started, the application displays a modal dialog box. You can call [setRequestResult](js-apis-app-ability-dialogRequest.md#requestcallbacksetrequestresult) to obtain the result.

Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|
| result | AsyncCallback&lt;[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md)&gt; | Yes| Callback used to return the result.|

**Example**

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

let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
};

try {
  this.context.requestDialogService(want, (err, result) => {
    if (err.code) {
      // Process service logic errors.
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // Carry out normal service processing.
    console.info('requestDialogService succeed, result = ' + JSON.stringify(result));
  });
} catch (err) {
  // Process input parameter errors.
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
}
  ```

  ## UIAbilityContext.requestDialogService

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

Starts a ServiceExtensionAbility that supports modal dialog boxes. After the ServiceExtensionAbility is started, the application displays a modal dialog box. You can call [setRequestResult](js-apis-app-ability-dialogRequest.md#requestcallbacksetrequestresult) to obtain the result, which is returned to the caller in promise mode.

Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ServiceExtensionAbility.|


**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md)&gt; | Promise used to return the result.

**Example**

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

let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'AuthAccountServiceExtension'
};

try {
  this.context.requestDialogService(want)
    .then((result) => {
      // Carry out normal service processing.
      console.info('requestDialogService succeed, result = ' + JSON.stringify(result));
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`);
}
2173
  ```