js-apis-inner-application-serviceExtensionContext.md 53.3 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;
G
Gloria 已提交
20
  let commRemote; // Release the instance when the connection is disconnected.
21
  class EntryAbility extends ServiceExtensionAbility {
G
Gloria 已提交
22
    onCreate() {
23
      context = this.context; // Obtain a ServiceExtensionContext instance.
G
Gloria 已提交
24
    }
25
  }
W
wusongqing 已提交
26
```
W
wusongqing 已提交
27

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

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

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

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

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

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

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

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
49
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
50
| 401 | Invalid input parameter. |
51 52 53 54 55 56 57 58
| 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.        |
G
Gloria 已提交
59
| 16000017 | The previous ability is starting, wait start later.        |
60 61 62 63 64 65 66 67 68
| 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 已提交
69

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

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

  try {
    this.context.startAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
G
Gloria 已提交
82
        console.error('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
83 84 85 86
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
87
    });
G
Gloria 已提交
88 89
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
90
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
91
  }
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
| 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.        |
G
Gloria 已提交
131
| 16000017 | The previous ability is starting, wait start later.        |
132 133 134 135 136 137 138 139 140
| 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 已提交
141

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

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

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

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

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

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

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

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
183 184
| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
185 186
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

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

| ID| Error Message|
| ------- | -------------------------------- |
191
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
192
| 401 | Invalid input parameter. |
193 194 195 196 197 198 199 200
| 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.        |
G
Gloria 已提交
201
| 16000017 | The previous ability is starting, wait start later.        |
202 203 204 205 206 207 208 209 210
| 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 216 217 218
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
219
  };
220
  let options = {
G
Gloria 已提交
221
    windowMode: 0
222
  };
G
Gloria 已提交
223 224 225 226 227

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

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

246 247 248 249 250
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).

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

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

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

289 290
**Example**

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

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

## ServiceExtensionContext.startAbilityWithAccount

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

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

321 322 323 324 325
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).

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

G
Gloria 已提交
339 340 341 342
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
343
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
344
| 401 | Invalid input parameter. |
345 346 347 348 349 350 351 352 353
| 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.        |
G
Gloria 已提交
354
| 16000017 | The previous ability is starting, wait start later.        |
355 356 357 358 359 360 361 362 363
| 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 已提交
364

365 366
**Example**

G
Gloria 已提交
367
  ```ts
368 369 370 371
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
372
  };
373 374
  let accountId = 100;
  let options = {
G
Gloria 已提交
375
    windowMode: 0
376
  };
G
Gloria 已提交
377 378 379 380 381

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


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

401 402 403 404 405
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).

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

418 419
**Return value**

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

G
Gloria 已提交
424 425 426 427
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
428
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
429
| 401 | Invalid input parameter. |
430 431 432 433 434 435 436 437 438
| 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.        |
G
Gloria 已提交
439
| 16000017 | The previous ability is starting, wait start later.        |
440 441 442 443 444 445 446 447 448
| 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 已提交
449

450 451
**Example**

G
Gloria 已提交
452
  ```ts
453 454 455 456
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
457
  };
458 459
  let accountId = 100;
  let options = {
G
Gloria 已提交
460
    windowMode: 0
461
  };
G
Gloria 已提交
462 463 464 465 466 467 468 469 470

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
G
Gloria 已提交
471
        console.error('startAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
472 473 474
      });
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
475
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
476
  }
W
wusongqing 已提交
477 478
  ```

479 480 481 482
## ServiceExtensionContext.startServiceExtensionAbility

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

483
Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
484 485 486 487 488 489 490 491 492

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

G
Gloria 已提交
496 497 498 499
**Error codes**

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

513 514
**Example**

G
Gloria 已提交
515
  ```ts
516 517 518 519
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
520
  };
G
Gloria 已提交
521 522 523 524 525

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

## ServiceExtensionContext.startServiceExtensionAbility

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

542
Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
543 544 545 546 547 548 549 550 551

**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|
| -------- | -------- | -------- | -------- |
552
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
553 554 555

**Return value**

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

G
Gloria 已提交
560 561 562 563
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
564
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
565
| 401 | Invalid input parameter. |
566 567 568 569 570 571 572 573 574 575
| 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 已提交
576

577 578
**Example**

G
Gloria 已提交
579
  ```ts
580 581 582 583
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
584
  };
G
Gloria 已提交
585 586 587 588 589 590 591 592 593

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
G
Gloria 已提交
594
        console.error('startServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
595 596 597
      });
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
598
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
599
  }
600 601 602 603 604 605
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

608
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
609 610 611 612 613 614 615 616 617

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

G
Gloria 已提交
622 623 624 625
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
626
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
627
| 401 | Invalid input parameter. |
628 629 630 631 632 633 634 635 636 637 638
| 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 已提交
639 640


641 642
**Example**

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

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

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

673
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
674 675 676 677 678 679 680 681 682

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

**Return value**

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

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

| ID| Error Message|
| ------- | -------------------------------- |
696
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
697
| 401 | Invalid input parameter. |
698 699 700 701 702 703 704 705 706 707 708
| 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 已提交
709

710 711
**Example**

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

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

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

| ID| Error Message|
| ------- | -------------------------------- |
757
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
758
| 401 | Invalid input parameter. |
759 760 761 762 763 764 765
| 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 已提交
766

767 768
**Example**

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

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

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

**Return value**

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

G
Gloria 已提交
814 815 816 817
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
818
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
819
| 401 | Invalid input parameter. |
820 821 822 823 824 825 826
| 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 已提交
827

828 829
**Example**

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

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

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

859
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
860 861 862 863 864 865 866 867 868

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

G
Gloria 已提交
873 874 875 876
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
877
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
878
| 401 | Invalid input parameter. |
879 880 881 882 883 884 885 886
| 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 已提交
887

888 889
**Example**

G
Gloria 已提交
890
  ```ts
891 892 893 894
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
895
  };
896
  let accountId = 100;
G
Gloria 已提交
897 898 899 900 901

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

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

920
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user)
921 922 923 924 925 926 927 928 929

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

**Return value**

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

G
Gloria 已提交
939 940 941 942
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
943
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
944
| 401 | Invalid input parameter. |
945 946 947 948 949 950 951 952
| 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 已提交
953

954 955
**Example**

G
Gloria 已提交
956
  ```ts
957 958 959 960
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
961
  };
962
  let accountId = 100;
G
Gloria 已提交
963 964 965 966 967 968 969 970 971

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
G
Gloria 已提交
972
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
973 974 975
      });
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
976
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
977
  }
978
  ```
W
wusongqing 已提交
979

W
wusongqing 已提交
980
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
981 982 983

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

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

W
wusongqing 已提交
986
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
987

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

W
wusongqing 已提交
990
**Parameters**
W
wusongqing 已提交
991

992 993 994
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
995 996 997 998 999

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1000
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1001
| 401 | Invalid input parameter. |
1002 1003 1004 1005
| 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 已提交
1006

W
wusongqing 已提交
1007 1008
**Example**

G
Gloria 已提交
1009 1010 1011 1012
  ```ts
  this.context.terminateSelf((error) => {
    if (error.code) {
      // Process service logic errors.
G
Gloria 已提交
1013
      console.error('terminateSelf failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
1014 1015 1016 1017
      return;
    }
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1018
  });
W
wusongqing 已提交
1019 1020
  ```

W
wusongqing 已提交
1021
## ServiceExtensionContext.terminateSelf
W
wusongqing 已提交
1022 1023 1024

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

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

W
wusongqing 已提交
1027
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1028

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

W
wusongqing 已提交
1031
**Return value**
W
wusongqing 已提交
1032

1033 1034 1035
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1036

G
Gloria 已提交
1037 1038 1039 1040
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1041
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1042
| 401 | Invalid input parameter. |
1043 1044 1045 1046
| 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 已提交
1047

W
wusongqing 已提交
1048 1049
**Example**

G
Gloria 已提交
1050
  ```ts
1051
  this.context.terminateSelf().then((data) => {
G
Gloria 已提交
1052 1053
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
1054
  }).catch((error) => {
G
Gloria 已提交
1055
    // Process service logic errors.
G
Gloria 已提交
1056
    console.error('terminateSelf failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
1057
  });
W
wusongqing 已提交
1058 1059
  ```

G
Gloria 已提交
1060
## ServiceExtensionContext.connectServiceExtensionAbility
W
wusongqing 已提交
1061

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

G
Gloria 已提交
1064
Connects this ability to a ServiceExtensionAbility.
W
wusongqing 已提交
1065

W
wusongqing 已提交
1066
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1067

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

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

1072 1073 1074 1075
| 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 已提交
1076

W
wusongqing 已提交
1077 1078
**Return value**

1079 1080 1081
| Type| Description|
| -------- | -------- |
| number | A number, based on which the connection will be interrupted.|
W
wusongqing 已提交
1082

G
Gloria 已提交
1083 1084 1085 1086
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1087
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1088
| 401 | Invalid input parameter. |
1089 1090 1091 1092 1093
| 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 已提交
1094

W
wusongqing 已提交
1095 1096
**Example**

G
Gloria 已提交
1097
  ```ts
1098 1099 1100
  let want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
W
wusongqing 已提交
1101
  };
1102
  let options = {
G
Gloria 已提交
1103 1104 1105 1106
    onConnect(elementName, remote) { 
      commRemote = remote;
      console.log('----------- onConnect -----------'); 
    },
1107
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
G
Gloria 已提交
1108
    onFailed(code) { console.error('----------- onFailed -----------') }
1109
  };
G
Gloria 已提交
1110

1111
  let connection = null;
G
Gloria 已提交
1112 1113 1114 1115
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
1116
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1117
  }
W
wusongqing 已提交
1118 1119
  ```

G
Gloria 已提交
1120
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1121

G
Gloria 已提交
1122
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

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|
| -------- | -------- | -------- | -------- |
1134
| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1135
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1136
| options | ConnectOptions | No| Remote object instance.|
1137 1138 1139 1140 1141 1142 1143

**Return value**

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

G
Gloria 已提交
1144 1145 1146 1147
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1148
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1149
| 401 | Invalid input parameter. |
1150 1151 1152 1153 1154 1155
| 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 已提交
1156

1157 1158
**Example**

G
Gloria 已提交
1159
  ```ts
1160 1161 1162 1163 1164 1165 1166
  let want = {
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
  };
  let accountId = 100;
  let options = {
G
Gloria 已提交
1167 1168 1169 1170
    onConnect(elementName, remote) { 
      commRemote = remote;
      console.log('----------- onConnect -----------');
    },
1171 1172
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); },
    onFailed(code) { console.log('----------- onFailed -----------'); }
1173
  };
G
Gloria 已提交
1174

1175
  let connection = null;
G
Gloria 已提交
1176 1177 1178 1179
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
1180
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1181
  }
1182
  ```
W
wusongqing 已提交
1183

G
Gloria 已提交
1184
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1185

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

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

W
wusongqing 已提交
1190
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1191

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

W
wusongqing 已提交
1194
**Parameters**
W
wusongqing 已提交
1195

1196 1197
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1198
| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1199
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
G
Gloria 已提交
1200 1201 1202 1203 1204

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1205
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1206
| 401 | Invalid input parameter. |
1207 1208 1209 1210
| 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 已提交
1211

W
wusongqing 已提交
1212 1213
**Example**

G
Gloria 已提交
1214 1215
  ```ts
  // connection is the return value of connectServiceExtensionAbility.
1216
  let connection = 1;
G
Gloria 已提交
1217 1218 1219

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
G
Gloria 已提交
1220
      commRemote = null;
G
Gloria 已提交
1221 1222
      if (error.code) {
        // Process service logic errors.
G
Gloria 已提交
1223
        console.error('disconnectServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
1224 1225 1226 1227
        return;
      }
      // Carry out normal service processing.
      console.log('disconnectServiceExtensionAbility succeed');
1228
    });
G
Gloria 已提交
1229
  } catch (paramError) {
G
Gloria 已提交
1230
    commRemote = null;
G
Gloria 已提交
1231
    // Process input parameter errors.
G
Gloria 已提交
1232
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1233
  }
W
wusongqing 已提交
1234 1235
  ```

G
Gloria 已提交
1236
## ServiceExtensionContext.disconnectServiceExtensionAbility
W
wusongqing 已提交
1237

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

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

W
wusongqing 已提交
1242
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1243

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

W
wusongqing 已提交
1246
**Parameters**
W
wusongqing 已提交
1247

1248 1249
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1250
| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
W
wusongqing 已提交
1251

W
wusongqing 已提交
1252 1253
**Return value**

1254 1255 1256
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
G
Gloria 已提交
1257 1258 1259 1260 1261

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1262
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1263
| 401 | Invalid input parameter. |
1264 1265 1266 1267
| 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 已提交
1268

W
wusongqing 已提交
1269 1270
**Example**

G
Gloria 已提交
1271
  ```ts
1272 1273
  // connection is the return value of connectServiceExtensionAbility.
  let connection = 1;
G
Gloria 已提交
1274 1275 1276 1277

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
G
Gloria 已提交
1278
        commRemote = null;
G
Gloria 已提交
1279 1280 1281 1282
        // Carry out normal service processing.
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
G
Gloria 已提交
1283
        commRemote = null;
G
Gloria 已提交
1284
        // Process service logic errors.
G
Gloria 已提交
1285
        console.error('disconnectServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
1286 1287
      });
  } catch (paramError) {
G
Gloria 已提交
1288
    commRemote = null;
G
Gloria 已提交
1289
    // Process input parameter errors.
G
Gloria 已提交
1290
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1291
  }
W
wusongqing 已提交
1292
  ```
1293 1294 1295 1296 1297

## ServiceExtensionContext.startAbilityByCall

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

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

1300 1301 1302 1303 1304
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).

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

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

1309 1310 1311 1312
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
1313
| 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.|
1314 1315 1316 1317 1318 1319 1320

**Return value**

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

G
Gloria 已提交
1321 1322 1323 1324
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
1325
| 201 | The application does not have permission to call the interface. |
G
Gloria 已提交
1326
| 401 | Invalid input parameter. |
1327 1328 1329 1330 1331 1332
| 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. |
G
Gloria 已提交
1333
| 16000017 | The previous ability is starting, wait start later.        |
1334
| 16000050 | Internal Error. |
G
Gloria 已提交
1335

1336 1337
**Example**

G
Gloria 已提交
1338 1339 1340
  Start an ability in the background.

  ```ts
1341
  let caller;
1342 1343

  // Start an ability in the background by not passing parameters.
1344 1345 1346 1347 1348
  let wantBackground = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: ''
1349
  };
G
Gloria 已提交
1350 1351 1352 1353 1354

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // Carry out normal service processing.
1355
        caller = obj;
G
Gloria 已提交
1356 1357 1358
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
G
Gloria 已提交
1359
        console.error('startAbilityByCall failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
1360 1361 1362
      });
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
1363
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1364 1365 1366 1367 1368 1369
  }
  ```

  Start an ability in the foreground.

  ```ts
1370 1371 1372 1373 1374 1375 1376 1377
  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: '',
1378
      parameters: {
1379
        'ohos.aafwk.param.callAbilityToForeground': true
1380 1381
      }
  };
G
Gloria 已提交
1382 1383 1384 1385 1386

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // Carry out normal service processing.
1387
        caller = obj;
G
Gloria 已提交
1388 1389 1390
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
G
Gloria 已提交
1391
        console.error('startAbilityByCall failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
G
Gloria 已提交
1392 1393 1394
      });
  } catch (paramError) {
    // Process input parameter errors.
G
Gloria 已提交
1395
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
G
Gloria 已提交
1396
  }
1397
  ```