harmony 鸿蒙Updating Widget Content by Widget Host (for System Applications Only)

  • 2023-06-24
  • 浏览 (717)

Updating Widget Content by Widget Host (for System Applications Only)

Widgets that are updated periodically are subject to the scheduled time or interval settings. To offer more flexible updates, the widget host can provide a button to proactively trigger a widget update. Specifically, the widget host calls the requestForm API to request a widget update. The system then calls the onUpdateForm lifecycle callback in the FormExtensionAbility of the widget provider. In the callback, the updateForm API can be used to update the widget content. For details about the onUpdateForm lifecycle callback, see Updating Widget Content Through FormExtensionAbility.

import formHost from '@ohos.app.form.formHost';
import Base from '@ohos.base';

@Entry()
@Component
struct WidgetCard {
  formId: string = 'formId'; // Widget ID

  build() {
    Button (`Update Widget`)
      .type(ButtonType.Capsule)
      .width('50%')
      .height(50)
      .onClick(() => {
        console.info('FormAbility update form click');
        // formId is the ID of the widget to be updated.
        formHost.requestForm(this.formId.toString()).then(() => {
          console.info('Succeeded in requestForming.');
        }).catch((error: Base.BusinessError) => {
          console.error('requestForm fail, error: ' + JSON.stringify(error));
        })
      })

    ...
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞