• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ServiceExtensionContext
2
3The **ServiceExtensionContext** module, inherited from **ExtensionContext**, provides context for ServiceExtensionAbilities.
4
5You can use the APIs of this module to start, terminate, connect, and disconnect abilities.
6
7> **NOTE**
8>
9>  - 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.
10>  - The APIs of this module can be used only in the stage model.
11
12## Modules to Import
13
14```ts
15import common from '@ohos.app.ability.common';
16```
17
18## Usage
19
20Before using the **ServiceExtensionContext** module, you must define a child class that inherits from **ServiceExtensionAbility**.
21
22```ts
23  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
24
25  let context = undefined;
26  class MainAbility extends ServiceExtensionAbility {
27    onCreate() {
28      context = this.context; // Obtain a ServiceExtensionContext instance.
29    }
30  }
31```
32
33## ServiceExtensionContext.startAbility
34
35startAbility(want: Want, callback: AsyncCallback<void>): void;
36
37Starts an ability. This API uses an asynchronous callback to return the result.
38
39**System capability**: SystemCapability.Ability.AbilityRuntime.Core
40
41**System API**: This is a system API and cannot be called by third-party applications.
42
43**Parameters**
44
45| Name| Type| Mandatory| Description|
46| -------- | -------- | -------- | -------- |
47| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
48| callback | AsyncCallback<void> | No| Callback used to return the result.|
49
50**Error codes**
51
52| ID| Error Message|
53| ------- | -------------------------------- |
54| 16000001 | The specified ability does not exist. |
55| 16000002 | Incorrect ability type. |
56| 16000004 | Can not start invisible component. |
57| 16000005 | The specified process does not have the permission. |
58| 16000006 | Cross-user operations are not allowed. |
59| 16000008 | The crowdtesting application expires. |
60| 16000009 | An ability cannot be started or stopped in Wukong mode. |
61| 16000010 | The call with the continuation flag is forbidden.        |
62| 16000011 | The context does not exist.        |
63| 16000050 | Internal error. |
64| 16000053 | The ability is not on the top of the UI. |
65| 16000055 | Installation-free timed out. |
66| 16200001 | The caller has been released. |
67
68For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
69
70**Example**
71
72  ```ts
73  let want = {
74    bundleName: 'com.example.myapp',
75    abilityName: 'MyAbility'
76  };
77
78  try {
79    this.context.startAbility(want, (error) => {
80      if (error.code) {
81        // Process service logic errors.
82        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
83          ' error.message: ' + JSON.stringify(error.message));
84        return;
85      }
86      // Carry out normal service processing.
87      console.log('startAbility succeed');
88    });
89  } catch (paramError) {
90    // Process input parameter errors.
91    console.log('error.code: ' + JSON.stringify(paramError.code) +
92      ' error.message: ' + JSON.stringify(paramError.message));
93  }
94  ```
95
96## ServiceExtensionContext.startAbility
97
98startAbility(want: Want, options?: StartOptions): Promise\<void>;
99
100Starts an ability. This API uses a promise to return the result.
101
102**System capability**: SystemCapability.Ability.AbilityRuntime.Core
103
104**System API**: This is a system API and cannot be called by third-party applications.
105
106**Parameters**
107
108| Name| Type| Mandatory| Description|
109| -------- | -------- | -------- | -------- |
110| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
111| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
112
113**Return value**
114
115| Type| Description|
116| -------- | -------- |
117| Promise&lt;void&gt; | Promise used to return the result.|
118
119**Error codes**
120
121| ID| Error Message|
122| ------- | -------------------------------- |
123| 16000001 | The specified ability does not exist. |
124| 16000002 | Incorrect ability type. |
125| 16000004 | Can not start invisible component. |
126| 16000005 | The specified process does not have the permission. |
127| 16000006 | Cross-user operations are not allowed. |
128| 16000008 | The crowdtesting application expires. |
129| 16000009 | An ability cannot be started or stopped in Wukong mode. |
130| 16000010 | The call with the continuation flag is forbidden.        |
131| 16000011 | The context does not exist.        |
132| 16000050 | Internal error. |
133| 16000053 | The ability is not on the top of the UI. |
134| 16000055 | Installation-free timed out. |
135| 16200001 | The caller has been released. |
136
137For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
138
139**Example**
140
141  ```ts
142  let want = {
143    bundleName: 'com.example.myapp',
144    abilityName: 'MyAbility'
145  };
146  let options = {
147  	windowMode: 0,
148  };
149
150  try {
151    this.context.startAbility(want, options)
152      .then((data) => {
153        // Carry out normal service processing.
154        console.log('startAbility succeed');
155      })
156      .catch((error) => {
157        // Process service logic errors.
158        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
159          ' error.message: ' + JSON.stringify(error.message));
160      });
161  } catch (paramError) {
162    // Process input parameter errors.
163    console.log('error.code: ' + JSON.stringify(paramError.code) +
164      ' error.message: ' + JSON.stringify(paramError.message));
165  }
166  ```
167
168## ServiceExtensionContext.startAbility
169
170startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
171
172Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
173
174**System capability**: SystemCapability.Ability.AbilityRuntime.Core
175
176**System API**: This is a system API and cannot be called by third-party applications.
177
178**Parameters**
179
180| Name| Type| Mandatory| Description|
181| -------- | -------- | -------- | -------- |
182| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability.|
183| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
184| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
185
186**Error codes**
187
188| ID| Error Message|
189| ------- | -------------------------------- |
190| 16000001 | The specified ability does not exist. |
191| 16000002 | Incorrect ability type. |
192| 16000004 | Can not start invisible component. |
193| 16000005 | The specified process does not have the permission. |
194| 16000006 | Cross-user operations are not allowed. |
195| 16000008 | The crowdtesting application expires. |
196| 16000009 | An ability cannot be started or stopped in Wukong mode. |
197| 16000010 | The call with the continuation flag is forbidden.        |
198| 16000011 | The context does not exist.        |
199| 16000050 | Internal error. |
200| 16000053 | The ability is not on the top of the UI. |
201| 16000055 | Installation-free timed out. |
202| 16200001 | The caller has been released. |
203
204For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
205
206**Example**
207
208  ```ts
209  let want = {
210    deviceId: '',
211    bundleName: 'com.extreme.test',
212    abilityName: 'MainAbility'
213  };
214  let options = {
215    windowMode: 0
216  };
217
218  try {
219    this.context.startAbility(want, options, (error) => {
220      if (error.code) {
221        // Process service logic errors.
222        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
223          ' error.message: ' + JSON.stringify(error.message));
224        return;
225      }
226      // Carry out normal service processing.
227      console.log('startAbility succeed');
228    });
229  } catch (paramError) {
230    // Process input parameter errors.
231    console.log('error.code: ' + JSON.stringify(paramError.code) +
232      ' error.message: ' + JSON.stringify(paramError.message));
233  }
234  ```
235
236## ServiceExtensionContext.startAbilityWithAccount
237
238startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
239
240Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result.
241
242Observe the following when using this API:
243 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
244 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
245 - 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).
246
247**System capability**: SystemCapability.Ability.AbilityRuntime.Core
248
249**System API**: This is a system API and cannot be called by third-party applications.
250
251**Parameters**
252
253| Name| Type| Mandatory| Description|
254| -------- | -------- | -------- | -------- |
255| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
256| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
257| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
258
259**Error codes**
260
261| ID| Error Message|
262| ------- | -------------------------------- |
263| 16000001 | The specified ability does not exist. |
264| 16000002 | Incorrect ability type. |
265| 16000004 | Can not start invisible component. |
266| 16000005 | The specified process does not have the permission. |
267| 16000006 | Cross-user operations are not allowed. |
268| 16000008 | The crowdtesting application expires. |
269| 16000009 | An ability cannot be started or stopped in Wukong mode. |
270| 16000010 | The call with the continuation flag is forbidden.        |
271| 16000011 | The context does not exist.        |
272| 16000050 | Internal error. |
273| 16000053 | The ability is not on the top of the UI. |
274| 16000055 | Installation-free timed out. |
275| 16200001 | The caller has been released. |
276
277For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
278
279**Example**
280
281  ```ts
282  let want = {
283    deviceId: '',
284    bundleName: 'com.extreme.test',
285    abilityName: 'MainAbility'
286  };
287  let accountId = 100;
288
289  try {
290    this.context.startAbilityWithAccount(want, accountId, (error) => {
291      if (error.code) {
292        // Process service logic errors.
293        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
294          ' error.message: ' + JSON.stringify(error.message));
295        return;
296      }
297      // Carry out normal service processing.
298      console.log('startAbilityWithAccount succeed');
299    });
300  } catch (paramError) {
301    // Process input parameter errors.
302    console.log('error.code: ' + JSON.stringify(paramError.code) +
303      ' error.message: ' + JSON.stringify(paramError.message));
304  }
305  ```
306
307## ServiceExtensionContext.startAbilityWithAccount
308
309startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
310
311Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.
312
313Observe the following when using this API:
314 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
315 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
316 - 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).
317
318**System capability**: SystemCapability.Ability.AbilityRuntime.Core
319
320**System API**: This is a system API and cannot be called by third-party applications.
321
322**Parameters**
323
324| Name| Type| Mandatory| Description|
325| -------- | -------- | -------- | -------- |
326| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
327| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
328| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
329| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
330
331**Error codes**
332
333| ID| Error Message|
334| ------- | -------------------------------- |
335| 16000001 | The specified ability does not exist. |
336| 16000002 | Incorrect ability type. |
337| 16000004 | Can not start invisible component. |
338| 16000005 | The specified process does not have the permission. |
339| 16000006 | Cross-user operations are not allowed. |
340| 16000008 | The crowdtesting application expires. |
341| 16000009 | An ability cannot be started or stopped in Wukong mode. |
342| 16000010 | The call with the continuation flag is forbidden.        |
343| 16000011 | The context does not exist.        |
344| 16000050 | Internal error. |
345| 16000053 | The ability is not on the top of the UI. |
346| 16000055 | Installation-free timed out. |
347| 16200001 | The caller has been released. |
348
349For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
350
351**Example**
352
353  ```ts
354  let want = {
355    deviceId: '',
356    bundleName: 'com.extreme.test',
357    abilityName: 'MainAbility'
358  };
359  let accountId = 100;
360  let options = {
361    windowMode: 0
362  };
363
364  try {
365    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
366      if (error.code) {
367        // Process service logic errors.
368        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
369          ' error.message: ' + JSON.stringify(error.message));
370        return;
371      }
372      // Carry out normal service processing.
373      console.log('startAbilityWithAccount succeed');
374    });
375  } catch (paramError) {
376    // Process input parameter errors.
377    console.log('error.code: ' + JSON.stringify(paramError.code) +
378      ' error.message: ' + JSON.stringify(paramError.message));
379  }
380  ```
381
382
383## ServiceExtensionContext.startAbilityWithAccount
384
385startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
386
387Starts an ability with the account ID specified. This API uses a promise to return the result.
388
389Observe the following when using this API:
390 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
391 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
392 - 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).
393
394**System capability**: SystemCapability.Ability.AbilityRuntime.Core
395
396**System API**: This is a system API and cannot be called by third-party applications.
397
398**Parameters**
399
400| Name| Type| Mandatory| Description|
401| -------- | -------- | -------- | -------- |
402| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
403| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
404| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
405
406**Return value**
407
408| Type| Description|
409| -------- | -------- |
410| Promise&lt;void&gt; | Promise used to return the result.|
411
412**Error codes**
413
414| ID| Error Message|
415| ------- | -------------------------------- |
416| 16000001 | The specified ability does not exist. |
417| 16000002 | Incorrect ability type. |
418| 16000004 | Can not start invisible component. |
419| 16000005 | The specified process does not have the permission. |
420| 16000006 | Cross-user operations are not allowed. |
421| 16000008 | The crowdtesting application expires. |
422| 16000009 | An ability cannot be started or stopped in Wukong mode. |
423| 16000010 | The call with the continuation flag is forbidden.        |
424| 16000011 | The context does not exist.        |
425| 16000050 | Internal error. |
426| 16000053 | The ability is not on the top of the UI. |
427| 16000055 | Installation-free timed out. |
428| 16200001 | The caller has been released. |
429
430For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
431
432**Example**
433
434  ```ts
435  let want = {
436    deviceId: '',
437    bundleName: 'com.extreme.test',
438    abilityName: 'MainAbility'
439  };
440  let accountId = 100;
441  let options = {
442    windowMode: 0
443  };
444
445  try {
446    this.context.startAbilityWithAccount(want, accountId, options)
447      .then((data) => {
448        // Carry out normal service processing.
449        console.log('startAbilityWithAccount succeed');
450      })
451      .catch((error) => {
452        // Process service logic errors.
453        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
454          ' error.message: ' + JSON.stringify(error.message));
455      });
456  } catch (paramError) {
457    // Process input parameter errors.
458    console.log('error.code: ' + JSON.stringify(paramError.code) +
459      ' error.message: ' + JSON.stringify(paramError.message));
460  }
461  ```
462
463## ServiceExtensionContext.startServiceExtensionAbility
464
465startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
466
467Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
468
469**System capability**: SystemCapability.Ability.AbilityRuntime.Core
470
471**System API**: This is a system API and cannot be called by third-party applications.
472
473**Parameters**
474
475| Name| Type| Mandatory| Description|
476| -------- | -------- | -------- | -------- |
477| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
478| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
479
480**Error codes**
481
482| ID| Error Message|
483| ------- | -------------------------------- |
484| 16000001 | The specified ability does not exist. |
485| 16000002 | Incorrect ability type. |
486| 16000005 | The specified process does not have the permission. |
487| 16000006 | Cross-user operations are not allowed. |
488| 16000008 | The crowdtesting application expires. |
489| 16000011 | The context does not exist.        |
490| 16000050 | Internal error. |
491| 16200001 | The caller has been released. |
492
493For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
494
495**Example**
496
497  ```ts
498  let want = {
499    deviceId: '',
500    bundleName: 'com.extreme.test',
501    abilityName: 'MainAbility'
502  };
503
504  try {
505    this.context.startServiceExtensionAbility(want, (error) => {
506      if (error.code) {
507        // Process service logic errors.
508        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
509          ' error.message: ' + JSON.stringify(error.message));
510        return;
511      }
512      // Carry out normal service processing.
513      console.log('startServiceExtensionAbility succeed');
514    });
515  } catch (paramError) {
516    // Process input parameter errors.
517    console.log('error.code: ' + JSON.stringify(paramError.code) +
518      ' error.message: ' + JSON.stringify(paramError.message));
519  }
520  ```
521
522## ServiceExtensionContext.startServiceExtensionAbility
523
524startServiceExtensionAbility(want: Want): Promise\<void>;
525
526Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
527
528**System capability**: SystemCapability.Ability.AbilityRuntime.Core
529
530**System API**: This is a system API and cannot be called by third-party applications.
531
532**Parameters**
533
534| Name| Type| Mandatory| Description|
535| -------- | -------- | -------- | -------- |
536| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
537
538**Return value**
539
540| Type| Description|
541| -------- | -------- |
542| Promise&lt;void&gt; | Promise used to return the result.|
543
544**Error codes**
545
546| ID| Error Message|
547| ------- | -------------------------------- |
548| 16000001 | The specified ability does not exist. |
549| 16000002 | Incorrect ability type. |
550| 16000005 | The specified process does not have the permission. |
551| 16000006 | Cross-user operations are not allowed. |
552| 16000008 | The crowdtesting application expires. |
553| 16000011 | The context does not exist.        |
554| 16000050 | Internal error. |
555| 16200001 | The caller has been released. |
556
557For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
558
559**Example**
560
561  ```ts
562  let want = {
563    deviceId: '',
564    bundleName: 'com.extreme.test',
565    abilityName: 'MainAbility'
566  };
567
568  try {
569    this.context.startServiceExtensionAbility(want)
570      .then((data) => {
571        // Carry out normal service processing.
572        console.log('startServiceExtensionAbility succeed');
573      })
574      .catch((error) => {
575        // Process service logic errors.
576        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
577          ' error.message: ' + JSON.stringify(error.message));
578      });
579  } catch (paramError) {
580    // Process input parameter errors.
581    console.log('error.code: ' + JSON.stringify(paramError.code) +
582      ' error.message: ' + JSON.stringify(paramError.message));
583  }
584  ```
585
586## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
587
588startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
589
590Starts a new ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result.
591
592**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
593
594**System capability**: SystemCapability.Ability.AbilityRuntime.Core
595
596**System API**: This is a system API and cannot be called by third-party applications.
597
598**Parameters**
599
600| Name| Type| Mandatory| Description|
601| -------- | -------- | -------- | -------- |
602| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
603| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
604| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
605
606**Error codes**
607
608| ID| Error Message|
609| ------- | -------------------------------- |
610| 16000001 | The specified ability does not exist. |
611| 16000002 | Incorrect ability type. |
612| 16000005 | The specified process does not have the permission. |
613| 16000006 | Cross-user operations are not allowed. |
614| 16000008 | The crowdtesting application expires. |
615| 16000011 | The context does not exist.        |
616| 16000050 | Internal error. |
617| 16200001 | The caller has been released. |
618
619For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
620
621**Example**
622
623  ```ts
624  let want = {
625    deviceId: '',
626    bundleName: 'com.extreme.test',
627    abilityName: 'MainAbility'
628  };
629  let accountId = 100;
630
631  try {
632    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
633      if (error.code) {
634        // Process service logic errors.
635        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
636          ' error.message: ' + JSON.stringify(error.message));
637        return;
638      }
639      // Carry out normal service processing.
640      console.log('startServiceExtensionAbilityWithAccount succeed');
641    });
642  } catch (paramError) {
643    // Process input parameter errors.
644    console.log('error.code: ' + JSON.stringify(paramError.code) +
645      ' error.message: ' + JSON.stringify(paramError.message));
646  }
647  ```
648
649## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
650
651startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
652
653Starts a new ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result.
654
655**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
656
657**System capability**: SystemCapability.Ability.AbilityRuntime.Core
658
659**System API**: This is a system API and cannot be called by third-party applications.
660
661**Parameters**
662
663| Name| Type| Mandatory| Description|
664| -------- | -------- | -------- | -------- |
665| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
666| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
667
668**Return value**
669
670| Type| Description|
671| -------- | -------- |
672| Promise&lt;void&gt; | Promise used to return the result.|
673
674**Error codes**
675
676| ID| Error Message|
677| ------- | -------------------------------- |
678| 16000001 | The specified ability does not exist. |
679| 16000002 | Incorrect ability type. |
680| 16000005 | The specified process does not have the permission. |
681| 16000006 | Cross-user operations are not allowed. |
682| 16000008 | The crowdtesting application expires. |
683| 16000011 | The context does not exist.        |
684| 16000050 | Internal error. |
685| 16200001 | The caller has been released. |
686
687For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
688
689**Example**
690
691  ```ts
692  let want = {
693    deviceId: '',
694    bundleName: 'com.extreme.test',
695    abilityName: 'MainAbility'
696  };
697  let accountId = 100;
698
699  try {
700    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
701      .then((data) => {
702        // Carry out normal service processing.
703        console.log('startServiceExtensionAbilityWithAccount succeed');
704      })
705      .catch((error) => {
706        // Process service logic errors.
707        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
708          ' error.message: ' + JSON.stringify(error.message));
709      });
710  } catch (paramError) {
711    // Process input parameter errors.
712    console.log('error.code: ' + JSON.stringify(paramError.code) +
713      ' error.message: ' + JSON.stringify(paramError.message));
714  }
715  ```
716
717## ServiceExtensionContext.stopServiceExtensionAbility
718
719stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
720
721Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
722
723**System capability**: SystemCapability.Ability.AbilityRuntime.Core
724
725**System API**: This is a system API and cannot be called by third-party applications.
726
727**Parameters**
728
729| Name| Type| Mandatory| Description|
730| -------- | -------- | -------- | -------- |
731| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
732| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
733
734**Error codes**
735
736| ID| Error Message|
737| ------- | -------------------------------- |
738| 16000001 | The specified ability does not exist. |
739| 16000002 | Incorrect ability type. |
740| 16000005 | The specified process does not have the permission. |
741| 16000006 | Cross-user operations are not allowed. |
742| 16000011 | The context does not exist.        |
743| 16000050 | Internal error. |
744| 16200001 | The caller has been released. |
745
746For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
747
748**Example**
749
750  ```ts
751  let want = {
752    deviceId: '',
753    bundleName: 'com.extreme.test',
754    abilityName: 'MainAbility'
755  };
756
757  try {
758    this.context.stopServiceExtensionAbility(want, (error) => {
759      if (error.code) {
760        // Process service logic errors.
761        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
762          ' error.message: ' + JSON.stringify(error.message));
763        return;
764      }
765      // Carry out normal service processing.
766      console.log('stopServiceExtensionAbility succeed');
767    });
768  } catch (paramError) {
769    // Process input parameter errors.
770    console.log('error.code: ' + JSON.stringify(paramError.code) +
771      ' error.message: ' + JSON.stringify(paramError.message));
772  }
773  ```
774
775## ServiceExtensionContext.stopServiceExtensionAbility
776
777stopServiceExtensionAbility(want: Want): Promise\<void>;
778
779Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
780
781**System capability**: SystemCapability.Ability.AbilityRuntime.Core
782
783**System API**: This is a system API and cannot be called by third-party applications.
784
785**Parameters**
786
787| Name| Type| Mandatory| Description|
788| -------- | -------- | -------- | -------- |
789| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
790
791**Return value**
792
793| Type| Description|
794| -------- | -------- |
795| Promise&lt;void&gt; | Promise used to return the result.|
796
797**Error codes**
798
799| ID| Error Message|
800| ------- | -------------------------------- |
801| 16000001 | The specified ability does not exist. |
802| 16000002 | Incorrect ability type. |
803| 16000005 | The specified process does not have the permission. |
804| 16000006 | Cross-user operations are not allowed. |
805| 16000011 | The context does not exist.        |
806| 16000050 | Internal error. |
807| 16200001 | The caller has been released. |
808
809For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
810
811**Example**
812
813  ```ts
814  let want = {
815    deviceId: '',
816    bundleName: 'com.extreme.test',
817    abilityName: 'MainAbility'
818  };
819
820  try {
821    this.context.stopServiceExtensionAbility(want)
822      .then((data) => {
823        // Carry out normal service processing.
824        console.log('stopServiceExtensionAbility succeed');
825      })
826      .catch((error) => {
827        // Process service logic errors.
828        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
829          ' error.message: ' + JSON.stringify(error.message));
830      });
831  } catch (paramError) {
832    // Process input parameter errors.
833    console.log('error.code: ' + JSON.stringify(paramError.code) +
834      ' error.message: ' + JSON.stringify(paramError.message));
835  }
836  ```
837
838## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
839
840stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
841
842Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses an asynchronous callback to return the result.
843
844**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
845
846**System capability**: SystemCapability.Ability.AbilityRuntime.Core
847
848**System API**: This is a system API and cannot be called by third-party applications.
849
850**Parameters**
851
852| Name| Type| Mandatory| Description|
853| -------- | -------- | -------- | -------- |
854| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
855| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
856| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
857
858**Error codes**
859
860| ID| Error Message|
861| ------- | -------------------------------- |
862| 16000001 | The specified ability does not exist. |
863| 16000002 | Incorrect ability type. |
864| 16000005 | The specified process does not have the permission. |
865| 16000006 | Cross-user operations are not allowed. |
866| 16000011 | The context does not exist.        |
867| 16000050 | Internal error. |
868| 16200001 | The caller has been released. |
869
870For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
871
872**Example**
873
874  ```ts
875  let want = {
876    deviceId: '',
877    bundleName: 'com.extreme.test',
878    abilityName: 'MainAbility'
879  };
880  let accountId = 100;
881
882  try {
883    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
884      if (error.code) {
885        // Process service logic errors.
886        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
887          ' error.message: ' + JSON.stringify(error.message));
888        return;
889      }
890      // Carry out normal service processing.
891      console.log('stopServiceExtensionAbilityWithAccount succeed');
892    });
893  } catch (paramError) {
894    // Process input parameter errors.
895    console.log('error.code: ' + JSON.stringify(paramError.code) +
896      ' error.message: ' + JSON.stringify(paramError.message));
897  }
898  ```
899
900## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
901
902stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
903
904Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses a promise to return the result.
905
906**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
907
908**System capability**: SystemCapability.Ability.AbilityRuntime.Core
909
910**System API**: This is a system API and cannot be called by third-party applications.
911
912**Parameters**
913
914| Name| Type| Mandatory| Description|
915| -------- | -------- | -------- | -------- |
916| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
917| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
918
919**Return value**
920
921| Type| Description|
922| -------- | -------- |
923| Promise&lt;void&gt; | Promise used to return the result.|
924
925**Error codes**
926
927| ID| Error Message|
928| ------- | -------------------------------- |
929| 16000001 | The specified ability does not exist. |
930| 16000002 | Incorrect ability type. |
931| 16000005 | The specified process does not have the permission. |
932| 16000006 | Cross-user operations are not allowed. |
933| 16000011 | The context does not exist.        |
934| 16000050 | Internal error. |
935| 16200001 | The caller has been released. |
936
937For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
938
939**Example**
940
941  ```ts
942  let want = {
943    deviceId: '',
944    bundleName: 'com.extreme.test',
945    abilityName: 'MainAbility'
946  };
947  let accountId = 100;
948
949  try {
950    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
951      .then((data) => {
952        // Carry out normal service processing.
953        console.log('stopServiceExtensionAbilityWithAccount succeed');
954      })
955      .catch((error) => {
956        // Process service logic errors.
957        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
958          ' error.message: ' + JSON.stringify(error.message));
959      });
960  } catch (paramError) {
961    // Process input parameter errors.
962    console.log('error.code: ' + JSON.stringify(paramError.code) +
963      ' error.message: ' + JSON.stringify(paramError.message));
964  }
965  ```
966
967## ServiceExtensionContext.terminateSelf
968
969terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
970
971Terminates this ability. This API uses an asynchronous callback to return the result.
972
973**System capability**: SystemCapability.Ability.AbilityRuntime.Core
974
975**System API**: This is a system API and cannot be called by third-party applications.
976
977**Parameters**
978
979| Name| Type| Mandatory| Description|
980| -------- | -------- | -------- | -------- |
981| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
982
983**Error codes**
984
985| ID| Error Message|
986| ------- | -------------------------------- |
987| 16000001 | The specified ability does not exist. |
988| 16000004 | Can not start invisible component. |
989| 16000005 | The specified process does not have the permission. |
990| 16000009 | An ability cannot be started or stopped in Wukong mode. |
991| 16000011 | The context does not exist.        |
992| 16000050 | Internal error. |
993
994For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
995
996**Example**
997
998  ```ts
999  this.context.terminateSelf((error) => {
1000    if (error.code) {
1001      // Process service logic errors.
1002      console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1003        ' error.message: ' + JSON.stringify(error.message));
1004      return;
1005    }
1006    // Carry out normal service processing.
1007    console.log('terminateSelf succeed');
1008  });
1009  ```
1010
1011## ServiceExtensionContext.terminateSelf
1012
1013terminateSelf(): Promise&lt;void&gt;;
1014
1015Terminates this ability. This API uses a promise to return the result.
1016
1017**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1018
1019**System API**: This is a system API and cannot be called by third-party applications.
1020
1021**Return value**
1022
1023| Type| Description|
1024| -------- | -------- |
1025| Promise&lt;void&gt; | Promise used to return the result.|
1026
1027**Error codes**
1028
1029| ID| Error Message|
1030| ------- | -------------------------------- |
1031| 16000001 | The specified ability does not exist. |
1032| 16000004 | Can not start invisible component. |
1033| 16000005 | The specified process does not have the permission. |
1034| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1035| 16000011 | The context does not exist.        |
1036| 16000050 | Internal error. |
1037
1038For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1039
1040**Example**
1041
1042  ```ts
1043  this.context.terminateSelf().then((data) => {
1044    // Carry out normal service processing.
1045    console.log('terminateSelf succeed');
1046  }).catch((error) => {
1047    // Process service logic errors.
1048    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1049      ' error.message: ' + JSON.stringify(error.message));
1050  });
1051  ```
1052
1053## ServiceExtensionContext.connectServiceExtensionAbility
1054
1055connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1056
1057Connects this ability to a ServiceExtensionAbility.
1058
1059**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1060
1061**System API**: This is a system API and cannot be called by third-party applications.
1062
1063**Parameters**
1064
1065| Name| Type| Mandatory| Description|
1066| -------- | -------- | -------- | -------- |
1067| want | [Want](js-apis-application-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
1068| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
1069
1070**Return value**
1071
1072| Type| Description|
1073| -------- | -------- |
1074| number | A number, based on which the connection will be interrupted.|
1075
1076**Error codes**
1077
1078| ID| Error Message|
1079| ------- | -------------------------------- |
1080| 16000001 | Input error. The specified ability name does not exist. |
1081| 16000005 | The specified process does not have the permission. |
1082| 16000011 | The context does not exist.        |
1083| 16000050 | Internal Error. |
1084
1085For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1086
1087**Example**
1088
1089  ```ts
1090  let want = {
1091    bundleName: 'com.example.myapp',
1092    abilityName: 'MyAbility'
1093  };
1094  let options = {
1095    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
1096    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
1097    onFailed(code) { console.log('----------- onFailed -----------') }
1098  };
1099
1100  let connection = null;
1101  try {
1102    connection = this.context.connectServiceExtensionAbility(want, options);
1103  } catch (paramError) {
1104    // Process input parameter errors.
1105    console.log('error.code: ' + JSON.stringify(paramError.code) +
1106      ' error.message: ' + JSON.stringify(paramError.message));
1107  }
1108  ```
1109
1110## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1111
1112connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1113
1114Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability.
1115
1116**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1117
1118**System API**: This is a system API and cannot be called by third-party applications.
1119
1120**Parameters**
1121
1122| Name| Type| Mandatory| Description|
1123| -------- | -------- | -------- | -------- |
1124| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.|
1125| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1126| options | ConnectOptions | No| Remote object instance.|
1127
1128**Return value**
1129
1130| Type| Description|
1131| -------- | -------- |
1132| number | Result code of the ability connection.|
1133
1134**Error codes**
1135
1136| ID| Error Message|
1137| ------- | -------------------------------- |
1138| 16000001 | Input error. The specified ability name does not exist. |
1139| 16000005 | The specified process does not have the permission. |
1140| 16000011 | The context does not exist.        |
1141| 16000050 | Internal Error. |
1142
1143For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1144
1145**Example**
1146
1147  ```ts
1148  let want = {
1149    deviceId: '',
1150    bundleName: 'com.extreme.test',
1151    abilityName: 'MainAbility'
1152  };
1153  let accountId = 100;
1154  let options = {
1155    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
1156    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
1157    onFailed(code) { console.log('----------- onFailed -----------') }
1158  }
1159
1160  let connection = null;
1161  try {
1162    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
1163  } catch (paramError) {
1164    // Process input parameter errors.
1165    console.log('error.code: ' + JSON.stringify(paramError.code) +
1166      ' error.message: ' + JSON.stringify(paramError.message));
1167  }
1168  ```
1169
1170## ServiceExtensionContext.disconnectServiceExtensionAbility
1171
1172disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
1173
1174Disconnects this ability from the ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
1175
1176**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1177
1178**System API**: This is a system API and cannot be called by third-party applications.
1179
1180**Parameters**
1181
1182| Name| Type| Mandatory| Description|
1183| -------- | -------- | -------- | -------- |
1184| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1185| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
1186
1187**Error codes**
1188
1189| ID| Error Message|
1190| ------- | -------------------------------- |
1191| 16000011 | The context does not exist.        |
1192| 16000050 | Internal Error. |
1193
1194For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1195
1196**Example**
1197
1198  ```ts
1199  // connection is the return value of connectServiceExtensionAbility.
1200  let connection = 1;
1201
1202  try {
1203    this.context.disconnectServiceExtensionAbility(connection, (error) => {
1204      if (error.code) {
1205        // Process service logic errors.
1206        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1207          ' error.message: ' + JSON.stringify(error.message));
1208        return;
1209      }
1210      // Carry out normal service processing.
1211      console.log('disconnectServiceExtensionAbility succeed');
1212    });
1213  } catch (paramError) {
1214    // Process input parameter errors.
1215    console.log('error.code: ' + JSON.stringify(paramError.code) +
1216      ' error.message: ' + JSON.stringify(paramError.message));
1217  }
1218  ```
1219
1220## ServiceExtensionContext.disconnectServiceExtensionAbility
1221
1222disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
1223
1224Disconnects this ability from the ServiceExtensionAbility. This API uses a promise to return the result.
1225
1226**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1227
1228**System API**: This is a system API and cannot be called by third-party applications.
1229
1230**Parameters**
1231
1232| Name| Type| Mandatory| Description|
1233| -------- | -------- | -------- | -------- |
1234| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1235
1236**Return value**
1237
1238| Type| Description|
1239| -------- | -------- |
1240| Promise&lt;void&gt; | Promise used to return the result.|
1241
1242**Error codes**
1243
1244| ID| Error Message|
1245| ------- | -------------------------------- |
1246| 16000011 | The context does not exist.        |
1247| 16000050 | Internal Error. |
1248
1249For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1250
1251**Example**
1252
1253  ```ts
1254  // connection is the return value of connectServiceExtensionAbility.
1255  let connection = 1;
1256
1257  try {
1258    this.context.disconnectServiceExtensionAbility(connection)
1259      .then((data) => {
1260        // Carry out normal service processing.
1261        console.log('disconnectServiceExtensionAbility succeed');
1262      })
1263      .catch((error) => {
1264        // Process service logic errors.
1265        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1266          ' error.message: ' + JSON.stringify(error.message));
1267      });
1268  } catch (paramError) {
1269    // Process input parameter errors.
1270    console.log('error.code: ' + JSON.stringify(paramError.code) +
1271      ' error.message: ' + JSON.stringify(paramError.message));
1272  }
1273  ```
1274
1275## ServiceExtensionContext.startAbilityByCall
1276
1277startAbilityByCall(want: Want): Promise&lt;Caller&gt;;
1278
1279Starts an ability in the foreground or background and obtains the caller object for communicating with the ability.
1280
1281Observe the following when using this API:
1282 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
1283 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1284 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
1285
1286**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1287
1288**System API**: This is a system API and cannot be called by third-party applications.
1289
1290**Parameters**
1291
1292| Name| Type| Mandatory| Description|
1293| -------- | -------- | -------- | -------- |
1294| want | [Want](js-apis-application-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId** (optional), and **parameters** (optional). If **deviceId** is left blank or null, the local ability is started. If **parameters** is left blank or null, the ability is started in the background.|
1295
1296**Return value**
1297
1298| Type| Description|
1299| -------- | -------- |
1300| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
1301
1302**Error codes**
1303
1304| ID| Error Message|
1305| ------- | -------------------------------- |
1306| 16000001 | Input error. The specified ability name does not exist. |
1307| 16000002 | Incorrect ability type. |
1308| 16000004 | Visibility verification failed. |
1309| 16000005 | Static permission denied. The specified process does not have the permission. |
1310| 16000006 | Cross-user operations are not allowed. |
1311| 16000008 | Crowdtest App Expiration. |
1312| 16000011 | The context does not exist. |
1313| 16000050 | Internal Error. |
1314| 16200001 | The caller has been released.        |
1315
1316For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1317
1318**Example**
1319
1320  Start an ability in the background.
1321
1322  ```ts
1323  let caller = undefined;
1324
1325  // Start an ability in the background by not passing parameters.
1326  let wantBackground = {
1327      bundleName: 'com.example.myservice',
1328      moduleName: 'entry',
1329      abilityName: 'MainAbility',
1330      deviceId: ''
1331  };
1332
1333  try {
1334    this.context.startAbilityByCall(wantBackground)
1335      .then((obj) => {
1336        // Carry out normal service processing.
1337        caller = obj;
1338        console.log('startAbilityByCall succeed');
1339      }).catch((error) => {
1340        // Process service logic errors.
1341        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
1342          ' error.message: ' + JSON.stringify(error.message));
1343      });
1344  } catch (paramError) {
1345    // Process input parameter errors.
1346    console.log('error.code: ' + JSON.stringify(paramError.code) +
1347      ' error.message: ' + JSON.stringify(paramError.message));
1348  }
1349  ```
1350
1351  Start an ability in the foreground.
1352
1353  ```ts
1354  let caller = undefined;
1355
1356  // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true.
1357  let wantForeground = {
1358      bundleName: 'com.example.myservice',
1359      moduleName: 'entry',
1360      abilityName: 'MainAbility',
1361      deviceId: '',
1362      parameters: {
1363        'ohos.aafwk.param.callAbilityToForeground': true
1364      }
1365  };
1366
1367  try {
1368    this.context.startAbilityByCall(wantForeground)
1369      .then((obj) => {
1370        // Carry out normal service processing.
1371        caller = obj;
1372        console.log('startAbilityByCall succeed');
1373      }).catch((error) => {
1374        // Process service logic errors.
1375        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
1376          ' error.message: ' + JSON.stringify(error.message));
1377      });
1378  } catch (paramError) {
1379    // Process input parameter errors.
1380    console.log('error.code: ' + JSON.stringify(paramError.code) +
1381      ' error.message: ' + JSON.stringify(paramError.message));
1382  }
1383  ```
1384