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

W
wusongqing 已提交
3
> **NOTE**<br/>
W
wusongqing 已提交
4
> 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.
W
wusongqing 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Provides **FormExtension** APIs.

## Modules to Import

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

## Required Permissions

None

## Attributes

W
wusongqing 已提交
20 21
**System capability**: SystemCapability.Ability.Form

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

## onCreate

onCreate(want: Want): formBindingData.FormBindingData

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

W
wusongqing 已提交
32
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
33

W
wusongqing 已提交
34 35 36
**Parameters**

  | Name| Type                                  | Mandatory| Description                                                        |
W
wusongqing 已提交
37
  | ------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
38
  | 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 已提交
39

W
wusongqing 已提交
40
**Return value**
W
wusongqing 已提交
41

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

W
wusongqing 已提交
46
**Example**
W
wusongqing 已提交
47

W
wusongqing 已提交
48
  ```js
W
wusongqing 已提交
49
  import formBindingData from '@ohos.application.formBindingData'
W
wusongqing 已提交
50 51 52 53 54 55 56 57 58 59
  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 已提交
60 61 62
  }
  ```

W
wusongqing 已提交
63
## FormExtension.onCastToNormal
W
wusongqing 已提交
64 65 66 67 68

onCastToNormal(formId: string): void

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

W
wusongqing 已提交
69 70 71
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
72

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

W
wusongqing 已提交
77
**Example**
W
wusongqing 已提交
78 79

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

W
wusongqing 已提交
87
## FormExtension.onUpdate
W
wusongqing 已提交
88 89 90 91 92

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 已提交
93
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
94

W
wusongqing 已提交
95 96 97
**Parameters**

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

W
wusongqing 已提交
101
**Example**
W
wusongqing 已提交
102

W
wusongqing 已提交
103
  ```js
W
wusongqing 已提交
104
  import formBindingData from '@ohos.application.formBindingData'
W
wusongqing 已提交
105 106 107 108 109 110 111 112 113 114
  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 已提交
115 116 117
  }
  ```

W
wusongqing 已提交
118
## FormExtension.onVisibilityChange
W
wusongqing 已提交
119 120 121 122 123

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

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

W
wusongqing 已提交
124 125 126
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
127

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

W
wusongqing 已提交
132
**Example**
W
wusongqing 已提交
133

W
wusongqing 已提交
134
  ```js
W
wusongqing 已提交
135
    import formBindingData from '@ohos.application.formBindingData'
W
wusongqing 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148
  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 已提交
149 150 151 152
      }
  }
  ```

W
wusongqing 已提交
153
## FormExtension.onEvent
W
wusongqing 已提交
154 155 156 157 158

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

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

W
wusongqing 已提交
159 160 161
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
162

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

W
wusongqing 已提交
168
**Example**
W
wusongqing 已提交
169

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

W
wusongqing 已提交
178
## FormExtension.onDestroy
W
wusongqing 已提交
179 180 181 182 183

onDestroy(formId: string): void

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

W
wusongqing 已提交
184
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
185

W
wusongqing 已提交
186 187 188
**Parameters**

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

W
wusongqing 已提交
192
**Example**
W
wusongqing 已提交
193

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

## 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 已提交
219
  class MyFormExtension extends FormExtension {
W
wusongqing 已提交
220 221 222 223 224
      onConfigurationUpdated(config) {
          console.log('onConfigurationUpdated, config:' + JSON.stringify(config));
      }
  }
  ```
W
wusongqing 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

  ## FormExtension.onAcquireFormState

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

Used by the widget provider to receive the widget state query request. By default, the initial widget state is returned.

**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;
      }
  }
  ```