js-apis-inner-application-serviceExtensionContext.md 63.0 KB
Newer Older
W
wusongqing 已提交
1 2
# ServiceExtensionContext

3
The **ServiceExtensionContext** module, inherited from **ExtensionContext**, provides context for ServiceExtensionAbilities.
4

G
Gloria 已提交
5
You can use the APIs of this module to start, terminate, connect, and disconnect abilities.
6

W
wusongqing 已提交
7 8
> **NOTE**
> 
G
Gloria 已提交
9 10
>  - 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.
W
wusongqing 已提交
11

12 13 14 15 16 17
## Modules to Import

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

18
## Usage
W
wusongqing 已提交
19

20
Before using the **ServiceExtensionContext** module, you must define a child class that inherits from **ServiceExtensionAbility**.
W
wusongqing 已提交
21

G
Gloria 已提交
22 23 24
```ts
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

25
  let context;
G
Gloria 已提交
26
  let commRemote; // Release the instance when the connection is disconnected.
27
  class EntryAbility extends ServiceExtensionAbility {
G
Gloria 已提交
28
    onCreate() {
29
      context = this.context; // Obtain a ServiceExtensionContext instance.
G
Gloria 已提交
30
    }
31
  }
W
wusongqing 已提交
32
```
W
wusongqing 已提交
33

G
Gloria 已提交
34
## ServiceExtensionContext.startAbility
W
wusongqing 已提交
35 36 37

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

38
Starts an ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
39

W
wusongqing 已提交
40
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
41

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

W
wusongqing 已提交
44
**Parameters**
W
wusongqing 已提交
45

46 47 48
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
49
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
G
Gloria 已提交
50 51 52 53 54

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
55 56 57 58 59 60 61 62 63
| 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.        |
64 65
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
66 67 68 69 70 71
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
W
wusongqing 已提交
72

W
wusongqing 已提交
73 74
**Example**

G
Gloria 已提交
75
  ```ts
76 77 78
  let want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
G
Gloria 已提交
79 80 81 82 83 84
  };

  try {
    this.context.startAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
85
        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
86 87 88 89
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
90
    });
G
Gloria 已提交
91 92
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
93
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
94
  }
W
wusongqing 已提交
95 96
  ```

G
Gloria 已提交
97
## ServiceExtensionContext.startAbility
W
wusongqing 已提交
98

99
startAbility(want: Want, options?: StartOptions): Promise\<void>;
W
wusongqing 已提交
100

W
wusongqing 已提交
101
Starts an ability. This API uses a promise to return the result.
W
wusongqing 已提交
102

W
wusongqing 已提交
103
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
104

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

W
wusongqing 已提交
107
**Parameters**
W
wusongqing 已提交
108

109 110 111
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
112
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
W
wusongqing 已提交
113

W
wusongqing 已提交
114 115
**Return value**

116 117 118
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
119

G
Gloria 已提交
120 121 122 123
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
124 125 126 127 128 129 130 131 132
| 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.        |
133 134
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
135 136 137 138 139 140
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
141

W
wusongqing 已提交
142 143
**Example**

G
Gloria 已提交
144
  ```ts
145 146 147
  let want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
G
Gloria 已提交
148
  };
149
  let options = {
G
Gloria 已提交
150 151
  	windowMode: 0,
  };
152

G
Gloria 已提交
153 154 155 156 157 158 159 160
  try {
    this.context.startAbility(want, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
161
        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
162 163 164
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
165
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
166
  }
167 168
  ```

G
Gloria 已提交
169
## ServiceExtensionContext.startAbility
170 171 172

startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void

173
Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
174 175 176 177 178 179 180 181 182

**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|
| -------- | -------- | -------- | -------- |
183 184
| 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.|
185 186
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

G
Gloria 已提交
187 188 189 190
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
191 192 193 194 195 196 197 198 199
| 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.        |
200 201
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
202 203 204 205 206 207
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
208

209
**Example**
G
Gloria 已提交
210 211

  ```ts
212 213 214 215
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
216
  };
217
  let options = {
G
Gloria 已提交
218
    windowMode: 0
219
  };
G
Gloria 已提交
220 221 222 223 224

  try {
    this.context.startAbility(want, options, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
225
        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
226 227 228 229 230 231 232
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
233
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
234
  }
235 236 237 238 239 240 241 242
  ```

## ServiceExtensionContext.startAbilityWithAccount

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.

243 244
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.
245
 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
246 247
 - 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).

248 249
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

250 251 252 253 254 255 256 257
**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|
| -------- | -------- | -------- | -------- |
258
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
259
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
260 261
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
262 263 264 265
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
266 267 268 269 270 271 272 273 274
| 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.        |
275 276
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
277 278 279 280 281 282
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
283

284 285
**Example**

G
Gloria 已提交
286
  ```ts
287 288 289 290
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
291
  };
292
  let accountId = 100;
293

G
Gloria 已提交
294 295 296 297
  try {
    this.context.startAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
298
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
299 300 301 302 303 304 305
        return;
      }
      // Carry out normal service processing.
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
306
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
307 308
  }
  ```
309 310 311 312 313

## ServiceExtensionContext.startAbilityWithAccount

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

G
Gloria 已提交
314
Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.
315

316 317
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.
318
 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
319 320
 - 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).

321 322
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

323 324 325 326 327 328 329 330
**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|
| -------- | -------- | -------- | -------- |
331
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
332
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
333
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
334 335
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
336 337 338 339
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
340 341 342 343 344 345 346 347 348
| 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.        |
349 350
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
351 352 353 354 355 356
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
357

358 359
**Example**

G
Gloria 已提交
360
  ```ts
361 362 363 364
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
365
  };
366 367
  let accountId = 100;
  let options = {
G
Gloria 已提交
368
    windowMode: 0
369
  };
G
Gloria 已提交
370 371 372 373 374

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
375
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
376 377 378 379 380 381 382
        return;
      }
      // Carry out normal service processing.
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
383
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
384
  }
385 386 387 388 389 390 391 392 393
  ```


## ServiceExtensionContext.startAbilityWithAccount

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.

394 395
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.
396
 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
397 398
 - 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).

399 400
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

401 402 403 404 405 406 407 408
**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|
| -------- | -------- | -------- | -------- |
409
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
410
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
411
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
412

413 414
**Return value**

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

G
Gloria 已提交
419 420 421 422
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
423 424 425 426 427 428 429 430 431
| 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.        |
432 433
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
434 435 436 437 438 439
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
440

441 442
**Example**

G
Gloria 已提交
443
  ```ts
444 445 446 447
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
448
  };
449 450
  let accountId = 100;
  let options = {
G
Gloria 已提交
451
    windowMode: 0
452
  };
G
Gloria 已提交
453 454 455 456 457 458 459 460 461

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
462
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
463 464 465
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
466
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
467
  }
W
wusongqing 已提交
468 469
  ```

470 471 472 473
## ServiceExtensionContext.startServiceExtensionAbility

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

474
Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
475 476 477 478 479 480 481 482 483

**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|
| -------- | -------- | -------- | -------- |
484
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
485 486
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
487 488 489 490
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
491 492
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
493
| 16000004 | Can not start invisible component. |
494 495 496 497
| 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.        |
498 499
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
500 501 502 503
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
504

505 506
**Example**

G
Gloria 已提交
507
  ```ts
508 509 510 511
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
512
  };
G
Gloria 已提交
513 514 515 516 517

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
518
        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
519 520 521 522 523 524 525
        return;
      }
      // Carry out normal service processing.
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
526
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
527
  }
528 529 530 531 532 533
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

534
Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
535 536 537 538 539 540 541 542 543

**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|
| -------- | -------- | -------- | -------- |
544
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
545 546 547

**Return value**

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

G
Gloria 已提交
552 553 554 555
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
556 557
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
558
| 16000004 | Can not start invisible component. |
559 560 561 562
| 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.        |
563 564
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
565 566 567 568
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
569

570 571
**Example**

G
Gloria 已提交
572
  ```ts
573 574 575 576
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
577
  };
G
Gloria 已提交
578 579 580 581 582 583 584 585 586

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
587
        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
588 589 590
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
591
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
592
  }
593 594 595 596 597 598
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

599
Starts a new ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result.
600

601 602 603 604 605
> **NOTE**
> 
> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
606 607 608 609 610 611 612 613 614

**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|
| -------- | -------- | -------- | -------- |
615
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
616
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
617 618
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
619 620 621 622
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
623 624
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
625
| 16000004 | Can not start invisible component. |
626 627 628 629
| 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.        |
630 631
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
632 633 634 635
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
636 637


638 639
**Example**

G
Gloria 已提交
640
  ```ts
641 642 643 644
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
645
  };
646
  let accountId = 100;
G
Gloria 已提交
647 648 649 650 651

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
652
        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
653 654 655 656 657 658 659
        return;
      }
      // Carry out normal service processing.
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
660
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
661
  }
662 663 664 665 666 667
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

668
Starts a new ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result.
669

670 671 672 673 674
> **NOTE**
> 
> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
675 676 677 678 679 680 681 682 683

**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|
| -------- | -------- | -------- | -------- |
684
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
685 686 687 688
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Return value**

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

G
Gloria 已提交
693 694 695 696
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
697 698
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
699
| 16000004 | Can not start invisible component. |
700 701 702 703
| 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.        |
704 705
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
706 707 708 709
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
710

711 712
**Example**

G
Gloria 已提交
713
  ```ts
714 715 716 717
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
718
  };
719
  let accountId = 100;
G
Gloria 已提交
720 721 722 723 724 725 726 727 728

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
729
        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
730 731 732
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
733
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
734
  }
735 736 737 738 739 740
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

741
Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
742 743 744 745 746 747 748 749 750

**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|
| -------- | -------- | -------- | -------- |
751
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
752 753
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
754 755 756 757
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
758 759
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
760
| 16000004 | Can not start invisible component. |
761 762 763 764 765 766 767
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
768

769 770
**Example**

G
Gloria 已提交
771
  ```ts
772 773 774 775
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
776
  };
G
Gloria 已提交
777 778 779 780 781

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
782
        console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
783 784 785 786 787 788 789
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
790
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
791
  }
792 793 794 795 796 797
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

798
Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
799 800 801 802 803 804 805 806 807

**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|
| -------- | -------- | -------- | -------- |
808
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
809 810 811

**Return value**

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

G
Gloria 已提交
816 817 818 819
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
820 821
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
822
| 16000004 | Can not start invisible component. |
823 824 825 826 827 828 829
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
830

831 832
**Example**

G
Gloria 已提交
833
  ```ts
834 835 836 837
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
838
  };
G
Gloria 已提交
839 840 841 842 843 844 845 846 847

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
848
        console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
849 850 851
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
852
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
853
  }
854 855 856 857 858 859
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

862 863 864 865 866
> **NOTE**
> 
> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
867 868 869 870 871 872 873 874 875

**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|
| -------- | -------- | -------- | -------- |
876
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
877
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
878 879
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
880 881 882 883
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
884 885
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
886
| 16000004 | Can not start invisible component. |
887 888 889 890 891 892 893
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
894

895 896
**Example**

G
Gloria 已提交
897
  ```ts
898 899 900 901
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
902
  };
903
  let accountId = 100;
G
Gloria 已提交
904 905 906 907 908

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
909
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code, error.message: ${error.message}');
G
Gloria 已提交
910 911 912 913 914 915 916
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
917
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
918
  }
919 920 921 922 923 924
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

927 928 929 930 931
> **NOTE**
> 
> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
932 933 934 935 936 937 938 939 940

**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|
| -------- | -------- | -------- | -------- |
941
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
942 943 944 945
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Return value**

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

G
Gloria 已提交
950 951 952 953
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
954 955
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
956
| 16000004 | Can not start invisible component. |
957 958 959 960 961 962 963
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
964

965 966
**Example**

G
Gloria 已提交
967
  ```ts
968 969 970 971
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
972
  };
973
  let accountId = 100;
G
Gloria 已提交
974 975 976 977 978 979 980 981 982

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
983
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
984 985 986
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
987
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
988
  }
989
  ```
W
wusongqing 已提交
990

W
wusongqing 已提交
991
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
992 993 994

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

995
Terminates this ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
996

W
wusongqing 已提交
997
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
998

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

W
wusongqing 已提交
1001
**Parameters**
W
wusongqing 已提交
1002

1003 1004
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1005
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
G
Gloria 已提交
1006 1007 1008 1009 1010

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1011 1012 1013 1014 1015 1016 1017 1018
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
W
wusongqing 已提交
1019

W
wusongqing 已提交
1020 1021
**Example**

G
Gloria 已提交
1022 1023 1024 1025
  ```ts
  this.context.terminateSelf((error) => {
    if (error.code) {
      // Process service logic errors.
Z
zhoujun62 已提交
1026
      console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
1027 1028 1029 1030
      return;
    }
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1031
  });
W
wusongqing 已提交
1032 1033
  ```

W
wusongqing 已提交
1034
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
1035 1036 1037

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

W
wusongqing 已提交
1038
Terminates this ability. This API uses a promise to return the result.
W
wusongqing 已提交
1039

W
wusongqing 已提交
1040
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1041

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

W
wusongqing 已提交
1044
**Return value**
W
wusongqing 已提交
1045

1046 1047 1048
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1049

G
Gloria 已提交
1050 1051 1052 1053
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1054 1055 1056 1057 1058 1059 1060 1061
| 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. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
1062

W
wusongqing 已提交
1063 1064
**Example**

G
Gloria 已提交
1065
  ```ts
1066
  this.context.terminateSelf().then((data) => {
G
Gloria 已提交
1067 1068
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1069
  }).catch((error) => {
G
Gloria 已提交
1070
    // Process service logic errors.
Z
zhoujun62 已提交
1071
    console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
1072
  });
W
wusongqing 已提交
1073 1074
  ```

G
Gloria 已提交
1075
## ServiceExtensionContext.connectServiceExtensionAbility
W
wusongqing 已提交
1076

G
Gloria 已提交
1077
connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
W
wusongqing 已提交
1078

G
Gloria 已提交
1079
Connects this ability to a ServiceExtensionAbility.
W
wusongqing 已提交
1080

W
wusongqing 已提交
1081
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1082

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

W
wusongqing 已提交
1085
**Parameters**
W
wusongqing 已提交
1086

1087 1088 1089 1090
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
W
wusongqing 已提交
1091

W
wusongqing 已提交
1092 1093
**Return value**

1094 1095 1096
| Type| Description|
| -------- | -------- |
| number | A number, based on which the connection will be interrupted.|
W
wusongqing 已提交
1097

G
Gloria 已提交
1098 1099 1100 1101
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1102 1103 1104
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
1105
| 16000005 | The specified process does not have the permission. |
1106 1107 1108 1109
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
1110
| 16000011 | The context does not exist.        |
1111
| 16000050 | Internal error. |
G
Gloria 已提交
1112

1113 1114
For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
1115 1116
**Example**

G
Gloria 已提交
1117
  ```ts
1118 1119 1120
  let want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
W
wusongqing 已提交
1121
  };
1122
  let options = {
G
Gloria 已提交
1123 1124 1125 1126
    onConnect(elementName, remote) { 
      commRemote = remote;
      console.log('----------- onConnect -----------'); 
    },
1127
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
G
Gloria 已提交
1128
    onFailed(code) { console.error('----------- onFailed -----------') }
1129
  };
G
Gloria 已提交
1130

1131
  let connection = null;
G
Gloria 已提交
1132 1133 1134 1135
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
1136
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
1137
  }
W
wusongqing 已提交
1138 1139
  ```

G
Gloria 已提交
1140
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1141

G
Gloria 已提交
1142
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1143 1144 1145

Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability.

1146 1147
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

1148 1149 1150 1151 1152 1153 1154 1155
**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|
| -------- | -------- | -------- | -------- |
1156
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1157
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1158
| options | ConnectOptions | Yes| Remote object instance.|
1159 1160 1161 1162 1163 1164 1165

**Return value**

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

G
Gloria 已提交
1166 1167 1168 1169
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1170 1171 1172
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
1173
| 16000005 | The specified process does not have the permission. |
1174 1175 1176 1177
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
1178
| 16000011 | The context does not exist.        |
1179
| 16000050 | Internal error. |
G
Gloria 已提交
1180

1181 1182
For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

1183 1184
**Example**

G
Gloria 已提交
1185
  ```ts
1186 1187 1188 1189 1190 1191 1192
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
  };
  let accountId = 100;
  let options = {
G
Gloria 已提交
1193 1194 1195 1196
    onConnect(elementName, remote) { 
      commRemote = remote;
      console.log('----------- onConnect -----------');
    },
1197 1198
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); },
    onFailed(code) { console.log('----------- onFailed -----------'); }
1199
  };
G
Gloria 已提交
1200

1201
  let connection = null;
G
Gloria 已提交
1202 1203 1204 1205
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
1206
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
1207
  }
1208
  ```
W
wusongqing 已提交
1209

G
Gloria 已提交
1210
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1211

G
Gloria 已提交
1212
disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
W
wusongqing 已提交
1213

G
Gloria 已提交
1214
Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses an asynchronous callback to return the result. 
W
wusongqing 已提交
1215

W
wusongqing 已提交
1216
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1217

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

W
wusongqing 已提交
1220
**Parameters**
W
wusongqing 已提交
1221

1222 1223
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1224
| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1225
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
G
Gloria 已提交
1226 1227 1228 1229 1230

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1231
| 16000011 | The context does not exist.        |
1232
| 16000050 | Internal error. |
W
wusongqing 已提交
1233

1234 1235
For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
1236 1237
**Example**

G
Gloria 已提交
1238 1239
  ```ts
  // connection is the return value of connectServiceExtensionAbility.
1240
  let connection = 1;
G
Gloria 已提交
1241 1242 1243

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
G
Gloria 已提交
1244
      commRemote = null;
G
Gloria 已提交
1245 1246
      if (error.code) {
        // Process service logic errors.
Z
zhoujun62 已提交
1247
        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
1248 1249 1250 1251
        return;
      }
      // Carry out normal service processing.
      console.log('disconnectServiceExtensionAbility succeed');
1252
    });
G
Gloria 已提交
1253
  } catch (paramError) {
G
Gloria 已提交
1254
    commRemote = null;
G
Gloria 已提交
1255
    // Process input parameter errors.
Z
zhoujun62 已提交
1256
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
1257
  }
W
wusongqing 已提交
1258 1259
  ```

G
Gloria 已提交
1260
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1261

G
Gloria 已提交
1262
disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
W
wusongqing 已提交
1263

G
Gloria 已提交
1264
Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses a promise to return the result. 
W
wusongqing 已提交
1265

W
wusongqing 已提交
1266
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1267

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

W
wusongqing 已提交
1270
**Parameters**
W
wusongqing 已提交
1271

1272 1273
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1274
| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
W
wusongqing 已提交
1275

W
wusongqing 已提交
1276 1277
**Return value**

1278 1279 1280
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
G
Gloria 已提交
1281 1282 1283 1284 1285

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1286
| 16000011 | The context does not exist.        |
1287
| 16000050 | Internal error. |
G
Gloria 已提交
1288

1289 1290
For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

W
wusongqing 已提交
1291 1292
**Example**

G
Gloria 已提交
1293
  ```ts
1294 1295
  // connection is the return value of connectServiceExtensionAbility.
  let connection = 1;
G
Gloria 已提交
1296 1297 1298 1299

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
G
Gloria 已提交
1300
        commRemote = null;
G
Gloria 已提交
1301 1302 1303 1304
        // Carry out normal service processing.
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
G
Gloria 已提交
1305
        commRemote = null;
G
Gloria 已提交
1306
        // Process service logic errors.
Z
zhoujun62 已提交
1307
        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
1308 1309
      });
  } catch (paramError) {
G
Gloria 已提交
1310
    commRemote = null;
G
Gloria 已提交
1311
    // Process input parameter errors.
Z
zhoujun62 已提交
1312
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
1313
  }
W
wusongqing 已提交
1314
  ```
1315 1316 1317 1318 1319

## ServiceExtensionContext.startAbilityByCall

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

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

1322 1323
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.
1324
 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1325 1326
 - 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).

1327 1328
**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

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

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

1333 1334 1335 1336
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1337
| 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.|
1338 1339 1340 1341 1342 1343 1344

**Return value**

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

G
Gloria 已提交
1345 1346 1347 1348
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1349
| 16000001 | The specified ability does not exist. |
1350
| 16000002 | Incorrect ability type. |
1351
| 16000004 | Can not start invisible component. |
1352
| 16000005 | Static permission denied. The specified process does not have the permission. |
1353
| 16000006 | Cross-user operations are not allowed. |
1354
| 16000008 | The crowdtesting application expires. |
1355
| 16000011 | The context does not exist. |
1356
| 16000050 | Internal error. |
1357 1358 1359
| 16200001 | The caller has been released.        |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
G
Gloria 已提交
1360

1361 1362
**Example**

G
Gloria 已提交
1363 1364 1365
  Start an ability in the background.

  ```ts
1366
  let caller;
1367 1368

  // Start an ability in the background by not passing parameters.
1369 1370 1371 1372 1373
  let wantBackground = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: ''
1374
  };
G
Gloria 已提交
1375 1376 1377 1378 1379

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // Carry out normal service processing.
1380
        caller = obj;
G
Gloria 已提交
1381 1382 1383
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
1384
        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
1385 1386 1387
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
1388
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
1389 1390 1391 1392 1393 1394
  }
  ```

  Start an ability in the foreground.

  ```ts
1395 1396 1397 1398 1399 1400 1401 1402
  let caller;

  // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true.
  let wantForeground = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
1403
      parameters: {
1404
        'ohos.aafwk.param.callAbilityToForeground': true
1405 1406
      }
  };
G
Gloria 已提交
1407 1408 1409 1410 1411

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // Carry out normal service processing.
1412
        caller = obj;
G
Gloria 已提交
1413 1414 1415
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
Z
zhoujun62 已提交
1416
        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
G
Gloria 已提交
1417 1418 1419
      });
  } catch (paramError) {
    // Process input parameter errors.
Z
zhoujun62 已提交
1420
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
G
Gloria 已提交
1421
  }
1422
  ```
1423
## ServiceExtensionContext.startRecentAbility
1424

1425
startRecentAbility(want: Want, callback: AsyncCallback\<void>): void;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

Starts an ability. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return 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 **exported** 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

**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.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Error codes**

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

| 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. |

**Example**

  ```ts
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

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

1490
startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558

Starts an ability with the start options specified. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return the result.
You can use this API to carry start options.

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 **exported** 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

**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.|
| 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**

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

| 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. |

**Example**

  ```ts
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};

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

1561
startRecentAbility(want: Want, options?: StartOptions): Promise\<void>;
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627

Starts an ability. If the ability has multiple instances, the latest instance is started.
This API uses a promise to return 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 **exported** 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

**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.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|

**Error codes**

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

| 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. |

**Example**

  ```ts
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};

try {
  this.context.startRecentAbility(want, options)
    .then(() => {
      // Carry out normal service processing.
      console.info('startRecentAbility succeed');
    })
    .catch((err) => {
      // Process service logic errors.
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // Process input parameter errors.
  console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
}
  ```
G
Gloria 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup>

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

Starts an ability with the account ID specified and obtains the caller object for communicating with the ability.

Observe the following when using this API:
 - If an application needs to call this API to start an ability that belongs to another user, it must have the **ohos.permission.ABILITY_BACKGROUND_COMMUNICATION** and **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permissions.
 - 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 **exported** 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).

1641 1642
**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER and ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

G
Gloria 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
**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| 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.|
| accountId | number | Yes| ID of a system account. The value **-1** indicates the current user. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Return value**

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

**Error codes**

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

| ID| Error Message|
| ------- | -------------------------------- |
1666
| 16000001 | The specified ability does not exist. |
G
Gloria 已提交
1667
| 16000002 | Incorrect ability type. |
1668
| 16000004 | Can not start invisible component. |
G
Gloria 已提交
1669 1670
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
1671
| 16000008 | The crowdtesting application expires. |
G
Gloria 已提交
1672
| 16000011 | The context does not exist. |
1673 1674 1675
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 16000050 | Internal error. |
G
Gloria 已提交
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
| 16200001 | The caller has been released.        |

**Example**

  ```ts
  let caller;

  // ID of a system account. The value -1 indicates the current user.
  let accountId = -1;

  // Specify the ability to start.
  let want = {
      bundleName: 'com.acts.actscalleeabilityrely',
      moduleName: 'entry',
1690 1691
      abilityName: 'EntryAbility',
      deviceId: '',
G
Gloria 已提交
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
      parameters: {
        // If the value of 'ohos.aafwk.param.callAbilityToForeground' is true, the ability is started in the foreground. If the value is false or not set, the ability is started in the background.
        'ohos.aafwk.param.callAbilityToForeground': true
      }
  };

  try {
    this.context.startAbilityByCallWithAccount(want, accountId)
      .then((obj) => {
        // Carry out normal service processing.
        caller = obj;
        console.log('startAbilityByCallWithAccount succeed');
      }).catch((error) => {
        // Process service logic errors.
        console.error('startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
  }
  ```