js-apis-service-extension-context.md 38.2 KB
Newer Older
W
wusongqing 已提交
1 2
# ServiceExtensionContext

3 4
The **ServiceExtensionContext** module, inherited from **ExtensionContext**, provides context for Service Extension abilities.

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
## Usage
W
wusongqing 已提交
13

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

G
Gloria 已提交
16 17 18 19
```ts
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

  let context = undefined;
20
  class MainAbility extends ServiceExtensionAbility {
G
Gloria 已提交
21 22 23
    onCreate() {
      context = this.context;
    }
24
  }
W
wusongqing 已提交
25
```
W
wusongqing 已提交
26

G
Gloria 已提交
27
## ServiceExtensionContext.startAbility
W
wusongqing 已提交
28 29 30

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

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

W
wusongqing 已提交
33
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
34

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

W
wusongqing 已提交
37
**Parameters**
W
wusongqing 已提交
38

W
wusongqing 已提交
39
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
40
  | -------- | -------- | -------- | -------- |
G
Gloria 已提交
41 42 43 44 45 46 47 48 49
  | 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> | Yes| Callback used to return the result.| 

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
W
wusongqing 已提交
50

W
wusongqing 已提交
51 52
**Example**

G
Gloria 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
  };

  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');
69
    });
G
Gloria 已提交
70 71 72 73 74
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
75 76
  ```

G
Gloria 已提交
77
## ServiceExtensionContext.startAbility
W
wusongqing 已提交
78

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

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

W
wusongqing 已提交
83
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
84

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

W
wusongqing 已提交
87
**Parameters**
W
wusongqing 已提交
88

W
wusongqing 已提交
89
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
90
  | -------- | -------- | -------- | -------- |
G
Gloria 已提交
91 92
  | 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-application-StartOptions.md) | No| Parameters used for starting the ability.|
W
wusongqing 已提交
93

W
wusongqing 已提交
94 95
**Return value**

W
wusongqing 已提交
96
  | Type| Description| 
W
wusongqing 已提交
97
  | -------- | -------- |
98
  | Promise&lt;void&gt; | Promise used to return the result.| 
W
wusongqing 已提交
99

G
Gloria 已提交
100 101 102 103 104 105 106
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

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

G
Gloria 已提交
109 110 111 112 113 114 115 116
  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
  };
  var options = {
  	windowMode: 0,
  };
117

G
Gloria 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  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));
  }
134 135
  ```

G
Gloria 已提交
136
## ServiceExtensionContext.startAbility
137 138 139

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

G
Gloria 已提交
140
Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
141 142 143 144 145 146 147 148 149

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
150
| want | [Want](js-apis-application-Want.md)  | Yes| Want information about the target ability.|
151 152 153
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

G
Gloria 已提交
154 155 156 157 158 159 160
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

161
**Example**
G
Gloria 已提交
162 163

  ```ts
164
  var want = {
G
Gloria 已提交
165 166 167
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
168 169
  };
  var options = {
G
Gloria 已提交
170
    windowMode: 0
171
  };
G
Gloria 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

  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));
  }
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  ```

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

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
205
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
206
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
207 208
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
209 210 211 212 213 214 215
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

216 217
**Example**

G
Gloria 已提交
218
  ```ts
219
  var want = {
G
Gloria 已提交
220 221 222
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
223 224 225
  };
  var accountId = 100;

G
Gloria 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  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));
  }
  ```
243 244 245 246 247

## ServiceExtensionContext.startAbilityWithAccount

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

G
Gloria 已提交
248
Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.
249 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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
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).|
G
Gloria 已提交
260
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
261 262
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
263 264 265 266 267 268 269
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

270 271
**Example**

G
Gloria 已提交
272
  ```ts
273
  var want = {
G
Gloria 已提交
274 275 276
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
277 278 279
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
280
    windowMode: 0
281
  };
G
Gloria 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

  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));
  }
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  ```


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

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
316
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
317
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
318 319
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|

320 321 322 323 324 325
**Return value**

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

G
Gloria 已提交
326 327 328 329 330 331 332
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

333 334
**Example**

G
Gloria 已提交
335
  ```ts
336
  var want = {
G
Gloria 已提交
337 338 339
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
340 341 342
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
343
    windowMode: 0
344
  };
G
Gloria 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

  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 已提交
362 363
  ```

364 365 366 367 368 369 370 371 372 373 374 375 376 377
## ServiceExtensionContext.startServiceExtensionAbility

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

Starts a new Service Extension ability. This API uses an asynchronous callback to return the result.

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

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
378
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
379 380
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
381 382 383 384 385 386 387
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

388 389
**Example**

G
Gloria 已提交
390
  ```ts
391
  var want = {
G
Gloria 已提交
392 393 394
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
395
  };
G
Gloria 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

  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));
  }
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

Starts a new Service Extension ability. This API uses a promise to return the result.

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

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
429
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
430 431 432 433 434 435

**Return value**

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

G
Gloria 已提交
437 438 439 440 441 442 443
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

444 445
**Example**

G
Gloria 已提交
446
  ```ts
447
  var want = {
G
Gloria 已提交
448 449 450
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
451
  };
G
Gloria 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

  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));
  }
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

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

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
487
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
488
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
489 490
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
491 492 493 494 495 496 497 498
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|


499 500
**Example**

G
Gloria 已提交
501
  ```ts
502
  var want = {
G
Gloria 已提交
503 504 505
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
506 507
  };
  var accountId = 100;
G
Gloria 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

  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));
  }
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

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

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
543
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
544 545 546 547 548 549 550
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Return value**

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

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

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

559 560
**Example**

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

  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));
  }
585 586 587 588 589 590
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

591
Stops a Service Extension ability in the same application. This API uses an asynchronous callback to return the result.
592 593 594 595 596 597 598 599 600

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
601
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
602 603
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

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

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

611 612
**Example**

G
Gloria 已提交
613
  ```ts
614
  var want = {
G
Gloria 已提交
615 616 617
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
618
  };
G
Gloria 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

  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));
  }
636 637 638 639 640 641
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

642
Stops a Service Extension ability in the same application. This API uses a promise to return the result.
643 644 645 646 647 648 649 650 651

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
652
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
653 654 655 656 657 658

**Return value**

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

G
Gloria 已提交
660 661 662 663 664 665 666
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

667 668
**Example**

G
Gloria 已提交
669
  ```ts
670
  var want = {
G
Gloria 已提交
671 672 673
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
674
  };
G
Gloria 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

  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));
  }
692 693 694 695 696 697
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

698
Stops a Service Extension ability in the same application with the account ID specified. This API uses an asynchronous callback to return the result.
699 700 701 702 703 704 705 706 707 708 709

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

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
710
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
711
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
712 713
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
714 715 716 717 718 719 720
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

721 722
**Example**

G
Gloria 已提交
723
  ```ts
724
  var want = {
G
Gloria 已提交
725 726 727
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
728 729
  };
  var accountId = 100;
G
Gloria 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

  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));
  }
747 748 749 750 751 752
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

753
Stops a Service Extension ability in the same application with the account ID specified. This API uses a promise to return the result.
754 755 756 757 758 759 760 761 762 763 764

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

**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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
765
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
766 767 768 769 770 771 772
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Return value**

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

G
Gloria 已提交
774 775 776 777 778 779 780
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

781 782
**Example**

G
Gloria 已提交
783
  ```ts
784
  var want = {
G
Gloria 已提交
785 786 787
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
788 789
  };
  var accountId = 100;
G
Gloria 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

  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));
  }
807
  ```
W
wusongqing 已提交
808

W
wusongqing 已提交
809
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
810 811 812

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

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

W
wusongqing 已提交
815
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
816

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

W
wusongqing 已提交
819
**Parameters**
W
wusongqing 已提交
820

W
wusongqing 已提交
821
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
822
  | -------- | -------- | -------- | -------- |
G
Gloria 已提交
823 824 825 826 827 828 829 830
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| 

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
W
wusongqing 已提交
831

W
wusongqing 已提交
832 833
**Example**

G
Gloria 已提交
834 835 836 837 838 839 840 841 842 843
  ```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');
844
  });
W
wusongqing 已提交
845 846
  ```

W
wusongqing 已提交
847
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
848 849 850

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

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

W
wusongqing 已提交
853
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
854

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

W
wusongqing 已提交
857
**Return value**
W
wusongqing 已提交
858

W
wusongqing 已提交
859
  | Type| Description| 
W
wusongqing 已提交
860
  | -------- | -------- |
861
  | Promise&lt;void&gt; | Promise used to return the result.| 
W
wusongqing 已提交
862

G
Gloria 已提交
863 864 865 866 867 868 869
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
870 871
**Example**

G
Gloria 已提交
872
  ```ts
873
  this.context.terminateSelf().then((data) => {
G
Gloria 已提交
874 875
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
876
  }).catch((error) => {
G
Gloria 已提交
877 878 879
    // Process service logic errors.
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
880
  });
W
wusongqing 已提交
881 882
  ```

G
Gloria 已提交
883
## ServiceExtensionContext.connectServiceExtensionAbility
W
wusongqing 已提交
884

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

W
wusongqing 已提交
887
Connects this ability to a Service ability.
W
wusongqing 已提交
888

W
wusongqing 已提交
889
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
890

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

W
wusongqing 已提交
893
**Parameters**
W
wusongqing 已提交
894

W
wusongqing 已提交
895
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
896
  | -------- | -------- | -------- | -------- |
G
Gloria 已提交
897
  | want | [Want](js-apis-application-Want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.| 
898
  | options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.| 
W
wusongqing 已提交
899

W
wusongqing 已提交
900 901
**Return value**

W
wusongqing 已提交
902
  | Type| Description| 
W
wusongqing 已提交
903
  | -------- | -------- |
W
wusongqing 已提交
904
  | number | A number, based on which the connection will be interrupted.| 
W
wusongqing 已提交
905

G
Gloria 已提交
906 907 908 909 910 911 912
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
913 914
**Example**

G
Gloria 已提交
915 916 917 918
  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
W
wusongqing 已提交
919
  };
G
Gloria 已提交
920
  var options = {
921 922 923
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
W
wusongqing 已提交
924
  }
G
Gloria 已提交
925 926 927 928 929 930 931 932 933

  var connection = null;
  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 已提交
934 935
  ```

G
Gloria 已提交
936
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
937

G
Gloria 已提交
938
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
939 940 941 942 943 944 945 946 947 948 949

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|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
950
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
951
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
G
Gloria 已提交
952
| options | ConnectOptions | Yes| Remote object instance.|
953 954 955 956 957 958 959

**Return value**

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

G
Gloria 已提交
960 961 962 963 964 965 966
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

967 968
**Example**

G
Gloria 已提交
969
  ```ts
970
  var want = {
G
Gloria 已提交
971 972 973
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
974 975 976 977 978 979 980
  };
  var accountId = 100;
  var options = {
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
G
Gloria 已提交
981 982 983 984 985 986 987 988 989

  var connection = null;
  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));
  }
990
  ```
W
wusongqing 已提交
991

G
Gloria 已提交
992
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
993

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

996
Disconnects this ability from the Service ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
997

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

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

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

W
wusongqing 已提交
1004
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
1005
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
1006
  | connection | number | Yes| Number returned after **connectAbility** is called.| 
G
Gloria 已提交
1007 1008 1009 1010 1011 1012 1013 1014
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| 

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
W
wusongqing 已提交
1015

W
wusongqing 已提交
1016 1017
**Example**

G
Gloria 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  ```ts
  // connection is the return value of connectServiceExtensionAbility.
  var connection = 1;

  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');
1032
    });
G
Gloria 已提交
1033 1034 1035 1036 1037
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1038 1039
  ```

G
Gloria 已提交
1040
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1041

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

W
wusongqing 已提交
1044
Disconnects this ability from the Service ability. This API uses a promise to return the result.
W
wusongqing 已提交
1045

W
wusongqing 已提交
1046
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1047

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

W
wusongqing 已提交
1050
**Parameters**
W
wusongqing 已提交
1051

W
wusongqing 已提交
1052
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
1053
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
1054
  | connection | number | Yes| Number returned after **connectAbility** is called.| 
W
wusongqing 已提交
1055

W
wusongqing 已提交
1056 1057
**Return value**

W
wusongqing 已提交
1058
  | Type| Description| 
W
wusongqing 已提交
1059
  | -------- | -------- |
1060
  | Promise&lt;void&gt; | Promise used to return the result.| 
G
Gloria 已提交
1061 1062 1063 1064 1065 1066 1067 1068

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
1069 1070
**Example**

G
Gloria 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
  ```ts
  // connection is the return value of connectAbility.
  var connection = 1;

  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 已提交
1091
  ```
1092 1093 1094 1095 1096

## ServiceExtensionContext.startAbilityByCall

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

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

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

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

1103 1104 1105 1106
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1107
| 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.|
1108 1109 1110 1111 1112 1113 1114

**Return value**

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

G
Gloria 已提交
1115 1116 1117 1118 1119 1120 1121
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1122 1123
**Example**

G
Gloria 已提交
1124 1125 1126 1127
  Start an ability in the background.

  ```ts
  var caller = undefined;
1128 1129 1130

  // Start an ability in the background by not passing parameters.
  var wantBackground = {
1131 1132 1133 1134
      bundleName: "com.example.myservice",
      moduleName: "entry",
      abilityName: "MainAbility",
      deviceId: ""
1135
  };
G
Gloria 已提交
1136 1137 1138 1139 1140

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // Carry out normal service processing.
1141
        caller = obj;
G
Gloria 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
        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
  var caller = undefined;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

  // Start an ability in the foreground with ohos.aafwk.param.callAbilityToForeground in parameters set to true.
  var wantForeground = {
      bundleName: "com.example.myservice",
      moduleName: "entry",
      abilityName: "MainAbility",
      deviceId: "",
      parameters: {
        "ohos.aafwk.param.callAbilityToForeground": true
      }
  };
G
Gloria 已提交
1170 1171 1172 1173 1174

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // Carry out normal service processing.
1175
        caller = obj;
G
Gloria 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
        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));
  }
1187 1188
  ```