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

  • 2025-06-12
  • 浏览 (5)

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 the message Event.

import { formHost } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

let storage = new LocalStorage();
const TAG: string = 'Index';
const DOMAIN_NUMBER: number = 0xFF00;

@Entry(storage)
@Component
struct Index {
  @StorageLink('formId') formId: number = 0;

  build() {
    Column() {
      Column() {
        //...
        Button() {
          //...
        }
        .onClick(() => {
          hilog.info(DOMAIN_NUMBER, TAG, `FormAbility update form click, formId: ${this.formId}`);
          // formId is the ID of the widget to be updated.
          formHost.requestForm(this.formId.toString()).then(() => {
            hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in requestForming.');
          }).catch((error: BusinessError) => {
            hilog.error(DOMAIN_NUMBER, TAG, `requestForm fail, error: ${JSON.stringify(error)}`);
          })
        })
        .margin(5)
      }
      //...
    }
    //...
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Form Kit

harmony 鸿蒙Configuring Widget Configuration Files

harmony 鸿蒙Creating an ArkTS Widget

harmony 鸿蒙Launching the UIAbility of the Widget Provider in the Background Through the call Event

harmony 鸿蒙Overview of ArkTs Widget Page Editing Interaction

harmony 鸿蒙Editing and Updating the Widget Content

harmony 鸿蒙Updating Widget Content Through the message Event

harmony 鸿蒙Widget Event Capability Overview

harmony 鸿蒙Launching the UIAbility of the Widget Provider Through the router Event

harmony 鸿蒙Updating Widget Content Through the router or call Event

0  赞