• 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  import rpc from '@ohos.rpc';
25
26  let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected.
27  class EntryAbility extends ServiceExtensionAbility {
28    onCreate() {
29        let context = this.context; // Obtain a ServiceExtensionContext instance.
30    }
31  }
32```
33
34## ServiceExtensionContext.startAbility
35
36startAbility(want: Want, callback: AsyncCallback<void>): void;
37
38Starts an ability. This API uses an asynchronous callback to return the result.
39
40**System capability**: SystemCapability.Ability.AbilityRuntime.Core
41
42**System API**: This is a system API and cannot be called by third-party applications.
43
44**Parameters**
45
46| Name| Type| Mandatory| Description|
47| -------- | -------- | -------- | -------- |
48| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
49| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
50
51**Error codes**
52
53| ID| Error Message|
54| ------- | -------------------------------- |
55| 16000001 | The specified ability does not exist. |
56| 16000002 | Incorrect ability type. |
57| 16000004 | Can not start invisible component. |
58| 16000005 | The specified process does not have the permission. |
59| 16000006 | Cross-user operations are not allowed. |
60| 16000008 | The crowdtesting application expires. |
61| 16000009 | An ability cannot be started or stopped in Wukong mode. |
62| 16000010 | The call with the continuation flag is forbidden.        |
63| 16000011 | The context does not exist.        |
64| 16000012 | The application is controlled.        |
65| 16000013 | The application is controlled by EDM.       |
66| 16000050 | Internal error. |
67| 16000053 | The ability is not on the top of the UI. |
68| 16000055 | Installation-free timed out. |
69| 16200001 | The caller has been released. |
70
71For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
72
73**Example**
74
75  ```ts
76  import Want from '@ohos.app.ability.Want';
77  import { BusinessError } from '@ohos.base';
78
79  let want: Want = {
80    bundleName: 'com.example.myapp',
81    abilityName: 'MyAbility'
82  };
83
84  try {
85    this.context.startAbility(want, (error: BusinessError) => {
86      if (error.code) {
87        // Process service logic errors.
88        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
89        return;
90      }
91      // Carry out normal service processing.
92      console.log('startAbility succeed');
93    });
94  } catch (paramError) {
95    // Process input parameter errors.
96    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
97  }
98  ```
99
100## ServiceExtensionContext.startAbility
101
102startAbility(want: Want, options?: StartOptions): Promise\<void>;
103
104Starts an ability. This API uses a promise to return the result.
105
106**System capability**: SystemCapability.Ability.AbilityRuntime.Core
107
108**System API**: This is a system API and cannot be called by third-party applications.
109
110**Parameters**
111
112| Name| Type| Mandatory| Description|
113| -------- | -------- | -------- | -------- |
114| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
115| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
116
117**Return value**
118
119| Type| Description|
120| -------- | -------- |
121| Promise&lt;void&gt; | Promise used to return the result.|
122
123**Error codes**
124
125| ID| Error Message|
126| ------- | -------------------------------- |
127| 16000001 | The specified ability does not exist. |
128| 16000002 | Incorrect ability type. |
129| 16000004 | Can not start invisible component. |
130| 16000005 | The specified process does not have the permission. |
131| 16000006 | Cross-user operations are not allowed. |
132| 16000008 | The crowdtesting application expires. |
133| 16000009 | An ability cannot be started or stopped in Wukong mode. |
134| 16000010 | The call with the continuation flag is forbidden.        |
135| 16000011 | The context does not exist.        |
136| 16000012 | The application is controlled.        |
137| 16000013 | The application is controlled by EDM.       |
138| 16000050 | Internal error. |
139| 16000053 | The ability is not on the top of the UI. |
140| 16000055 | Installation-free timed out. |
141| 16200001 | The caller has been released. |
142
143For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
144
145**Example**
146
147  ```ts
148  import Want from '@ohos.app.ability.Want';
149  import StartOptions from '@ohos.app.ability.StartOptions';
150  import { BusinessError } from '@ohos.base';
151
152  let want: Want = {
153    bundleName: 'com.example.myapp',
154    abilityName: 'MyAbility'
155  };
156  let options: StartOptions = {
157  	windowMode: 0,
158  };
159
160  try {
161    this.context.startAbility(want, options)
162      .then((data: void) => {
163        // Carry out normal service processing.
164        console.log('startAbility succeed');
165      })
166      .catch((error: BusinessError) => {
167        // Process service logic errors.
168        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
169      });
170  } catch (paramError) {
171    // Process input parameter errors.
172    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
173  }
174  ```
175
176## ServiceExtensionContext.startAbility
177
178startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
179
180Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
181
182**System capability**: SystemCapability.Ability.AbilityRuntime.Core
183
184**System API**: This is a system API and cannot be called by third-party applications.
185
186**Parameters**
187
188| Name| Type| Mandatory| Description|
189| -------- | -------- | -------- | -------- |
190| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
191| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
192| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
193
194**Error codes**
195
196| ID| Error Message|
197| ------- | -------------------------------- |
198| 16000001 | The specified ability does not exist. |
199| 16000002 | Incorrect ability type. |
200| 16000004 | Can not start invisible component. |
201| 16000005 | The specified process does not have the permission. |
202| 16000006 | Cross-user operations are not allowed. |
203| 16000008 | The crowdtesting application expires. |
204| 16000009 | An ability cannot be started or stopped in Wukong mode. |
205| 16000010 | The call with the continuation flag is forbidden.        |
206| 16000011 | The context does not exist.        |
207| 16000012 | The application is controlled.        |
208| 16000013 | The application is controlled by EDM.       |
209| 16000050 | Internal error. |
210| 16000053 | The ability is not on the top of the UI. |
211| 16000055 | Installation-free timed out. |
212| 16200001 | The caller has been released. |
213
214For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
215
216**Example**
217
218  ```ts
219  import Want from '@ohos.app.ability.Want';
220  import StartOptions from '@ohos.app.ability.StartOptions';
221  import { BusinessError } from '@ohos.base';
222
223  let want: Want = {
224    deviceId: '',
225    bundleName: 'com.example.myapplication',
226    abilityName: 'EntryAbility'
227  };
228  let options: StartOptions = {
229    windowMode: 0
230  };
231
232  try {
233    this.context.startAbility(want, options, (error: BusinessError) => {
234      if (error.code) {
235        // Process service logic errors.
236        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
237        return;
238      }
239      // Carry out normal service processing.
240      console.log('startAbility succeed');
241    });
242  } catch (paramError) {
243    // Process input parameter errors.
244    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
245  }
246  ```
247
248## ServiceExtensionContext.startAbilityWithAccount
249
250startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
251
252Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result.
253
254Observe the following when using this API:
255 - 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.
256 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
257 - 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).
258
259**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
260
261**System capability**: SystemCapability.Ability.AbilityRuntime.Core
262
263**System API**: This is a system API and cannot be called by third-party applications.
264
265**Parameters**
266
267| Name| Type| Mandatory| Description|
268| -------- | -------- | -------- | -------- |
269| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
270| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
271| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
272
273**Error codes**
274
275| ID| Error Message|
276| ------- | -------------------------------- |
277| 16000001 | The specified ability does not exist. |
278| 16000002 | Incorrect ability type. |
279| 16000004 | Can not start invisible component. |
280| 16000005 | The specified process does not have the permission. |
281| 16000006 | Cross-user operations are not allowed. |
282| 16000008 | The crowdtesting application expires. |
283| 16000009 | An ability cannot be started or stopped in Wukong mode. |
284| 16000010 | The call with the continuation flag is forbidden.        |
285| 16000011 | The context does not exist.        |
286| 16000012 | The application is controlled.        |
287| 16000013 | The application is controlled by EDM.       |
288| 16000050 | Internal error. |
289| 16000053 | The ability is not on the top of the UI. |
290| 16000055 | Installation-free timed out. |
291| 16200001 | The caller has been released. |
292
293For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
294
295**Example**
296
297  ```ts
298  import Want from '@ohos.app.ability.Want';
299  import { BusinessError } from '@ohos.base';
300
301  let want: Want = {
302    deviceId: '',
303    bundleName: 'com.example.myapplication',
304    abilityName: 'EntryAbility'
305  };
306  let accountId = 100;
307
308  try {
309    this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => {
310      if (error.code) {
311        // Process service logic errors.
312        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
313        return;
314      }
315      // Carry out normal service processing.
316      console.log('startAbilityWithAccount succeed');
317    });
318  } catch (paramError) {
319    // Process input parameter errors.
320    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
321  }
322  ```
323
324## ServiceExtensionContext.startAbilityWithAccount
325
326startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
327
328Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.
329
330Observe the following when using this API:
331 - 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.
332 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
333 - 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).
334
335**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
336
337**System capability**: SystemCapability.Ability.AbilityRuntime.Core
338
339**System API**: This is a system API and cannot be called by third-party applications.
340
341**Parameters**
342
343| Name| Type| Mandatory| Description|
344| -------- | -------- | -------- | -------- |
345| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
346| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
347| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
348| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
349
350**Error codes**
351
352| ID| Error Message|
353| ------- | -------------------------------- |
354| 16000001 | The specified ability does not exist. |
355| 16000002 | Incorrect ability type. |
356| 16000004 | Can not start invisible component. |
357| 16000005 | The specified process does not have the permission. |
358| 16000006 | Cross-user operations are not allowed. |
359| 16000008 | The crowdtesting application expires. |
360| 16000009 | An ability cannot be started or stopped in Wukong mode. |
361| 16000010 | The call with the continuation flag is forbidden.        |
362| 16000011 | The context does not exist.        |
363| 16000012 | The application is controlled.        |
364| 16000013 | The application is controlled by EDM.       |
365| 16000050 | Internal error. |
366| 16000053 | The ability is not on the top of the UI. |
367| 16000055 | Installation-free timed out. |
368| 16200001 | The caller has been released. |
369
370For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
371
372**Example**
373
374  ```ts
375  import Want from '@ohos.app.ability.Want';
376  import StartOptions from '@ohos.app.ability.StartOptions';
377  import { BusinessError } from '@ohos.base';
378
379  let want: Want = {
380    deviceId: '',
381    bundleName: 'com.example.myapplication',
382    abilityName: 'EntryAbility'
383  };
384  let accountId = 100;
385  let options: StartOptions = {
386    windowMode: 0
387  };
388
389  try {
390    this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => {
391      if (error.code) {
392        // Process service logic errors.
393        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
394        return;
395      }
396      // Carry out normal service processing.
397      console.log('startAbilityWithAccount succeed');
398    });
399  } catch (paramError) {
400    // Process input parameter errors.
401    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
402  }
403  ```
404
405
406## ServiceExtensionContext.startAbilityWithAccount
407
408startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
409
410Starts an ability with the account ID specified. This API uses a promise to return the result.
411
412Observe the following when using this API:
413 - 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.
414 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
415 - 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).
416
417**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
418
419**System capability**: SystemCapability.Ability.AbilityRuntime.Core
420
421**System API**: This is a system API and cannot be called by third-party applications.
422
423**Parameters**
424
425| Name| Type| Mandatory| Description|
426| -------- | -------- | -------- | -------- |
427| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
428| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
429| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
430
431**Return value**
432
433| Type| Description|
434| -------- | -------- |
435| Promise&lt;void&gt; | Promise used to return the result.|
436
437**Error codes**
438
439| ID| Error Message|
440| ------- | -------------------------------- |
441| 16000001 | The specified ability does not exist. |
442| 16000002 | Incorrect ability type. |
443| 16000004 | Can not start invisible component. |
444| 16000005 | The specified process does not have the permission. |
445| 16000006 | Cross-user operations are not allowed. |
446| 16000008 | The crowdtesting application expires. |
447| 16000009 | An ability cannot be started or stopped in Wukong mode. |
448| 16000010 | The call with the continuation flag is forbidden.        |
449| 16000011 | The context does not exist.        |
450| 16000012 | The application is controlled.        |
451| 16000013 | The application is controlled by EDM.       |
452| 16000050 | Internal error. |
453| 16000053 | The ability is not on the top of the UI. |
454| 16000055 | Installation-free timed out. |
455| 16200001 | The caller has been released. |
456
457For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
458
459**Example**
460
461  ```ts
462  import Want from '@ohos.app.ability.Want';
463  import StartOptions from '@ohos.app.ability.StartOptions';
464  import { BusinessError } from '@ohos.base';
465
466  let want: Want = {
467    deviceId: '',
468    bundleName: 'com.example.myapplication',
469    abilityName: 'EntryAbility'
470  };
471  let accountId = 100;
472  let options: StartOptions = {
473    windowMode: 0
474  };
475
476  try {
477    this.context.startAbilityWithAccount(want, accountId, options)
478      .then((data: void) => {
479        // Carry out normal service processing.
480        console.log('startAbilityWithAccount succeed');
481      })
482      .catch((error: BusinessError) => {
483        // Process service logic errors.
484        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
485      });
486  } catch (paramError) {
487    // Process input parameter errors.
488    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
489  }
490  ```
491
492## ServiceExtensionContext.startServiceExtensionAbility
493
494startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
495
496Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
497
498**System capability**: SystemCapability.Ability.AbilityRuntime.Core
499
500**System API**: This is a system API and cannot be called by third-party applications.
501
502**Parameters**
503
504| Name| Type| Mandatory| Description|
505| -------- | -------- | -------- | -------- |
506| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
507| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
508
509**Error codes**
510
511| ID| Error Message|
512| ------- | -------------------------------- |
513| 16000001 | The specified ability does not exist. |
514| 16000002 | Incorrect ability type. |
515| 16000004 | Can not start invisible component. |
516| 16000005 | The specified process does not have the permission. |
517| 16000006 | Cross-user operations are not allowed. |
518| 16000008 | The crowdtesting application expires. |
519| 16000011 | The context does not exist.        |
520| 16000012 | The application is controlled.        |
521| 16000013 | The application is controlled by EDM.       |
522| 16000050 | Internal error. |
523| 16200001 | The caller has been released. |
524
525For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
526
527**Example**
528
529  ```ts
530  import Want from '@ohos.app.ability.Want';
531  import { BusinessError } from '@ohos.base';
532
533  let want: Want = {
534    deviceId: '',
535    bundleName: 'com.example.myapplication',
536    abilityName: 'EntryAbility'
537  };
538
539  try {
540    this.context.startServiceExtensionAbility(want, (error: BusinessError) => {
541      if (error.code) {
542        // Process service logic errors.
543        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
544        return;
545      }
546      // Carry out normal service processing.
547      console.log('startServiceExtensionAbility succeed');
548    });
549  } catch (paramError) {
550    // Process input parameter errors.
551    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
552  }
553  ```
554
555## ServiceExtensionContext.startServiceExtensionAbility
556
557startServiceExtensionAbility(want: Want): Promise\<void>;
558
559Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
560
561**System capability**: SystemCapability.Ability.AbilityRuntime.Core
562
563**System API**: This is a system API and cannot be called by third-party applications.
564
565**Parameters**
566
567| Name| Type| Mandatory| Description|
568| -------- | -------- | -------- | -------- |
569| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
570
571**Return value**
572
573| Type| Description|
574| -------- | -------- |
575| Promise&lt;void&gt; | Promise used to return the result.|
576
577**Error codes**
578
579| ID| Error Message|
580| ------- | -------------------------------- |
581| 16000001 | The specified ability does not exist. |
582| 16000002 | Incorrect ability type. |
583| 16000004 | Can not start invisible component. |
584| 16000005 | The specified process does not have the permission. |
585| 16000006 | Cross-user operations are not allowed. |
586| 16000008 | The crowdtesting application expires. |
587| 16000011 | The context does not exist.        |
588| 16000012 | The application is controlled.        |
589| 16000013 | The application is controlled by EDM.       |
590| 16000050 | Internal error. |
591| 16200001 | The caller has been released. |
592
593For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
594
595**Example**
596
597  ```ts
598  import Want from '@ohos.app.ability.Want';
599  import { BusinessError } from '@ohos.base';
600
601  let want: Want = {
602    deviceId: '',
603    bundleName: 'com.example.myapplication',
604    abilityName: 'EntryAbility'
605  };
606
607  try {
608    this.context.startServiceExtensionAbility(want)
609      .then((data: void) => {
610        // Carry out normal service processing.
611        console.log('startServiceExtensionAbility succeed');
612      })
613      .catch((error: BusinessError) => {
614        // Process service logic errors.
615        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
616      });
617  } catch (paramError) {
618    // Process input parameter errors.
619    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
620  }
621  ```
622
623## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
624
625startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
626
627Starts a new ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result.
628
629> **NOTE**
630>
631> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.
632
633**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
634
635**System capability**: SystemCapability.Ability.AbilityRuntime.Core
636
637**System API**: This is a system API and cannot be called by third-party applications.
638
639**Parameters**
640
641| Name| Type| Mandatory| Description|
642| -------- | -------- | -------- | -------- |
643| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
644| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
645| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
646
647**Error codes**
648
649| ID| Error Message|
650| ------- | -------------------------------- |
651| 16000001 | The specified ability does not exist. |
652| 16000002 | Incorrect ability type. |
653| 16000004 | Can not start invisible component. |
654| 16000005 | The specified process does not have the permission. |
655| 16000006 | Cross-user operations are not allowed. |
656| 16000008 | The crowdtesting application expires. |
657| 16000011 | The context does not exist.        |
658| 16000012 | The application is controlled.        |
659| 16000013 | The application is controlled by EDM.       |
660| 16000050 | Internal error. |
661| 16200001 | The caller has been released. |
662
663For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
664
665
666**Example**
667
668  ```ts
669  import Want from '@ohos.app.ability.Want';
670  import { BusinessError } from '@ohos.base';
671
672  let want: Want = {
673    deviceId: '',
674    bundleName: 'com.example.myapplication',
675    abilityName: 'EntryAbility'
676  };
677  let accountId = 100;
678
679  try {
680    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
681      if (error.code) {
682        // Process service logic errors.
683        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
684        return;
685      }
686      // Carry out normal service processing.
687      console.log('startServiceExtensionAbilityWithAccount succeed');
688    });
689  } catch (paramError) {
690    // Process input parameter errors.
691    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
692  }
693  ```
694
695## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
696
697startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
698
699Starts a new ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result.
700
701> **NOTE**
702>
703> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.
704
705**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
706
707**System capability**: SystemCapability.Ability.AbilityRuntime.Core
708
709**System API**: This is a system API and cannot be called by third-party applications.
710
711**Parameters**
712
713| Name| Type| Mandatory| Description|
714| -------- | -------- | -------- | -------- |
715| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
716| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
717
718**Return value**
719
720| Type| Description|
721| -------- | -------- |
722| Promise&lt;void&gt; | Promise used to return the result.|
723
724**Error codes**
725
726| ID| Error Message|
727| ------- | -------------------------------- |
728| 16000001 | The specified ability does not exist. |
729| 16000002 | Incorrect ability type. |
730| 16000004 | Can not start invisible component. |
731| 16000005 | The specified process does not have the permission. |
732| 16000006 | Cross-user operations are not allowed. |
733| 16000008 | The crowdtesting application expires. |
734| 16000011 | The context does not exist.        |
735| 16000012 | The application is controlled.        |
736| 16000013 | The application is controlled by EDM.       |
737| 16000050 | Internal error. |
738| 16200001 | The caller has been released. |
739
740For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
741
742**Example**
743
744  ```ts
745  import Want from '@ohos.app.ability.Want';
746  import { BusinessError } from '@ohos.base';
747
748  let want: Want = {
749    deviceId: '',
750    bundleName: 'com.example.myapplication',
751    abilityName: 'EntryAbility'
752  };
753  let accountId = 100;
754
755  try {
756    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
757      .then((data: void) => {
758        // Carry out normal service processing.
759        console.log('startServiceExtensionAbilityWithAccount succeed');
760      })
761      .catch((error: BusinessError) => {
762        // Process service logic errors.
763        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
764      });
765  } catch (paramError) {
766    // Process input parameter errors.
767    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
768  }
769  ```
770
771## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
772
773startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void;
774
775Starts an ability with the caller information specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses an asynchronous callback to return the result.
776
777Observe the following when using this API:
778 - 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.
779 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
780 - 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).
781
782**System capability**: SystemCapability.Ability.AbilityRuntime.Core
783
784**System API**: This is a system API and cannot be called by third-party applications.
785
786**Parameters**
787
788| Name| Type| Mandatory| Description|
789| -------- | -------- | -------- | -------- |
790| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
791| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**. Otherwise, **err** is an error object.|
792
793**Error codes**
794
795| ID| Error Message|
796| ------- | -------------------------------- |
797| 16000001 | The specified ability does not exist. |
798| 16000002 | Incorrect ability type. |
799| 16000004 | Can not start invisible component. |
800| 16000005 | The specified process does not have the permission. |
801| 16000006 | Cross-user operations are not allowed. |
802| 16000008 | The crowdtesting application expires. |
803| 16000009 | An ability cannot be started or stopped in Wukong mode. |
804| 16000010 | The call with the continuation flag is forbidden.        |
805| 16000011 | The context does not exist.        |
806| 16000012 | The application is controlled.        |
807| 16000013 | The application is controlled by EDM.       |
808| 16000050 | Internal error. |
809| 16000053 | The ability is not on the top of the UI. |
810| 16000055 | Installation-free timed out. |
811| 16200001 | The caller has been released. |
812
813For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
814
815**Example**
816
817```ts
818import extension from '@ohos.app.ability.ServiceExtensionAbility';
819import Want from '@ohos.app.ability.Want';
820
821export default class EntryAbility extends extension {
822  onCreate(want: Want) {
823    // want contains the information about the caller who starts the application.
824    let localWant: Want = want;
825    localWant.bundleName = 'com.example.demo';
826    localWant.moduleName = 'entry';
827    localWant.abilityName = 'TestAbility';
828
829    // Start a new ability using the caller information.
830    this.context.startAbilityAsCaller(localWant, (err) => {
831      if (err && err.code != 0) {
832        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
833      } else {
834        console.log('startAbilityAsCaller success.');
835      }
836    })
837  }
838}
839
840```
841
842## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
843
844startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
845
846Starts an ability with the caller information and start options specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses an asynchronous callback to return the result.
847
848Observe the following when using this API:
849 - 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.
850 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
851 - 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).
852
853**System capability**: SystemCapability.Ability.AbilityRuntime.Core
854
855**System API**: This is a system API and cannot be called by third-party applications.
856
857**Parameters**
858
859| Name| Type| Mandatory| Description|
860| -------- | -------- | -------- | -------- |
861| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
862| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
863| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**. Otherwise, **err** is an error object.|
864
865**Error codes**
866
867| ID| Error Message|
868| ------- | -------------------------------- |
869| 16000001 | The specified ability does not exist. |
870| 16000004 | Can not start invisible component. |
871| 16000005 | The specified process does not have the permission. |
872| 16000006 | Cross-user operations are not allowed. |
873| 16000008 | The crowdtesting application expires. |
874| 16000009 | An ability cannot be started or stopped in Wukong mode. |
875| 16000011 | The context does not exist.        |
876| 16000012 | The application is controlled.        |
877| 16000013 | The application is controlled by EDM.       |
878| 16000050 | Internal error. |
879| 16000053 | The ability is not on the top of the UI. |
880| 16000055 | Installation-free timed out. |
881| 16200001 | The caller has been released. |
882
883For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
884
885**Example**
886
887```ts
888import extension from '@ohos.app.ability.ServiceExtensionAbility';
889import Want from '@ohos.app.ability.Want';
890import StartOptions from '@ohos.app.ability.StartOptions';
891
892export default class EntryAbility extends extension {
893  onCreate(want: Want) {
894    // want contains the information about the caller who starts the application.
895    let localWant: Want = want;
896    localWant.bundleName = 'com.example.demo';
897    localWant.moduleName = 'entry';
898    localWant.abilityName = 'TestAbility';
899
900    let option: StartOptions = {
901      displayId: 0
902    }
903
904    // Start a new ability using the caller information.
905    this.context.startAbilityAsCaller(localWant, option, (err) => {
906      if (err && err.code != 0) {
907        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
908      } else {
909        console.log('startAbilityAsCaller success.');
910      }
911    })
912  }
913}
914
915```
916
917## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
918
919startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>;
920
921Starts an ability with the caller information specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses a promise to return the result.
922
923Observe the following when using this API:
924 - 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.
925 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
926 - 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).
927
928**System capability**: SystemCapability.Ability.AbilityRuntime.Core
929
930**System API**: This is a system API and cannot be called by third-party applications.
931
932**Parameters**
933
934| Name| Type| Mandatory| Description|
935| -------- | -------- | -------- | -------- |
936| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
937| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
938
939**Return value**
940
941| Type| Description|
942| -------- | -------- |
943| Promise&lt;void&gt; | Promise that returns no value.|
944
945**Error codes**
946
947| ID| Error Message|
948| ------- | -------------------------------- |
949| 16000001 | The specified ability does not exist. |
950| 16000002 | Incorrect ability type. |
951| 16000004 | Can not start invisible component. |
952| 16000005 | The specified process does not have the permission. |
953| 16000006 | Cross-user operations are not allowed. |
954| 16000008 | The crowdtesting application expires. |
955| 16000009 | An ability cannot be started or stopped in Wukong mode. |
956| 16000010 | The call with the continuation flag is forbidden.        |
957| 16000011 | The context does not exist.        |
958| 16000012 | The application is controlled.        |
959| 16000013 | The application is controlled by EDM.       |
960| 16000050 | Internal error. |
961| 16000053 | The ability is not on the top of the UI. |
962| 16000055 | Installation-free timed out. |
963| 16200001 | The caller has been released. |
964
965For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
966
967**Example**
968
969```ts
970import extension from '@ohos.app.ability.ServiceExtensionAbility';
971import Want from '@ohos.app.ability.Want';
972import StartOptions from '@ohos.app.ability.StartOptions';
973import { BusinessError } from '@ohos.base';
974
975export default class EntryAbility extends extension {
976  onCreate(want: Want) {
977    // want contains the information about the caller who starts the application.
978    let localWant: Want = want;
979    localWant.bundleName = 'com.example.demo';
980    localWant.moduleName = 'entry';
981    localWant.abilityName = 'TestAbility';
982
983    let option: StartOptions = {
984      displayId: 0
985    }
986
987    // Start a new ability using the caller information.
988    this.context.startAbilityAsCaller(localWant, option)
989      .then(() => {
990        console.log('startAbilityAsCaller success.');
991      })
992      .catch((err: BusinessError) => {
993        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
994      })
995  }
996}
997
998```
999
1000## ServiceExtensionContext.stopServiceExtensionAbility
1001
1002stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
1003
1004Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
1005
1006**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1007
1008**System API**: This is a system API and cannot be called by third-party applications.
1009
1010**Parameters**
1011
1012| Name| Type| Mandatory| Description|
1013| -------- | -------- | -------- | -------- |
1014| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1015| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
1016
1017**Error codes**
1018
1019| ID| Error Message|
1020| ------- | -------------------------------- |
1021| 16000001 | The specified ability does not exist. |
1022| 16000002 | Incorrect ability type. |
1023| 16000004 | Can not start invisible component. |
1024| 16000005 | The specified process does not have the permission. |
1025| 16000006 | Cross-user operations are not allowed. |
1026| 16000011 | The context does not exist.        |
1027| 16000050 | Internal error. |
1028| 16200001 | The caller has been released. |
1029
1030For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1031
1032**Example**
1033
1034  ```ts
1035  import Want from '@ohos.app.ability.Want';
1036  import { BusinessError } from '@ohos.base';
1037
1038  let want: Want = {
1039    deviceId: '',
1040    bundleName: 'com.example.myapplication',
1041    abilityName: 'EntryAbility'
1042  };
1043
1044  try {
1045    this.context.stopServiceExtensionAbility(want, (error: BusinessError) => {
1046      if (error.code) {
1047        // Process service logic errors.
1048        console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
1049        return;
1050      }
1051      // Carry out normal service processing.
1052      console.log('stopServiceExtensionAbility succeed');
1053    });
1054  } catch (paramError) {
1055    // Process input parameter errors.
1056    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1057  }
1058  ```
1059
1060## ServiceExtensionContext.stopServiceExtensionAbility
1061
1062stopServiceExtensionAbility(want: Want): Promise\<void>;
1063
1064Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
1065
1066**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1067
1068**System API**: This is a system API and cannot be called by third-party applications.
1069
1070**Parameters**
1071
1072| Name| Type| Mandatory| Description|
1073| -------- | -------- | -------- | -------- |
1074| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1075
1076**Return value**
1077
1078| Type| Description|
1079| -------- | -------- |
1080| Promise&lt;void&gt; | Promise used to return the result.|
1081
1082**Error codes**
1083
1084| ID| Error Message|
1085| ------- | -------------------------------- |
1086| 16000001 | The specified ability does not exist. |
1087| 16000002 | Incorrect ability type. |
1088| 16000004 | Can not start invisible component. |
1089| 16000005 | The specified process does not have the permission. |
1090| 16000006 | Cross-user operations are not allowed. |
1091| 16000011 | The context does not exist.        |
1092| 16000050 | Internal error. |
1093| 16200001 | The caller has been released. |
1094
1095For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1096
1097**Example**
1098
1099  ```ts
1100  import Want from '@ohos.app.ability.Want';
1101  import { BusinessError } from '@ohos.base';
1102
1103  let want: Want = {
1104    deviceId: '',
1105    bundleName: 'com.example.myapplication',
1106    abilityName: 'EntryAbility'
1107  };
1108
1109  try {
1110    this.context.stopServiceExtensionAbility(want)
1111      .then(() => {
1112        // Carry out normal service processing.
1113        console.log('stopServiceExtensionAbility succeed');
1114      })
1115      .catch((error: BusinessError) => {
1116        // Process service logic errors.
1117        console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
1118      });
1119  } catch (paramError) {
1120    // Process input parameter errors.
1121    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1122  }
1123  ```
1124
1125## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
1126
1127stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
1128
1129Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses an asynchronous callback to return the result.
1130
1131> **NOTE**
1132>
1133> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.
1134
1135**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1136
1137**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1138
1139**System API**: This is a system API and cannot be called by third-party applications.
1140
1141**Parameters**
1142
1143| Name| Type| Mandatory| Description|
1144| -------- | -------- | -------- | -------- |
1145| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1146| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1147| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
1148
1149**Error codes**
1150
1151| ID| Error Message|
1152| ------- | -------------------------------- |
1153| 16000001 | The specified ability does not exist. |
1154| 16000002 | Incorrect ability type. |
1155| 16000004 | Can not start invisible component. |
1156| 16000005 | The specified process does not have the permission. |
1157| 16000006 | Cross-user operations are not allowed. |
1158| 16000011 | The context does not exist.        |
1159| 16000050 | Internal error. |
1160| 16200001 | The caller has been released. |
1161
1162For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1163
1164**Example**
1165
1166  ```ts
1167  import Want from '@ohos.app.ability.Want';
1168  import { BusinessError } from '@ohos.base';
1169
1170  let want: Want = {
1171    deviceId: '',
1172    bundleName: 'com.example.myapplication',
1173    abilityName: 'EntryAbility'
1174  };
1175  let accountId = 100;
1176
1177  try {
1178    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
1179      if (error.code) {
1180        // Process service logic errors.
1181        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code, error.message: ${error.message}');
1182        return;
1183      }
1184      // Carry out normal service processing.
1185      console.log('stopServiceExtensionAbilityWithAccount succeed');
1186    });
1187  } catch (paramError) {
1188    // Process input parameter errors.
1189    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1190  }
1191  ```
1192
1193## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
1194
1195stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
1196
1197Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses a promise to return the result.
1198
1199> **NOTE**
1200>
1201> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user.
1202
1203**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1204
1205**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1206
1207**System API**: This is a system API and cannot be called by third-party applications.
1208
1209**Parameters**
1210
1211| Name| Type| Mandatory| Description|
1212| -------- | -------- | -------- | -------- |
1213| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1214| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1215
1216**Return value**
1217
1218| Type| Description|
1219| -------- | -------- |
1220| Promise&lt;void&gt; | Promise used to return the result.|
1221
1222**Error codes**
1223
1224| ID| Error Message|
1225| ------- | -------------------------------- |
1226| 16000001 | The specified ability does not exist. |
1227| 16000002 | Incorrect ability type. |
1228| 16000004 | Can not start invisible component. |
1229| 16000005 | The specified process does not have the permission. |
1230| 16000006 | Cross-user operations are not allowed. |
1231| 16000011 | The context does not exist.        |
1232| 16000050 | Internal error. |
1233| 16200001 | The caller has been released. |
1234
1235For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1236
1237**Example**
1238
1239  ```ts
1240  import Want from '@ohos.app.ability.Want';
1241  import { BusinessError } from '@ohos.base';
1242
1243  let want: Want = {
1244    deviceId: '',
1245    bundleName: 'com.example.myapplication',
1246    abilityName: 'EntryAbility'
1247  };
1248  let accountId = 100;
1249
1250  try {
1251    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
1252      .then(() => {
1253        // Carry out normal service processing.
1254        console.log('stopServiceExtensionAbilityWithAccount succeed');
1255      })
1256      .catch((error: BusinessError) => {
1257        // Process service logic errors.
1258        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
1259      });
1260  } catch (paramError) {
1261    // Process input parameter errors.
1262    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1263  }
1264  ```
1265
1266## ServiceExtensionContext.terminateSelf
1267
1268terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1269
1270Terminates this ability. This API uses an asynchronous callback to return the result.
1271
1272**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1273
1274**System API**: This is a system API and cannot be called by third-party applications.
1275
1276**Parameters**
1277
1278| Name| Type| Mandatory| Description|
1279| -------- | -------- | -------- | -------- |
1280| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
1281
1282**Error codes**
1283
1284| ID| Error Message|
1285| ------- | -------------------------------- |
1286| 16000001 | The specified ability does not exist. |
1287| 16000004 | Can not start invisible component. |
1288| 16000005 | The specified process does not have the permission. |
1289| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1290| 16000011 | The context does not exist.        |
1291| 16000050 | Internal error. |
1292
1293For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1294
1295**Example**
1296
1297  ```ts
1298  import { BusinessError } from '@ohos.base';
1299
1300  this.context.terminateSelf((error: BusinessError) => {
1301    if (error.code) {
1302      // Process service logic errors.
1303      console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
1304      return;
1305    }
1306    // Carry out normal service processing.
1307    console.log('terminateSelf succeed');
1308  });
1309  ```
1310
1311## ServiceExtensionContext.terminateSelf
1312
1313terminateSelf(): Promise&lt;void&gt;;
1314
1315Terminates this ability. This API uses a promise to return the result.
1316
1317**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1318
1319**System API**: This is a system API and cannot be called by third-party applications.
1320
1321**Return value**
1322
1323| Type| Description|
1324| -------- | -------- |
1325| Promise&lt;void&gt; | Promise used to return the result.|
1326
1327**Error codes**
1328
1329| ID| Error Message|
1330| ------- | -------------------------------- |
1331| 16000001 | The specified ability does not exist. |
1332| 16000004 | Can not start invisible component. |
1333| 16000005 | The specified process does not have the permission. |
1334| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1335| 16000011 | The context does not exist.        |
1336| 16000050 | Internal error. |
1337
1338For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1339
1340**Example**
1341
1342  ```ts
1343  import { BusinessError } from '@ohos.base';
1344
1345  this.context.terminateSelf().then(() => {
1346    // Carry out normal service processing.
1347    console.log('terminateSelf succeed');
1348  }).catch((error: BusinessError) => {
1349    // Process service logic errors.
1350    console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
1351  });
1352  ```
1353
1354## ServiceExtensionContext.connectServiceExtensionAbility
1355
1356connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1357
1358Connects this ability to a ServiceExtensionAbility.
1359
1360**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1361
1362**System API**: This is a system API and cannot be called by third-party applications.
1363
1364**Parameters**
1365
1366| Name| Type| Mandatory| Description|
1367| -------- | -------- | -------- | -------- |
1368| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
1369| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
1370
1371**Return value**
1372
1373| Type| Description|
1374| -------- | -------- |
1375| number | A number, based on which the connection will be interrupted.|
1376
1377**Error codes**
1378
1379| ID| Error Message|
1380| ------- | -------------------------------- |
1381| 16000001 | The specified ability does not exist. |
1382| 16000002 | Incorrect ability type. |
1383| 16000004 | Can not start invisible component. |
1384| 16000005 | The specified process does not have the permission. |
1385| 16000006 | Cross-user operations are not allowed. |
1386| 16000008 | The crowdtesting application expires. |
1387| 16000053 | The ability is not on the top of the UI. |
1388| 16000055 | Installation-free timed out. |
1389| 16000011 | The context does not exist.        |
1390| 16000050 | Internal error. |
1391
1392For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1393
1394**Example**
1395
1396  ```ts
1397  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
1398  import common from '@ohos.app.ability.common';
1399  import Want from '@ohos.app.ability.Want';
1400  import { BusinessError } from '@ohos.base';
1401
1402  let want: Want = {
1403    bundleName: 'com.example.myapp',
1404    abilityName: 'MyAbility'
1405  };
1406  let options: common.ConnectOptions = {
1407    onConnect(elementName, remote) {
1408      commRemote = remote;
1409      console.log('----------- onConnect -----------');
1410    },
1411    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
1412    onFailed(code) { console.error('----------- onFailed -----------') }
1413  };
1414  let connection: number;
1415  try {
1416    connection = this.context.connectServiceExtensionAbility(want, options);
1417  } catch (paramError) {
1418    // Process input parameter errors.
1419    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1420  }
1421  ```
1422
1423## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1424
1425connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1426
1427Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability.
1428
1429**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1430
1431**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1432
1433**System API**: This is a system API and cannot be called by third-party applications.
1434
1435**Parameters**
1436
1437| Name| Type| Mandatory| Description|
1438| -------- | -------- | -------- | -------- |
1439| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1440| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1441| options | ConnectOptions | Yes| Remote object instance.|
1442
1443**Return value**
1444
1445| Type| Description|
1446| -------- | -------- |
1447| number | Result code of the ability connection.|
1448
1449**Error codes**
1450
1451| ID| Error Message|
1452| ------- | -------------------------------- |
1453| 16000001 | The specified ability does not exist. |
1454| 16000002 | Incorrect ability type. |
1455| 16000004 | Can not start invisible component. |
1456| 16000005 | The specified process does not have the permission. |
1457| 16000006 | Cross-user operations are not allowed. |
1458| 16000008 | The crowdtesting application expires. |
1459| 16000053 | The ability is not on the top of the UI. |
1460| 16000055 | Installation-free timed out. |
1461| 16000011 | The context does not exist.        |
1462| 16000050 | Internal error. |
1463
1464For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1465
1466**Example**
1467
1468  ```ts
1469  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
1470  import common from '@ohos.app.ability.common';
1471  import Want from '@ohos.app.ability.Want';
1472  import { BusinessError } from '@ohos.base';
1473
1474  let want: Want = {
1475    deviceId: '',
1476    bundleName: 'com.example.myapplication',
1477    abilityName: 'EntryAbility'
1478  };
1479  let accountId = 100;
1480  let options: common.ConnectOptions = {
1481    onConnect(elementName, remote) {
1482      commRemote = remote;
1483      console.log('----------- onConnect -----------');
1484    },
1485    onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); },
1486    onFailed(code) { console.log('----------- onFailed -----------'); }
1487  };
1488  let connection: number;
1489  try {
1490    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
1491  } catch (paramError) {
1492    // Process input parameter errors.
1493    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1494  }
1495  ```
1496
1497## ServiceExtensionContext.disconnectServiceExtensionAbility
1498
1499disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
1500
1501Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses an asynchronous callback to return the result.
1502
1503**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1504
1505**System API**: This is a system API and cannot be called by third-party applications.
1506
1507**Parameters**
1508
1509| Name| Type| Mandatory| Description|
1510| -------- | -------- | -------- | -------- |
1511| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1512| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
1513
1514**Error codes**
1515
1516| ID| Error Message|
1517| ------- | -------------------------------- |
1518| 16000011 | The context does not exist.        |
1519| 16000050 | Internal error. |
1520
1521For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1522
1523**Example**
1524
1525  ```ts
1526  import { BusinessError } from '@ohos.base';
1527
1528  // connection is the return value of connectServiceExtensionAbility.
1529  let connection = 1;
1530
1531  try {
1532    this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => {
1533      commRemote = null;
1534      if (error.code) {
1535        // Process service logic errors.
1536        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
1537        return;
1538      }
1539      // Carry out normal service processing.
1540      console.log('disconnectServiceExtensionAbility succeed');
1541    });
1542  } catch (paramError) {
1543    commRemote = null;
1544    // Process input parameter errors.
1545    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1546  }
1547  ```
1548
1549## ServiceExtensionContext.disconnectServiceExtensionAbility
1550
1551disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
1552
1553Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses a promise to return the result.
1554
1555**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1556
1557**System API**: This is a system API and cannot be called by third-party applications.
1558
1559**Parameters**
1560
1561| Name| Type| Mandatory| Description|
1562| -------- | -------- | -------- | -------- |
1563| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1564
1565**Return value**
1566
1567| Type| Description|
1568| -------- | -------- |
1569| Promise&lt;void&gt; | Promise used to return the result.|
1570
1571**Error codes**
1572
1573| ID| Error Message|
1574| ------- | -------------------------------- |
1575| 16000011 | The context does not exist.        |
1576| 16000050 | Internal error. |
1577
1578For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1579
1580**Example**
1581
1582  ```ts
1583  import { BusinessError } from '@ohos.base';
1584
1585  // connection is the return value of connectServiceExtensionAbility.
1586  let connection = 1;
1587
1588  try {
1589    this.context.disconnectServiceExtensionAbility(connection)
1590      .then(() => {
1591        commRemote = null;
1592        // Carry out normal service processing.
1593        console.log('disconnectServiceExtensionAbility succeed');
1594      })
1595      .catch((error: BusinessError) => {
1596        commRemote = null;
1597        // Process service logic errors.
1598        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
1599      });
1600  } catch (paramError) {
1601    commRemote = null;
1602    // Process input parameter errors.
1603    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1604  }
1605  ```
1606
1607## ServiceExtensionContext.startAbilityByCall
1608
1609startAbilityByCall(want: Want): Promise&lt;Caller&gt;;
1610
1611Starts an ability in the foreground or background and obtains the caller object for communicating with the ability.
1612
1613Observe the following when using this API:
1614 - 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.
1615 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1616 - The rules for using this API in the same-device and cross-device scenarios are different. For details, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
1617
1618**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION
1619
1620**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1621
1622**System API**: This is a system API and cannot be called by third-party applications.
1623
1624**Parameters**
1625
1626| Name| Type| Mandatory| Description|
1627| -------- | -------- | -------- | -------- |
1628| want | [Want](js-apis-app-ability-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.|
1629
1630**Return value**
1631
1632| Type| Description|
1633| -------- | -------- |
1634| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
1635
1636**Error codes**
1637
1638| ID| Error Message|
1639| ------- | -------------------------------- |
1640| 16000001 | The specified ability does not exist. |
1641| 16000002 | Incorrect ability type. |
1642| 16000004 | Can not start invisible component. |
1643| 16000005 | Static permission denied. The specified process does not have the permission. |
1644| 16000006 | Cross-user operations are not allowed. |
1645| 16000008 | The crowdtesting application expires. |
1646| 16000011 | The context does not exist. |
1647| 16000050 | Internal error. |
1648| 16200001 | The caller has been released.        |
1649
1650For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1651
1652**Example**
1653
1654  Start an ability in the background.
1655
1656  ```ts
1657  import { Caller } from '@ohos.app.ability.UIAbility';
1658  import Want from '@ohos.app.ability.Want';
1659  import { BusinessError } from '@ohos.base';
1660
1661  let caller: Caller;
1662
1663  // Start an ability in the background by not passing parameters.
1664  let wantBackground: Want = {
1665      bundleName: 'com.example.myservice',
1666      moduleName: 'entry',
1667      abilityName: 'EntryAbility',
1668      deviceId: ''
1669  };
1670
1671  try {
1672    this.context.startAbilityByCall(wantBackground)
1673      .then((obj: Caller) => {
1674        // Carry out normal service processing.
1675        caller = obj;
1676        console.log('startAbilityByCall succeed');
1677      }).catch((error: BusinessError) => {
1678        // Process service logic errors.
1679        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
1680      });
1681  } catch (paramError) {
1682    // Process input parameter errors.
1683    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1684  }
1685  ```
1686
1687  Start an ability in the foreground.
1688
1689  ```ts
1690  import { Caller } from '@ohos.app.ability.UIAbility';
1691  import Want from '@ohos.app.ability.Want';
1692  import { BusinessError } from '@ohos.base';
1693
1694  let caller: Caller;
1695
1696  // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true.
1697  let wantForeground: Want = {
1698      bundleName: 'com.example.myservice',
1699      moduleName: 'entry',
1700      abilityName: 'EntryAbility',
1701      deviceId: '',
1702      parameters: {
1703        'ohos.aafwk.param.callAbilityToForeground': true
1704      }
1705  };
1706
1707  try {
1708    this.context.startAbilityByCall(wantForeground)
1709      .then((obj: Caller) => {
1710        // Carry out normal service processing.
1711        caller = obj;
1712        console.log('startAbilityByCall succeed');
1713      }).catch((error: BusinessError) => {
1714        // Process service logic errors.
1715        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
1716      });
1717  } catch (paramError) {
1718    // Process input parameter errors.
1719    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
1720  }
1721  ```
1722## ServiceExtensionContext.startRecentAbility
1723
1724startRecentAbility(want: Want, callback: AsyncCallback\<void>): void;
1725
1726Starts an ability. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return the result.
1727
1728Observe the following when using this API:
1729 - 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.
1730 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1731 - 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).
1732
1733**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1734
1735**System API**: This is a system API and cannot be called by third-party applications.
1736
1737**Parameters**
1738
1739| Name| Type| Mandatory| Description|
1740| -------- | -------- | -------- | -------- |
1741| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1742| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
1743
1744**Error codes**
1745
1746For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1747
1748| ID| Error Message|
1749| ------- | -------------------------------- |
1750| 16000001 | The specified ability does not exist. |
1751| 16000002 | Incorrect ability type. |
1752| 16000004 | Can not start invisible component. |
1753| 16000005 | The specified process does not have the permission. |
1754| 16000006 | Cross-user operations are not allowed. |
1755| 16000008 | The crowdtesting application expires. |
1756| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1757| 16000010 | The call with the continuation flag is forbidden. |
1758| 16000011 | The context does not exist. |
1759| 16000050 | Internal error. |
1760| 16000053 | The ability is not on the top of the UI. |
1761| 16000055 | Installation-free timed out. |
1762| 16200001 | The caller has been released. |
1763
1764**Example**
1765
1766  ```ts
1767import Want from '@ohos.app.ability.Want';
1768import { BusinessError } from '@ohos.base';
1769
1770let want: Want = {
1771  bundleName: 'com.example.myapplication',
1772  abilityName: 'EntryAbility'
1773};
1774
1775try {
1776  this.context.startRecentAbility(want, (err: BusinessError) => {
1777    if (err.code) {
1778      // Process service logic errors.
1779      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
1780      return;
1781    }
1782    // Carry out normal service processing.
1783    console.info('startRecentAbility succeed');
1784  });
1785} catch (err) {
1786  // Process input parameter errors.
1787  let code = (err as BusinessError).code;
1788  let message = (err as BusinessError).message;
1789  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
1790}
1791  ```
1792## ServiceExtensionContext.startRecentAbility
1793
1794startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
1795
1796Starts an ability with the start options specified. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return the result.
1797You can use this API to carry start options.
1798
1799Observe the following when using this API:
1800 - 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.
1801 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1802 - 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).
1803
1804**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1805
1806**System API**: This is a system API and cannot be called by third-party applications.
1807
1808**Parameters**
1809
1810| Name| Type| Mandatory| Description|
1811| -------- | -------- | -------- | -------- |
1812| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1813| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
1814| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
1815
1816**Error codes**
1817
1818For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1819
1820| ID| Error Message|
1821| ------- | -------------------------------- |
1822| 16000001 | The specified ability does not exist. |
1823| 16000002 | Incorrect ability type. |
1824| 16000004 | Can not start invisible component. |
1825| 16000005 | The specified process does not have the permission. |
1826| 16000006 | Cross-user operations are not allowed. |
1827| 16000008 | The crowdtesting application expires. |
1828| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1829| 16000010 | The call with the continuation flag is forbidden. |
1830| 16000011 | The context does not exist. |
1831| 16000050 | Internal error. |
1832| 16000053 | The ability is not on the top of the UI. |
1833| 16000055 | Installation-free timed out. |
1834| 16200001 | The caller has been released. |
1835
1836**Example**
1837
1838  ```ts
1839import Want from '@ohos.app.ability.Want';
1840import StartOptions from '@ohos.app.ability.StartOptions';
1841import { BusinessError } from '@ohos.base';
1842
1843let want: Want = {
1844  deviceId: '',
1845  bundleName: 'com.example.myapplication',
1846  abilityName: 'EntryAbility'
1847};
1848let options: StartOptions = {
1849  windowMode: 0
1850};
1851
1852try {
1853  this.context.startRecentAbility(want, options, (err: BusinessError) => {
1854    if (err.code) {
1855      // Process service logic errors.
1856      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
1857      return;
1858    }
1859    // Carry out normal service processing.
1860    console.info('startRecentAbility succeed');
1861  });
1862} catch (err) {
1863  // Process input parameter errors.
1864  let code = (err as BusinessError).code;
1865  let message = (err as BusinessError).message;
1866  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
1867}
1868  ```
1869## ServiceExtensionContext.startRecentAbility
1870
1871startRecentAbility(want: Want, options?: StartOptions): Promise\<void>;
1872
1873Starts an ability. If the ability has multiple instances, the latest instance is started.
1874This API uses a promise to return the result.
1875
1876Observe the following when using this API:
1877 - 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.
1878 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1879 - 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).
1880
1881**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1882
1883**System API**: This is a system API and cannot be called by third-party applications.
1884
1885**Parameters**
1886
1887| Name| Type| Mandatory| Description|
1888| -------- | -------- | -------- | -------- |
1889| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1890| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
1891
1892**Error codes**
1893
1894For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1895
1896| ID| Error Message|
1897| ------- | -------------------------------- |
1898| 16000001 | The specified ability does not exist. |
1899| 16000002 | Incorrect ability type. |
1900| 16000004 | Can not start invisible component. |
1901| 16000005 | The specified process does not have the permission. |
1902| 16000006 | Cross-user operations are not allowed. |
1903| 16000008 | The crowdtesting application expires. |
1904| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1905| 16000010 | The call with the continuation flag is forbidden. |
1906| 16000011 | The context does not exist. |
1907| 16000050 | Internal error. |
1908| 16000053 | The ability is not on the top of the UI. |
1909| 16000055 | Installation-free timed out. |
1910| 16200001 | The caller has been released. |
1911
1912**Example**
1913
1914  ```ts
1915import Want from '@ohos.app.ability.Want';
1916import StartOptions from '@ohos.app.ability.StartOptions';
1917import { BusinessError } from '@ohos.base';
1918
1919let want: Want = {
1920  bundleName: 'com.example.myapplication',
1921  abilityName: 'EntryAbility'
1922};
1923let options: StartOptions = {
1924  windowMode: 0,
1925};
1926
1927try {
1928  this.context.startRecentAbility(want, options)
1929    .then(() => {
1930      // Carry out normal service processing.
1931      console.info('startRecentAbility succeed');
1932    })
1933    .catch((err: BusinessError) => {
1934      // Process service logic errors.
1935      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
1936    });
1937} catch (err) {
1938  // Process input parameter errors.
1939  let code = (err as BusinessError).code;
1940  let message = (err as BusinessError).message;
1941  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
1942}
1943  ```
1944
1945## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup>
1946
1947startAbilityByCallWithAccount(want: Want, accountId: number): Promise&lt;Caller&gt;;
1948
1949Starts an ability with the account ID specified and obtains the caller object for communicating with the ability.
1950
1951Observe the following when using this API:
1952 - If an application needs to call this API to start an ability that belongs to another user, it must have the **ohos.permission.ABILITY_BACKGROUND_COMMUNICATION** and **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permissions.
1953 - 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.
1954 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1955 - The rules for using this API in the same-device and cross-device scenarios are different. For details, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
1956
1957**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION and ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1958
1959**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1960
1961**System API**: This is a system API and cannot be called by third-party applications.
1962
1963**Parameters**
1964
1965| Name| Type| Mandatory| Description|
1966| -------- | -------- | -------- | -------- |
1967| want | [Want](js-apis-app-ability-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.|
1968| accountId | number | Yes| ID of a system account. The value **-1** indicates the current user. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1969
1970**Return value**
1971
1972| Type| Description|
1973| -------- | -------- |
1974| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
1975
1976**Error codes**
1977
1978For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
1979
1980| ID| Error Message|
1981| ------- | -------------------------------- |
1982| 16000001 | The specified ability does not exist. |
1983| 16000002 | Incorrect ability type. |
1984| 16000004 | Can not start invisible component. |
1985| 16000005 | Static permission denied. The specified process does not have the permission. |
1986| 16000006 | Cross-user operations are not allowed. |
1987| 16000008 | The crowdtesting application expires. |
1988| 16000011 | The context does not exist. |
1989| 16000012 | The application is controlled.        |
1990| 16000013 | The application is controlled by EDM.       |
1991| 16000050 | Internal error. |
1992| 16200001 | The caller has been released.        |
1993
1994**Example**
1995
1996  ```ts
1997  import { Caller } from '@ohos.app.ability.UIAbility';
1998  import Want from '@ohos.app.ability.Want';
1999  import StartOptions from '@ohos.app.ability.StartOptions';
2000  import { BusinessError } from '@ohos.base';
2001
2002  let caller: Caller;
2003  // ID of a system account. The value -1 indicates the current user.
2004  let accountId = -1;
2005  // Specify the ability to start.
2006  let want: Want = {
2007      bundleName: 'com.acts.actscalleeabilityrely',
2008      moduleName: 'entry',
2009      abilityName: 'EntryAbility',
2010      deviceId: '',
2011      parameters: {
2012        // If the value of 'ohos.aafwk.param.callAbilityToForeground' is true, the ability is started in the foreground. If the value is false or not set, the ability is started in the background.
2013        'ohos.aafwk.param.callAbilityToForeground': true
2014      }
2015  };
2016
2017  try {
2018    this.context.startAbilityByCallWithAccount(want, accountId)
2019      .then((obj: Caller) => {
2020        // Carry out normal service processing.
2021        caller = obj;
2022        console.log('startAbilityByCallWithAccount succeed');
2023      }).catch((error: BusinessError) => {
2024        // Process service logic errors.
2025        console.error('startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
2026      });
2027  } catch (paramError) {
2028    // Process input parameter errors.
2029    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
2030  }
2031  ```
2032