1e41f4b71Sopenharmony_ci# UIServiceExtensionContext (系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciUIServiceExtensionContext模块是[UIServiceExtension](js-apis-app-ability-uiServiceExtensionAbility-sys.md)的上下文环境,继承自[ExtensionContext](js-apis-inner-application-extensionContext.md)。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciUIServiceExtensionContext模块提供访问[UIServiceExtension](js-apis-app-ability-uiServiceExtensionAbility-sys.md)特定资源以及具有的能力,包括启动、停止、绑定、解绑Ability。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>  - 本模块首批接口从API version 13开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10e41f4b71Sopenharmony_ci>  - 本模块接口仅可在Stage模型下使用。
11e41f4b71Sopenharmony_ci>  - 本模块接口需要在主线程中使用,不要在Worker、TaskPool等子线程中使用。
12e41f4b71Sopenharmony_ci>  - 本模块接口为系统接口。
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## 导入模块
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci```ts
17e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
18e41f4b71Sopenharmony_ci```
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci## 使用说明
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci在使用UIServiceExtensionContext的功能前,需要通过[UIServiceExtension](js-apis-app-ability-uiServiceExtensionAbility-sys.md)子类实例获取上下文环境。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**示例:**
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 ; // 获取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_ci启动Ability,结果以Promise的形式返回。
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci> **说明:**
44e41f4b71Sopenharmony_ci>
45e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**参数:**
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci| 参数名 | 类型 | 只读 | 可选 | 说明 |
54e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
55e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md)  | 是 | 否 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
56e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 |是 | 启动Ability所携带的参数。 |
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**返回值:**
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci| 类型 | 说明 |
61e41f4b71Sopenharmony_ci| -------- | -------- |
62e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**错误码:**
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
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**示例:**
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          // 执行正常业务
109e41f4b71Sopenharmony_ci          console.log('startAbility succeed');
110e41f4b71Sopenharmony_ci        })
111e41f4b71Sopenharmony_ci        .catch((error: BusinessError) => {
112e41f4b71Sopenharmony_ci          // 处理业务逻辑错误
113e41f4b71Sopenharmony_ci          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
114e41f4b71Sopenharmony_ci        });
115e41f4b71Sopenharmony_ci    } catch (paramError) {
116e41f4b71Sopenharmony_ci      // 处理入参错误异常
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_ci销毁[UIServiceExtension](js-apis-app-ability-uiServiceExtensionAbility-sys.md)。
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**返回值:**
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci| 类型 | 说明 |
137e41f4b71Sopenharmony_ci| -------- | -------- |
138e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**错误码:**
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci无。
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**示例:**
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      // 执行正常业务
154e41f4b71Sopenharmony_ci      console.log('terminateSelf succeed');
155e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
156e41f4b71Sopenharmony_ci      // 处理业务逻辑错误
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_ci按目标ability的类型启动[UIAbility](js-apis-app-ability-uiAbility.md)或[UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md)。仅支持处于前台的应用调用。
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci> **说明:**
172e41f4b71Sopenharmony_ci>
173e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci**参数:**
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci| 参数名 | 类型 | 只读 | 可选 | 说明 |
182e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |  -------- |
183e41f4b71Sopenharmony_ci| type | string  | 是 | 否 |  目标ability类型。 |
184e41f4b71Sopenharmony_ci| wantParam | Record&lt;string, Object&gt;| 是 | 否 | want 参数。 |
185e41f4b71Sopenharmony_ci| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md)| 是 | 否| 回调。 |
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci**返回值:**
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci| 类型 | 说明 |
190e41f4b71Sopenharmony_ci| -------- | -------- |
191e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**错误码:**
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
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**示例:**
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')], // 收件人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
224e41f4b71Sopenharmony_ci              'cc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // 抄收人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
225e41f4b71Sopenharmony_ci              'bcc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // 密送人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
226e41f4b71Sopenharmony_ci              'subject': encodeURI('邮件主题'), // 邮件主题,对内容使用encodeURI()方法进行url编码
227e41f4b71Sopenharmony_ci              'body': encodeURI('邮件正文'), // 邮件正文,对内容使用encodeURI()方法进行url编码
228e41f4b71Sopenharmony_ci              'ability.params.stream': [encodeURI('附件uri1'), encodeURI('附件uri2')], // 附件uri,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
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              // 按目标ability的类型启动UIAbility或UIExtensionAbility
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_ci连接到[UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md),返回连接id。
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci> **说明:**
264e41f4b71Sopenharmony_ci>
265e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci**参数:**
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci| 参数名               | 类型                     | 只读 | 可选 | 说明              |
274e41f4b71Sopenharmony_ci| -------------------- | ------------------------ | ---- | ---- |----------------- |
275e41f4b71Sopenharmony_ci| want                 | [Want](js-apis-app-ability-want.md) | 是  | 否 | Want 参数。       |
276e41f4b71Sopenharmony_ci| options              | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 |是   | 连接选项。 |
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci**返回值:**
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci| 类型 | 说明 |
281e41f4b71Sopenharmony_ci| -------- | -------- |
282e41f4b71Sopenharmony_ci| number | 返回连接id。 |
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci**错误码:**
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci| 错误码ID | 错误信息  |
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**示例:**
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_ci断开与[UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md)的连接, 与[connectServiceExtensionAbility](#uiserviceextensioncontextconnectserviceextensionability13)功能相反。
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci**参数:**
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci| 参数名                | 类型                     | 只读 | 可选 | 说明              |
393e41f4b71Sopenharmony_ci| -------------------- | ------------------------ | ---- | ----------------- | ----------------- |
394e41f4b71Sopenharmony_ci| connectionId         | number                   | 是  | 否 | 从[connectServiceExtensionAbility](#uiserviceextensioncontextconnectserviceextensionability13)接口返回的连接Id。 |
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci**返回值:**
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci| 类型                | 说明                              |
400e41f4b71Sopenharmony_ci| ------------------- | ---------------------------------|
401e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci**错误码:**
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
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**示例:**
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为调用connectServiceExtensionAbility接口时的返回值,需开发者自行维护
439e41f4b71Sopenharmony_ci            context.disconnectServiceExtensionAbility(connectionId).then(() => {
440e41f4b71Sopenharmony_ci              hilog.info(DOMAIN_NUMBER, TAG, 'disconnectServiceExtensionAbility success');
441e41f4b71Sopenharmony_ci              // 成功断连后台服务
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