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