js-apis-formextension.md 8.4 KB
Newer Older
W
wusongqing 已提交
1 2
# FormExtension

W
wusongqing 已提交
3 4 5 6
> **NOTE**
> 
> 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. 
> The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Provides **FormExtension** APIs.

## Modules to Import

```
import FormExtension from '@ohos.application.FormExtension';
```

## Required Permissions

None

## Attributes

W
wusongqing 已提交
22 23
**System capability**: SystemCapability.Ability.Form

W
wusongqing 已提交
24
| Name   | Type                                               | Readable| Writable| Description                                               |
W
wusongqing 已提交
25
| ------- | ------------------------------------------------------- | ---- | ---- | --------------------------------------------------- |
W
wusongqing 已提交
26
| context | [FormExtensionContext](js-apis-formextensioncontext.md) | Yes  | No  | Context of the **FormExtension**. This context is inherited from **ExtensionContext**.|
W
wusongqing 已提交
27 28 29 30 31 32 33

## onCreate

onCreate(want: Want): formBindingData.FormBindingData

Called to notify the widget provider that a **Form** instance (widget) has been created.

W
wusongqing 已提交
34
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
35

W
wusongqing 已提交
36 37 38
**Parameters**

  | Name| Type                                  | Mandatory| Description                                                        |
W
wusongqing 已提交
39
  | ------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
40
  | want   | [Want](js-apis-application-Want.md) | Yes  | Information related to the extension, including the widget ID, name, and style. The information must be managed as persistent data to facilitate subsequent widget update and deletion.|
W
wusongqing 已提交
41

W
wusongqing 已提交
42
**Return value**
W
wusongqing 已提交
43

W
wusongqing 已提交
44
  | Type                                                        | Description                                                       |
W
wusongqing 已提交
45 46 47
  | ------------------------------------------------------------ | ----------------------------------------------------------- |
  | [formBindingData.FormBindingData](js-apis-formbindingdata.md#formbindingdata) | A **formBindingData.FormBindingData** object containing the data to be displayed on the widget.|

W
wusongqing 已提交
48
**Example**
W
wusongqing 已提交
49

W
wusongqing 已提交
50
  ```js
W
wusongqing 已提交
51
  import formBindingData from '@ohos.application.formBindingData'
W
wusongqing 已提交
52 53 54 55 56 57 58 59 60 61
  export default class MyFormExtension extends FormExtension {
      onCreate(want) {
          console.log('FormExtension onCreate, want:' + want.abilityName);
          let dataObj1 = {
              temperature:"11c",
              "time":"11:00"
          };
          let obj1 = formBindingData.createFormBindingData(dataObj1);
          return obj1;
      }
W
wusongqing 已提交
62 63 64
  }
  ```

W
wusongqing 已提交
65
## FormExtension.onCastToNormal
W
wusongqing 已提交
66 67 68 69 70

onCastToNormal(formId: string): void

Called to notify the widget provider that a temporary widget has been converted to a normal one.

W
wusongqing 已提交
71 72 73
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
74

W
wusongqing 已提交
75
  | Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
76
  | ------ | ------ | ---- | ------------------------ |
W
wusongqing 已提交
77
  | formId | string | Yes  | ID of the widget that requests to be converted to a normal one.|
W
wusongqing 已提交
78

W
wusongqing 已提交
79
**Example**
W
wusongqing 已提交
80 81

  ```
W
wusongqing 已提交
82 83 84 85
  export default class MyFormExtension extends FormExtension {
      onCastToNormal(formId) {
          console.log('FormExtension onCastToNormal, formId:' + formId);
      }
W
wusongqing 已提交
86 87 88
  }
  ```

W
wusongqing 已提交
89
## FormExtension.onUpdate
W
wusongqing 已提交
90 91 92 93 94

onUpdate(formId: string): void

Called to notify the widget provider that a widget has been updated. After obtaining the latest data, the caller invokes **updateForm** of the [FormExtensionContext](js-apis-formextensioncontext.md) class to update the widget data.

W
wusongqing 已提交
95
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
96

W
wusongqing 已提交
97 98 99
**Parameters**

  | Name| Type  | Mandatory| Description              |
W
wusongqing 已提交
100
  | ------ | ------ | ---- | ------------------ |
W
wusongqing 已提交
101
  | formId | string | Yes  | ID of the widget that requests to be updated.|
W
wusongqing 已提交
102

W
wusongqing 已提交
103
**Example**
W
wusongqing 已提交
104

W
wusongqing 已提交
105
  ```js
W
wusongqing 已提交
106
  import formBindingData from '@ohos.application.formBindingData'
W
wusongqing 已提交
107 108 109 110 111 112 113 114 115 116
  export default class MyFormExtension extends FormExtension {
      onUpdate(formId) {
          console.log('FormExtension onUpdate, formId:' + formId);
          let obj2 = formBindingData.createFormBindingData({temperature:"22c", time:"22:00"});
          this.context.updateForm(formId, obj2)
              .then((data)=>{
                  console.log('FormExtension context updateForm, data:' + data);
              }).catch((error) => {
              console.error('Operation updateForm failed. Cause: ' + error);});
      }
W
wusongqing 已提交
117 118 119
  }
  ```

W
wusongqing 已提交
120
## FormExtension.onVisibilityChange
W
wusongqing 已提交
121 122 123 124 125

onVisibilityChange(newStatus: { [key: string]: number }): void

Called to notify the widget provider of the change of visibility.

W
wusongqing 已提交
126 127 128
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
129

W
wusongqing 已提交
130
  | Name   | Type                     | Mandatory| Description                        |
W
wusongqing 已提交
131
  | --------- | ------------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
132
  | newStatus | { [key: string]: number } | Yes  | ID and visibility status of the widget to be changed.|
W
wusongqing 已提交
133

W
wusongqing 已提交
134
**Example**
W
wusongqing 已提交
135

W
wusongqing 已提交
136
  ```js
W
wusongqing 已提交
137
    import formBindingData from '@ohos.application.formBindingData'
W
wusongqing 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150
  export default class MyFormExtension extends FormExtension {
      onVisibilityChange(newStatus) {
          console.log('FormExtension onVisibilityChange, newStatus:' + newStatus);
          let obj2 = formBindingData.createFormBindingData({temperature:"22c", time:"22:00"});
  
          for (let key in newStatus) {
              console.log('FormExtension onVisibilityChange, key:' + key + ", value=" + newStatus[key]);
              this.context.updateForm(key, obj2)
                  .then((data)=>{
                      console.log('FormExtension context updateForm, data:' + data);
                  }).catch((error) => {
                  console.error('Operation updateForm failed. Cause: ' + error);});
          }
W
wusongqing 已提交
151 152 153 154
      }
  }
  ```

W
wusongqing 已提交
155
## FormExtension.onEvent
W
wusongqing 已提交
156 157 158 159 160

onEvent(formId: string, message: string): void

Called to instruct the widget provider to receive and process the widget event.

W
wusongqing 已提交
161 162 163
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
164

W
wusongqing 已提交
165
  | Name | Type  | Mandatory| Description                  |
W
wusongqing 已提交
166
  | ------- | ------ | ---- | ---------------------- |
W
wusongqing 已提交
167 168
  | formId  | string | Yes  | ID of the widget that requests the event.|
  | message | string | Yes  | Event message.            |
W
wusongqing 已提交
169

W
wusongqing 已提交
170
**Example**
W
wusongqing 已提交
171

W
wusongqing 已提交
172
  ```js
W
wusongqing 已提交
173 174 175 176
  export default class MyFormExtension extends FormExtension {
      onEvent(formId, message) {
          console.log('FormExtension onEvent, formId:' + formId + ", message:" + message);
      }
W
wusongqing 已提交
177 178 179
  }
  ```

W
wusongqing 已提交
180
## FormExtension.onDestroy
W
wusongqing 已提交
181 182 183 184 185

onDestroy(formId: string): void

Called to notify the widget provider that a **Form** instance (widget) has been destroyed.

W
wusongqing 已提交
186
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
187

W
wusongqing 已提交
188 189 190
**Parameters**

  | Name| Type  | Mandatory| Description              |
W
wusongqing 已提交
191
  | ------ | ------ | ---- | ------------------ |
W
wusongqing 已提交
192
  | formId | string | Yes  | ID of the widget to be destroyed.|
W
wusongqing 已提交
193

W
wusongqing 已提交
194
**Example**
W
wusongqing 已提交
195

W
wusongqing 已提交
196
  ```js
W
wusongqing 已提交
197 198 199 200
  export default class MyFormExtension extends FormExtension {
      onDestroy(formId) {
          console.log('FormExtension onDestroy, formId:' + formId);
      }
W
wusongqing 已提交
201 202
  }
  ```
W
wusongqing 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

## FormExtension.onConfigurationUpdated

onConfigurationUpdated(config: Configuration): void;

Called when the configuration of the environment where the ability is running is updated.

**System capability**: SystemCapability.Ability.Form

**Parameters**

  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | config | [Configuration](#section188911144124715) | Yes| New configuration.| 

**Example**
    
  ```js
W
wusongqing 已提交
221
  class MyFormExtension extends FormExtension {
W
wusongqing 已提交
222 223 224 225 226
      onConfigurationUpdated(config) {
          console.log('onConfigurationUpdated, config:' + JSON.stringify(config));
      }
  }
  ```
W
wusongqing 已提交
227 228 229 230 231

  ## FormExtension.onAcquireFormState

onAcquireFormState?(want: Want): formInfo.FormState;

W
wusongqing 已提交
232
Called when the widget provider receives the status query result of a specified service widget. By default, the initial state is returned.
W
wusongqing 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

**System capability**: SystemCapability.Ability.Form

**Parameters**

  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | want | [Want](js-apis-application-Want.md) | No| Description of the widget state, including the bundle name, ability name, module name, widget name, and widget dimension.| 

**Example**
    
  ```js
  import fromInfo from '@ohos.application.fromInfo'
  class MyFormExtension extends FormExtension {
      onAcquireFormState(want) {
          console.log('FormExtension onAcquireFormState, want:' + want);
          return fromInfo.FormState.UNKNOWN;
      }
  }
  ```