js-apis-app-form-formProvider.md 20.4 KB
Newer Older
1
# @ohos.app.form.formProvider (formProvider)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

The **FormProvider** module provides APIs related to the widget provider. You can use the APIs to update a widget, set the next refresh time for a widget, obtain widget information, and request a widget release.

> **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.

## Modules to Import

```ts
import formProvider from '@ohos.app.form.formProvider';
```

## setFormNextRefreshTime

setFormNextRefreshTime(formId: string, minute: number, callback: AsyncCallback<void>): void

Sets the next refresh time for a widget. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name| Type   | Mandatory| Description                                  |
| ------ | ------ | ---- | ------------------------------------- |
| formId | string | Yes  | Widget ID.                              |
| minute | number | Yes  | Refresh interval, in minutes. The value must be greater than or equal to 5.    |
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
34 35
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
36 37 38 39

**Example**

```ts
40 41
import formProvider from '@ohos.app.form.formProvider';

42
let formId = '12400633174999288';
43 44 45
try {
  formProvider.setFormNextRefreshTime(formId, 5, (error, data) => {
    if (error) {
46
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
47 48 49 50 51
    } else {
      console.log(`formProvider setFormNextRefreshTime success`);
    }
  });
} catch (error) {
52
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}
```

## setFormNextRefreshTime

setFormNextRefreshTime(formId: string, minute: number): Promise<void>

Sets the next refresh time for a widget. This API uses a promise to return the result.

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

**Parameters**

| Name| Type   | Mandatory| Description                                  |
| ------ | ------ | ---- | ------------------------------------- |
| formId | string | Yes  | Widget ID.                              |
| minute | number | Yes  | Refresh interval, in minutes. The value must be greater than or equal to 5.    |

**Return value**

| Type         | Description                             |
| ------------- | ---------------------------------- |
| Promise\<void> | Promise that returns no value.     |

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
81 82
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
83 84 85 86

**Example**

```ts
87 88
import formProvider from '@ohos.app.form.formProvider';

89
let formId = '12400633174999288';
90 91
try {
  formProvider.setFormNextRefreshTime(formId, 5).then(() => {
92
    console.log(`formProvider setFormNextRefreshTime success`);
93
  }).catch((error) => {
94
    console.log(`promise error, code: ${error.code}, message: ${error.message})`);
95 96
  });
} catch (error) {
97
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
}
```

## updateForm

updateForm(formId: string, formBindingData: formBindingData.FormBindingData,callback: AsyncCallback&lt;void&gt;): void

Updates a widget. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name| Type                                                                   | Mandatory| Description            |
| ------ | ---------------------------------------------------------------------- | ---- | ---------------- |
| formId | string                                                                 | Yes  | ID of the widget to update.|
| formBindingData.FormBindingData | [FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | Yes  | Data to be used for the update.   |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
121 122
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
123 124 125 126

**Example**

```ts
G
Gloria 已提交
127
import formBindingData from '@ohos.app.form.formBindingData';
128 129
import formProvider from '@ohos.app.form.formProvider';

130
let formId = '12400633174999288';
131
try {
132
  let obj = formBindingData.createFormBindingData({temperature:'22c', time:'22:00'});
133 134
  formProvider.updateForm(formId, obj, (error, data) => {
    if (error) {
135
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
136 137 138 139 140
    } else {
      console.log(`formProvider updateForm success`);
    }
  });
} catch (error) {
141
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}
```

## updateForm

updateForm(formId: string, formBindingData: formBindingData.FormBindingData): Promise&lt;void&gt;

Updates a widget. This API uses a promise to return the result.

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

**Parameters**

| Name| Type                                                                   | Mandatory| Description            |
| ------ | ---------------------------------------------------------------------- | ---- | ---------------- |
| formId | string                                                                 | Yes  | ID of the widget to update.|
| formBindingData.FormBindingData | [FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | Yes  | Data to be used for the update.   |

**Return value**

| Type          | Description                               |
| -------------- | ----------------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
170 171
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
172 173 174 175

**Example**

```ts
G
Gloria 已提交
176
import formBindingData from '@ohos.app.form.formBindingData';
177 178
import formProvider from '@ohos.app.form.formProvider';

179 180
let formId = '12400633174999288';
let obj = formBindingData.createFormBindingData({ temperature: '22c', time: '22:00' });
181 182
try {
  formProvider.updateForm(formId, obj).then(() => {
183
    console.log(`formProvider updateForm success`);
184
  }).catch((error) => {
185
    console.log(`promise error, code: ${error.code}, message: ${error.message})`);
186 187
  });
} catch (error) {
188
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}
```

## getFormsInfo

getFormsInfo(callback: AsyncCallback&lt;Array&lt;formInfo.FormInfo&gt;&gt;): void

Obtains the application's widget information on the device. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name| Type   | Mandatory| Description   |
| ------ | ------ | ---- | ------- |
204
| callback | AsyncCallback&lt;Array&lt;[formInfo.FormInfo](js-apis-app-form-formInfo.md)&gt;&gt; | Yes| Callback used to return the information obtained.|
205 206 207 208 209

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
210 211
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
212 213 214 215 216


**Example**

```ts
217 218
import formProvider from '@ohos.app.form.formProvider';

219 220 221
try {
  formProvider.getFormsInfo((error, data) => {
    if (error) {
222
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
223
    } else {
224
      console.log('formProvider getFormsInfo, data: ${JSON.stringify(data)}');
225 226 227
    }
  });
} catch (error) {
228
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
}
```
## getFormsInfo

getFormsInfo(filter: formInfo.FormInfoFilter, callback: AsyncCallback&lt;Array&lt;formInfo.FormInfo&gt;&gt;): void

Obtains the application's widget information that meets a filter criterion on the device. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name| Type   | Mandatory| Description   |
| ------ | ------ | ---- | ------- |
| filter | [formInfo.FormInfoFilter](js-apis-app-form-formInfo.md#forminfofilter) | Yes| Filter criterion.|
244
| callback | AsyncCallback&lt;Array&lt;[formInfo.FormInfo](js-apis-app-form-formInfo.md)&gt;&gt; | Yes| Callback used to return the information obtained.|
245 246 247 248 249

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
250 251
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
252 253 254 255

**Example**

```ts
256 257 258 259 260
import formInfo from '@ohos.app.form.formInfo';
import formProvider from '@ohos.app.form.formProvider';

const filter: formInfo.FormInfoFilter = {
  // get info of forms belong to module entry.
261
  moduleName: 'entry'
262 263 264 265
};
try {
  formProvider.getFormsInfo(filter, (error, data) => {
    if (error) {
266
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
267
    } else {
268
      console.log('formProvider getFormsInfo, data: ${JSON.stringify(data)}');
269 270
    }
  });
271 272
} catch (error) {
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}
```

## getFormsInfo

getFormsInfo(filter?: formInfo.FormInfoFilter): Promise&lt;Array&lt;formInfo.FormInfo&gt;&gt;

Obtains the application's widget information on the device. This API uses a promise to return the result.

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

**Parameters**

| Name| Type   | Mandatory| Description   |
| ------ | ------ | ---- | ------- |
| filter | [formInfo.FormInfoFilter](js-apis-app-form-formInfo.md#forminfofilter) | No| Filter criterion.|

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
294
| Promise&lt;Array&lt;[formInfo.FormInfo](js-apis-app-form-formInfo.md)&gt;&gt; | Promise used to return the information obtained.|
295 296 297 298 299

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
300 301
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
302 303 304 305

**Example**

```ts
306 307 308 309 310
import formInfo from '@ohos.app.form.formInfo';
import formProvider from '@ohos.app.form.formProvider';

const filter: formInfo.FormInfoFilter = {
  // get info of forms belong to module entry.
311
  moduleName: 'entry'
312 313 314
};
try {
  formProvider.getFormsInfo(filter).then((data) => {
315
    console.log('formProvider getFormsInfo, data: ${JSON.stringify(data)}');
316
  }).catch((error) => {
317
    console.log(`promise error, code: ${error.code}, message: ${error.message})`);
318 319
  });
} catch (error) {
320
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
}
```

## requestPublishForm

requestPublishForm(want: Want, formBindingData: formBindingData.FormBindingData, callback: AsyncCallback\<string>): void

Requests to publish a widget carrying data to the widget host. This API uses an asynchronous callback to return the result. This API is usually called by the home screen.

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

**System API**: This is a system API.

**Parameters**

| Name| Type                                                                   | Mandatory| Description            |
| ------ | ---------------------------------------------------------------------- | ---- | ---------------- |
338
| want | [Want](js-apis-application-want.md)                           | Yes  | Request used for publishing. The following fields must be included:<br>Information about the target widget.<br>**abilityName**: ability of the target widget.<br>**parameters**:<br>'ohos.extra.param.key.form_dimension'<br>'ohos.extra.param.key.form_name'<br>'ohos.extra.param.key.module_name' |
339
| formBindingData | [formBindingData.FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | Yes  | Data used for creating the widget.|
340 341 342 343 344 345
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the widget ID.|

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
346 347
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
348 349 350 351

**Example**

```ts
G
Gloria 已提交
352
import formBindingData from '@ohos.app.form.formBindingData';
353 354 355
import formProvider from '@ohos.app.form.formProvider';

let want = {
356
  abilityName: 'FormAbility',
357
  parameters: {
358 359 360
    'ohos.extra.param.key.form_dimension': 2,
    'ohos.extra.param.key.form_name': 'widget',
    'ohos.extra.param.key.module_name': 'entry'
361 362 363
  }
};
try {
364
  let obj = formBindingData.createFormBindingData({ temperature: '22c', time: '22:00' });
365 366
  formProvider.requestPublishForm(want, obj, (error, data) => {
    if (error) {
367
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
368
    } else {
369
      console.log('formProvider requestPublishForm, form ID is: ${JSON.stringify(data)}');
370 371 372
    }
  });
} catch (error) {
373
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
}
```

## requestPublishForm

requestPublishForm(want: Want, callback: AsyncCallback&lt;string&gt;): void

Requests to publish a widget to the widget host. This API uses an asynchronous callback to return the result. This API is usually called by the home screen.

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

**System API**: This is a system API.

**Parameters**

| Name  | Type                               | Mandatory| Description                                                        |
| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
391
| want     | [Want](js-apis-application-want.md) | Yes  | Request used for publishing. The following fields must be included:<br>Information about the target widget.<br>**abilityName**: ability of the target widget.<br>**parameters**:<br>'ohos.extra.param.key.form_dimension'<br>'ohos.extra.param.key.form_name'<br>'ohos.extra.param.key.module_name' |
392 393 394 395 396 397
| callback | AsyncCallback&lt;string&gt;         | Yes  |  Callback used to return the widget ID.|

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
398 399
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
400 401 402 403

**Example**

```ts
404 405 406
import formProvider from '@ohos.app.form.formProvider';

let want = {
407
  abilityName: 'FormAbility',
408
  parameters: {
409 410 411
    'ohos.extra.param.key.form_dimension': 2,
    'ohos.extra.param.key.form_name': 'widget',
    'ohos.extra.param.key.module_name': 'entry'
412 413 414 415 416
  }
};
try {
  formProvider.requestPublishForm(want, (error, data) => {
    if (error) {
417
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
418
    } else {
419
      console.log('formProvider requestPublishForm, form ID is: ${JSON.stringify(data)}');
420 421 422
    }
  });
} catch (error) {
423
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
}
```

## requestPublishForm

requestPublishForm(want: Want, formBindingData?: formBindingData.FormBindingData): Promise&lt;string&gt;

Requests to publish a widget to the widget host. This API uses a promise to return the result. This API is usually called by the home screen.

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

**System API**: This is a system API.

**Parameters**

| Name         | Type                                                        | Mandatory| Description                                                        |
| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
441
| want            | [Want](js-apis-application-want.md)                          | Yes  | Request used for publishing. The following fields must be included:<br>Information about the target widget.<br>**abilityName**: ability of the target widget.<br>**parameters**:<br>'ohos.extra.param.key.form_dimension'<br>'ohos.extra.param.key.form_name'<br>'ohos.extra.param.key.module_name' |
442
| formBindingData | [formBindingData.FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | No  | Data used for creating the widget.                                          |
443 444 445 446 447 448 449 450 451 452 453

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;string&gt; | Promise used to return the widget ID.|

**Error codes**

| Error Code ID| Error Message|
| -------- | -------- |
454 455
| 401 | Incorrect input parameter.|
|For details about the error codes, see [Form Error Codes](../errorcodes/errorcode-form.md).|
456 457 458 459

**Example**

```ts
460 461 462
import formProvider from '@ohos.app.form.formProvider';

let want = {
463
  abilityName: 'FormAbility',
464
  parameters: {
465 466 467
    'ohos.extra.param.key.form_dimension': 2,
    'ohos.extra.param.key.form_name': 'widget',
    'ohos.extra.param.key.module_name': 'entry'
468 469 470 471
  }
};
try {
  formProvider.requestPublishForm(want).then((data) => {
472
    console.log('formProvider requestPublishForm success, form ID is : ${JSON.stringify(data)}');
473
  }).catch((error) => {
474
    console.log(`promise error, code: ${error.code}, message: ${error.message})`);
475 476
  });
} catch (error) {
477
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
}
```

## isRequestPublishFormSupported

isRequestPublishFormSupported(callback: AsyncCallback&lt;boolean&gt;): void

Checks whether a widget can be published to the widget host. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

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

**Parameters**

| Name| Type   | Mandatory| Description   |
| ------ | ------ | ---- | ------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the widget can be published to the widget host.|

**Example**

```ts
500 501
import formProvider from '@ohos.app.form.formProvider';

502 503
try {
  formProvider.isRequestPublishFormSupported((error, isSupported) => {
504 505 506 507 508
    if (error) {
      console.log(`callback error, code: ${error.code}, message: ${error.message})`);
    } else {
      if (isSupported) {
        var want = {
509
          abilityName: 'FormAbility',
510
          parameters: {
511 512 513
            'ohos.extra.param.key.form_dimension': 2,
            'ohos.extra.param.key.form_name': 'widget',
            'ohos.extra.param.key.module_name': 'entry'
514
          }
515 516 517 518 519 520
        };
        try {
          formProvider.requestPublishForm(want, (error, data) => {
            if (error) {
              console.log(`callback error, code: ${error.code}, message: ${error.message})`);
            } else {
521
              console.log('formProvider requestPublishForm, form ID is: ${JSON.stringify(data)}');
522 523 524 525 526
            }
          });
        } catch (error) {
          console.log(`catch error, code: ${error.code}, message: ${error.message})`);
        }
527 528
      }
    }
529
  });
530
} catch (error) {
531
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
}
```

## isRequestPublishFormSupported

isRequestPublishFormSupported(): Promise&lt;boolean&gt;

Checks whether a widget can be published to the widget host. This API uses a promise to return the result.

**System API**: This is a system API.

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

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return whether the widget can be published to the widget host.|

**Example**

```ts
554 555
import formProvider from '@ohos.app.form.formProvider';

556 557 558 559
try {
  formProvider.isRequestPublishFormSupported().then((isSupported) => {
    if (isSupported) {
      var want = {
560
        abilityName: 'FormAbility',
561
        parameters: {
562 563 564
          'ohos.extra.param.key.form_dimension': 2,
          'ohos.extra.param.key.form_name': 'widget',
          'ohos.extra.param.key.module_name': 'entry'
565
        }
566 567 568
      };
      try {
        formProvider.requestPublishForm(want).then((data) => {
569
          console.log('formProvider requestPublishForm success, form ID is : ${JSON.stringify(data)}');
570
        }).catch((error) => {
571
          console.log(`promise error, code: ${error.code}, message: ${error.message})`);
572 573
        });
      } catch (error) {
574
        console.log(`catch error, code: ${error.code}, message: ${error.message})`);
575 576 577
      }
    }
  }).catch((error) => {
578
    console.log(`promise error, code: ${error.code}, message: ${error.message})`);
579 580
  });
} catch (error) {
581
  console.log(`catch error, code: ${error.code}, message: ${error.message})`);
582 583
}
```