1e41f4b71Sopenharmony_ci# UIServiceProxy 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciUIServiceProxy functions as a proxy to send data from the UIServiceExtensionAbility client to the server. 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## UIServiceProxy.sendData 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_cisendData(data: Record\<string, Object>): void 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciSends data to the UIServiceExtensionAbility server. 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 to be sent to the UIServiceExtensionAbility server.| 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Error codes** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| ID| Error Message | 43e41f4b71Sopenharmony_ci| -------- | ----------------------------| 44e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 45e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Example** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```ts 50e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 51e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ciconst TAG: string = '[Extension] '; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci@Entry 56e41f4b71Sopenharmony_ci@Component 57e41f4b71Sopenharmony_cistruct UIServiceExtensionAbility { 58e41f4b71Sopenharmony_ci comProxy: common.UIServiceProxy | null = null; 59e41f4b71Sopenharmony_ci dataCallBack: common.UIServiceExtensionConnectCallback = { 60e41f4b71Sopenharmony_ci onData: (data: Record<string, Object>) => { 61e41f4b71Sopenharmony_ci console.log(TAG + `dataCallBack received data: `, JSON.stringify(data)); 62e41f4b71Sopenharmony_ci }, 63e41f4b71Sopenharmony_ci onDisconnect: () => { 64e41f4b71Sopenharmony_ci console.log(TAG + `dataCallBack onDisconnect`); 65e41f4b71Sopenharmony_ci this.comProxy = null; 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci build() { 70e41f4b71Sopenharmony_ci Scroll() { 71e41f4b71Sopenharmony_ci Column() { 72e41f4b71Sopenharmony_ci // Create a button for connecting to the UIServiceExtensionAbility. 73e41f4b71Sopenharmony_ci Button('connectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) 74e41f4b71Sopenharmony_ci .margin({ 75e41f4b71Sopenharmony_ci top: 5, 76e41f4b71Sopenharmony_ci left: 10, 77e41f4b71Sopenharmony_ci right: 10, 78e41f4b71Sopenharmony_ci bottom: 5 79e41f4b71Sopenharmony_ci }) 80e41f4b71Sopenharmony_ci .alignRules({ 81e41f4b71Sopenharmony_ci center: { anchor: '__container__', align: VerticalAlign.Center }, 82e41f4b71Sopenharmony_ci middle: { anchor: '__container__', align: HorizontalAlign.Center } 83e41f4b71Sopenharmony_ci }) 84e41f4b71Sopenharmony_ci .onClick(() => { 85e41f4b71Sopenharmony_ci this.myConnectUIServiceExtensionAbility() 86e41f4b71Sopenharmony_ci }); 87e41f4b71Sopenharmony_ci } 88e41f4b71Sopenharmony_ci .width('100%') 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci .height('100%') 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci // Customize a function for connecting to the UIServiceExtensionAbility. 94e41f4b71Sopenharmony_ci myConnectUIServiceExtensionAbility() { 95e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; 96e41f4b71Sopenharmony_ci let startWant: Want = { 97e41f4b71Sopenharmony_ci deviceId: '', 98e41f4b71Sopenharmony_ci bundleName: 'com.acts.myapplication', 99e41f4b71Sopenharmony_ci abilityName: 'UiServiceExtensionAbility' 100e41f4b71Sopenharmony_ci }; 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci try { 103e41f4b71Sopenharmony_ci // Connect to the UIServiceExtensionAbility. 104e41f4b71Sopenharmony_ci context.connectUIServiceExtensionAbility(startWant, this.dataCallBack) 105e41f4b71Sopenharmony_ci .then((proxy: common.UIServiceProxy) => { 106e41f4b71Sopenharmony_ci console.log(TAG + `try to connectUIServiceExtensionAbility ${proxy}}`); 107e41f4b71Sopenharmony_ci this.comProxy = proxy; 108e41f4b71Sopenharmony_ci let formData: Record<string,string> = { 109e41f4b71Sopenharmony_ci 'PATH': '/tmp/aaa.jpg' 110e41f4b71Sopenharmony_ci }; 111e41f4b71Sopenharmony_ci try { 112e41f4b71Sopenharmony_ci console.log(TAG + `sendData`); 113e41f4b71Sopenharmony_ci // Send data to the UIServiceExtensionAbility. 114e41f4b71Sopenharmony_ci this.comProxy.sendData(formData); 115e41f4b71Sopenharmony_ci } catch (err) { 116e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 117e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 118e41f4b71Sopenharmony_ci console.log(TAG + `sendData failed, code is ${code}, message is ${message}`); 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci }).catch((err: Error) => { 121e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 122e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 123e41f4b71Sopenharmony_ci console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 124e41f4b71Sopenharmony_ci }); 125e41f4b71Sopenharmony_ci } catch (err) { 126e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 127e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 128e41f4b71Sopenharmony_ci console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci} 132e41f4b71Sopenharmony_ci``` 133