• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UIServiceExtensionConnectCallback
2
3UIServiceExtensionConnectCallback provides callbacks for the connection to a UIServiceExtensionAbility.
4
5
6> **NOTE**
7>
8>  - The initial APIs of this module are supported since API version 14. Newly added APIs will be marked with a superscript to indicate their earliest API version.
9>  - The APIs of this module can be used only in the stage model.
10>  - The APIs of this module must be used in the main thread, but not in child threads such as Worker and TaskPool.
11
12## Modules to Import
13
14```ts
15import { common } from '@kit.AbilityKit';
16```
17
18## UIServiceExtensionConnectCallback.onData
19
20 onData(data: Record<string, Object>): void
21
22Called to receive data when a connection to the UIServiceExtensionAbility is established.
23
24> **NOTE**
25>
26> 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).
27>
28
29**Atomic service API**: This API can be used in atomic services since API version 14.
30
31**System capability**: SystemCapability.Ability.AbilityRuntime.Core
32
33**Parameters**
34
35| Name| Type                  | Mandatory| Description        |
36| ------ | ---------------------- | ---- | ------------ |
37| data   | Record<string, Object> | Yes| Data about the UIServiceExtensionAbility connection.|
38
39**Example**
40
41```ts
42import { common, Want } from '@kit.AbilityKit';
43import { BusinessError } from '@kit.BasicServicesKit';
44
45const TAG: string = '[Extension] ';
46
47@Entry
48@Component
49struct UIServiceExtensionAbility {
50  comProxy: common.UIServiceProxy | null = null;
51  dataCallBack: common.UIServiceExtensionConnectCallback = {
52    onData: (data: Record<string, Object>) => {
53      console.info(`${TAG} dataCallBack received data: ${JSON.stringify(data)}.`);
54    },
55    onDisconnect: () => {
56      console.info(`${TAG} dataCallBack onDisconnect.`);
57      this.comProxy = null;
58    }
59  }
60
61  build() {
62    Scroll() {
63      Column() {
64        // Create a button for connecting to the UIServiceExtensionAbility.
65        Button('connectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true })
66          .margin({
67            top: 5,
68            left: 10,
69            right: 10,
70            bottom: 5
71          })
72          .alignRules({
73            center: { anchor: '__container__', align: VerticalAlign.Center },
74            middle: { anchor: '__container__', align: HorizontalAlign.Center }
75          })
76          .onClick(() => {
77            this.myConnectUIServiceExtensionAbility()
78          });
79      }
80      .width('100%')
81    }
82    .height('100%')
83  }
84
85  myConnectUIServiceExtensionAbility() {
86    // Obtain the context.
87    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
88    let startWant: Want = {
89      deviceId: '',
90      bundleName: 'com.acts.myapplication',
91      abilityName: 'UiServiceExtensionAbility'
92    };
93
94    try {
95      // Connect to the UIServiceExtensionAbility.
96      context.connectUIServiceExtensionAbility(startWant, this.dataCallBack)
97        .then((proxy: common.UIServiceProxy) => {
98          console.info(TAG + `try to connectUIServiceExtensionAbility ${proxy}`);
99          this.comProxy = proxy;
100          let formData: Record<string, string> = {
101            'PATH': '/tmp/aaa.jpg'
102          };
103          try {
104            console.info(`${TAG} sendData.`);
105            this.comProxy.sendData(formData);
106          } catch (err) {
107            let code = (err as BusinessError).code;
108            let message = (err as BusinessError).message;
109            console.error(`${TAG} sendData failed, code is ${code}, message is ${message}.`);
110          }
111        }).catch((err: Error) => {
112        let code = (err as BusinessError).code;
113        let message = (err as BusinessError).message;
114        console.error(`${TAG} connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}.`);
115      });
116    } catch (err) {
117      let code = (err as BusinessError).code;
118      let message = (err as BusinessError).message;
119      console.error(`${TAG} connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}.`);
120    }
121  }
122}
123```
124
125## UIServiceExtensionConnectCallback.onDisconnect
126
127onDisconnect(): void
128
129Called when the connection to the UIServiceExtensionAbility is interrupted.
130
131> **NOTE**
132>
133> 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).
134>
135
136**Atomic service API**: This API can be used in atomic services since API version 14.
137
138**System capability**: SystemCapability.Ability.AbilityRuntime.Core
139
140**Example**
141```ts
142import { common } from '@kit.AbilityKit';
143import { BusinessError } from '@kit.BasicServicesKit';
144
145const TAG: string = '[Extension] ';
146
147@Entry
148@Component
149struct UIServiceExtensionAbility {
150  comProxy: common.UIServiceProxy | null = null;
151  // Callback for the connection.
152  dataCallBack: common.UIServiceExtensionConnectCallback = {
153    onData: (data: Record<string, Object>) => {
154      console.info(`${TAG} dataCallBack received data: ${JSON.stringify(data)}.`);
155    },
156    onDisconnect: () => {
157      // Callback for the disconnection.
158      console.info(`${TAG} dataCallBack onDisconnect.`);
159      this.comProxy = null;
160    }
161  }
162
163  build() {
164    Scroll() {
165      Column() {
166        // Create a button for disconnecting from the UIServiceExtensionAbility.
167        Button('disConnectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true })
168          .margin({
169            top: 5,
170            left: 10,
171            right: 10,
172            bottom: 5
173          })
174          .alignRules({
175            center: { anchor: '__container__', align: VerticalAlign.Center },
176            middle: { anchor: '__container__', align: HorizontalAlign.Center }
177          })
178          .onClick(() => {
179            this.myConnectUIServiceExtensionAbility()
180          });
181      }
182      .width('100%')
183    }
184    .height('100%')
185  }
186
187  myConnectUIServiceExtensionAbility() {
188    // Obtain the context.
189    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
190    // Disconnect from the UIServiceExtensionAbility.
191    try {
192      // this.comProxy is saved when the connection is established.
193      context.disconnectUIServiceExtensionAbility(this.comProxy).then(() => {
194        console.info(`${TAG} disconnectUIServiceExtensionAbility success.`);
195      }).catch((err: Error) => {
196        let code = (err as BusinessError).code;
197        let message = (err as BusinessError).message;
198        console.error(`${TAG} disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}.`);
199      });
200    } catch (err) {
201      let code = (err as BusinessError).code;
202      let message = (err as BusinessError).message;
203      console.error(`${TAG} disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}.`);
204    }
205  }
206}
207```
208