• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.Want (Want)
2
3Want 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 UIAbility A needs to start UIAbility B and transfer some data to UIAbility B, it can use Want a carrier to transfer the data.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import Want from '@ohos.app.ability.Want';
13```
14
15## Attributes
16
17**System capability**: SystemCapability.Ability.AbilityBase
18
19| Name       | Type                | Mandatory| Description                                                        |
20| ----------- | -------------------- | ---- | ------------------------------------------------------------ |
21| deviceId    | string               | No  | ID of the device running the ability. If this field is unspecified, the local device is used.                               |
22| bundleName   | string               | No  | Bundle name of the ability.|
23| moduleName | string | No| Name of the module to which the ability belongs.|
24| abilityName  | string               | No  | Name 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.|
25| action | string               | No  | Action 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 about the definition and matching rules of implicit Want, see [Matching Rules of Explicit Want and Implicit Want](../../application-models/explicit-implicit-want-mappings.md).            |
26| entities | Array\<string> | No| Additional category information (such as browser and video player) of the ability. It is a supplement to the **action** field for implicit Want. and is used to filter ability types.|
27| uri | string | No| Data carried. This field is used together with **type** to specify the data type. If **uri** is specified in a Want, the Want will match the specified URI information, including **scheme**, **schemeSpecificPart**, **authority**, and **path**.|
28| type | string | No| MIME 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.|
29| parameters   | {[key: string]: any} | No  | Want parameters in the form of custom key-value (KV) pairs. By default, the following keys are carried:<br>- **ohos.aafwk.callerPid**: PID of the caller.<br>- **ohos.aafwk.param.callerBundleName**: bundle name of the caller.<br>- **ohos.aafwk.param.callerToken**: token of the caller.<br>- **ohos.aafwk.param.callerUid**: UID in [BundleInfo](js-apis-bundleManager-bundleInfo.md#bundleinfo-1), that is, the application UID in the bundle information.<br>- **component.startup.newRules**: whether to enable the new control rule.<br>- **moduleName**: module name of the caller. No matter what this field is set to, the correct module name will be sent to the peer.<br>- **ohos.dlp.params.sandbox**: available only for DLP files.|
30| [flags](js-apis-ability-wantConstant.md#wantconstantflags) | number | No| How the **Want** object will be handled. By default, a number is passed in.<br>For example, **wantConstant.Flags.FLAG_ABILITY_CONTINUATION** specifies whether to start the ability in cross-device migration scenarios.|
31
32**Example**
33
34- Basic usage: called in a UIAbility object, as shown in the example below. For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
35
36  ```ts
37  let context = ...; // UIAbilityContext
38  let want = {
39    'deviceId': '', // An empty deviceId indicates the local device.
40    'bundleName': 'com.example.myapplication',
41    'abilityName': 'FuncAbility',
42    'moduleName': 'entry' // moduleName is optional.
43  };
44
45  context.startAbility(want, (err) => {
46    // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
47    console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
48  });
49  ```
50
51- Currently, the following data types are supported: string, number, Boolean, object, array, and file descriptor (FD).
52
53    * String
54        ```ts
55        let context = ...; // UIAbilityContext
56        let want = {
57          bundleName: 'com.example.myapplication',
58          abilityName: 'FuncAbility',
59          parameters: {
60            keyForString: 'str',
61          },
62        };
63
64        context.startAbility(want, (err) => {
65          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
66        });
67        ```
68    * Number
69        ```ts
70        let context = ...; // UIAbilityContext
71        let want = {
72          bundleName: 'com.example.myapplication',
73          abilityName: 'FuncAbility',
74          parameters: {
75            keyForInt: 100,
76            keyForDouble: 99.99,
77          },
78        };
79
80        context.startAbility(want, (err) => {
81          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
82        });
83        ```
84    * Boolean
85        ```ts
86        let context = ...; // UIAbilityContext
87        let want = {
88          bundleName: 'com.example.myapplication',
89          abilityName: 'FuncAbility',
90          parameters: {
91            keyForBool: true,
92          },
93        };
94
95        context.startAbility(want, (err) => {
96          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
97        });
98        ```
99    * Object
100        ```ts
101        let context = ...; // UIAbilityContext
102        let want = {
103          bundleName: 'com.example.myapplication',
104          abilityName: 'FuncAbility',
105          parameters: {
106            keyForObject: {
107              keyForObjectString: 'str',
108              keyForObjectInt: -200,
109              keyForObjectDouble: 35.5,
110              keyForObjectBool: false,
111            },
112          },
113        };
114
115        context.startAbility(want, (err) => {
116          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
117        });
118        ```
119    * Array
120        ```ts
121        let context = ...; // UIAbilityContext
122        let want = {
123          bundleName: 'com.example.myapplication',
124          abilityName: 'FuncAbility',
125          parameters: {
126            keyForArrayString: ['str1', 'str2', 'str3'],
127            keyForArrayInt: [100, 200, 300, 400],
128            keyForArrayDouble: [0.1, 0.2],
129            keyForArrayObject: [{ obj1: 'aaa' }, { obj2: 100 }],
130          },
131        };
132
133        context.startAbility(want, (err) => {
134          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
135        });
136        ```
137    * FD
138        ```ts
139        import fs from '@ohos.file.fs';
140
141        let context = ...; // UIAbilityContext
142
143        let fd;
144        try {
145          fd = fs.openSync('/data/storage/el2/base/haps/pic.png').fd;
146        } catch(err) {
147          console.error(`Failed to openSync. Code: ${err.code}, message: ${err.message}`);
148        }
149        let want = {
150          'deviceId': '', // An empty deviceId indicates the local device.
151          'bundleName': 'com.example.myapplication',
152          'abilityName': 'FuncAbility',
153          'moduleName': 'entry', // moduleName is optional.
154          'parameters': {
155            'keyFd': { 'type': 'FD', 'value': fd } // {'type':'FD', 'value':fd} is a fixed usage, indicating that the data is a file descriptor.
156          }
157        };
158
159        context.startAbility(want, (err) => {
160          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
161        });
162        ```
163