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