• 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## Usage
19
20Before using the **ServiceExtensionContext** module, you must first obtain a **FormExtensionAbility** instance.
21
22```ts
23import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
24import formBindingData from '@ohos.app.form.formBindingData';
25export default class MyFormExtensionAbility extends FormExtensionAbility {
26    onAddForm() {
27        let formContext = this.context; // ��ȡFormExtensionContext
28        // ...
29        let dataObj1 = {
30            temperature:'11c',
31            'time':'11:00'
32        };
33        let obj1 = formBindingData.createFormBindingData(dataObj1);
34        return obj1;
35    }
36}
37```
38
39## startAbility
40
41startAbility(want: Want, callback: AsyncCallback<void>): void
42
43Starts an ability. This API uses an asynchronous callback to return the result.
44
45**System API**: This is a system API.
46
47**System capability**: SystemCapability.Ability.Form
48
49**Error codes**
50
51| ID| Error Message|
52| -------- | -------- |
53| 202 | The application is not a system application. |
54| 401 | If the input parameter is not valid parameter. |
55| 16500050 | An IPC connection error happened. |
56| 16500100 | Failed to obtain the configuration information. |
57| 16500101 | The application is not a system application. |
58| 16501000 | An internal functional error occurred. |
59|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
60
61**Parameters**
62
63| Name|                Type              | Mandatory|              Description              |
64| ------| --------------------------------- | ---- | -------------------------------------- |
65| want| [Want](js-apis-application-want.md) | Yes | Information about the ability to start, such as the bundle name, ability name, and custom parameters.|
66| callback| AsyncCallback<void>       | Yes | Callback used to return the result. If the ability is started, **err** is undefined; otherwise, **err** is an error object.|
67
68**Example**
69
70```ts
71let want = {
72    deviceId: '',
73    bundleName: 'com.example.formstartability',
74    abilityName: 'MainAbility',
75    action: 'action1',
76    entities: ['entity1'],
77    type: 'MIMETYPE',
78    uri: 'key={true,true,false}',
79    parameters: {}
80};
81this.context.startAbility(want, (error) => {
82    if (error) {
83      console.log('FormExtensionContext startAbility, error:' + JSON.stringify(error));
84    } else {
85      console.log(`FormExtensionContext startAbility success`);
86    }
87});
88```
89
90## startAbility
91
92startAbility(want: Want): Promise<void>
93
94Starts an ability. This API uses a promise to return the result.
95
96**System API**: This is a system API.
97
98**System capability**: SystemCapability.Ability.Form
99
100**Parameters**
101
102| Name|                Type              | Mandatory|              Description              |
103| ------| --------------------------------- | ---- | -------------------------------------- |
104| want| [Want](js-apis-application-want.md) | Yes | Information about the ability to start, such as the bundle name, ability name, and custom parameters.|
105
106**Return value**
107
108| Type         | Description                               |
109| ------------ | ---------------------------------- |
110| Promise<void< | Promise that returns no value.|
111
112**Error codes**
113
114| ID| Error Message|
115| -------- | -------- |
116| 202 | The application is not a system application. |
117| 401 | If the input parameter is not valid parameter. |
118| 16500050 | An IPC connection error happened. |
119| 16500100 | Failed to obtain the configuration information. |
120| 16500101 | The application is not a system application. |
121| 16501000 | An internal functional error occurred. |
122|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
123
124**Example**
125
126```ts
127let want = {
128    deviceId: '',
129    bundleName: 'com.example.formstartability',
130    abilityName: 'MainAbility',
131    action: 'action1',
132    entities: ['entity1'],
133    type: 'MIMETYPE',
134    uri: 'key={true,true,false}',
135    parameters: {}
136};
137this.context.startAbility(want).then(() => {
138    console.info('StartAbility Success');
139}).catch((error) => {
140    console.info('StartAbility failed');
141});
142```
143