js-apis-inner-application-serviceExtensionContext.md 48.6 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

G
Gloria 已提交
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 25
```ts
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

  let context = undefined;
26
  class MainAbility extends ServiceExtensionAbility {
G
Gloria 已提交
27
    onCreate() {
28
      context = this.context; // Obtain a ServiceExtensionContext instance.
G
Gloria 已提交
29
    }
30
  }
W
wusongqing 已提交
31
```
W
wusongqing 已提交
32

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

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

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

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

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

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

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.|
| callback | AsyncCallback<void> | No| Callback used to return the result.|
G
Gloria 已提交
49 50 51 52 53

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| 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. |

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

W
wusongqing 已提交
70 71
**Example**

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

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

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

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

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

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

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

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

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.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
W
wusongqing 已提交
112

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

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

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

| ID| Error Message|
| ------- | -------------------------------- |
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| 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. |

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

W
wusongqing 已提交
139 140
**Example**

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

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

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

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

G
Gloria 已提交
172
Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
173 174 175 176 177 178 179 180 181

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

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

| ID| Error Message|
| ------- | -------------------------------- |
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 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. |

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

206
**Example**
G
Gloria 已提交
207 208

  ```ts
209 210 211 212
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
213
  };
214
  let options = {
G
Gloria 已提交
215
    windowMode: 0
216
  };
G
Gloria 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  try {
    this.context.startAbility(want, options, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
234 235 236 237 238 239 240 241
  ```

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

242 243
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.
244
 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
245 246
 - 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).

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

G
Gloria 已提交
259 260 261 262
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| 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. |

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

279 280
**Example**

G
Gloria 已提交
281
  ```ts
282 283 284 285
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
286
  };
287
  let accountId = 100;
288

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

## ServiceExtensionContext.startAbilityWithAccount

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

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

313 314
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.
315
 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
316 317
 - 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).

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

G
Gloria 已提交
331 332 333 334
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
| 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 已提交
350

351 352
**Example**

G
Gloria 已提交
353
  ```ts
354 355 356 357
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
358
  };
359 360
  let accountId = 100;
  let options = {
G
Gloria 已提交
361
    windowMode: 0
362
  };
G
Gloria 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
380 381 382 383 384 385 386 387 388
  ```


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

389 390
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.
391
 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
392 393
 - 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).

394 395 396 397 398 399 400 401
**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|
| -------- | -------- | -------- | -------- |
402
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
403
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
404
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
405

406 407
**Return value**

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

G
Gloria 已提交
412 413 414 415
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
| 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 已提交
431

432 433
**Example**

G
Gloria 已提交
434
  ```ts
435 436 437 438
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
439
  };
440 441
  let accountId = 100;
  let options = {
G
Gloria 已提交
442
    windowMode: 0
443
  };
G
Gloria 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
461 462
  ```

463 464 465 466
## ServiceExtensionContext.startServiceExtensionAbility

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

467
Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
468 469 470 471 472 473 474 475 476

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

G
Gloria 已提交
480 481 482 483
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
484 485 486 487 488 489 490 491 492 493
| 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. |

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

495 496
**Example**

G
Gloria 已提交
497
  ```ts
498 499 500 501
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
502
  };
G
Gloria 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
520 521 522 523 524 525
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

526
Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
527 528 529 530 531 532 533 534 535

**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|
| -------- | -------- | -------- | -------- |
536
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
537 538 539

**Return value**

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

G
Gloria 已提交
544 545 546 547
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
548 549 550 551 552 553 554 555 556 557
| 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. |

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

559 560
**Example**

G
Gloria 已提交
561
  ```ts
562 563 564 565
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
566
  };
G
Gloria 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
584 585 586 587 588 589
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

592
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
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|
| -------- | -------- | -------- | -------- |
602
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
603
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
604 605
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
606 607 608 609
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
610 611 612 613 614 615 616 617
| 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. |
G
Gloria 已提交
618

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

621 622
**Example**

G
Gloria 已提交
623
  ```ts
624 625 626 627
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
628
  };
629
  let accountId = 100;
G
Gloria 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
647 648 649 650 651 652
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

655
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
656 657 658 659 660 661 662 663 664

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

**Return value**

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

G
Gloria 已提交
674 675 676 677
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
678 679 680 681 682 683 684 685 686 687
| 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. |

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

689 690
**Example**

G
Gloria 已提交
691
  ```ts
692 693 694 695
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
696
  };
697
  let accountId = 100;
G
Gloria 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
715 716 717 718 719 720
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

721
Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
722 723 724 725 726 727 728 729 730

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

G
Gloria 已提交
734 735 736 737
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
738 739 740 741 742 743 744 745 746
| 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. |

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

748 749
**Example**

G
Gloria 已提交
750
  ```ts
751 752 753 754
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
755
  };
G
Gloria 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
773 774 775 776 777 778
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

779
Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
780 781 782 783 784 785 786 787 788

**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|
| -------- | -------- | -------- | -------- |
789
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
790 791 792

**Return value**

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

G
Gloria 已提交
797 798 799 800
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
801 802 803 804 805 806 807 808 809
| 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. |

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

811 812
**Example**

G
Gloria 已提交
813
  ```ts
814 815 816 817
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
818
  };
G
Gloria 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
836 837 838 839 840 841
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

844
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
845 846 847 848 849 850 851 852 853

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

G
Gloria 已提交
858 859 860 861
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
862 863 864 865 866 867 868 869 870
| 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. |

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

872 873
**Example**

G
Gloria 已提交
874
  ```ts
875 876 877 878
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
879
  };
880
  let accountId = 100;
G
Gloria 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
898 899 900 901 902 903
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

906
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
907 908 909 910 911 912 913 914 915

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

**Return value**

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

G
Gloria 已提交
925 926 927 928
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
929 930 931 932 933 934 935 936 937
| 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. |

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

939 940
**Example**

G
Gloria 已提交
941
  ```ts
942 943 944 945
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
946
  };
947
  let accountId = 100;
G
Gloria 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
965
  ```
W
wusongqing 已提交
966

W
wusongqing 已提交
967
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
968 969 970

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

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

W
wusongqing 已提交
973
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
974

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

W
wusongqing 已提交
977
**Parameters**
W
wusongqing 已提交
978

979 980 981
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
982 983 984 985 986

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
987 988 989 990 991 992 993 994
| 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 已提交
995

W
wusongqing 已提交
996 997
**Example**

G
Gloria 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007
  ```ts
  this.context.terminateSelf((error) => {
    if (error.code) {
      // Process service logic errors.
      console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
        ' error.message: ' + JSON.stringify(error.message));
      return;
    }
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1008
  });
W
wusongqing 已提交
1009 1010
  ```

W
wusongqing 已提交
1011
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
1012 1013 1014

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

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

W
wusongqing 已提交
1017
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1018

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

W
wusongqing 已提交
1021
**Return value**
W
wusongqing 已提交
1022

1023 1024 1025
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1026

G
Gloria 已提交
1027 1028 1029 1030
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1031 1032 1033 1034 1035 1036 1037 1038
| 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 已提交
1039

W
wusongqing 已提交
1040 1041
**Example**

G
Gloria 已提交
1042
  ```ts
1043
  this.context.terminateSelf().then((data) => {
G
Gloria 已提交
1044 1045
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1046
  }).catch((error) => {
G
Gloria 已提交
1047 1048 1049
    // Process service logic errors.
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
1050
  });
W
wusongqing 已提交
1051 1052
  ```

G
Gloria 已提交
1053
## ServiceExtensionContext.connectServiceExtensionAbility
W
wusongqing 已提交
1054

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

1057
Connects this ability to a ServiceExtensionAbility.
W
wusongqing 已提交
1058

W
wusongqing 已提交
1059
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1060

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

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

1065 1066 1067 1068
| 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 已提交
1069

W
wusongqing 已提交
1070 1071
**Return value**

1072 1073 1074
| Type| Description|
| -------- | -------- |
| number | A number, based on which the connection will be interrupted.|
W
wusongqing 已提交
1075

G
Gloria 已提交
1076 1077 1078 1079
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1080
| 16000001 | Input error. The specified ability name does not exist. |
1081 1082
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist.        |
1083
| 16000050 | Internal Error. |
G
Gloria 已提交
1084

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

W
wusongqing 已提交
1087 1088
**Example**

G
Gloria 已提交
1089
  ```ts
1090 1091 1092
  let want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
W
wusongqing 已提交
1093
  };
1094
  let options = {
1095 1096 1097
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
1098
  };
G
Gloria 已提交
1099

1100
  let connection = null;
G
Gloria 已提交
1101 1102 1103 1104 1105 1106 1107
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1108 1109
  ```

G
Gloria 已提交
1110
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1111

G
Gloria 已提交
1112
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

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

**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|
| -------- | -------- | -------- | -------- |
1124
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1125
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1126
| options | ConnectOptions | No| Remote object instance.|
1127 1128 1129 1130 1131 1132 1133

**Return value**

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

G
Gloria 已提交
1134 1135 1136 1137
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1138
| 16000001 | Input error. The specified ability name does not exist. |
1139 1140
| 16000005 | The specified process does not have the permission. |
| 16000011 | The context does not exist.        |
1141
| 16000050 | Internal Error. |
G
Gloria 已提交
1142

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

1145 1146
**Example**

G
Gloria 已提交
1147
  ```ts
1148 1149 1150 1151
  let want = {
    deviceId: '',
    bundleName: 'com.extreme.test',
    abilityName: 'MainAbility'
1152
  };
1153 1154
  let accountId = 100;
  let options = {
1155 1156 1157 1158
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
G
Gloria 已提交
1159

1160
  let connection = null;
G
Gloria 已提交
1161 1162 1163 1164 1165 1166 1167
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1168
  ```
W
wusongqing 已提交
1169

G
Gloria 已提交
1170
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1171

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

1174
Disconnects this ability from the ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1175

W
wusongqing 已提交
1176
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1177

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

W
wusongqing 已提交
1180
**Parameters**
W
wusongqing 已提交
1181

1182 1183
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1184
| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1185
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
1186 1187 1188 1189 1190

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1191
| 16000011 | The context does not exist.        |
1192
| 16000050 | Internal Error. |
W
wusongqing 已提交
1193

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

W
wusongqing 已提交
1196 1197
**Example**

G
Gloria 已提交
1198 1199
  ```ts
  // connection is the return value of connectServiceExtensionAbility.
1200
  let connection = 1;
G
Gloria 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('disconnectServiceExtensionAbility succeed');
1212
    });
G
Gloria 已提交
1213 1214 1215 1216 1217
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1218 1219
  ```

G
Gloria 已提交
1220
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1221

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

1224
Disconnects this ability from the ServiceExtensionAbility. This API uses a promise to return the result.
W
wusongqing 已提交
1225

W
wusongqing 已提交
1226
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1227

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

W
wusongqing 已提交
1230
**Parameters**
W
wusongqing 已提交
1231

1232 1233
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1234
| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
W
wusongqing 已提交
1235

W
wusongqing 已提交
1236 1237
**Return value**

1238 1239 1240
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
G
Gloria 已提交
1241 1242 1243 1244 1245

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1246
| 16000011 | The context does not exist.        |
1247
| 16000050 | Internal Error. |
G
Gloria 已提交
1248

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

W
wusongqing 已提交
1251 1252
**Example**

G
Gloria 已提交
1253
  ```ts
1254 1255
  // connection is the return value of connectServiceExtensionAbility.
  let connection = 1;
G
Gloria 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
        // Carry out normal service processing.
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1273
  ```
1274 1275 1276 1277 1278

## ServiceExtensionContext.startAbilityByCall

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

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

1281 1282
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.
1283 1284
 - If **visible** of the target ability is **false**, 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).
1285

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

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

1290 1291 1292 1293
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1294
| 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.|
1295 1296 1297 1298 1299 1300 1301

**Return value**

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

G
Gloria 已提交
1302 1303 1304 1305
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1306
| 16000001 | Input error. The specified ability name does not exist. |
1307
| 16000002 | Incorrect ability type. |
1308 1309
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
1310
| 16000006 | Cross-user operations are not allowed. |
1311
| 16000008 | Crowdtest App Expiration. |
1312
| 16000011 | The context does not exist. |
1313
| 16000050 | Internal Error. |
1314 1315 1316
| 16200001 | The caller has been released.        |

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

1318 1319
**Example**

G
Gloria 已提交
1320 1321 1322
  Start an ability in the background.

  ```ts
1323
  let caller = undefined;
1324 1325

  // Start an ability in the background by not passing parameters.
1326 1327 1328 1329 1330
  let wantBackground = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'MainAbility',
      deviceId: ''
1331
  };
G
Gloria 已提交
1332 1333 1334 1335 1336

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // Carry out normal service processing.
1337
        caller = obj;
G
Gloria 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
  ```

  Start an ability in the foreground.

  ```ts
1354 1355 1356 1357 1358 1359 1360 1361
  let caller = undefined;

  // 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: 'MainAbility',
      deviceId: '',
1362
      parameters: {
1363
        'ohos.aafwk.param.callAbilityToForeground': true
1364 1365
      }
  };
G
Gloria 已提交
1366 1367 1368 1369 1370

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // Carry out normal service processing.
1371
        caller = obj;
G
Gloria 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1383
  ```