1e41f4b71Sopenharmony_ci# UIServiceExtensionContext (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe UIServiceExtensionContext module, inherited from [ExtensionContext](js-apis-inner-application-extensionContext.md), provides the context environment for a [UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md).
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciUIServiceExtensionContext provides access to a UIServiceExtensionAbility and APIs for operating the ability, for example, starting, terminating, connecting, and disconnecting ability.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **NOTE**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>  - The initial APIs of this module are supported since API version 13. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10e41f4b71Sopenharmony_ci>  - The APIs of this module can be used only in the stage model.
11e41f4b71Sopenharmony_ci>  - The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool.
12e41f4b71Sopenharmony_ci>  - The APIs provided by this module are system APIs.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Modules to Import
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci```ts
17e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
18e41f4b71Sopenharmony_ci```
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci## How to Use
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciBefore using the APIs of UIServiceExtensionContext, you must obtain the context through a child class instance [UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md).
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**Example**
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci```ts
27e41f4b71Sopenharmony_ciimport { common, UIServiceExtensionAbility } from '@kit.AbilityKit';
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciclass UIServiceExtAbility extends UIServiceExtensionAbility {
30e41f4b71Sopenharmony_ci  onCreate() {
31e41f4b71Sopenharmony_ci    let context:common.UIServiceExtensionContext = this.context; // Obtain the UIServiceExtensionContext.
32e41f4b71Sopenharmony_ci  }
33e41f4b71Sopenharmony_ci}
34e41f4b71Sopenharmony_ci```
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci## UIServiceExtensionContext.startAbility<sup>13+<sup>
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_cistartAbility(want: Want, options?: StartOptions): Promise&lt;void&gt;
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciStarts an ability. This API uses a promise to return the result.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci> **NOTE**
44e41f4b71Sopenharmony_ci>
45e41f4b71Sopenharmony_ci> 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).
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**System API**: This is a system API.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**Parameters**
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci| Name| Type| Read Only| Optional| Description|
54e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
55e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md)  | Yes| No| Want information about the target ability, such as the ability name and bundle name.|
56e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes|Yes| Parameters used for starting the ability.|
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**Return value**
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci| Type| Description|
61e41f4b71Sopenharmony_ci| -------- | -------- |
62e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**Error codes**
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci| ID| Error Message|
69e41f4b71Sopenharmony_ci| ------- | -------- |
70e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. |
71e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
72e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. |
73e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. |
74e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. |
75e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. |
76e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. |
77e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. |
78e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. |
79e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden.        |
80e41f4b71Sopenharmony_ci| 16000011 | The context does not exist.        |
81e41f4b71Sopenharmony_ci| 16000012 | The application is controlled.        |
82e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM.       |
83e41f4b71Sopenharmony_ci| 16000019 | Can not match any component.       |
84e41f4b71Sopenharmony_ci| 16000050 | Internal error. |
85e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. |
86e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. |
87e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. |
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci**Example**
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci```ts
92e41f4b71Sopenharmony_ciimport { UIServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
93e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ciclass UIEntryAbility extends UIServiceExtensionAbility {
96e41f4b71Sopenharmony_ci  onCreate() {
97e41f4b71Sopenharmony_ci    let want: Want = {
98e41f4b71Sopenharmony_ci      bundleName: 'com.example.myapp',
99e41f4b71Sopenharmony_ci      abilityName: 'MyAbility'
100e41f4b71Sopenharmony_ci    };
101e41f4b71Sopenharmony_ci    let options: StartOptions = {
102e41f4b71Sopenharmony_ci      windowMode: 0,
103e41f4b71Sopenharmony_ci    };
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci    try {
106e41f4b71Sopenharmony_ci      this.context.startAbility(want, options)
107e41f4b71Sopenharmony_ci        .then((data: void) => {
108e41f4b71Sopenharmony_ci          // Carry out normal service processing.
109e41f4b71Sopenharmony_ci          console.log('startAbility succeed');
110e41f4b71Sopenharmony_ci        })
111e41f4b71Sopenharmony_ci        .catch((error: BusinessError) => {
112e41f4b71Sopenharmony_ci          // Process service logic errors.
113e41f4b71Sopenharmony_ci          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
114e41f4b71Sopenharmony_ci        });
115e41f4b71Sopenharmony_ci    } catch (paramError) {
116e41f4b71Sopenharmony_ci      // Process input parameter errors.
117e41f4b71Sopenharmony_ci      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
118e41f4b71Sopenharmony_ci    }
119e41f4b71Sopenharmony_ci  }
120e41f4b71Sopenharmony_ci}
121e41f4b71Sopenharmony_ci```
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci## UIServiceExtensionContext.terminateSelf<sup>13+<sup>
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_citerminateSelf(): Promise&lt;void&gt;
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciTerminates this [UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md).
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**System API**: This is a system API.
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**Return value**
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci| Type| Description|
137e41f4b71Sopenharmony_ci| -------- | -------- |
138e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**Error codes**
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ciN/A
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**Example**
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci```ts
147e41f4b71Sopenharmony_ciimport { UIServiceExtensionAbility } from '@kit.AbilityKit';
148e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ciclass UIEntryAbility extends UIServiceExtensionAbility {
151e41f4b71Sopenharmony_ci  onCreate() {
152e41f4b71Sopenharmony_ci    this.context.terminateSelf().then(() => {
153e41f4b71Sopenharmony_ci      // Carry out normal service processing.
154e41f4b71Sopenharmony_ci      console.log('terminateSelf succeed');
155e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
156e41f4b71Sopenharmony_ci      // Process service logic errors.
157e41f4b71Sopenharmony_ci      console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
158e41f4b71Sopenharmony_ci    });
159e41f4b71Sopenharmony_ci  }
160e41f4b71Sopenharmony_ci}
161e41f4b71Sopenharmony_ci```
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci## UIServiceExtensionContext.startAbilityByType<sup>13+<sup>
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_cistartAbilityByType(type: string, wantParam: Record&lt;string, Object&gt;,
166e41f4b71Sopenharmony_ci    abilityStartCallback: AbilityStartCallback): Promise&lt;void&gt;
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ciStarts a [UIAbility](js-apis-app-ability-uiAbility.md) or [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md) based on the type of the target ability. This API can be called only by applications running in the foreground.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci> **NOTE**
172e41f4b71Sopenharmony_ci>
173e41f4b71Sopenharmony_ci> 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).
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci**System API**: This is a system API.
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci**Parameters**
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci| Name| Type| Read Only| Optional| Description|
182e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |  -------- |
183e41f4b71Sopenharmony_ci| type | string  | Yes| No|  Type of the target ability.|
184e41f4b71Sopenharmony_ci| wantParam | Record&lt;string, Object&gt;| Yes| No| Want parameter.|
185e41f4b71Sopenharmony_ci| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md)| Yes| No| Callback.|
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci**Return value**
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci| Type| Description|
190e41f4b71Sopenharmony_ci| -------- | -------- |
191e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**Error codes**
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci| ID| Error Message|
198e41f4b71Sopenharmony_ci| ------- | -------- |
199e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
200e41f4b71Sopenharmony_ci| 16000050  | Internal error.                                                                                         |
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci**Example**
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci```ts
205e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
206e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ciconst TAG: string = '[Extension_Sub] ';
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci@Entry
211e41f4b71Sopenharmony_ci@Component
212e41f4b71Sopenharmony_cistruct SubIndex {
213e41f4b71Sopenharmony_ci  build() {
214e41f4b71Sopenharmony_ci    Row() {
215e41f4b71Sopenharmony_ci      Column() {
216e41f4b71Sopenharmony_ci        Button("startAbilityByType")
217e41f4b71Sopenharmony_ci          .fontSize(10)
218e41f4b71Sopenharmony_ci          .fontWeight(FontWeight.Bold)
219e41f4b71Sopenharmony_ci          .onClick(() => {
220e41f4b71Sopenharmony_ci            let context = getContext(this) as common.UIServiceExtensionContext;
221e41f4b71Sopenharmony_ci            let startWant: Record<string, Object> = {
222e41f4b71Sopenharmony_ci              'sceneType': 1,
223e41f4b71Sopenharmony_ci              'email': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // Email address of the recipient. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
224e41f4b71Sopenharmony_ci              'cc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // Email address of the CC recipient. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
225e41f4b71Sopenharmony_ci              'bcc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // Email address of the BCC recipient. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
226e41f4b71Sopenharmony_ci              'subject': encodeURI('Email subject'), // Email subject. The content is URL encoded using encodeURI().
227e41f4b71Sopenharmony_ci              'body': encodeURI('Email body'), // Email body. The content is URL encoded using encodeURI().
228e41f4b71Sopenharmony_ci              'ability.params.stream':[encodeURI('attachment uri1'), encodeURI('attachment uri2')], // Attachment URIs. Multiple values are separated by commas (,). The array content is URL encoded using encodeURI().
229e41f4b71Sopenharmony_ci              'ability.want.params.uriPermissionFlag': 1
230e41f4b71Sopenharmony_ci            };
231e41f4b71Sopenharmony_ci            let abilityStartCallback: common.AbilityStartCallback = {
232e41f4b71Sopenharmony_ci              onError: (code: number, name: string, message: string) => {
233e41f4b71Sopenharmony_ci                console.log(TAG + `code: ${code}  name:${name}  message:${message}`);
234e41f4b71Sopenharmony_ci              }
235e41f4b71Sopenharmony_ci            };
236e41f4b71Sopenharmony_ci            try {
237e41f4b71Sopenharmony_ci              // Start a UIAbility or UIExtensionAbility based on the type of the target ability.
238e41f4b71Sopenharmony_ci              context.startAbilityByType("mail", startWant, abilityStartCallback)
239e41f4b71Sopenharmony_ci                .then(() => {
240e41f4b71Sopenharmony_ci                  console.log(TAG + `Successed in windows starting ability`);
241e41f4b71Sopenharmony_ci                }).catch((err: BusinessError) => {
242e41f4b71Sopenharmony_ci                console.log(TAG + `Failed to windows starting ability, Code is ${err.code}, message is ${err.message}`);
243e41f4b71Sopenharmony_ci              })
244e41f4b71Sopenharmony_ci            } catch (err) {
245e41f4b71Sopenharmony_ci              console.log(TAG + `Failed to windows starting ability, Code is ${err.code}, message is ${err.message}`);
246e41f4b71Sopenharmony_ci            }
247e41f4b71Sopenharmony_ci          })
248e41f4b71Sopenharmony_ci      }
249e41f4b71Sopenharmony_ci      .width('100%')
250e41f4b71Sopenharmony_ci    }
251e41f4b71Sopenharmony_ci    .height('100%')
252e41f4b71Sopenharmony_ci  }
253e41f4b71Sopenharmony_ci}
254e41f4b71Sopenharmony_ci```
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci## UIServiceExtensionContext.connectServiceExtensionAbility<sup>13+<sup>
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ciconnectServiceExtensionAbility(want: Want, options: ConnectOptions): number
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ciConnects to a [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md) and returns the connection ID.
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci> **NOTE**
264e41f4b71Sopenharmony_ci>
265e41f4b71Sopenharmony_ci> 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).
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci**System API**: This is a system API.
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci**Parameters**
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci| Name              | Type                    | Read Only| Optional| Description             |
274e41f4b71Sopenharmony_ci| -------------------- | ------------------------ | ---- | ---- |----------------- |
275e41f4b71Sopenharmony_ci| want                 | [Want](js-apis-app-ability-want.md) | Yes | No| Want parameter.      |
276e41f4b71Sopenharmony_ci| options              | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes|Yes  | Connection options.|
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci**Return value**
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci| Type| Description|
281e41f4b71Sopenharmony_ci| -------- | -------- |
282e41f4b71Sopenharmony_ci| number | Connection ID.|
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci**Error codes**
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci| ID| Error Message |
289e41f4b71Sopenharmony_ci| -------- | --- |
290e41f4b71Sopenharmony_ci| 201     | Not system application.   |
291e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
292e41f4b71Sopenharmony_ci| 16000001  | The specified ability does not exist.         |
293e41f4b71Sopenharmony_ci| 16000002   | Incorrect ability type.         |
294e41f4b71Sopenharmony_ci| 16000004   | Failed to start the invisible ability.         |
295e41f4b71Sopenharmony_ci| 16000005   | The specified process does not have the permission.         |
296e41f4b71Sopenharmony_ci| 16000006   | Cross-user operations are not allowed.         |
297e41f4b71Sopenharmony_ci| 16000008   | The crowdtesting application expires.        |
298e41f4b71Sopenharmony_ci| 16000011   | The context does not exist.         |
299e41f4b71Sopenharmony_ci| 16000050   | Internal error.        |
300e41f4b71Sopenharmony_ci| 16000053   | The ability is not on the top of the UI.        |
301e41f4b71Sopenharmony_ci| 16000055   | Installation-free timed out.         |
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci**Example**
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci```ts
307e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
308e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit';
309e41f4b71Sopenharmony_ciimport { promptAction } from '@kit.ArkUI';
310e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
311e41f4b71Sopenharmony_ci// The client needs to import idl_service_ext_proxy.ts provided by the server to the local project.
312e41f4b71Sopenharmony_ciimport IdlServiceExtProxy from '../IdlServiceExt/idl_service_ext_proxy';
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ciconst TAG: string = '[Page_ServiceExtensionAbility]';
315e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00;
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_cilet connectionId: number;
318e41f4b71Sopenharmony_cilet want: Want = {
319e41f4b71Sopenharmony_ci  deviceId: '',
320e41f4b71Sopenharmony_ci  bundleName: 'com.samples.stagemodelabilitydevelop',
321e41f4b71Sopenharmony_ci  abilityName: 'ServiceExtAbility'
322e41f4b71Sopenharmony_ci};
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_cilet options: common.ConnectOptions = {
325e41f4b71Sopenharmony_ci  onConnect(elementName, remote: rpc.IRemoteObject): void {
326e41f4b71Sopenharmony_ci    hilog.info(DOMAIN_NUMBER, TAG, 'onConnect callback');
327e41f4b71Sopenharmony_ci    if (remote === null) {
328e41f4b71Sopenharmony_ci      hilog.info(DOMAIN_NUMBER, TAG, `onConnect remote is null`);
329e41f4b71Sopenharmony_ci      return;
330e41f4b71Sopenharmony_ci    }
331e41f4b71Sopenharmony_ci    let serviceExtProxy: IdlServiceExtProxy = new IdlServiceExtProxy(remote);
332e41f4b71Sopenharmony_ci    // Communication is carried out by API calling, without exposing RPC details.
333e41f4b71Sopenharmony_ci    serviceExtProxy.processData(1, (errorCode: number, retVal: number) => {
334e41f4b71Sopenharmony_ci      hilog.info(DOMAIN_NUMBER, TAG, `processData, errorCode: ${errorCode}, retVal: ${retVal}`);
335e41f4b71Sopenharmony_ci    });
336e41f4b71Sopenharmony_ci    serviceExtProxy.insertDataToMap('theKey', 1, (errorCode: number) => {
337e41f4b71Sopenharmony_ci      hilog.info(DOMAIN_NUMBER, TAG, `insertDataToMap, errorCode: ${errorCode}`);
338e41f4b71Sopenharmony_ci    })
339e41f4b71Sopenharmony_ci  },
340e41f4b71Sopenharmony_ci  onDisconnect(elementName): void {
341e41f4b71Sopenharmony_ci    hilog.info(DOMAIN_NUMBER, TAG, 'onDisconnect callback');
342e41f4b71Sopenharmony_ci  },
343e41f4b71Sopenharmony_ci  onFailed(code: number): void {
344e41f4b71Sopenharmony_ci    hilog.info(DOMAIN_NUMBER, TAG, 'onFailed callback', JSON.stringify(code));
345e41f4b71Sopenharmony_ci  }
346e41f4b71Sopenharmony_ci};
347e41f4b71Sopenharmony_ci@Entry
348e41f4b71Sopenharmony_ci@Component
349e41f4b71Sopenharmony_cistruct Page_UIServiceExtensionAbility {
350e41f4b71Sopenharmony_ci  build() {
351e41f4b71Sopenharmony_ci    Column() {
352e41f4b71Sopenharmony_ci      //...
353e41f4b71Sopenharmony_ci      List({ initialIndex: 0 }) {
354e41f4b71Sopenharmony_ci        ListItem() {
355e41f4b71Sopenharmony_ci          Row() {
356e41f4b71Sopenharmony_ci            //...
357e41f4b71Sopenharmony_ci          }
358e41f4b71Sopenharmony_ci          .onClick(() => {
359e41f4b71Sopenharmony_ci            let context: common.UIServiceExtensionContext = getContext(this) as common.UIServiceExtensionContext;
360e41f4b71Sopenharmony_ci            // The ID returned after the connection is set up must be saved. The ID will be used for disconnection.
361e41f4b71Sopenharmony_ci            connectionId = context.connectServiceExtensionAbility(want, options);
362e41f4b71Sopenharmony_ci            // The background service is connected.
363e41f4b71Sopenharmony_ci            promptAction.showToast({
364e41f4b71Sopenharmony_ci              message: $r('app.string.SuccessfullyConnectBackendService')
365e41f4b71Sopenharmony_ci            });
366e41f4b71Sopenharmony_ci            // connectionId = context.connectAbility(want, options);
367e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `connectionId is : ${connectionId}`);
368e41f4b71Sopenharmony_ci          })
369e41f4b71Sopenharmony_ci        }
370e41f4b71Sopenharmony_ci        //...
371e41f4b71Sopenharmony_ci      }
372e41f4b71Sopenharmony_ci      //...
373e41f4b71Sopenharmony_ci    }
374e41f4b71Sopenharmony_ci    //...
375e41f4b71Sopenharmony_ci  }
376e41f4b71Sopenharmony_ci}
377e41f4b71Sopenharmony_ci```
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci## UIServiceExtensionContext.disconnectServiceExtensionAbility<sup>13+<sup>
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_cidisconnectServiceExtensionAbility(connectionId: number): Promise&lt;void&gt;
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ciDisconnects from a [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md). This API is opposite to [connectServiceExtensionAbility](#uiserviceextensioncontextconnectserviceextensionability13).
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci**System API**: This is a system API.
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci**Parameters**
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci| Name               | Type                    | Read Only| Optional| Description             |
393e41f4b71Sopenharmony_ci| -------------------- | ------------------------ | ---- | ----------------- | ----------------- |
394e41f4b71Sopenharmony_ci| connectionId         | number                   | Yes | No| Connection ID returned by [connectServiceExtensionAbility](#uiserviceextensioncontextconnectserviceextensionability13).|
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci**Return value**
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci| Type               | Description                             |
400e41f4b71Sopenharmony_ci| ------------------- | ---------------------------------|
401e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci**Error codes**
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci| ID| Error Message|
408e41f4b71Sopenharmony_ci| -------- | --------------------- |
409e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
410e41f4b71Sopenharmony_ci| 16000011  | The context does not exist.      |
411e41f4b71Sopenharmony_ci| 16000050 | Internal error.      |
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci**Example**
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ci```ts
416e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
417e41f4b71Sopenharmony_ciimport { promptAction } from '@kit.ArkUI';
418e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
419e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ciconst TAG: string = '[Page_ServiceExtensionAbility]';
422e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00;
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_cilet connectionId: number;
425e41f4b71Sopenharmony_ci@Entry
426e41f4b71Sopenharmony_ci@Component
427e41f4b71Sopenharmony_cistruct Page_UIServiceExtensionAbility {
428e41f4b71Sopenharmony_ci  build() {
429e41f4b71Sopenharmony_ci    Column() {
430e41f4b71Sopenharmony_ci      //...
431e41f4b71Sopenharmony_ci      List({ initialIndex: 0 }) {
432e41f4b71Sopenharmony_ci        ListItem() {
433e41f4b71Sopenharmony_ci          Row() {
434e41f4b71Sopenharmony_ci            //...
435e41f4b71Sopenharmony_ci          }
436e41f4b71Sopenharmony_ci          .onClick(() => {
437e41f4b71Sopenharmony_ci            let context: common.UIServiceExtensionContext = getContext(this) as common.UIServiceExtensionContext;
438e41f4b71Sopenharmony_ci            // connectionId is returned when connectServiceExtensionAbility is called and needs to be manually maintained.
439e41f4b71Sopenharmony_ci            context.disconnectServiceExtensionAbility(connectionId).then(() => {
440e41f4b71Sopenharmony_ci              hilog.info(DOMAIN_NUMBER, TAG, 'disconnectServiceExtensionAbility success');
441e41f4b71Sopenharmony_ci              // The background service is disconnected.
442e41f4b71Sopenharmony_ci              promptAction.showToast({
443e41f4b71Sopenharmony_ci                message: $r('app.string.SuccessfullyDisconnectBackendService')
444e41f4b71Sopenharmony_ci              });
445e41f4b71Sopenharmony_ci            }).catch((error: BusinessError) => {
446e41f4b71Sopenharmony_ci              hilog.error(DOMAIN_NUMBER, TAG, 'disconnectServiceExtensionAbility failed');
447e41f4b71Sopenharmony_ci            });
448e41f4b71Sopenharmony_ci          })
449e41f4b71Sopenharmony_ci        }
450e41f4b71Sopenharmony_ci        //...
451e41f4b71Sopenharmony_ci      }
452e41f4b71Sopenharmony_ci      //...
453e41f4b71Sopenharmony_ci    }
454e41f4b71Sopenharmony_ci    //...
455e41f4b71Sopenharmony_ci  }
456e41f4b71Sopenharmony_ci}
457e41f4b71Sopenharmony_ci```
458