openharmony 鸿蒙 js-apis-inner-ability-want

2025-06-12 浏览 (1)

Want

Want is a carrier for information transfer between objects (application components). Want can be used as a parameter of startAbility to specify a startup target and information that needs to be carried during startup, for example, bundleName and abilityName, which respectively indicate the bundle name of the target ability and the ability name in the bundle. When ability A needs to start ability B and transfer some data to ability B, it can use Want a carrier to transfer the data.

NOTE

The APIs of this module are supported since API version 6 and deprecated since API version 9. You are advised to use @ohos.app.ability.Want instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

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

Attributes

System capability: SystemCapability.Ability.AbilityBase

NameTypeMandatoryDescription
deviceIdstringNoID of the device running the ability. If this field is unspecified, the local device is used.
bundleNamestringNoBundle name.
abilityNamestringNoName of the ability. If both bundleName and abilityName are specified in a Want object, the Want object can match a specific ability. The value of abilityName must be unique in an application.
uristringNoURI. If uri is specified in a Want object, the Want object will match the specified URI information, including scheme, schemeSpecificPart, authority, and path.
typestringNoMIME type, that is, the type of the file to open, for example, 'text/xml' and 'image/*'. For details about the MIME type definition, see https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com.
flagsnumberNoHow the Want object will be handled. By default, numbers are passed in. For details, see flags.
actionstringNoAction to take, such as viewing and sharing application details. In implicit Want, you can define this field and use it together with uri or parameters to specify the operation to be performed on the data. For details, see action. For details about the definition and matching rules of implicit Want, see Matching Rules of Explicit Want and Implicit Want.
parameters{ [key: string]: any }NoWant parameters in the form of custom key-value (KV) pairs. By default, the following keys are carried:
- ohos.aafwk.callerPid: PID of the caller.
- ohos.aafwk.param.callerToken: token of the caller.
- ohos.aafwk.param.callerUid: UID in bundleInfo, that is, the application UID in the bundle information.
- component.startup.newRules: whether to enable the new control rule.
- moduleName: module name of the caller. No matter what this field is set to, the correct module name will be sent to the peer.
- ohos.dlp.params.sandbox: available only for DLP files.
entitiesArray<string>NoAdditional category information (such as browser and video player) of the target ability. It is a supplement to action in implicit Want and is used to filter ability types.

Example

  • Basic usage (called in a UIAbility object, where context in the example is the context object of the UIAbility).

    import AbilityConstant from '@ohos.app.ability.AbilityConstant';
    import UIAbility from '@ohos.app.ability.UIAbility';
    import Want from '@ohos.app.ability.Want';
    import { BusinessError } from '@ohos.base';
    
    let want: Want = {
      deviceId: '', // An empty deviceId indicates the local device.
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility',
      moduleName: 'entry', // moduleName is optional.
    };
    class MyAbility extends UIAbility{
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam){
        this.context.startAbility(want, (error: BusinessError) => {
          // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
          console.error(`error.code = ${error.code}`);
        });
      }
    }
    
  • Passing a file descriptor (FD) (called in a UIAbility object, where context in the example is the context object of the UIAbility):

    import fs from '@ohos.file.fs';
    import Want from '@ohos.app.ability.Want';
    import { BusinessError } from '@ohos.base';
    import AbilityConstant from '@ohos.app.ability.AbilityConstant';
    import UIAbility from '@ohos.app.ability.UIAbility';
    
    // ...
    let fd: number = 0;
    try {
      fd = fs.openSync('/data/storage/el2/base/haps/pic.png').fd;
    } catch(e) {
      console.error(`openSync fail: ${JSON.stringify(e)}`);
    }
    
    
    let want: Want = {
      deviceId: '', // An empty deviceId indicates the local device.
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility',
      moduleName: 'entry', // moduleName is optional.
      parameters: {
        'keyFd':{'type':'FD', 'value':fd}
      }
    };
    class MyAbility extends UIAbility{
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam){
        this.context.startAbility(want, (error: BusinessError) => {
          // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
          console.error(`error.code = ${error.code}`);
        });
      }
    }
    // ...
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/4ZsjUJ