• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ServiceExtensionContext (System API)
2
3The ServiceExtensionContext module, inherited from **ExtensionContext**, provides context for the ServiceExtensionAbility.
4
5You can use the APIs of this module to start, terminate, connect, and disconnect an ability.
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>  - The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool.
12>  - The APIs provided by this module are system APIs.
13
14## Modules to Import
15
16```ts
17import { common } from '@kit.AbilityKit';
18```
19
20## Usage
21
22Before using the **ServiceExtensionContext** module, you must define a child class that inherits from **ServiceExtensionAbility**.
23
24**Example**
25
26```ts
27import { ServiceExtensionAbility } from '@kit.AbilityKit';
28import { rpc } from '@kit.IPCKit';
29
30let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected.
31
32class EntryAbility extends ServiceExtensionAbility {
33  onCreate() {
34    let context = this.context; // Obtain a ServiceExtensionContext instance.
35  }
36}
37```
38
39## ServiceExtensionContext.startAbility
40
41startAbility(want: Want, callback: AsyncCallback<void>): void;
42
43Starts an ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
44
45**System capability**: SystemCapability.Ability.AbilityRuntime.Core
46
47**System API**: This is a system API.
48
49**Parameters**
50
51| Name| Type| Mandatory| Description|
52| -------- | -------- | -------- | -------- |
53| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
54| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
55
56**Error codes**
57
58For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
59
60| ID| Error Message|
61| ------- | -------- |
62| 201 | The application does not have permission to call the interface. |
63| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
64| 16000001 | The specified ability does not exist. |
65| 16000002 | Incorrect ability type. |
66| 16000004 | Failed to start the invisible ability. |
67| 16000005 | The specified process does not have the permission. |
68| 16000006 | Cross-user operations are not allowed. |
69| 16000008 | The crowdtesting application expires. |
70| 16000009 | An ability cannot be started or stopped in Wukong mode. |
71| 16000010 | The call with the continuation flag is forbidden.        |
72| 16000011 | The context does not exist.        |
73| 16000012 | The application is controlled.        |
74| 16000013 | The application is controlled by EDM.       |
75| 16000019 | No matching ability is found. |
76| 16000050 | Internal error. |
77| 16000053 | The ability is not on the top of the UI. |
78| 16000055 | Installation-free timed out. |
79| 16000071 | App clone is not supported. |
80| 16000072 | App clone or multi-instance is not supported. |
81| 16000073 | The app clone index is invalid. |
82| 16000076 | The app instance key is invalid. |
83| 16000077 | The number of app instances reaches the limit. |
84| 16000078 | The multi-instance is not supported. |
85| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
86| 16000080 | Creating an instance is not supported. |
87| 16000082 | The UIAbility is being started. |
88| 16200001 | The caller has been released. |
89
90**Example**
91
92```ts
93import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
94import { BusinessError } from '@kit.BasicServicesKit';
95
96class EntryAbility extends ServiceExtensionAbility {
97  onCreate() {
98    let want: Want = {
99      bundleName: 'com.example.myapp',
100      abilityName: 'MyAbility'
101    };
102
103    try {
104      this.context.startAbility(want, (error: BusinessError) => {
105        if (error.code) {
106          // Process service logic errors.
107          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
108          return;
109        }
110        // Carry out normal service processing.
111        console.log('startAbility succeed');
112      });
113    } catch (paramError) {
114      // Process input parameter errors.
115      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
116    }
117  }
118}
119```
120
121## ServiceExtensionContext.startAbility
122
123startAbility(want: Want, options?: StartOptions): Promise\<void>;
124
125Starts an ability. This API uses a promise to return the result. It can be called only by the main thread.
126
127**System capability**: SystemCapability.Ability.AbilityRuntime.Core
128
129**System API**: This is a system API.
130
131**Parameters**
132
133| Name| Type| Mandatory| Description|
134| -------- | -------- | -------- | -------- |
135| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
136| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
137
138**Return value**
139
140| Type| Description|
141| -------- | -------- |
142| Promise&lt;void&gt; | Promise used to return the result.|
143
144**Error codes**
145
146For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
147
148| ID| Error Message|
149| ------- | -------- |
150| 201 | The application does not have permission to call the interface. |
151| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
152| 16000001 | The specified ability does not exist. |
153| 16000002 | Incorrect ability type. |
154| 16000004 | Failed to start the invisible ability. |
155| 16000005 | The specified process does not have the permission. |
156| 16000006 | Cross-user operations are not allowed. |
157| 16000008 | The crowdtesting application expires. |
158| 16000009 | An ability cannot be started or stopped in Wukong mode. |
159| 16000010 | The call with the continuation flag is forbidden.        |
160| 16000011 | The context does not exist.        |
161| 16000012 | The application is controlled.        |
162| 16000013 | The application is controlled by EDM.       |
163| 16000019 | No matching ability is found. |
164| 16000050 | Internal error. |
165| 16000053 | The ability is not on the top of the UI. |
166| 16000055 | Installation-free timed out. |
167| 16000071 | App clone is not supported. |
168| 16000072 | App clone or multi-instance is not supported. |
169| 16000073 | The app clone index is invalid. |
170| 16000076 | The app instance key is invalid. |
171| 16000077 | The number of app instances reaches the limit. |
172| 16000078 | The multi-instance is not supported. |
173| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
174| 16000080 | Creating an instance is not supported. |
175| 16000082 | The UIAbility is being started. |
176| 16200001 | The caller has been released. |
177
178**Example**
179
180```ts
181import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
182import { BusinessError } from '@kit.BasicServicesKit';
183
184class EntryAbility extends ServiceExtensionAbility {
185  onCreate() {
186    let want: Want = {
187      bundleName: 'com.example.myapp',
188      abilityName: 'MyAbility'
189    };
190    let options: StartOptions = {
191      windowMode: 0,
192    };
193
194    try {
195      this.context.startAbility(want, options)
196        .then((data: void) => {
197          // Carry out normal service processing.
198          console.log('startAbility succeed');
199        })
200        .catch((error: BusinessError) => {
201          // Process service logic errors.
202          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
203        });
204    } catch (paramError) {
205      // Process input parameter errors.
206      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
207    }
208  }
209}
210```
211
212## ServiceExtensionContext.startAbility
213
214startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
215
216Starts an ability with the start options specified. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
217
218**System capability**: SystemCapability.Ability.AbilityRuntime.Core
219
220**System API**: This is a system API.
221
222**Parameters**
223
224| Name| Type| Mandatory| Description|
225| -------- | -------- | -------- | -------- |
226| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
227| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
228| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
229
230**Error codes**
231
232For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
233
234| ID| Error Message|
235| ------- | -------- |
236| 201 | The application does not have permission to call the interface. |
237| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
238| 16000001 | The specified ability does not exist. |
239| 16000002 | Incorrect ability type. |
240| 16000004 | Failed to start the invisible ability. |
241| 16000005 | The specified process does not have the permission. |
242| 16000006 | Cross-user operations are not allowed. |
243| 16000008 | The crowdtesting application expires. |
244| 16000009 | An ability cannot be started or stopped in Wukong mode. |
245| 16000010 | The call with the continuation flag is forbidden.        |
246| 16000011 | The context does not exist.        |
247| 16000012 | The application is controlled.        |
248| 16000013 | The application is controlled by EDM.       |
249| 16000019 | No matching ability is found. |
250| 16000050 | Internal error. |
251| 16000053 | The ability is not on the top of the UI. |
252| 16000055 | Installation-free timed out. |
253| 16000071 | App clone is not supported. |
254| 16000072 | App clone or multi-instance is not supported. |
255| 16000073 | The app clone index is invalid. |
256| 16000076 | The app instance key is invalid. |
257| 16000077 | The number of app instances reaches the limit. |
258| 16000078 | The multi-instance is not supported. |
259| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
260| 16000080 | Creating an instance is not supported. |
261| 16000082 | The UIAbility is being started. |
262| 16200001 | The caller has been released. |
263
264**Example**
265
266```ts
267import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
268import { BusinessError } from '@kit.BasicServicesKit';
269
270class EntryAbility extends ServiceExtensionAbility {
271  onCreate() {
272    let want: Want = {
273      deviceId: '',
274      bundleName: 'com.example.myapplication',
275      abilityName: 'EntryAbility'
276    };
277    let options: StartOptions = {
278      windowMode: 0
279    };
280
281    try {
282      this.context.startAbility(want, options, (error: BusinessError) => {
283        if (error.code) {
284          // Process service logic errors.
285          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
286          return;
287        }
288        // Carry out normal service processing.
289        console.log('startAbility succeed');
290      });
291    } catch (paramError) {
292      // Process input parameter errors.
293      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
294    }
295  }
296}
297```
298
299## ServiceExtensionContext.startAbilityWithAccount
300
301startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
302
303Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
304
305> **NOTE**
306>
307> 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).
308
309**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
310
311**System capability**: SystemCapability.Ability.AbilityRuntime.Core
312
313**System API**: This is a system API.
314
315**Parameters**
316
317| Name| Type| Mandatory| Description|
318| -------- | -------- | -------- | -------- |
319| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
320| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).|
321| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
322
323**Error codes**
324
325For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
326
327| ID| Error Message|
328| ------- | -------- |
329| 201 | The application does not have permission to call the interface. |
330| 202 | The application is not system-app, can not use system-api. |
331| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
332| 16000001 | The specified ability does not exist. |
333| 16000002 | Incorrect ability type. |
334| 16000004 | Failed to start the invisible ability. |
335| 16000005 | The specified process does not have the permission. |
336| 16000006 | Cross-user operations are not allowed. |
337| 16000008 | The crowdtesting application expires. |
338| 16000009 | An ability cannot be started or stopped in Wukong mode. |
339| 16000010 | The call with the continuation flag is forbidden.        |
340| 16000011 | The context does not exist.        |
341| 16000012 | The application is controlled.        |
342| 16000013 | The application is controlled by EDM.       |
343| 16000019 | No matching ability is found. |
344| 16000050 | Internal error. |
345| 16000053 | The ability is not on the top of the UI. |
346| 16000055 | Installation-free timed out. |
347| 16000071 | App clone is not supported. |
348| 16000072 | App clone or multi-instance is not supported. |
349| 16000073 | The app clone index is invalid. |
350| 16000076 | The app instance key is invalid. |
351| 16000077 | The number of app instances reaches the limit. |
352| 16000078 | The multi-instance is not supported. |
353| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
354| 16000080 | Creating an instance is not supported. |
355| 16000082 | The UIAbility is being started. |
356| 16200001 | The caller has been released. |
357
358**Example**
359
360```ts
361import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
362import { BusinessError } from '@kit.BasicServicesKit';
363
364class EntryAbility extends ServiceExtensionAbility {
365  onCreate() {
366    let want: Want = {
367      deviceId: '',
368      bundleName: 'com.example.myapplication',
369      abilityName: 'EntryAbility'
370    };
371    let accountId = 100;
372
373    try {
374      this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => {
375        if (error.code) {
376          // Process service logic errors.
377          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
378          return;
379        }
380        // Carry out normal service processing.
381        console.log('startAbilityWithAccount succeed');
382      });
383    } catch (paramError) {
384      // Process input parameter errors.
385      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
386    }
387  }
388}
389```
390
391## ServiceExtensionContext.startAbilityWithAccount
392
393startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
394
395Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
396
397> **NOTE**
398>
399> 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).
400
401**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
402
403**System capability**: SystemCapability.Ability.AbilityRuntime.Core
404
405**System API**: This is a system API.
406
407**Parameters**
408
409| Name| Type| Mandatory| Description|
410| -------- | -------- | -------- | -------- |
411| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
412| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).|
413| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
414| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
415
416**Error codes**
417
418For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
419
420| ID| Error Message|
421| ------- | -------- |
422| 201 | The application does not have permission to call the interface. |
423| 202 | The application is not system-app, can not use system-api. |
424| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
425| 16000001 | The specified ability does not exist. |
426| 16000002 | Incorrect ability type. |
427| 16000004 | Failed to start the invisible ability. |
428| 16000005 | The specified process does not have the permission. |
429| 16000006 | Cross-user operations are not allowed. |
430| 16000008 | The crowdtesting application expires. |
431| 16000009 | An ability cannot be started or stopped in Wukong mode. |
432| 16000010 | The call with the continuation flag is forbidden.        |
433| 16000011 | The context does not exist.        |
434| 16000012 | The application is controlled.        |
435| 16000013 | The application is controlled by EDM.       |
436| 16000019 | No matching ability is found. |
437| 16000050 | Internal error. |
438| 16000053 | The ability is not on the top of the UI. |
439| 16000055 | Installation-free timed out. |
440| 16000071 | App clone is not supported. |
441| 16000072 | App clone or multi-instance is not supported. |
442| 16000073 | The app clone index is invalid. |
443| 16000076 | The app instance key is invalid. |
444| 16000077 | The number of app instances reaches the limit. |
445| 16000078 | The multi-instance is not supported. |
446| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
447| 16000080 | Creating an instance is not supported. |
448| 16000082 | The UIAbility is being started. |
449| 16200001 | The caller has been released. |
450
451**Example**
452
453```ts
454import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
455import { BusinessError } from '@kit.BasicServicesKit';
456
457class EntryAbility extends ServiceExtensionAbility {
458  onCreate() {
459    let want: Want = {
460      deviceId: '',
461      bundleName: 'com.example.myapplication',
462      abilityName: 'EntryAbility'
463    };
464    let accountId = 100;
465    let options: StartOptions = {
466      windowMode: 0
467    };
468
469    try {
470      this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => {
471        if (error.code) {
472          // Process service logic errors.
473          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
474          return;
475        }
476        // Carry out normal service processing.
477        console.log('startAbilityWithAccount succeed');
478      });
479    } catch (paramError) {
480      // Process input parameter errors.
481      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
482    }
483  }
484}
485```
486
487
488## ServiceExtensionContext.startAbilityWithAccount
489
490startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
491
492Starts an ability with the account ID specified. This API uses a promise to return the result. It can be called only by the main thread.
493
494> **NOTE**
495>
496> 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).
497
498**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
499
500**System capability**: SystemCapability.Ability.AbilityRuntime.Core
501
502**System API**: This is a system API.
503
504**Parameters**
505
506| Name| Type| Mandatory| Description|
507| -------- | -------- | -------- | -------- |
508| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
509| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).|
510| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
511
512**Return value**
513
514| Type| Description|
515| -------- | -------- |
516| Promise&lt;void&gt; | Promise used to return the result.|
517
518**Error codes**
519
520For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
521
522| ID| Error Message|
523| ------- | -------- |
524| 201 | The application does not have permission to call the interface. |
525| 202 | The application is not system-app, can not use system-api. |
526| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
527| 16000001 | The specified ability does not exist. |
528| 16000002 | Incorrect ability type. |
529| 16000004 | Failed to start the invisible ability. |
530| 16000005 | The specified process does not have the permission. |
531| 16000006 | Cross-user operations are not allowed. |
532| 16000008 | The crowdtesting application expires. |
533| 16000009 | An ability cannot be started or stopped in Wukong mode. |
534| 16000010 | The call with the continuation flag is forbidden.        |
535| 16000011 | The context does not exist.        |
536| 16000012 | The application is controlled.        |
537| 16000013 | The application is controlled by EDM.       |
538| 16000019 | No matching ability is found. |
539| 16000050 | Internal error. |
540| 16000053 | The ability is not on the top of the UI. |
541| 16000055 | Installation-free timed out. |
542| 16000071 | App clone is not supported. |
543| 16000072 | App clone or multi-instance is not supported. |
544| 16000073 | The app clone index is invalid. |
545| 16000076 | The app instance key is invalid. |
546| 16000077 | The number of app instances reaches the limit. |
547| 16000078 | The multi-instance is not supported. |
548| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
549| 16000080 | Creating an instance is not supported. |
550| 16000082 | The UIAbility is being started. |
551| 16200001 | The caller has been released. |
552
553**Example**
554
555```ts
556import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
557import { BusinessError } from '@kit.BasicServicesKit';
558
559class EntryAbility extends ServiceExtensionAbility {
560  onCreate() {
561    let want: Want = {
562      deviceId: '',
563      bundleName: 'com.example.myapplication',
564      abilityName: 'EntryAbility'
565    };
566    let accountId = 100;
567    let options: StartOptions = {
568      windowMode: 0
569    };
570
571    try {
572      this.context.startAbilityWithAccount(want, accountId, options)
573        .then((data: void) => {
574          // Carry out normal service processing.
575          console.log('startAbilityWithAccount succeed');
576        })
577        .catch((error: BusinessError) => {
578          // Process service logic errors.
579          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
580        });
581    } catch (paramError) {
582      // Process input parameter errors.
583      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
584    }
585  }
586}
587```
588
589## ServiceExtensionContext.startServiceExtensionAbility
590
591startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
592
593Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result.
594
595**System capability**: SystemCapability.Ability.AbilityRuntime.Core
596
597**System API**: This is a system API.
598
599**Parameters**
600
601| Name| Type| Mandatory| Description|
602| -------- | -------- | -------- | -------- |
603| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
604| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
605
606**Error codes**
607
608For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
609
610| ID| Error Message|
611| ------- | -------- |
612| 201 | The application does not have permission to call the interface. |
613| 202 | The application is not system-app, can not use system-api. |
614| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
615| 16000001 | The specified ability does not exist. |
616| 16000002 | Incorrect ability type. |
617| 16000004 | Failed to start the invisible ability. |
618| 16000005 | The specified process does not have the permission. |
619| 16000006 | Cross-user operations are not allowed. |
620| 16000008 | The crowdtesting application expires. |
621| 16000011 | The context does not exist.        |
622| 16000012 | The application is controlled.        |
623| 16000013 | The application is controlled by EDM.       |
624| 16000019 | No matching ability is found. |
625| 16000050 | Internal error. |
626| 16200001 | The caller has been released. |
627
628**Example**
629
630```ts
631import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
632import { BusinessError } from '@kit.BasicServicesKit';
633
634class EntryAbility extends ServiceExtensionAbility {
635  onCreate() {
636    let want: Want = {
637      deviceId: '',
638      bundleName: 'com.example.myapplication',
639      abilityName: 'EntryAbility'
640    };
641
642    try {
643      this.context.startServiceExtensionAbility(want, (error: BusinessError) => {
644        if (error.code) {
645          // Process service logic errors.
646          console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
647          return;
648        }
649        // Carry out normal service processing.
650        console.log('startServiceExtensionAbility succeed');
651      });
652    } catch (paramError) {
653      // Process input parameter errors.
654      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
655    }
656  }
657}
658```
659
660## ServiceExtensionContext.startServiceExtensionAbility
661
662startServiceExtensionAbility(want: Want): Promise\<void>;
663
664Starts a new ServiceExtensionAbility. This API uses a promise to return the result.
665
666**System capability**: SystemCapability.Ability.AbilityRuntime.Core
667
668**System API**: This is a system API.
669
670**Parameters**
671
672| Name| Type| Mandatory| Description|
673| -------- | -------- | -------- | -------- |
674| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
675
676**Return value**
677
678| Type| Description|
679| -------- | -------- |
680| Promise&lt;void&gt; | Promise used to return the result.|
681
682**Error codes**
683
684For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
685
686| ID| Error Message|
687| ------- | -------- |
688| 201 | The application does not have permission to call the interface. |
689| 202 | The application is not system-app, can not use system-api. |
690| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
691| 16000001 | The specified ability does not exist. |
692| 16000002 | Incorrect ability type. |
693| 16000004 | Failed to start the invisible ability. |
694| 16000005 | The specified process does not have the permission. |
695| 16000006 | Cross-user operations are not allowed. |
696| 16000008 | The crowdtesting application expires. |
697| 16000011 | The context does not exist.        |
698| 16000012 | The application is controlled.        |
699| 16000013 | The application is controlled by EDM.       |
700| 16000019 | No matching ability is found. |
701| 16000050 | Internal error. |
702| 16200001 | The caller has been released. |
703
704**Example**
705
706```ts
707import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
708import { BusinessError } from '@kit.BasicServicesKit';
709
710class EntryAbility extends ServiceExtensionAbility {
711  onCreate() {
712    let want: Want = {
713      deviceId: '',
714      bundleName: 'com.example.myapplication',
715      abilityName: 'EntryAbility'
716    };
717
718    try {
719      this.context.startServiceExtensionAbility(want)
720        .then((data) => {
721          // Carry out normal service processing.
722          console.log('startServiceExtensionAbility succeed');
723        })
724        .catch((error: BusinessError) => {
725          // Process service logic errors.
726          console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
727        });
728    } catch (paramError) {
729      // Process input parameter errors.
730      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
731    }
732  }
733}
734```
735
736## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
737
738startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
739
740Starts a new ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result.
741
742> **NOTE**
743>
744> 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).
745> Permission verification is not required when **accountId** specifies the current user.
746
747**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
748
749**System capability**: SystemCapability.Ability.AbilityRuntime.Core
750
751**System API**: This is a system API.
752
753**Parameters**
754
755| Name| Type| Mandatory| Description|
756| -------- | -------- | -------- | -------- |
757| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
758| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).|
759| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
760
761**Error codes**
762
763For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
764
765| ID| Error Message|
766| ------- | -------- |
767| 201 | The application does not have permission to call the interface. |
768| 202 | The application is not system-app, can not use system-api. |
769| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
770| 16000001 | The specified ability does not exist. |
771| 16000002 | Incorrect ability type. |
772| 16000004 | Failed to start the invisible ability. |
773| 16000005 | The specified process does not have the permission. |
774| 16000006 | Cross-user operations are not allowed. |
775| 16000008 | The crowdtesting application expires. |
776| 16000011 | The context does not exist.        |
777| 16000012 | The application is controlled.        |
778| 16000013 | The application is controlled by EDM.       |
779| 16000019 | No matching ability is found. |
780| 16000050 | Internal error. |
781| 16200001 | The caller has been released. |
782
783**Example**
784
785```ts
786import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
787import { BusinessError } from '@kit.BasicServicesKit';
788
789class EntryAbility extends ServiceExtensionAbility {
790  onCreate() {
791    let want: Want = {
792      deviceId: '',
793      bundleName: 'com.example.myapplication',
794      abilityName: 'EntryAbility'
795    };
796    let accountId = 100;
797
798    try {
799      this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
800        if (error.code) {
801          // Process service logic errors.
802          console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
803          return;
804        }
805        // Carry out normal service processing.
806        console.log('startServiceExtensionAbilityWithAccount succeed');
807      });
808    } catch (paramError) {
809      // Process input parameter errors.
810      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
811    }
812  }
813}
814```
815
816## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
817
818startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
819
820Starts a new ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result.
821
822> **NOTE**
823>
824> 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).
825> Permission verification is not required when **accountId** specifies the current user.
826
827**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
828
829**System capability**: SystemCapability.Ability.AbilityRuntime.Core
830
831**System API**: This is a system API.
832
833**Parameters**
834
835| Name| Type| Mandatory| Description|
836| -------- | -------- | -------- | -------- |
837| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
838| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).|
839
840**Return value**
841
842| Type| Description|
843| -------- | -------- |
844| Promise&lt;void&gt; | Promise used to return the result.|
845
846**Error codes**
847
848For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
849
850| ID| Error Message|
851| ------- | -------- |
852| 201 | The application does not have permission to call the interface. |
853| 202 | The application is not system-app, can not use system-api. |
854| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
855| 16000001 | The specified ability does not exist. |
856| 16000002 | Incorrect ability type. |
857| 16000004 | Failed to start the invisible ability. |
858| 16000005 | The specified process does not have the permission. |
859| 16000006 | Cross-user operations are not allowed. |
860| 16000008 | The crowdtesting application expires. |
861| 16000011 | The context does not exist.        |
862| 16000012 | The application is controlled.        |
863| 16000013 | The application is controlled by EDM.       |
864| 16000019 | No matching ability is found. |
865| 16000050 | Internal error. |
866| 16200001 | The caller has been released. |
867
868**Example**
869
870```ts
871import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
872import { BusinessError } from '@kit.BasicServicesKit';
873
874class EntryAbility extends ServiceExtensionAbility {
875  onCreate() {
876    let want: Want = {
877      deviceId: '',
878      bundleName: 'com.example.myapplication',
879      abilityName: 'EntryAbility'
880    };
881    let accountId = 100;
882
883    try {
884      this.context.startServiceExtensionAbilityWithAccount(want, accountId)
885        .then((data: void) => {
886          // Carry out normal service processing.
887          console.log('startServiceExtensionAbilityWithAccount succeed');
888        })
889        .catch((error: BusinessError) => {
890          // Process service logic errors.
891          console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
892        });
893    } catch (paramError) {
894      // Process input parameter errors.
895      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
896    }
897  }
898}
899```
900
901## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
902
903startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void;
904
905Starts 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. It can be called only by the main thread.
906
907> **NOTE**
908>
909> 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).
910
911**System capability**: SystemCapability.Ability.AbilityRuntime.Core
912
913**System API**: This is a system API.
914
915**Parameters**
916
917| Name| Type| Mandatory| Description|
918| -------- | -------- | -------- | -------- |
919| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
920| 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.|
921
922**Error codes**
923
924For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
925
926| ID| Error Message|
927| ------- | -------- |
928| 201 | The application does not have permission to call the interface. |
929| 202 | The application is not system-app, can not use system-api. |
930| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
931| 16000001 | The specified ability does not exist. |
932| 16000002 | Incorrect ability type. |
933| 16000004 | Failed to start the invisible ability. |
934| 16000005 | The specified process does not have the permission. |
935| 16000006 | Cross-user operations are not allowed. |
936| 16000008 | The crowdtesting application expires. |
937| 16000009 | An ability cannot be started or stopped in Wukong mode. |
938| 16000010 | The call with the continuation flag is forbidden.        |
939| 16000011 | The context does not exist.        |
940| 16000012 | The application is controlled.        |
941| 16000013 | The application is controlled by EDM.       |
942| 16000050 | Internal error. |
943| 16000053 | The ability is not on the top of the UI. |
944| 16000055 | Installation-free timed out. |
945| 16000071 | App clone is not supported. |
946| 16000072 | App clone or multi-instance is not supported. |
947| 16000073 | The app clone index is invalid. |
948| 16000076 | The app instance key is invalid. |
949| 16000077 | The number of app instances reaches the limit. |
950| 16000078 | The multi-instance is not supported. |
951| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
952| 16000080 | Creating an instance is not supported. |
953| 16000082 | The UIAbility is being started. |
954| 16200001 | The caller has been released. |
955
956**Example**
957
958```ts
959import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
960
961class EntryAbility extends ServiceExtensionAbility {
962  onCreate(want: Want) {
963    // want contains the information about the caller who starts the application.
964    let localWant: Want = want;
965    localWant.bundleName = 'com.example.demo';
966    localWant.moduleName = 'entry';
967    localWant.abilityName = 'TestAbility';
968
969    // Start a new ability using the caller information.
970    this.context.startAbilityAsCaller(localWant, (err) => {
971      if (err && err.code != 0) {
972        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
973      } else {
974        console.log('startAbilityAsCaller success.');
975      }
976    })
977  }
978}
979```
980
981## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
982
983startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
984
985Starts 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. It can be called only by the main thread.
986
987> **NOTE**
988>
989> 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).
990
991**System capability**: SystemCapability.Ability.AbilityRuntime.Core
992
993**System API**: This is a system API.
994
995**Parameters**
996
997| Name| Type| Mandatory| Description|
998| -------- | -------- | -------- | -------- |
999| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
1000| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
1001| 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.|
1002
1003**Error codes**
1004
1005For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1006
1007| ID| Error Message|
1008| ------- | -------- |
1009| 201 | The application does not have permission to call the interface. |
1010| 202 | The application is not system-app, can not use system-api. |
1011| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1012| 16000001 | The specified ability does not exist. |
1013| 16000004 | Failed to start the invisible ability. |
1014| 16000005 | The specified process does not have the permission. |
1015| 16000006 | Cross-user operations are not allowed. |
1016| 16000008 | The crowdtesting application expires. |
1017| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1018| 16000011 | The context does not exist.        |
1019| 16000012 | The application is controlled.        |
1020| 16000013 | The application is controlled by EDM.       |
1021| 16000050 | Internal error. |
1022| 16000053 | The ability is not on the top of the UI. |
1023| 16000055 | Installation-free timed out. |
1024| 16000071 | App clone is not supported. |
1025| 16000072 | App clone or multi-instance is not supported. |
1026| 16000073 | The app clone index is invalid. |
1027| 16000076 | The app instance key is invalid. |
1028| 16000077 | The number of app instances reaches the limit. |
1029| 16000078 | The multi-instance is not supported. |
1030| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
1031| 16000080 | Creating an instance is not supported. |
1032| 16000082 | The UIAbility is being started. |
1033| 16200001 | The caller has been released. |
1034
1035**Example**
1036
1037```ts
1038import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
1039
1040class EntryAbility extends ServiceExtensionAbility {
1041  onCreate(want: Want) {
1042    // want contains the information about the caller who starts the application.
1043    let localWant: Want = want;
1044    localWant.bundleName = 'com.example.demo';
1045    localWant.moduleName = 'entry';
1046    localWant.abilityName = 'TestAbility';
1047
1048    let option: StartOptions = {
1049      displayId: 0
1050    }
1051
1052    // Start a new ability using the caller information.
1053    this.context.startAbilityAsCaller(localWant, option, (err) => {
1054      if (err && err.code != 0) {
1055        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
1056      } else {
1057        console.log('startAbilityAsCaller success.');
1058      }
1059    })
1060  }
1061}
1062```
1063
1064## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
1065
1066startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>;
1067
1068Starts 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. It can be called only by the main thread.
1069
1070> **NOTE**
1071>
1072> 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).
1073
1074**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1075
1076**System API**: This is a system API.
1077
1078**Parameters**
1079
1080| Name| Type| Mandatory| Description|
1081| -------- | -------- | -------- | -------- |
1082| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
1083| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
1084
1085**Return value**
1086
1087| Type| Description|
1088| -------- | -------- |
1089| Promise&lt;void&gt; | Promise that returns no value.|
1090
1091**Error codes**
1092
1093For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1094
1095| ID| Error Message|
1096| ------- | -------- |
1097| 201 | The application does not have permission to call the interface. |
1098| 202 | The application is not system-app, can not use system-api. |
1099| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1100| 16000001 | The specified ability does not exist. |
1101| 16000002 | Incorrect ability type. |
1102| 16000004 | Failed to start the invisible ability. |
1103| 16000005 | The specified process does not have the permission. |
1104| 16000006 | Cross-user operations are not allowed. |
1105| 16000008 | The crowdtesting application expires. |
1106| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1107| 16000010 | The call with the continuation flag is forbidden.        |
1108| 16000011 | The context does not exist.        |
1109| 16000012 | The application is controlled.        |
1110| 16000013 | The application is controlled by EDM.       |
1111| 16000050 | Internal error. |
1112| 16000053 | The ability is not on the top of the UI. |
1113| 16000055 | Installation-free timed out. |
1114| 16000071 | App clone is not supported. |
1115| 16000072 | App clone or multi-instance is not supported. |
1116| 16000073 | The app clone index is invalid. |
1117| 16000076 | The app instance key is invalid. |
1118| 16000077 | The number of app instances reaches the limit. |
1119| 16000078 | The multi-instance is not supported. |
1120| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
1121| 16000080 | Creating an instance is not supported. |
1122| 16000082 | The UIAbility is being started. |
1123| 16200001 | The caller has been released. |
1124
1125**Example**
1126
1127```ts
1128import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
1129import { BusinessError } from '@kit.BasicServicesKit';
1130
1131class EntryAbility extends ServiceExtensionAbility {
1132  onCreate(want: Want) {
1133    // want contains the information about the caller who starts the application.
1134    let localWant: Want = want;
1135    localWant.bundleName = 'com.example.demo';
1136    localWant.moduleName = 'entry';
1137    localWant.abilityName = 'TestAbility';
1138
1139    let option: StartOptions = {
1140      displayId: 0
1141    };
1142
1143    // Start a new ability using the caller information.
1144    this.context.startAbilityAsCaller(localWant, option)
1145      .then(() => {
1146        console.log('startAbilityAsCaller success.');
1147      })
1148      .catch((err: BusinessError) => {
1149        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
1150      })
1151  }
1152}
1153```
1154
1155## ServiceExtensionContext.stopServiceExtensionAbility
1156
1157stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
1158
1159Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result.
1160
1161**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1162
1163**System API**: This is a system API.
1164
1165**Parameters**
1166
1167| Name| Type| Mandatory| Description|
1168| -------- | -------- | -------- | -------- |
1169| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1170| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
1171
1172**Error codes**
1173
1174For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1175
1176| ID| Error Message|
1177| ------- | -------- |
1178| 201 | The application does not have permission to call the interface. |
1179| 202 | The application is not system-app, can not use system-api. |
1180| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1181| 16000001 | The specified ability does not exist. |
1182| 16000002 | Incorrect ability type. |
1183| 16000004 | Failed to start the invisible ability. |
1184| 16000005 | The specified process does not have the permission. |
1185| 16000006 | Cross-user operations are not allowed. |
1186| 16000011 | The context does not exist.        |
1187| 16000050 | Internal error. |
1188| 16200001 | The caller has been released. |
1189
1190**Example**
1191
1192```ts
1193import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1194import { BusinessError } from '@kit.BasicServicesKit';
1195
1196class EntryAbility extends ServiceExtensionAbility {
1197  onCreate() {
1198    let want: Want = {
1199      deviceId: '',
1200      bundleName: 'com.example.myapplication',
1201      abilityName: 'EntryAbility'
1202    };
1203
1204    try {
1205      this.context.stopServiceExtensionAbility(want, (error: BusinessError) => {
1206        if (error.code) {
1207          // Process service logic errors.
1208          console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1209          return;
1210        }
1211        // Carry out normal service processing.
1212        console.log('stopServiceExtensionAbility succeed');
1213      });
1214    } catch (paramError) {
1215      // Process input parameter errors.
1216      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1217    }
1218  }
1219}
1220```
1221
1222## ServiceExtensionContext.stopServiceExtensionAbility
1223
1224stopServiceExtensionAbility(want: Want): Promise\<void>;
1225
1226Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result.
1227
1228**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1229
1230**System API**: This is a system API.
1231
1232**Parameters**
1233
1234| Name| Type| Mandatory| Description|
1235| -------- | -------- | -------- | -------- |
1236| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1237
1238**Return value**
1239
1240| Type| Description|
1241| -------- | -------- |
1242| Promise&lt;void&gt; | Promise used to return the result.|
1243
1244**Error codes**
1245
1246For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1247
1248| ID| Error Message|
1249| ------- | -------- |
1250| 201 | The application does not have permission to call the interface. |
1251| 202 | The application is not system-app, can not use system-api. |
1252| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1253| 16000001 | The specified ability does not exist. |
1254| 16000002 | Incorrect ability type. |
1255| 16000004 | Failed to start the invisible ability. |
1256| 16000005 | The specified process does not have the permission. |
1257| 16000006 | Cross-user operations are not allowed. |
1258| 16000011 | The context does not exist.        |
1259| 16000050 | Internal error. |
1260| 16200001 | The caller has been released. |
1261
1262**Example**
1263
1264```ts
1265import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1266import { BusinessError } from '@kit.BasicServicesKit';
1267
1268class EntryAbility extends ServiceExtensionAbility {
1269  onCreate() {
1270    let want: Want = {
1271      deviceId: '',
1272      bundleName: 'com.example.myapplication',
1273      abilityName: 'EntryAbility'
1274    };
1275
1276    try {
1277      this.context.stopServiceExtensionAbility(want)
1278        .then(() => {
1279          // Carry out normal service processing.
1280          console.log('stopServiceExtensionAbility succeed');
1281        })
1282        .catch((error: BusinessError) => {
1283          // Process service logic errors.
1284          console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1285        });
1286    } catch (paramError) {
1287      // Process input parameter errors.
1288      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1289    }
1290  }
1291}
1292```
1293
1294## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
1295
1296stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
1297
1298Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses an asynchronous callback to return the result.
1299
1300> **NOTE**
1301>
1302> Permission verification is not required when **accountId** specifies the current user.
1303
1304**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1305
1306**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1307
1308**System API**: This is a system API.
1309
1310**Parameters**
1311
1312| Name| Type| Mandatory| Description|
1313| -------- | -------- | -------- | -------- |
1314| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1315| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).|
1316| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
1317
1318**Error codes**
1319
1320For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1321
1322| ID| Error Message|
1323| ------- | -------- |
1324| 201 | The application does not have permission to call the interface. |
1325| 202 | The application is not system-app, can not use system-api. |
1326| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1327| 16000001 | The specified ability does not exist. |
1328| 16000002 | Incorrect ability type. |
1329| 16000004 | Failed to start the invisible ability. |
1330| 16000005 | The specified process does not have the permission. |
1331| 16000006 | Cross-user operations are not allowed. |
1332| 16000011 | The context does not exist.        |
1333| 16000050 | Internal error. |
1334| 16200001 | The caller has been released. |
1335
1336**Example**
1337
1338```ts
1339import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1340import { BusinessError } from '@kit.BasicServicesKit';
1341
1342class EntryAbility extends ServiceExtensionAbility {
1343  onCreate() {
1344    let want: Want = {
1345      deviceId: '',
1346      bundleName: 'com.example.myapplication',
1347      abilityName: 'EntryAbility'
1348    };
1349    let accountId = 100;
1350
1351    try {
1352      this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
1353        if (error.code) {
1354          // Process service logic errors.
1355          console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
1356          return;
1357        }
1358        // Carry out normal service processing.
1359        console.log('stopServiceExtensionAbilityWithAccount succeed');
1360      });
1361    } catch (paramError) {
1362      // Process input parameter errors.
1363      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1364    }
1365  }
1366}
1367```
1368
1369## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
1370
1371stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
1372
1373Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses a promise to return the result.
1374
1375> **NOTE**
1376>
1377> Permission verification is not required when **accountId** specifies the current user.
1378
1379**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1380
1381**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1382
1383**System API**: This is a system API.
1384
1385**Parameters**
1386
1387| Name| Type| Mandatory| Description|
1388| -------- | -------- | -------- | -------- |
1389| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1390| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).|
1391
1392**Return value**
1393
1394| Type| Description|
1395| -------- | -------- |
1396| Promise&lt;void&gt; | Promise used to return the result.|
1397
1398**Error codes**
1399
1400For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1401
1402| ID| Error Message|
1403| ------- | -------- |
1404| 201 | The application does not have permission to call the interface. |
1405| 202 | The application is not system-app, can not use system-api. |
1406| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1407| 16000001 | The specified ability does not exist. |
1408| 16000002 | Incorrect ability type. |
1409| 16000004 | Failed to start the invisible ability. |
1410| 16000005 | The specified process does not have the permission. |
1411| 16000006 | Cross-user operations are not allowed. |
1412| 16000011 | The context does not exist.        |
1413| 16000050 | Internal error. |
1414| 16200001 | The caller has been released. |
1415
1416**Example**
1417
1418```ts
1419import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1420import { BusinessError } from '@kit.BasicServicesKit';
1421
1422class EntryAbility extends ServiceExtensionAbility {
1423  onCreate() {
1424    let want: Want = {
1425      deviceId: '',
1426      bundleName: 'com.example.myapplication',
1427      abilityName: 'EntryAbility'
1428    };
1429    let accountId = 100;
1430
1431    try {
1432      this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
1433        .then(() => {
1434          // Carry out normal service processing.
1435          console.log('stopServiceExtensionAbilityWithAccount succeed');
1436        })
1437        .catch((error: BusinessError) => {
1438          // Process service logic errors.
1439          console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
1440        });
1441    } catch (paramError) {
1442      // Process input parameter errors.
1443      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1444    }
1445  }
1446}
1447```
1448
1449## ServiceExtensionContext.terminateSelf
1450
1451terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1452
1453Terminates this ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
1454
1455**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1456
1457**System API**: This is a system API.
1458
1459**Parameters**
1460
1461| Name| Type| Mandatory| Description|
1462| -------- | -------- | -------- | -------- |
1463| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
1464
1465**Error codes**
1466
1467For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1468
1469| ID| Error Message|
1470| ------- | -------- |
1471| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1472| 16000001 | The specified ability does not exist. |
1473| 16000004 | Failed to start the invisible ability. |
1474| 16000005 | The specified process does not have the permission. |
1475| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1476| 16000011 | The context does not exist.        |
1477| 16000050 | Internal error. |
1478
1479**Example**
1480
1481```ts
1482import { ServiceExtensionAbility } from '@kit.AbilityKit';
1483import { BusinessError } from '@kit.BasicServicesKit';
1484
1485class EntryAbility extends ServiceExtensionAbility {
1486  onCreate() {
1487    this.context.terminateSelf((error: BusinessError) => {
1488      if (error.code) {
1489        // Process service logic errors.
1490        console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
1491        return;
1492      }
1493      // Carry out normal service processing.
1494      console.log('terminateSelf succeed');
1495    });
1496  }
1497}
1498```
1499
1500## ServiceExtensionContext.terminateSelf
1501
1502terminateSelf(): Promise&lt;void&gt;;
1503
1504Terminates this ability. This API uses a promise to return the result. It can be called only by the main thread.
1505
1506**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1507
1508**System API**: This is a system API.
1509
1510**Return value**
1511
1512| Type| Description|
1513| -------- | -------- |
1514| Promise&lt;void&gt; | Promise used to return the result.|
1515
1516**Error codes**
1517
1518For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
1519
1520| ID| Error Message|
1521| ------- | -------------------------------- |
1522| 16000001 | The specified ability does not exist. |
1523| 16000004 | Failed to start the invisible ability. |
1524| 16000005 | The specified process does not have the permission. |
1525| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1526| 16000011 | The context does not exist.        |
1527| 16000050 | Internal error. |
1528
1529**Example**
1530
1531```ts
1532import { ServiceExtensionAbility } from '@kit.AbilityKit';
1533import { BusinessError } from '@kit.BasicServicesKit';
1534
1535class EntryAbility extends ServiceExtensionAbility {
1536  onCreate() {
1537    this.context.terminateSelf().then(() => {
1538      // Carry out normal service processing.
1539      console.log('terminateSelf succeed');
1540    }).catch((error: BusinessError) => {
1541      // Process service logic errors.
1542      console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
1543    });
1544  }
1545}
1546```
1547
1548## ServiceExtensionContext.connectServiceExtensionAbility
1549
1550connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1551
1552Connects this ability to a ServiceExtensionAbility. It can be called only by the main thread.
1553
1554> **NOTE**
1555>
1556> 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).
1557
1558**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1559
1560**System API**: This is a system API.
1561
1562**Parameters**
1563
1564| Name| Type| Mandatory| Description|
1565| -------- | -------- | -------- | -------- |
1566| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
1567| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
1568
1569**Return value**
1570
1571| Type| Description|
1572| -------- | -------- |
1573| number | A number, based on which the connection will be interrupted.|
1574
1575**Error codes**
1576
1577For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1578
1579| ID| Error Message|
1580| ------- | -------- |
1581| 201 | The application does not have permission to call the interface. |
1582| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1583| 16000001 | The specified ability does not exist. |
1584| 16000002 | Incorrect ability type. |
1585| 16000004 | Failed to start the invisible ability. |
1586| 16000005 | The specified process does not have the permission. |
1587| 16000006 | Cross-user operations are not allowed. |
1588| 16000008 | The crowdtesting application expires. |
1589| 16000053 | The ability is not on the top of the UI. |
1590| 16000055 | Installation-free timed out. |
1591| 16000011 | The context does not exist.        |
1592| 16000050 | Internal error. |
1593
1594**Example**
1595
1596```ts
1597import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit';
1598import { rpc } from '@kit.IPCKit';
1599import { BusinessError } from '@kit.BasicServicesKit';
1600
1601let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected.
1602
1603class EntryAbility extends ServiceExtensionAbility {
1604  onCreate() {
1605    let want: Want = {
1606      bundleName: 'com.example.myapp',
1607      abilityName: 'MyAbility'
1608    };
1609    let options: common.ConnectOptions = {
1610      onConnect(elementName, remote) {
1611        commRemote = remote;
1612        console.log('----------- onConnect -----------');
1613      },
1614      onDisconnect(elementName) {
1615        console.log('----------- onDisconnect -----------');
1616      },
1617      onFailed(code) {
1618        console.error('----------- onFailed -----------');
1619      }
1620    };
1621    let connection: number;
1622
1623    try {
1624      connection = this.context.connectServiceExtensionAbility(want, options);
1625    } catch (paramError) {
1626      // Process input parameter errors.
1627      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1628    }
1629  }
1630}
1631```
1632
1633## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1634
1635connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1636
1637Connects this ability to a ServiceExtensionAbility of a given account. It can be called only by the main thread.
1638
1639Currently, this API takes effect only on mobile phones and tablets.
1640
1641> **NOTE**
1642>
1643> 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).
1644> Permission verification is not required when **accountId** specifies the current user.
1645
1646**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1647
1648**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1649
1650**System API**: This is a system API.
1651
1652**Parameters**
1653
1654| Name| Type| Mandatory| Description|
1655| -------- | -------- | -------- | -------- |
1656| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1657| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).|
1658| options | ConnectOptions | Yes| Remote object instance.|
1659
1660**Return value**
1661
1662| Type| Description|
1663| -------- | -------- |
1664| number | Result code of the connection.|
1665
1666**Error codes**
1667
1668For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1669
1670| ID| Error Message|
1671| ------- | -------- |
1672| 201 | The application does not have permission to call the interface. |
1673| 202 | The application is not system-app, can not use system-api. |
1674| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1675| 16000001 | The specified ability does not exist. |
1676| 16000002 | Incorrect ability type. |
1677| 16000004 | Failed to start the invisible ability. |
1678| 16000005 | The specified process does not have the permission. |
1679| 16000006 | Cross-user operations are not allowed. |
1680| 16000008 | The crowdtesting application expires. |
1681| 16000053 | The ability is not on the top of the UI. |
1682| 16000055 | Installation-free timed out. |
1683| 16000011 | The context does not exist.        |
1684| 16000050 | Internal error. |
1685
1686**Example**
1687
1688```ts
1689import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit';
1690import { rpc } from '@kit.IPCKit';
1691import { BusinessError } from '@kit.BasicServicesKit';
1692
1693let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected.
1694
1695class EntryAbility extends ServiceExtensionAbility {
1696  onCreate() {
1697    let want: Want = {
1698      deviceId: '',
1699      bundleName: 'com.example.myapplication',
1700      abilityName: 'EntryAbility'
1701    };
1702    let accountId = 100;
1703    let options: common.ConnectOptions = {
1704      onConnect(elementName, remote) {
1705        commRemote = remote;
1706        console.log('----------- onConnect -----------');
1707      },
1708      onDisconnect(elementName) {
1709        console.log('----------- onDisconnect -----------');
1710      },
1711      onFailed(code) {
1712        console.log('----------- onFailed -----------');
1713      }
1714    };
1715    let connection: number;
1716
1717    try {
1718      connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
1719    } catch (paramError) {
1720      // Process input parameter errors.
1721      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1722    }
1723  }
1724}
1725```
1726
1727## ServiceExtensionContext.disconnectServiceExtensionAbility
1728
1729disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
1730
1731Disconnects 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. It can be called only by the main thread.
1732
1733**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1734
1735**System API**: This is a system API.
1736
1737**Parameters**
1738
1739| Name| Type| Mandatory| Description|
1740| -------- | -------- | -------- | -------- |
1741| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1742| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
1743
1744**Error codes**
1745
1746For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1747
1748| ID| Error Message|
1749| ------- | -------- |
1750| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1751| 16000011 | The context does not exist.        |
1752| 16000050 | Internal error. |
1753
1754**Example**
1755
1756```ts
1757import { ServiceExtensionAbility } from '@kit.AbilityKit';
1758import { rpc } from '@kit.IPCKit';
1759import { BusinessError } from '@kit.BasicServicesKit';
1760
1761let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected.
1762
1763class EntryAbility extends ServiceExtensionAbility {
1764  onCreate() {
1765    // connection is the return value of connectServiceExtensionAbility.
1766    let connection = 1;
1767    try {
1768      this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => {
1769        commRemote = null;
1770        if (error.code) {
1771          // Process service logic errors.
1772          console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1773          return;
1774        }
1775        // Carry out normal service processing.
1776        console.log('disconnectServiceExtensionAbility succeed');
1777      });
1778    } catch (paramError) {
1779      commRemote = null;
1780      // Process input parameter errors.
1781      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1782    }
1783  }
1784}
1785```
1786
1787## ServiceExtensionContext.disconnectServiceExtensionAbility
1788
1789disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
1790
1791Disconnects 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. It can be called only by the main thread.
1792
1793**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1794
1795**System API**: This is a system API.
1796
1797**Parameters**
1798
1799| Name| Type| Mandatory| Description|
1800| -------- | -------- | -------- | -------- |
1801| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.|
1802
1803**Return value**
1804
1805| Type| Description|
1806| -------- | -------- |
1807| Promise&lt;void&gt; | Promise used to return the result.|
1808
1809**Error codes**
1810
1811For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1812
1813| ID| Error Message|
1814| ------- | -------- |
1815| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1816| 16000011 | The context does not exist.        |
1817| 16000050 | Internal error. |
1818
1819**Example**
1820
1821```ts
1822import { ServiceExtensionAbility } from '@kit.AbilityKit';
1823import { rpc } from '@kit.IPCKit';
1824import { BusinessError } from '@kit.BasicServicesKit';
1825
1826let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected.
1827
1828class EntryAbility extends ServiceExtensionAbility {
1829  onCreate() {
1830    // connection is the return value of connectServiceExtensionAbility.
1831    let connection = 1;
1832    try {
1833      this.context.disconnectServiceExtensionAbility(connection)
1834        .then(() => {
1835          commRemote = null;
1836          // Carry out normal service processing.
1837          console.log('disconnectServiceExtensionAbility succeed');
1838        })
1839        .catch((error: BusinessError) => {
1840          commRemote = null;
1841          // Process service logic errors.
1842          console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1843        });
1844    } catch (paramError) {
1845      commRemote = null;
1846      // Process input parameter errors.
1847      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1848    }
1849  }
1850}
1851```
1852
1853## ServiceExtensionContext.startAbilityByCall
1854
1855startAbilityByCall(want: Want): Promise&lt;Caller&gt;;
1856
1857Starts an ability in the foreground or background and obtains the caller object for communicating with the ability. It can be called only by the main thread.
1858
1859This API cannot be used to start the UIAbility with the launch type set to [specified](../../application-models/uiability-launch-type.md#specified).
1860
1861Observe the following when using this API:
1862 - 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.
1863 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
1864 - 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).
1865
1866**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION
1867
1868**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1869
1870**System API**: This is a system API.
1871
1872**Parameters**
1873
1874| Name| Type| Mandatory| Description|
1875| -------- | -------- | -------- | -------- |
1876| want | [Want](js-apis-app-ability-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId**, and **parameters** (optional). If **parameters** is left blank or null, the ability is started in the background.|
1877
1878**Return value**
1879
1880| Type| Description|
1881| -------- | -------- |
1882| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
1883
1884**Error codes**
1885
1886For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
1887
1888| ID| Error Message|
1889| ------- | -------- |
1890| 201 | The application does not have permission to call the interface. |
1891| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1892| 16000001 | The specified ability does not exist. |
1893| 16000002 | Incorrect ability type. |
1894| 16000004 | Failed to start the invisible ability. |
1895| 16000005 | Static permission denied. The specified process does not have the permission. |
1896| 16000006 | Cross-user operations are not allowed. |
1897| 16000008 | The crowdtesting application expires. |
1898| 16000011 | The context does not exist. |
1899| 16000050 | Internal error. |
1900| 16200001 | The caller has been released.        |
1901
1902**Example**
1903
1904Start an ability in the background.
1905
1906```ts
1907import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit';
1908import { BusinessError } from '@kit.BasicServicesKit';
1909
1910class EntryAbility extends ServiceExtensionAbility {
1911  onCreate() {
1912    let caller: Caller;
1913    // Start an ability in the background by not passing parameters.
1914    let wantBackground: Want = {
1915      bundleName: 'com.example.myservice',
1916      moduleName: 'entry',
1917      abilityName: 'EntryAbility',
1918      deviceId: ''
1919    };
1920
1921    try {
1922      this.context.startAbilityByCall(wantBackground)
1923        .then((obj: Caller) => {
1924          // Carry out normal service processing.
1925          caller = obj;
1926          console.log('startAbilityByCall succeed');
1927        }).catch((error: BusinessError) => {
1928        // Process service logic errors.
1929        console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`);
1930      });
1931    } catch (paramError) {
1932      // Process input parameter errors.
1933      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1934    }
1935  }
1936}
1937```
1938
1939Start an ability in the foreground.
1940
1941```ts
1942import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit';
1943import { BusinessError } from '@kit.BasicServicesKit';
1944
1945class EntryAbility extends ServiceExtensionAbility {
1946  onCreate() {
1947    let caller: Caller;
1948    // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true.
1949    let wantForeground: Want = {
1950      bundleName: 'com.example.myservice',
1951      moduleName: 'entry',
1952      abilityName: 'EntryAbility',
1953      deviceId: '',
1954      parameters: {
1955        'ohos.aafwk.param.callAbilityToForeground': true
1956      }
1957    };
1958
1959    try {
1960      this.context.startAbilityByCall(wantForeground)
1961        .then((obj: Caller) => {
1962          // Carry out normal service processing.
1963          caller = obj;
1964          console.log('startAbilityByCall succeed');
1965        }).catch((error: BusinessError) => {
1966        // Process service logic errors.
1967        console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`);
1968      });
1969    } catch (paramError) {
1970      // Process input parameter errors.
1971      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1972    }
1973  }
1974}
1975```
1976## ServiceExtensionContext.startRecentAbility
1977
1978startRecentAbility(want: Want, callback: AsyncCallback\<void>): void;
1979
1980Starts an ability. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
1981
1982> **NOTE**
1983>
1984> 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).
1985
1986**System capability**: SystemCapability.Ability.AbilityRuntime.Core
1987
1988**System API**: This is a system API.
1989
1990**Parameters**
1991
1992| Name| Type| Mandatory| Description|
1993| -------- | -------- | -------- | -------- |
1994| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
1995| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
1996
1997**Error codes**
1998
1999For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2000
2001| ID| Error Message|
2002| ------- | -------- |
2003| 201 | The application does not have permission to call the interface. |
2004| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2005| 16000001 | The specified ability does not exist. |
2006| 16000002 | Incorrect ability type. |
2007| 16000004 | Failed to start the invisible ability. |
2008| 16000005 | The specified process does not have the permission. |
2009| 16000006 | Cross-user operations are not allowed. |
2010| 16000008 | The crowdtesting application expires. |
2011| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2012| 16000010 | The call with the continuation flag is forbidden. |
2013| 16000011 | The context does not exist. |
2014| 16000050 | Internal error. |
2015| 16000053 | The ability is not on the top of the UI. |
2016| 16000055 | Installation-free timed out. |
2017| 16000082 | The UIAbility is being started. |
2018| 16200001 | The caller has been released. |
2019
2020**Example**
2021
2022```ts
2023import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2024import { BusinessError } from '@kit.BasicServicesKit';
2025
2026class EntryAbility extends ServiceExtensionAbility {
2027  onCreate() {
2028    let want: Want = {
2029      bundleName: 'com.example.myapplication',
2030      abilityName: 'EntryAbility'
2031    };
2032
2033    try {
2034      this.context.startRecentAbility(want, (err: BusinessError) => {
2035        if (err.code) {
2036          // Process service logic errors.
2037          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
2038          return;
2039        }
2040        // Carry out normal service processing.
2041        console.info('startRecentAbility succeed');
2042      });
2043    } catch (err) {
2044      // Process input parameter errors.
2045      let code = (err as BusinessError).code;
2046      let message = (err as BusinessError).message;
2047      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
2048    }
2049  }
2050}
2051```
2052## ServiceExtensionContext.startRecentAbility
2053
2054startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
2055
2056Starts 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.
2057You can use this API to carry start options. It can be called only by the main thread.
2058
2059> **NOTE**
2060>
2061> 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).
2062
2063**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2064
2065**System API**: This is a system API.
2066
2067**Parameters**
2068
2069| Name| Type| Mandatory| Description|
2070| -------- | -------- | -------- | -------- |
2071| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
2072| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
2073| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
2074
2075**Error codes**
2076
2077For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2078
2079| ID| Error Message|
2080| ------- | -------- |
2081| 201 | The application does not have permission to call the interface. |
2082| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2083| 16000001 | The specified ability does not exist. |
2084| 16000002 | Incorrect ability type. |
2085| 16000004 | Failed to start the invisible ability. |
2086| 16000005 | The specified process does not have the permission. |
2087| 16000006 | Cross-user operations are not allowed. |
2088| 16000008 | The crowdtesting application expires. |
2089| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2090| 16000010 | The call with the continuation flag is forbidden. |
2091| 16000011 | The context does not exist. |
2092| 16000050 | Internal error. |
2093| 16000053 | The ability is not on the top of the UI. |
2094| 16000055 | Installation-free timed out. |
2095| 16000082 | The UIAbility is being started. |
2096| 16200001 | The caller has been released. |
2097
2098**Example**
2099
2100```ts
2101import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
2102import { BusinessError } from '@kit.BasicServicesKit';
2103
2104class EntryAbility extends ServiceExtensionAbility {
2105  onCreate() {
2106    let want: Want = {
2107      deviceId: '',
2108      bundleName: 'com.example.myapplication',
2109      abilityName: 'EntryAbility'
2110    };
2111    let options: StartOptions = {
2112      windowMode: 0
2113    };
2114
2115    try {
2116      this.context.startRecentAbility(want, options, (err: BusinessError) => {
2117        if (err.code) {
2118          // Process service logic errors.
2119          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
2120          return;
2121        }
2122        // Carry out normal service processing.
2123        console.info('startRecentAbility succeed');
2124      });
2125    } catch (err) {
2126      // Process input parameter errors.
2127      let code = (err as BusinessError).code;
2128      let message = (err as BusinessError).message;
2129      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
2130    }
2131  }
2132}
2133```
2134## ServiceExtensionContext.startRecentAbility
2135
2136startRecentAbility(want: Want, options?: StartOptions): Promise\<void>;
2137
2138Starts an ability. If the ability has multiple instances, the latest instance is started.
2139This API uses a promise to return the result. It can be called only by the main thread.
2140
2141> **NOTE**
2142>
2143> 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).
2144
2145**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2146
2147**System API**: This is a system API.
2148
2149**Parameters**
2150
2151| Name| Type| Mandatory| Description|
2152| -------- | -------- | -------- | -------- |
2153| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
2154| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
2155
2156**Error codes**
2157
2158For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2159
2160| ID| Error Message|
2161| ------- | -------- |
2162| 201 | The application does not have permission to call the interface. |
2163| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2164| 16000001 | The specified ability does not exist. |
2165| 16000002 | Incorrect ability type. |
2166| 16000004 | Failed to start the invisible ability. |
2167| 16000005 | The specified process does not have the permission. |
2168| 16000006 | Cross-user operations are not allowed. |
2169| 16000008 | The crowdtesting application expires. |
2170| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2171| 16000010 | The call with the continuation flag is forbidden. |
2172| 16000011 | The context does not exist. |
2173| 16000050 | Internal error. |
2174| 16000053 | The ability is not on the top of the UI. |
2175| 16000055 | Installation-free timed out. |
2176| 16000082 | The UIAbility is being started. |
2177| 16200001 | The caller has been released. |
2178
2179**Example**
2180
2181```ts
2182import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
2183import { BusinessError } from '@kit.BasicServicesKit';
2184
2185class EntryAbility extends ServiceExtensionAbility {
2186  onCreate() {
2187    let want: Want = {
2188      bundleName: 'com.example.myapplication',
2189      abilityName: 'EntryAbility'
2190    };
2191    let options: StartOptions = {
2192      windowMode: 0,
2193    };
2194
2195    try {
2196      this.context.startRecentAbility(want, options)
2197        .then(() => {
2198          // Carry out normal service processing.
2199          console.info('startRecentAbility succeed');
2200        })
2201        .catch((err: BusinessError) => {
2202          // Process service logic errors.
2203          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
2204        });
2205    } catch (err) {
2206      // Process input parameter errors.
2207      let code = (err as BusinessError).code;
2208      let message = (err as BusinessError).message;
2209      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
2210    }
2211  }
2212}
2213```
2214
2215## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup>
2216
2217startAbilityByCallWithAccount(want: Want, accountId: number): Promise&lt;Caller&gt;;
2218
2219Starts an ability with the account ID specified and obtains the caller object for communicating with the ability. It can be called only by the main thread.
2220
2221This API cannot be used to start the UIAbility with the launch type set to [specified](../../application-models/uiability-launch-type.md#specified).
2222
2223Observe the following when using this API:
2224 - 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.
2225 - 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.
2226 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
2227 - 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).
2228
2229**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION and ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
2230
2231**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2232
2233**System API**: This is a system API.
2234
2235**Parameters**
2236
2237| Name| Type| Mandatory| Description|
2238| -------- | -------- | -------- | -------- |
2239| 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.|
2240| accountId | number | Yes| ID of a system account. The value **-1** indicates the current user. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).|
2241
2242**Return value**
2243
2244| Type| Description|
2245| -------- | -------- |
2246| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
2247
2248**Error codes**
2249
2250For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2251
2252| ID| Error Message|
2253| ------- | -------- |
2254| 201 | The application does not have permission to call the interface. |
2255| 202 | The application is not system-app, can not use system-api. |
2256| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2257| 16000001 | The specified ability does not exist. |
2258| 16000002 | Incorrect ability type. |
2259| 16000004 | Failed to start the invisible ability. |
2260| 16000005 | Static permission denied. The specified process does not have the permission. |
2261| 16000006 | Cross-user operations are not allowed. |
2262| 16000008 | The crowdtesting application expires. |
2263| 16000011 | The context does not exist. |
2264| 16000012 | The application is controlled.        |
2265| 16000013 | The application is controlled by EDM.       |
2266| 16000050 | Internal error. |
2267| 16200001 | The caller has been released.        |
2268
2269**Example**
2270
2271```ts
2272import { ServiceExtensionAbility, Want, Caller } from '@kit.AbilityKit';
2273import { BusinessError } from '@kit.BasicServicesKit';
2274
2275class EntryAbility extends ServiceExtensionAbility {
2276  onCreate() {
2277    let caller: Caller;
2278    // ID of a system account. The value -1 indicates the current user.
2279    let accountId = -1;
2280    // Specify the ability to start.
2281    let want: Want = {
2282      bundleName: 'com.acts.actscalleeabilityrely',
2283      moduleName: 'entry',
2284      abilityName: 'EntryAbility',
2285      deviceId: '',
2286      parameters: {
2287        // 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.
2288        'ohos.aafwk.param.callAbilityToForeground': true
2289      }
2290    };
2291
2292    try {
2293      this.context.startAbilityByCallWithAccount(want, accountId)
2294        .then((obj: Caller) => {
2295          // Carry out normal service processing.
2296          caller = obj;
2297          console.log('startAbilityByCallWithAccount succeed');
2298        }).catch((error: BusinessError) => {
2299        // Process service logic errors.
2300        console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
2301      });
2302    } catch (paramError) {
2303      // Process input parameter errors.
2304      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
2305    }
2306  }
2307}
2308```
2309
2310## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup>
2311
2312requestModalUIExtension(pickerWant: Want): Promise\<void>
2313
2314Requests the specified foreground application to start the UIExtensionAbility of the corresponding type. The foreground application is specified by **bundleName** in **want.parameters**. If **bundleName** is left unspecified, or if the application specified by **bundleName** is not running in the foreground or does not exist, the UIExtensionAbility is directly started on the system UI. The UIExtensionAbility to start is determined by the combination of the **bundleName**, **abilityName**, and **moduleName** fields in **want**, and its type is determined by the **ability.want.params.uiExtensionType** field in **want.parameters**. This API uses a promise to return the result. It can be called only by the main thread.
2315
2316Before starting the UIExtensionAbility, ensure that the foreground application has finished page initialization. Otherwise, the UIExtensionAbility fails to start and the error message "uiContent is nullptr" is displayed. The application can determine the time to start the UIExtensionAbility by listening for the page loading status. After the page initialization is successful, the key log information "UIContentImpl: focus again" is recorded.
2317
2318> **NOTE**
2319>
2320> 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).
2321
2322**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2323
2324**System API**: This is a system API.
2325
2326**Parameters**
2327
2328| Name| Type| Mandatory| Description|
2329| -------- | -------- | -------- | -------- |
2330| pickerWant | [Want](js-apis-app-ability-want.md)  | Yes| Want information used to start the UIExtensionAbility.|
2331
2332**Return value**
2333
2334| Type| Description|
2335| -------- | -------- |
2336| Promise&lt;void&gt; | Promise that returns no value.|
2337
2338**Error codes**
2339
2340For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2341
2342| ID| Error Message|
2343| ------- | -------- |
2344| 202 | The application is not system-app, can not use system-api. |
2345| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2346| 16000050 | Internal error. |
2347
2348**Example**
2349
2350```ts
2351import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2352import { BusinessError } from '@kit.BasicServicesKit';
2353
2354class ServiceExtension extends ServiceExtensionAbility {
2355  onCreate() {
2356    let pickerWant: Want = {
2357      bundleName: 'com.example.myapplication',
2358      abilityName: 'UIExtAbility',
2359      moduleName: 'entry_test',
2360      parameters: {
2361        'bundleName': 'com.example.myapplication',
2362        // The value is the same as the value of type configured for com.example.myapplication.UIExtAbility.
2363        'ability.want.params.uiExtensionType': 'sys/commonUI'
2364      }
2365    };
2366
2367    try {
2368      this.context.requestModalUIExtension(pickerWant)
2369        .then(() => {
2370          // Carry out normal service processing.
2371          console.info('requestModalUIExtension succeed');
2372        })
2373        .catch((err: BusinessError) => {
2374          // Process service logic errors.
2375          console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`);
2376        });
2377    } catch (err) {
2378      // Process input parameter errors.
2379      let code = (err as BusinessError).code;
2380      let message = (err as BusinessError).message;
2381      console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`);
2382    }
2383  }
2384}
2385```
2386
2387## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup>
2388
2389requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void
2390
2391Requests the specified foreground application to start the UIExtensionAbility of the corresponding type. The foreground application is specified by **bundleName** in **want.parameters**. If **bundleName** is left unspecified, or if the application specified by **bundleName** is not running in the foreground or does not exist, the UIExtensionAbility is directly started on the system UI. The UIExtensionAbility to start is determined by the combination of the **bundleName**, **abilityName**, and **moduleName** fields in **want**, and its type is determined by the **ability.want.params.uiExtensionType** field in **want.parameters**. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
2392
2393Before starting the UIExtensionAbility, ensure that the foreground application has finished page initialization. Otherwise, the UIExtensionAbility fails to start and the error message "uiContent is nullptr" is displayed. The application can determine the time to start the UIExtensionAbility by listening for the page loading status. After the page initialization is successful, the key log information "UIContentImpl: focus again" is recorded.
2394
2395> **NOTE**
2396>
2397> 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).
2398
2399**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2400
2401**System API**: This is a system API.
2402
2403**Parameters**
2404
2405| Name| Type| Mandatory| Description|
2406| -------- | -------- | -------- | -------- |
2407| pickerWant | [Want](js-apis-app-ability-want.md)  | Yes| Want information used to start the UIExtensionAbility.|
2408| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the UIExtensionAbility is started, **err** is **undefined**; otherwise, **err** is an error object.|
2409
2410**Error codes**
2411
2412For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2413
2414| ID| Error Message|
2415| ------- | -------- |
2416| 202 | The application is not system-app, can not use system-api. |
2417| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2418| 16000050 | Internal error. |
2419
2420**Example**
2421
2422```ts
2423import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2424import { BusinessError } from '@kit.BasicServicesKit';
2425
2426class ServiceExtension extends ServiceExtensionAbility {
2427  onCreate() {
2428    let pickerWant: Want = {
2429      bundleName: 'com.example.myapplication',
2430      abilityName: 'com.example.myapplication.UIExtAbility',
2431      moduleName: 'entry_test',
2432      parameters: {
2433        'bundleName': 'com.example.myapplication',
2434        // The value is the same as the value of type configured for com.example.myapplication.UIExtAbility.
2435        'ability.want.params.uiExtensionType': 'sys/commonUI'
2436      }
2437    };
2438
2439    try {
2440      this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => {
2441        if (err.code) {
2442          // Process service logic errors.
2443          console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`);
2444          return;
2445        }
2446        // Carry out normal service processing.
2447        console.info('requestModalUIExtension succeed');
2448      });
2449    } catch (err) {
2450      // Process input parameter errors.
2451      let code = (err as BusinessError).code;
2452      let message = (err as BusinessError).message;
2453      console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`);
2454    }
2455  }
2456}
2457```
2458
2459## ServiceExtensionContext.openLink<sup>12+<sup>
2460openLink(link:string, options?: OpenLinkOptions): Promise&lt;void&gt;
2461
2462Starts a UIAbility through App Linking. This API uses a promise to return the result. It can be called only by the main thread.
2463
2464A URL in the standard format is passed in to the **link** field to start the target UIAbility based on the implicit Want matching rules. The target UIAbility must have the following filter characteristics to process links of App Linking:
2465- The **actions** field contains **ohos.want.action.viewData**.
2466- The **entities** field contains **entity.system.browsable**.
2467- The **uris** field contains elements whose **scheme** is **https** and **domainVerify** is **true**.
2468
2469If an input parameter is invalid, for example, a mandatory parameter is not set or the URL set in **link** is not in the standard format, an exception is thrown. If the parameter verification is successful but an error occurs when starting the target UIAbility, the error information is returned through promise.
2470
2471> **NOTE**
2472>
2473> 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).
2474
2475**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2476
2477**System API**: This is a system API.
2478
2479**Parameters**
2480
2481| Name| Type| Mandatory| Description|
2482| -------- | -------- | -------- | -------- |
2483| link | string | Yes| URL to open, which must be in the standard format.|
2484| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | No| Options of the URL.|
2485
2486**Return value**
2487
2488| Type| Description|
2489| -------- | -------- |
2490| Promise&lt;void&gt; | Promise that returns no value.|
2491
2492**Error codes**
2493
2494For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2495
2496| ID| Error Message|
2497| ------- | -------- |
2498| 201 | The application does not have permission to call the interface. |
2499| 202 | The application is not system-app, can not use system-api. |
2500| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2501| 16000001 | The specified ability does not exist. |
2502| 16000002 | Incorrect ability type. |
2503| 16000004 | Failed to start the invisible ability. |
2504| 16000005 | The specified process does not have the permission. |
2505| 16000006 | Cross-user operations are not allowed. |
2506| 16000008 | The crowdtesting application expires. |
2507| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2508| 16000010 | The call with the continuation flag is forbidden.        |
2509| 16000011 | The context does not exist.        |
2510| 16000012 | The application is controlled.        |
2511| 16000013 | The application is controlled by EDM.       |
2512| 16000019 | No matching ability is found. |
2513| 16200001 | The caller has been released. |
2514| 16000082 | The UIAbility is being started. |
2515
2516**Example**
2517
2518```ts
2519import { ServiceExtensionAbility, Want, OpenLinkOptions } from '@kit.AbilityKit';
2520import { BusinessError } from '@kit.BasicServicesKit';
2521
2522function log(info: string) {
2523  console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`);
2524}
2525
2526export default class ServiceExtAbility extends ServiceExtensionAbility {
2527  onCreate(want: Want) {
2528    log(`ServiceExtAbility OnCreate`);
2529  }
2530
2531  onRequest(want: Want, startId: number) {
2532    log(`ServiceExtAbility onRequest`);
2533    let link: string = 'https://www.example.com';
2534    let openLinkOptions: OpenLinkOptions = {
2535      appLinkingOnly: false
2536    };
2537    try {
2538      this.context.openLink(
2539        link,
2540        openLinkOptions
2541      ).then(() => {
2542        log(`open link success.`);
2543      }).catch((err: BusinessError) => {
2544        log(`open link failed, errCode ${JSON.stringify(err.code)}`);
2545      });
2546    }
2547    catch (e) {
2548      log(`exception occured, errCode ${JSON.stringify(e.code)}`);
2549    }
2550  }
2551
2552  onDestroy() {
2553    log(`ServiceExtAbility onDestroy`);
2554  }
2555}
2556```
2557
2558## ServiceExtensionContext.preStartMission<sup>12+<sup>
2559preStartMission(bundleName:string, moduleName: string, abilitName: string, startTime: string): Promise&lt;void&gt;
2560
2561Starts an atomic service and pre-opens the window, with the loading box skipped. This API uses a promise to return the result.
2562
2563If parameter verification is successful but the atomic service fails to start, you need to implement an exception mechanism to capture the error.
2564
2565**Required permissions**: ohos.permission.PRE_START_ATOMIC_SERVICE
2566
2567**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2568
2569**System API**: This is a system API.
2570
2571**Parameters**
2572
2573| Name| Type| Mandatory| Description|
2574| -------- | -------- | -------- | -------- |
2575| bundleName | string | Yes| Bundle name of the atomic service.|
2576| moduleName | string | Yes| Module name of the atomic service.|
2577| abilityName | string | Yes| Ability name of the atomic service.|
2578| startTime | string | Yes| Start time to open the atomic service, in milliseconds.|
2579
2580
2581**Return value**
2582
2583| Type| Description|
2584| -------- | -------- |
2585| Promise&lt;void&gt; | Promise that returns no value.|
2586
2587**Error codes**
2588
2589For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2590
2591| ID| Error Message|
2592| ------- | -------- |
2593| 201 | The application does not have permission to call the interface. |
2594| 202 | The application is not system-app, can not use system-api. |
2595| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2596| 16300007 | The target free install task does not exist. |
2597| 16000011 | The context does not exist.        |
2598
2599**Example**
2600
2601```ts
2602import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2603import { BusinessError } from '@kit.BasicServicesKit';
2604
2605function log(info: string) {
2606  console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`);
2607}
2608
2609export default class ServiceExtAbility extends ServiceExtensionAbility {
2610  onCreate(want: Want) {
2611    log(`ServiceExtAbility OnCreate`);
2612  }
2613
2614  onRequest(want: Want, startId: number) {
2615    log(`ServiceExtAbility onRequest`);
2616    try {
2617      this.context.preStartMission(
2618        want.bundleName,
2619        want.moduleName,
2620        want.abilityName,
2621        want.parameters["ohos.aafwk.param.startTime"]
2622      ).then(() => {
2623        log(`pre-start mission success.`);
2624      }).catch((err: BusinessError) => {
2625        log(`pre-start mission failed, errCode ${JSON.stringify(err.code)}`);
2626      });
2627    }
2628    catch (e) {
2629      log(`exception occured, errCode ${JSON.stringify(e.code)}`);
2630    }
2631  }
2632
2633  onDestroy() {
2634    log(`ServiceExtAbility onDestroy`);
2635  }
2636}
2637```
2638
2639## ServiceExtensionContext.startUIServiceExtensionAbility<sup>14+<sup>
2640startUIServiceExtensionAbility(want: Want): Promise&lt;void&gt;
2641
2642Starts a new [UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md). This API uses a promise to return the result.
2643
2644> **NOTE**
2645>
2646> 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).
2647
2648**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2649
2650**System API**: This is a system API.
2651
2652**Parameters**
2653| Name| Type| Read Only| Optional| Description                |
2654| ------ | ---- | ---- | -------------------- | -------------------- |
2655| want   | [Want](js-apis-app-ability-want.md) | Yes | No| [Want](js-apis-app-ability-want.md) used to pass the information about the ability to start, such as the ability name and bundle name.|
2656
2657**Return value**
2658
2659| Type               | Description                                  |
2660| ------------------- | -------------------------------------- |
2661| Promise&lt;void&gt; | Promise that returns no value.|
2662
2663**Error codes**
2664
2665For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2666| ID| Error Message                                                             |
2667| -------- | ---------------------------------------------------------------------|
2668| 201      | The application does not have permission to call the interface.      |
2669| 202      | The application is not system-app, can not use system-api.           |
2670| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2671| 801      | The Ability is not supported.                       |
2672| 16000001 | The specified ability does not exist.               |
2673| 16000002 | Incorrect ability type.                             |
2674| 16000004 | Failed to start the invisible ability.              |
2675| 16000005 | The specified process does not have the permission. |
2676| 16000006 | Cross-user operations are not allowed.              |
2677| 16000008 | The crowdtesting application expires.               |
2678| 16000011 | The context does not exist.                         |
2679| 16000012 | The application is controlled.                      |
2680| 16000013 | The application is controlled by EDM.               |
2681| 16000019 | No matching ability is found.                       |
2682| 16000050 | Internal error.                                     |
2683| 16200001 | The caller has been released.                       |
2684
2685**Example**
2686
2687```ts
2688import { BusinessError } from '@ohos.base';
2689import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2690
2691export default class MyServiceExtensionAbility extends ServiceExtensionAbility {
2692  onRequest(want: Want, startId: number) {
2693    const startWant: Want = {
2694      bundleName: 'com.example.myapplication',
2695      abilityName: 'UIServiceExtensionAbility'
2696    }
2697    // Start a UIServiceExtensionAbility.
2698    this.context.startUIServiceExtensionAbility(startWant).then(() => {
2699      console.info('succeeded');
2700    }).catch((error: BusinessError) => {
2701      console.error(`error code: ${error.code}, error essage : ${error.message}`);
2702    })
2703  }
2704}
2705```
2706
2707## ServiceExtensionContext.openAtomicService<sup>18+<sup>
2708openAtomicService(appId: string, options?: AtomicServiceOptions): Promise&lt;void&gt;
2709
2710Starts an atomic service based on an application ID. This API uses a promise to return the result.
2711
2712> **NOTE**
2713>
2714> 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).
2715
2716**System capability**: SystemCapability.Ability.AbilityRuntime.Core
2717
2718**System API**: This is a system API.
2719
2720**Parameters**
2721
2722| Name| Type| Mandatory| Description|
2723| -------- | -------- | -------- | -------- |
2724| appId | string | Yes| Unique ID of the application, which is allocated by the cloud.|
2725| options | [AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md) | No| Parameter carried in the request for starting the atomic service in jump-out mode.|
2726
2727**Return value**
2728
2729| Type| Description|
2730| -------- | -------- |
2731| Promise&lt;void&gt; | Promise that returns no value.|
2732
2733**Error codes**
2734
2735For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
2736
2737| ID| Error Message                                                    |
2738| -------- | ------------------------------------------------------------ |
2739| 201      | The application does not have permission to call the interface. |
2740| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
2741| 16000002 | Incorrect ability type.                                      |
2742| 16000004 | Failed to start the invisible ability.                       |
2743| 16000011 | The context does not exist.                                  |
2744| 16000012 | The application is controlled.                               |
2745| 16000050 | Internal error.                                              |
2746| 16200001 | The caller has been released.                                |
2747
2748**Example**
2749
2750```ts
2751import { ServiceExtensionAbility, AtomicServiceOptions } from '@kit.AbilityKit';
2752import { BusinessError } from '@kit.BasicServicesKit';
2753
2754export default class ServiceExtension extends ServiceExtensionAbility {
2755  onRequest(want: Want, startId: number) {
2756    let appId: string = '6918661953712445909';
2757    let options: AtomicServiceOptions = {
2758      displayId: 0,
2759    };
2760    try {
2761      this.context.openAtomicService(appId, options)
2762        .then(() => {
2763          console.info('openAtomicService succeed');
2764        })
2765        .catch((err: BusinessError) => {
2766          console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`);
2767        });
2768    } catch (err) {
2769      let code = (err as BusinessError).code;
2770      let message = (err as BusinessError).message;
2771      console.error(`openAtomicService failed, code is ${code}, message is ${message}`);
2772    }
2773  }
2774}
2775```
2776