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