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

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

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

W
wusongqing 已提交
7 8
> **NOTE**
> 
G
Gloria 已提交
9 10
>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>  - The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
11

12
## 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
```ts
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

19
  let context;
20
  class EntryAbility extends ServiceExtensionAbility {
G
Gloria 已提交
21
    onCreate() {
22
      context = this.context; // Obtain a ServiceExtensionContext instance.
G
Gloria 已提交
23
    }
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

39 40 41 42
| 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 已提交
43 44 45 46 47

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
48
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
49
| 401 | Invalid input parameter. |
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
W
wusongqing 已提交
67

W
wusongqing 已提交
68 69
**Example**

G
Gloria 已提交
70
  ```ts
71 72 73
  let want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
G
Gloria 已提交
74 75 76 77 78 79
  };

  try {
    this.context.startAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
80
        console.log('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
81 82 83 84
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
85
    });
G
Gloria 已提交
86 87
  } catch (paramError) {
    // Process input parameter errors.
88
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
89
  }
W
wusongqing 已提交
90 91
  ```

G
Gloria 已提交
92
## ServiceExtensionContext.startAbility
W
wusongqing 已提交
93

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

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

W
wusongqing 已提交
98
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
99

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

W
wusongqing 已提交
102
**Parameters**
W
wusongqing 已提交
103

104 105 106 107
| 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 已提交
108

W
wusongqing 已提交
109 110
**Return value**

111 112 113
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
114

G
Gloria 已提交
115 116 117 118
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
119
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
120
| 401 | Invalid input parameter. |
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
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
  try {
    this.context.startAbility(want, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
158
        console.log('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
159 160 161
      });
  } catch (paramError) {
    // Process input parameter errors.
162
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
163
  }
164 165
  ```

G
Gloria 已提交
166
## ServiceExtensionContext.startAbility
167 168 169

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

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

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

G
Gloria 已提交
184 185 186 187
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
188
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
189
| 401 | Invalid input parameter. |
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
207

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

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

  try {
    this.context.startAbility(want, options, (error) => {
      if (error.code) {
        // Process service logic errors.
224
        console.log('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
225 226 227 228 229 230 231
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
232
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
233
  }
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 244 245 246
Observe the following when using this API:
 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).

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
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
264
| 401 | Invalid input parameter. |
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
283

284 285
**Example**

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

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

## ServiceExtensionContext.startAbilityWithAccount

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

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

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

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

G
Gloria 已提交
334 335 336 337
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
338
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
339
| 401 | Invalid input parameter. |
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
358

359 360
**Example**

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

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


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

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

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

412 413
**Return value**

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

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

| ID| Error Message|
| ------- | -------------------------------- |
422
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
423
| 401 | Invalid input parameter. |
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
442

443 444
**Example**

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

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

472 473 474 475
## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

G
Gloria 已提交
489 490 491 492
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
493
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
494
| 401 | Invalid input parameter. |
495 496 497 498 499 500 501 502 503 504
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
505

506 507
**Example**

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

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

## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

**Return value**

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

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

| ID| Error Message|
| ------- | -------------------------------- |
557
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
558
| 401 | Invalid input parameter. |
559 560 561 562 563 564 565 566 567 568
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
569

570 571
**Example**

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

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

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

601
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
602 603 604 605 606 607 608 609 610

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

G
Gloria 已提交
615 616 617 618
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
619
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
620
| 401 | Invalid input parameter. |
621 622 623 624 625 626 627 628 629 630 631
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
632 633


634 635
**Example**

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

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

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

666
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
667 668 669 670 671 672 673 674 675

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

**Return value**

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

G
Gloria 已提交
685 686 687 688
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
689
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
690
| 401 | Invalid input parameter. |
691 692 693 694 695 696 697 698 699 700 701
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
702

703 704
**Example**

G
Gloria 已提交
705
  ```ts
706 707 708 709
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
710
  };
711
  let accountId = 100;
G
Gloria 已提交
712 713 714 715 716 717 718 719 720

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
721
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
722 723 724
      });
  } catch (paramError) {
    // Process input parameter errors.
725
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
726
  }
727 728 729 730 731 732
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

733
Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
734 735 736 737 738 739 740 741 742

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

G
Gloria 已提交
746 747 748 749
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
750
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
751
| 401 | Invalid input parameter. |
752 753 754 755 756 757 758
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
759

760 761
**Example**

G
Gloria 已提交
762
  ```ts
763 764 765 766
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
767
  };
G
Gloria 已提交
768 769 770 771 772

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
773
        console.log('stopServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
774 775 776 777 778 779 780
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
781
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
782
  }
783 784 785 786 787 788
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

789
Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
790 791 792 793 794 795 796 797 798

**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|
| -------- | -------- | -------- | -------- |
799
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
800 801 802

**Return value**

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

G
Gloria 已提交
807 808 809 810
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
811
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
812
| 401 | Invalid input parameter. |
813 814 815 816 817 818 819
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
820

821 822
**Example**

G
Gloria 已提交
823
  ```ts
824 825 826 827
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
828
  };
G
Gloria 已提交
829 830 831 832 833 834 835 836 837

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
838
        console.log('stopServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
839 840 841
      });
  } catch (paramError) {
    // Process input parameter errors.
842
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
843
  }
844 845 846 847 848 849
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

852
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
853 854 855 856 857 858 859 860 861

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

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

| ID| Error Message|
| ------- | -------------------------------- |
870
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
871
| 401 | Invalid input parameter. |
872 873 874 875 876 877 878 879
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
880

881 882
**Example**

G
Gloria 已提交
883
  ```ts
884 885 886 887
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
888
  };
889
  let accountId = 100;
G
Gloria 已提交
890 891 892 893 894

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
895
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code), error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
896 897 898 899 900 901 902
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
903
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
904
  }
905 906 907 908 909 910
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

913
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
914 915 916 917 918 919 920 921 922

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

**Return value**

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

G
Gloria 已提交
932 933 934 935
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
936
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
937
| 401 | Invalid input parameter. |
938 939 940 941 942 943 944 945
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
G
Gloria 已提交
946

947 948
**Example**

G
Gloria 已提交
949
  ```ts
950 951 952 953
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
954
  };
955
  let accountId = 100;
G
Gloria 已提交
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.
965
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
966 967 968
      });
  } catch (paramError) {
    // Process input parameter errors.
969
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
970
  }
971
  ```
W
wusongqing 已提交
972

W
wusongqing 已提交
973
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
974 975 976

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

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

W
wusongqing 已提交
979
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
980

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

W
wusongqing 已提交
983
**Parameters**
W
wusongqing 已提交
984

985 986 987
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
988 989 990 991 992

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
993
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
994
| 401 | Invalid input parameter. |
995 996 997 998
| 16000001 | Input error. The specified ability name does not exist. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
W
wusongqing 已提交
999

W
wusongqing 已提交
1000 1001
**Example**

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

W
wusongqing 已提交
1014
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
1015 1016 1017

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

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

W
wusongqing 已提交
1020
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1021

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

W
wusongqing 已提交
1024
**Return value**
W
wusongqing 已提交
1025

1026 1027 1028
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1029

G
Gloria 已提交
1030 1031 1032 1033
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1034
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1035
| 401 | Invalid input parameter. |
1036 1037 1038 1039
| 16000001 | Input error. The specified ability name does not exist. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
G
Gloria 已提交
1040

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

G
Gloria 已提交
1043
  ```ts
1044
  this.context.terminateSelf().then((data) => {
G
Gloria 已提交
1045 1046
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1047
  }).catch((error) => {
G
Gloria 已提交
1048
    // Process service logic errors.
1049
    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 ServiceAbility.
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
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1081
| 401 | Invalid input parameter. |
1082 1083 1084 1085 1086
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
G
Gloria 已提交
1087

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

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

1101
  let connection = null;
G
Gloria 已提交
1102 1103 1104 1105
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // Process input parameter errors.
1106
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1107
  }
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
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1139
| 401 | Invalid input parameter. |
1140 1141 1142 1143 1144 1145
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
G
Gloria 已提交
1146

1147 1148
**Example**

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

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

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

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

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

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

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

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

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

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1192
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1193
| 401 | Invalid input parameter. |
1194 1195 1196 1197
| 16000001 | Input error. The specified ability name does not exist. |
| 16000003 | Input error. The specified id does not exist. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
W
wusongqing 已提交
1198

W
wusongqing 已提交
1199 1200
**Example**

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

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

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

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

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

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

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

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

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

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

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

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1247
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1248
| 401 | Invalid input parameter. |
1249 1250 1251 1252
| 16000001 | Input error. The specified ability name does not exist. |
| 16000003 | Input error. The specified id does not exist. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
G
Gloria 已提交
1253

W
wusongqing 已提交
1254 1255
**Example**

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

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

## ServiceExtensionContext.startAbilityByCall

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

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

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

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

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

1291 1292 1293 1294
**Parameters**

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

**Return value**

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

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

| ID| Error Message|
| ------- | -------------------------------- |
1307
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1308
| 401 | Invalid input parameter. |
1309 1310 1311 1312 1313 1314 1315
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000050 | Internal Error. |
G
Gloria 已提交
1316

1317 1318
**Example**

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

  ```ts
1322
  let caller;
1323 1324

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

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

  Start an ability in the foreground.

  ```ts
1351 1352 1353 1354 1355 1356 1357 1358
  let caller;

  // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true.
  let wantForeground = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
1359
      parameters: {
1360
        'ohos.aafwk.param.callAbilityToForeground': true
1361 1362
      }
  };
G
Gloria 已提交
1363 1364 1365 1366 1367

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