harmony 鸿蒙Widget Event Capability Overview

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

Widget Event Capability Overview

For dynamic ATS widgets, the postCardAction() API is provided for interaction between the widget internal and the provider application. Currently, this API supports the router, message, and call events and can be called only in the widget. For static ATS widgets, the FormLink component is provided for interaction between the widget internal and the provider application.

Dynamic Widget Event Capability

WidgetPostCardAction

Definition: postCardAction(component: Object, action: Object): void

Parameters

Name Type Mandatory Description
component Object Yes Instance of the current custom component. Generally, this is passed in.
action Object Yes Action description. For details, see the following table.

Description of the action parameter

Key Value Description
“action” string Action type.
- “router”: redirection to the specified UIAbility of the widget provider.
- “message”: custom message. If this type of action is triggered, the onFormEvent() lifecycle callback of the provider FormExtensionAbility is called.
- “call”: launch of the widget provider in the background. If this type of action is triggered, the specified UIAbility (whose launch type must be singleton) of the widget provider is started in the background, but not displayed in the foreground. This action type requires that the widget provider should have the ohos.permission.KEEP_BACKGROUND_RUNNING permission.
“bundleName” string Name of the target bundle when action is “router” or “call”. This parameter is optional.
“moduleName” string Name of the target module when action is “router” or “call”. This parameter is optional.
“abilityName” string Name of the target UIAbility when action is “router” or “call”. This parameter is mandatory.
“params” Object Additional parameters carried in the current action. The value is a key-value pair in JSON format. This parameter is mandatory.

NOTE

When action is “router” or “call”, ‘method’ of the string type must be passed to params to trigger the corresponding method in the UIAbility.

Sample code of the postCardAction() API:

Button ('Redirect')
  .width('40%')
  .height('20%')
  .onClick(() => {
    postCardAction(this, {
      'action': 'router',
      'bundleName': 'com.example.myapplication',
      'abilityName': 'EntryAbility',
      'params': {
        'message': 'testForRouter' // Customize the message to send.
      }
    });
  })

Button ('Start in Background')
  .width('40%')
  .height('20%')
  .onClick(() => {
    postCardAction(this, {
      'action': 'call',
      'bundleName': 'com.example.myapplication',
      'abilityName': 'EntryAbility',
      'params': {
        'method': 'fun', // Set the name of the method to call. It is mandatory.
        'message': 'testForCall' // Customize the message to send.
      }
    });
  })

Static Widget Event Capability

See FormLink.

你可能感兴趣的鸿蒙文章

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  赞