harmony 鸿蒙Want Overview

  • 2023-02-03
  • 浏览 (640)

Want Overview

Definition and Usage of Want

Want is an object that transfers information between application components. It is often used as a parameter of startAbility(). For example, when UIAbilityA needs to start UIAbilityB and transfer some data to UIAbilityB, it can use the want parameter in startAbility() to transfer the data.

Figure 1 Want usage

usage-of-want

Types of Want

  • Explicit Want: If abilityName and bundleName are specified in the want parameter when starting an application component, explicit Want is used.

    Explicit Want is usually used to start a known target application component in the same application. The target application component is started by specifying bundleName of the application where the target application component is located and abilityName in the Want object. When there is an explicit object to process the request, explicit Want is a simple and effective way to start the target application component.

  import Want from '@ohos.app.ability.Want';

  let wantInfo: Want = {
    deviceId: '', // An empty deviceId indicates the local device.
    bundleName: 'com.example.myapplication',
    abilityName: 'FuncAbility',
  }
  • Implicit Want: If abilityName is not specified in the want parameter when starting an application component, implicit Want is used.

Implicit Want can be used when the object used to process the request is unclear and the current application wants to use a capability (defined by the skills tag) provided by another application. The system matches all applications that declare to support the capability. For example, for a link open request, the system matches all applications that support the request and provides the available ones for users to select.

  import Want from '@ohos.app.ability.Want';

  let wantInfo: Want = {
    // Uncomment the line below if you want to implicitly query data only in the specific bundle.
    // bundleName: 'com.example.myapplication',
    action: 'ohos.want.action.search',
    // entities can be omitted.
    entities: [ 'entity.system.browsable' ],
    uri: 'https://www.test.com:8080/query/student',
    type: 'text/plain',
  };

NOTE - Depending on the application component matching result, the following cases may be possible when you attempt to use implicit Want to start the application component. - No application component is matched. The startup fails. - An application component that meets the conditions is matched. That application component is started. - Multiple application components that meet the conditions are matched. A dialog box is displayed for users to select one of them.

  • In the scenario for starting the ServiceExtensionAbility component:

    • If the want parameter passed in contains abilityName, the ServiceExtensionAbility component cannot be started through implicit Want.

    • If the want parameter passed in contains bundleName, the startServiceExtensionAbility() method can be used to implicitly start the ServiceExtensionAbility component. By default, the ServiceExtensionAbility component with the highest priority is returned. If all the matching ServiceExtensionAbility components have the same priority, the first ServiceExtensionAbility component is returned.

你可能感兴趣的鸿蒙文章

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  赞