js-apis-inner-application-serviceExtensionContext.md 52.7 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 19
```ts
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

  let context = undefined;
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  ```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');
86
    });
G
Gloria 已提交
87 88 89 90 91
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
92 93
  ```

G
Gloria 已提交
94
## ServiceExtensionContext.startAbility
W
wusongqing 已提交
95

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

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

W
wusongqing 已提交
100
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
101

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

W
wusongqing 已提交
104
**Parameters**
W
wusongqing 已提交
105

106 107 108 109
| 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 已提交
110

W
wusongqing 已提交
111 112
**Return value**

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

G
Gloria 已提交
117 118 119 120
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
121
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
122
| 401 | Invalid input parameter. |
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| 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 已提交
140

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

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

G
Gloria 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  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));
  }
168 169
  ```

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

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

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

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

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

| ID| Error Message|
| ------- | -------------------------------- |
192
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
193
| 401 | Invalid input parameter. |
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| 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 已提交
211

212
**Example**
G
Gloria 已提交
213 214

  ```ts
215
  var want = {
G
Gloria 已提交
216
    deviceId: "",
217 218
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
219 220
  };
  var options = {
G
Gloria 已提交
221
    windowMode: 0
222
  };
G
Gloria 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

  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));
  }
240 241 242 243 244 245 246 247
  ```

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

248 249 250 251 252
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).

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

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

| ID| Error Message|
| ------- | -------------------------------- |
269
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
270
| 401 | Invalid input parameter. |
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
| 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 已提交
289

290 291
**Example**

G
Gloria 已提交
292
  ```ts
293
  var want = {
G
Gloria 已提交
294
    deviceId: "",
295 296
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
297 298 299
  };
  var accountId = 100;

G
Gloria 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  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));
  }
  ```
317 318 319 320 321

## ServiceExtensionContext.startAbilityWithAccount

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

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

324 325 326 327 328
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).

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

G
Gloria 已提交
342 343 344 345
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
346
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
347
| 401 | Invalid input parameter. |
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
| 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 已提交
366

367 368
**Example**

G
Gloria 已提交
369
  ```ts
370
  var want = {
G
Gloria 已提交
371
    deviceId: "",
372 373
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
374 375 376
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
377
    windowMode: 0
378
  };
G
Gloria 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

  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));
  }
396 397 398 399 400 401 402 403 404
  ```


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

405 406 407 408 409
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).

410 411 412 413 414 415 416 417
**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|
| -------- | -------- | -------- | -------- |
418
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
419
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
420
| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
421

422 423
**Return value**

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

G
Gloria 已提交
428 429 430 431
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
432
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
433
| 401 | Invalid input parameter. |
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
| 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 已提交
452

453 454
**Example**

G
Gloria 已提交
455
  ```ts
456
  var want = {
G
Gloria 已提交
457
    deviceId: "",
458 459
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
460 461 462
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
463
    windowMode: 0
464
  };
G
Gloria 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

  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 已提交
482 483
  ```

484 485 486 487
## ServiceExtensionContext.startServiceExtensionAbility

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

488
Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
489 490 491 492 493 494 495 496 497

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

G
Gloria 已提交
501 502 503 504
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
505
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
506
| 401 | Invalid input parameter. |
507 508 509 510 511 512 513 514 515 516
| 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 已提交
517

518 519
**Example**

G
Gloria 已提交
520
  ```ts
521
  var want = {
G
Gloria 已提交
522
    deviceId: "",
523 524
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
525
  };
G
Gloria 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

  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));
  }
543 544 545 546 547 548
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

549
Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
550 551 552 553 554 555 556 557 558

**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|
| -------- | -------- | -------- | -------- |
559
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
560 561 562

**Return value**

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

G
Gloria 已提交
567 568 569 570
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
571
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
572
| 401 | Invalid input parameter. |
573 574 575 576 577 578 579 580 581 582
| 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 已提交
583

584 585
**Example**

G
Gloria 已提交
586
  ```ts
587
  var want = {
G
Gloria 已提交
588
    deviceId: "",
589 590
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
591
  };
G
Gloria 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

  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));
  }
609 610 611 612 613 614
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

617
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
618 619 620 621 622 623 624 625 626

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

G
Gloria 已提交
631 632 633 634
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
635
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
636
| 401 | Invalid input parameter. |
637 638 639 640 641 642 643 644 645 646 647
| 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 已提交
648 649


650 651
**Example**

G
Gloria 已提交
652
  ```ts
653
  var want = {
G
Gloria 已提交
654
    deviceId: "",
655 656
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
657 658
  };
  var accountId = 100;
G
Gloria 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

  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));
  }
676 677 678 679 680 681
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

684
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
685 686 687 688 689 690 691 692 693

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

**Return value**

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

G
Gloria 已提交
703 704 705 706
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
707
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
708
| 401 | Invalid input parameter. |
709 710 711 712 713 714 715 716 717 718 719
| 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 已提交
720

721 722
**Example**

G
Gloria 已提交
723
  ```ts
724
  var want = {
G
Gloria 已提交
725
    deviceId: "",
726 727
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
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.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));
  }
747 748 749 750 751 752
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

753
Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
754 755 756 757 758 759 760 761 762

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

G
Gloria 已提交
766 767 768 769
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
770
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
771
| 401 | Invalid input parameter. |
772 773 774 775 776 777 778
| 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 已提交
779

780 781
**Example**

G
Gloria 已提交
782
  ```ts
783
  var want = {
G
Gloria 已提交
784
    deviceId: "",
785 786
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
787
  };
G
Gloria 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

  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));
  }
805 806 807 808 809 810
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

811
Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
812 813 814 815 816 817 818 819 820

**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|
| -------- | -------- | -------- | -------- |
821
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
822 823 824

**Return value**

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

G
Gloria 已提交
829 830 831 832
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
833
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
834
| 401 | Invalid input parameter. |
835 836 837 838 839 840 841
| 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 已提交
842

843 844
**Example**

G
Gloria 已提交
845
  ```ts
846
  var want = {
G
Gloria 已提交
847
    deviceId: "",
848 849
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
850
  };
G
Gloria 已提交
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

  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));
  }
868 869 870 871 872 873
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

876
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
877 878 879 880 881 882 883 884 885

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

G
Gloria 已提交
890 891 892 893
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
894
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
895
| 401 | Invalid input parameter. |
896 897 898 899 900 901 902 903
| 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 已提交
904

905 906
**Example**

G
Gloria 已提交
907
  ```ts
908
  var want = {
G
Gloria 已提交
909
    deviceId: "",
910 911
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
912 913
  };
  var accountId = 100;
G
Gloria 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

  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));
  }
931 932 933 934 935 936
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

939
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
940 941 942 943 944 945 946 947 948

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

**Return value**

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

G
Gloria 已提交
958 959 960 961
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
962
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
963
| 401 | Invalid input parameter. |
964 965 966 967 968 969 970 971
| 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 已提交
972

973 974
**Example**

G
Gloria 已提交
975
  ```ts
976
  var want = {
G
Gloria 已提交
977
    deviceId: "",
978 979
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
980 981
  };
  var accountId = 100;
G
Gloria 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

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

W
wusongqing 已提交
1001
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
1002 1003 1004

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

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

W
wusongqing 已提交
1007
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1008

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

W
wusongqing 已提交
1011
**Parameters**
W
wusongqing 已提交
1012

1013 1014 1015
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
1016 1017 1018 1019 1020

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1021
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1022
| 401 | Invalid input parameter. |
1023 1024 1025 1026
| 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 已提交
1027

W
wusongqing 已提交
1028 1029
**Example**

G
Gloria 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  ```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');
1040
  });
W
wusongqing 已提交
1041 1042
  ```

W
wusongqing 已提交
1043
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
1044 1045 1046

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

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

W
wusongqing 已提交
1049
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1050

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

W
wusongqing 已提交
1053
**Return value**
W
wusongqing 已提交
1054

1055 1056 1057
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1058

G
Gloria 已提交
1059 1060 1061 1062
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1063
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1064
| 401 | Invalid input parameter. |
1065 1066 1067 1068
| 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 已提交
1069

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

G
Gloria 已提交
1072
  ```ts
1073
  this.context.terminateSelf().then((data) => {
G
Gloria 已提交
1074 1075
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1076
  }).catch((error) => {
G
Gloria 已提交
1077 1078 1079
    // Process service logic errors.
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
1080
  });
W
wusongqing 已提交
1081 1082
  ```

G
Gloria 已提交
1083
## ServiceExtensionContext.connectServiceExtensionAbility
W
wusongqing 已提交
1084

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

1087
Connects this ability to a ServiceAbility.
W
wusongqing 已提交
1088

W
wusongqing 已提交
1089
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1090

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

W
wusongqing 已提交
1093
**Parameters**
W
wusongqing 已提交
1094

1095 1096 1097 1098
| 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 已提交
1099

W
wusongqing 已提交
1100 1101
**Return value**

1102 1103 1104
| Type| Description|
| -------- | -------- |
| number | A number, based on which the connection will be interrupted.|
W
wusongqing 已提交
1105

G
Gloria 已提交
1106 1107 1108 1109
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1110
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1111
| 401 | Invalid input parameter. |
1112 1113 1114 1115 1116
| 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 已提交
1117

W
wusongqing 已提交
1118 1119
**Example**

G
Gloria 已提交
1120 1121 1122 1123
  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
W
wusongqing 已提交
1124
  };
G
Gloria 已提交
1125
  var options = {
1126 1127 1128
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
W
wusongqing 已提交
1129
  }
G
Gloria 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138

  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 已提交
1139 1140
  ```

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

G
Gloria 已提交
1143
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

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

**Return value**

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

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

| ID| Error Message|
| ------- | -------------------------------- |
1169
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1170
| 401 | Invalid input parameter. |
1171 1172 1173 1174 1175 1176
| 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 已提交
1177

1178 1179
**Example**

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

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

G
Gloria 已提交
1203
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1204

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

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

W
wusongqing 已提交
1209
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1210

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

W
wusongqing 已提交
1213
**Parameters**
W
wusongqing 已提交
1214

1215 1216 1217 1218
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Number returned after **connectAbility** is called.|
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
1219 1220 1221 1222 1223

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1224
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1225
| 401 | Invalid input parameter. |
1226 1227 1228 1229
| 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 已提交
1230

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

G
Gloria 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
  ```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');
1247
    });
G
Gloria 已提交
1248 1249 1250 1251 1252
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1253 1254
  ```

G
Gloria 已提交
1255
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1256

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

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

W
wusongqing 已提交
1261
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1262

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

W
wusongqing 已提交
1265
**Parameters**
W
wusongqing 已提交
1266

1267 1268 1269
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Number returned after **connectAbility** is called.|
W
wusongqing 已提交
1270

W
wusongqing 已提交
1271 1272
**Return value**

1273 1274 1275
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
G
Gloria 已提交
1276 1277 1278 1279 1280

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1281
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1282
| 401 | Invalid input parameter. |
1283 1284 1285 1286
| 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 已提交
1287

W
wusongqing 已提交
1288 1289
**Example**

G
Gloria 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
  ```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 已提交
1310
  ```
1311 1312 1313 1314 1315

## ServiceExtensionContext.startAbilityByCall

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

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

1318 1319 1320 1321 1322
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).

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

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

1327 1328 1329 1330
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1331
| 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.|
1332 1333 1334 1335 1336 1337 1338

**Return value**

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

G
Gloria 已提交
1339 1340 1341 1342
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1343
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1344
| 401 | Invalid input parameter. |
1345 1346 1347 1348 1349 1350 1351
| 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 已提交
1352

1353 1354
**Example**

G
Gloria 已提交
1355 1356 1357 1358
  Start an ability in the background.

  ```ts
  var caller = undefined;
1359 1360 1361

  // Start an ability in the background by not passing parameters.
  var wantBackground = {
1362 1363
      bundleName: "com.example.myservice",
      moduleName: "entry",
1364
      abilityName: "EntryAbility",
1365
      deviceId: ""
1366
  };
G
Gloria 已提交
1367 1368 1369 1370 1371

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // Carry out normal service processing.
1372
        caller = obj;
G
Gloria 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
        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;
1390 1391 1392 1393 1394

  // Start an ability in the foreground with ohos.aafwk.param.callAbilityToForeground in parameters set to true.
  var wantForeground = {
      bundleName: "com.example.myservice",
      moduleName: "entry",
1395
      abilityName: "EntryAbility",
1396 1397 1398 1399 1400
      deviceId: "",
      parameters: {
        "ohos.aafwk.param.callAbilityToForeground": true
      }
  };
G
Gloria 已提交
1401 1402 1403 1404 1405

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // Carry out normal service processing.
1406
        caller = obj;
G
Gloria 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        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));
  }
1418
  ```