• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# FormExtensionContext
2
3The **FormExtensionContext** module, inherited from **ExtensionContext**, provides context for FormExtensionAbilities.
4
5You can use the APIs of this module to start FormExtensionAbilities.
6
7> **NOTE**
8>
9> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10> The APIs of this module can be used only in the stage model.
11
12## Modules to Import
13
14```ts
15import common from '@ohos.app.ability.common';
16```
17
18## FormExtensionContext.startAbility
19
20startAbility(want: Want, callback: AsyncCallback<void>): void
21
22Starts an ability. This API uses an asynchronous callback to return the result.
23
24**System API**: This is a system API.
25
26**System capability**: SystemCapability.Ability.Form
27
28**Error codes**
29
30| ID| Error Message|
31| -------- | -------- |
32| 202 | The application is not a system application. |
33| 401 | If the input parameter is not valid parameter. |
34| 16500050 | An IPC connection error happened. |
35| 16500100 | Failed to obtain the configuration information. |
36| 16500101 | The application is not a system application. |
37| 16501000 | An internal functional error occurred. |
38
39For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).
40
41**Parameters**
42
43| Name|                Type              | Mandatory|              Description              |
44| ------| --------------------------------- | ---- | -------------------------------------- |
45| want| [Want](js-apis-app-ability-want.md) | Yes | Information about the ability to start, such as the bundle name, ability name, and custom parameters.|
46| callback| AsyncCallback<void>       | Yes | Callback used to return the result. If the ability is started, **err** is undefined; otherwise, **err** is an error object.|
47
48**Example**
49
50```ts
51import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
52import Want from '@ohos.app.ability.Want';
53import Base from '@ohos.base';
54
55export default class MyFormExtensionAbility extends FormExtensionAbility {
56  onFormEvent(formId: string, message: string) {
57    // Call startAbility() when the message event is triggered.
58    console.log(`FormExtensionAbility onFormEvent, formId: ${formId}, message:${message}`);
59    let want: Want = {
60      deviceId: '',
61      bundleName: 'com.example.formstartability',
62      abilityName: 'EntryAbility',
63      parameters: {
64        'message': message
65      }
66    };
67    this.context.startAbility(want, (error: Base.BusinessError) => {
68      if (error) {
69        console.error(`FormExtensionContext startAbility, error:${JSON.stringify(error)}`);
70      } else {
71        console.log('FormExtensionContext startAbility success');
72      }
73    });
74  }
75};
76```
77
78## FormExtensionContext.startAbility
79
80startAbility(want: Want): Promise<void>
81
82Starts an ability. This API uses a promise to return the result.
83
84**System API**: This is a system API.
85
86**System capability**: SystemCapability.Ability.Form
87
88**Parameters**
89
90| Name|                Type              | Mandatory|              Description              |
91| ------| --------------------------------- | ---- | -------------------------------------- |
92| want| [Want](js-apis-app-ability-want.md) | Yes | Information about the ability to start, such as the bundle name, ability name, and custom parameters.|
93
94**Return value**
95
96| Type         | Description                               |
97| ------------ | ---------------------------------- |
98| Promise<void> | Promise that returns no value.|
99
100**Error codes**
101
102| ID| Error Message|
103| -------- | -------- |
104| 202 | The application is not a system application. |
105| 401 | If the input parameter is not valid parameter. |
106| 16500050 | An IPC connection error happened. |
107| 16500100 | Failed to obtain the configuration information. |
108| 16500101 | The application is not a system application. |
109| 16501000 | An internal functional error occurred. |
110
111For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).
112
113**Example**
114
115```ts
116import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
117import Want from '@ohos.app.ability.Want';
118import Base from '@ohos.base';
119
120export default class MyFormExtensionAbility extends FormExtensionAbility {
121  onFormEvent(formId: string, message: string) {
122    // Call startAbility() when the message event is triggered.
123    console.log(`FormExtensionAbility onFormEvent, formId:${formId}, message:${message}`);
124    let want: Want = {
125      deviceId: '',
126      bundleName: 'com.example.formstartability',
127      abilityName: 'EntryAbility',
128      parameters: {
129        'message': message
130      }
131    };
132    this.context.startAbility(want).then(() => {
133      console.info('StartAbility Success');
134    }).catch((error: Base.BusinessError) => {
135      console.error('StartAbility failed');
136    });
137  }
138};
139```
140
141## FormExtensionContext.connectServiceExtensionAbility<sup>10+</sup>
142
143connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
144
145Connects this ability to a ServiceExtensionAbility.
146
147**System capability**: SystemCapability.Ability.Form
148
149**System API**: This is a system API and cannot be called by third-party applications.
150
151**Parameters**
152
153| Name| Type| Mandatory| Description|
154| -------- | -------- | -------- | -------- |
155| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
156| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
157
158**Return value**
159
160| Type| Description|
161| -------- | -------- |
162| number | Returns a connect ID, which will be used for the disconnection.|
163
164**Error codes**
165
166| ID| Error Message|
167| ------- | -------------------------------- |
168| 16000001 | The specified ability does not exist. |
169| 16000002 | Incorrect ability type. |
170| 16000004 | Can not start invisible component. |
171| 16000005 | The specified process does not have the permission. |
172| 16000006 | Cross-user operations are not allowed. |
173| 16000008 | The crowdtesting application expires. |
174| 16000053 | The ability is not on the top of the UI. |
175| 16000055 | Installation-free timed out. |
176| 16000011 | The context does not exist.        |
177| 16000050 | Internal error. |
178
179For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
180
181**Example**
182
183```ts
184import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
185import Want from '@ohos.app.ability.Want';
186import rpc from '@ohos.rpc';
187import common from '@ohos.app.ability.common';
188
189let commRemote: rpc.IRemoteObject | null = null;
190export default class MyFormExtensionAbility extends FormExtensionAbility {
191  onFormEvent(formId: string, message: string) {
192    // Call connectServiceExtensionAbility() when the message event is triggered.
193    console.log(`FormExtensionAbility onFormEvent, formId:${formId}, message:${message}`);
194    let want: Want = {
195      deviceId: '',
196      bundleName: 'com.example.formstartability',
197      abilityName: 'EntryAbility',
198      parameters: {
199        'message': message
200      }
201    };
202    let options: common.ConnectOptions = {
203      onConnect(elementName, remote) {
204        commRemote = remote; // remote is used to communicate with the ServiceExtensionAbility.
205        console.log('----------- onConnect -----------');
206      },
207      onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
208      onFailed(code) { console.error('----------- onFailed -----------') }
209    };
210
211    let connection: number | null = null;
212    try {
213      connection = this.context.connectServiceExtensionAbility(want, options);
214    } catch (paramError) {
215      // Process input parameter errors.
216      console.error(`error.code: ${(paramError as Base.BusinessError).code}, error.message: ${(paramError as Base.BusinessError).message}`);
217    }
218  }
219};
220```
221
222## FormExtensionContext.disconnectServiceExtensionAbility<sup>10+</sup>
223
224disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
225
226Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses an asynchronous callback to return the result.
227
228**System capability**: SystemCapability.Ability.Form
229
230**System API**: This is a system API and cannot be called by third-party applications.
231
232**Parameters**
233
234| Name| Type| Mandatory| Description|
235| -------- | -------- | -------- | -------- |
236| connection | number | Yes| Connection ID returned after **connectServiceExtensionAbility** is called.|
237| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
238
239**Error codes**
240
241| ID| Error Message|
242| ------- | -------------------------------- |
243| 16000011 | The context does not exist.        |
244| 16000050 | Internal error. |
245
246For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
247
248**Example**
249
250```ts
251import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
252import rpc from '@ohos.rpc';
253import common from '@ohos.app.ability.common';
254import Base from '@ohos.base';
255
256// commRemote is the remote object returned in the onConnect() callback. The value null is meaningless and is only an example.
257let commRemote: rpc.IRemoteObject | null = null;
258export default class MyFormExtensionAbility extends FormExtensionAbility {
259  onFormEvent(formId: string, message: string) {
260    // In actual use, connection is the return value of connectServiceExtensionAbility(). The value 1 is meaningless and is only an example.
261    let connection: number = 1;
262
263    try {
264      this.context.disconnectServiceExtensionAbility(connection, (error: Base.BusinessError) => {
265        commRemote = null;
266        if (error.code) {
267          // Process service logic errors.
268          console.error(
269            `disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
270          return;
271        }
272        // Carry out normal service processing.
273        console.log('disconnectServiceExtensionAbility succeed');
274      });
275    } catch (paramError) {
276      commRemote = null;
277      // Process input parameter errors.
278      console.error(`error.code: ${(paramError as Base.BusinessError).code}, error.message: ${(paramError as Base.BusinessError).message}`);
279    }
280  }
281};
282```
283
284## FormExtensionContext.disconnectServiceExtensionAbility<sup>10+</sup>
285
286disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
287
288Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses a promise to return the result.
289
290**System capability**: SystemCapability.Ability.Form
291
292**System API**: This is a system API and cannot be called by third-party applications.
293
294**Parameters**
295
296| Name| Type| Mandatory| Description|
297| -------- | -------- | -------- | -------- |
298| connection | number | Yes| Connection ID returned after **connectServiceExtensionAbility** is called.|
299
300**Return value**
301
302| Type| Description|
303| -------- | -------- |
304| Promise&lt;void&gt; | Promise used to return the result.|
305
306**Error codes**
307
308| ID| Error Message|
309| ------- | -------------------------------- |
310| 16000011 | The context does not exist.        |
311| 16000050 | Internal error. |
312
313For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
314
315**Example**
316
317```ts
318import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
319import rpc from '@ohos.rpc';
320import common from '@ohos.app.ability.common';
321import Base from '@ohos.base';
322
323// commRemote is the remote object returned in the onConnect() callback. The value null is meaningless and is only an example.
324let commRemote: rpc.IRemoteObject | null = null;
325export default class MyFormExtensionAbility extends FormExtensionAbility {
326  onFormEvent(formId: string, message: string) {
327    // In actual use, connection is the return value of connectServiceExtensionAbility(). The value 1 is meaningless and is only an example.
328    let connection: number = 1;
329
330    try {
331      this.context.disconnectServiceExtensionAbility(connection)
332        .then(() => {
333          commRemote = null;
334          // Carry out normal service processing.
335          console.log('disconnectServiceExtensionAbility succeed');
336        })
337        .catch((error: Base.BusinessError) => {
338          commRemote = null;
339          // Process service logic errors.
340          console.error(
341            `disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
342        });
343    } catch (paramError) {
344      commRemote = null;
345      // Process input parameter errors.
346      console.error(`error.code: ${(paramError as Base.BusinessError).code}, error.message: ${(paramError as Base.BusinessError).message}`);
347    }
348  }
349};
350```
351