• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.Want (Want)
2
3Want is a carrier for information transfer between objects (application components).
4
5A typical scenario is when a UIAbility (for example, UIAbility A) needs to launch another UIAbility (for example, UIAbility B) and pass some data along. In this case, a Want can be used as the medium. For example, in the **want** parameter of the **startAbility** API, you can specify the target ability using the **abilityName** field or include additional data via the **parameters** field.
6
7> **NOTE**
8>
9> 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.
10
11## Constraints
12
13Launching an ability is subject to IPC limitations. The maximum data supported in the **Want** field is 100 KB.
14
15## Modules to Import
16
17```ts
18import { Want } from '@kit.AbilityKit';
19```
20
21## Want
22
23**Atomic service API**: This API can be used in atomic services since API version 11.
24
25**System capability**: SystemCapability.Ability.AbilityBase
26
27| Name       | Type                | Read-Only| Optional| Description                                                        |
28| ----------- | -------------------- | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ |
29| deviceId    | string               | No| Yes| Device ID. It indicates the device ID of the target application in the application launch scenario. If not specified, it defaults to the current device.              |
30| bundleName   | string               | No| Yes | Bundle name of the application. It represents the bundle name of the target application in the application launch scenario.|
31| moduleName | string | No| Yes| Module name of the application. It represents the module name of the target application in the application launch scenario.<br>**NOTE**<br> If the ability belongs to a [HAR](../../quick-start/har-package.md) module, **moduleName** must be set to the name of the [HAP](../../quick-start/hap-package.md) or [HSP](../../quick-start/in-app-hsp.md) module that depends on this HAR.|
32| abilityName  | string               | No| Yes | Ability name of the application. It represents the ability name of the target application in the application launch scenario. 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.|
33| action | string               | No| Yes | 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).     |
34| entities | Array\<string> | No| Yes| 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.|
35| uri | string | No| Yes| URI, which is used with **type** to specify the data type to be processed in the application launch scenario. If **uri** is specified in a Want, the Want will match the specified URI information, including **scheme**, **schemeSpecificPart**, **authority**, and **path**.|
36| type | string | No| Yes| 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 [Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com).|
37| parameters   | Record\<string, Object> | No| Yes | List of parameters in the Want object.<br>1. The values of the following keys are assigned by the system. Manual settings do not take effect, since the system automatically changes the values to the actual values during data transfer.<br>- **ohos.aafwk.param.callerPid**: PID of the caller. The value is a string.<br>- **ohos.aafwk.param.callerBundleName**: bundle name of the caller. The value is a string.<br>- **ohos.aafwk.param.callerAbilityName**: ability name of the caller. The value is a string.<br>- **ohos.aafwk.param.callerNativeName**: process name of the caller when the native method is called. The value is a string.<br>- **ohos.aafwk.param.callerAppId**: appId of the caller. The value is a string.<br>- **ohos.aafwk.param.callerAppIdentifier**: appIdentifier of the caller. The value is a string.<br>- **ohos.aafwk.param.callerToken**: token of the caller. The value is a string.<br>- **ohos.aafwk.param.callerUid**: UID in [BundleInfo](js-apis-bundleManager-bundleInfo.md#bundleinfo-1), that is, the application's UID in the bundle information. The value is a number.<br>- **ohos.param.callerAppCloneIndex**: clone index of the caller. The value is of the numeric type.<br>- **component.startup.newRules**: enabled status of the new control rule. The value is of the Boolean type.<br>- **moduleName**: module name of the caller. The value is a string.<br>- **ohos.ability.params.abilityRecoveryRestart**: support for ability restart upon fault recovery. The value is of the Boolean type.<br>- **ohos.extra.param.key.showMode**: mode to show the atomic service startup. The value is an enumerated value of [wantConstant.ShowMode](js-apis-app-ability-wantConstant.md#showmode12).<br><br>2. Certain keys are defined by the system, and their values need to be manually assigned. For details about the keys and their values, see [wantConstant.Params](js-apis-app-ability-wantConstant.md#params)<!--Del--> and [wantConstant.Params (for System Applications Only)](js-apis-app-ability-wantConstant-sys.md#params)<!--DelEnd-->.<br><br>3. In addition to the foregoing cases, applications may further agree on the key-value pairs to transfer.<br><br>**NOTE**<br>For details about the constants of **Params** in **want**, see [wantConstant](js-apis-app-ability-wantConstant.md).<br>Note that a maximum of 100 KB data that can be transferred by using **WantParams**. If the data volume exceeds 100 KB, transfer data in [WriteRawDataBuffer](../apis-ipc-kit/js-apis-rpc.md#writerawdatabuffer11) or [uri](../apis-arkts/js-apis-uri.md) mode.<br>The values of **parameters** must be of the following basic data types: String, Number, Boolean, Object, undefined, and null. Functions in an object cannot be transferred.|
38| [flags](js-apis-app-ability-wantConstant.md#flags) | number | No| Yes| How the Want object will be handled. By default, a number is passed in.<br>For example, if the value is 0x00000001 (**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**), the receiver is temporarily granted the permission to read the data pointed to by the URI.|
39| fds<sup>15+</sup> | Record\<string, number> | Yes| Yes| File descriptor (FD). The FD written by the launcher in the application launch scenario is set to this parameter.<br>**Atomic service API**: This API can be used in atomic services since API version 15.|
40
41**Example**
42
43- 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).
44
45  ```ts
46  import { UIAbility, Want } from '@kit.AbilityKit';
47  import { window } from '@kit.ArkUI';
48  import { BusinessError } from '@kit.BasicServicesKit';
49
50  export default class EntryAbility extends UIAbility {
51    onWindowStageCreate(windowStage: window.WindowStage): void {
52      let want: Want = {
53        deviceId: '', // An empty deviceId indicates the local device.
54        bundleName: 'com.example.myapplication',
55        abilityName: 'FuncAbility',
56        moduleName: 'entry' // moduleName is optional.
57      };
58
59      this.context.startAbility(want, (err: BusinessError) => {
60        if (err.code) {
61          // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
62          console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
63        }
64      });
65    }
66  }
67  ```
68
69- Currently, the following data types are supported: string, number, Boolean, object, array, and file descriptor (FD).
70
71    * String
72        ```ts
73        import { UIAbility, Want } from '@kit.AbilityKit';
74        import { window } from '@kit.ArkUI';
75        import { BusinessError } from '@kit.BasicServicesKit';
76
77        export default class EntryAbility extends UIAbility {
78          onWindowStageCreate(windowStage: window.WindowStage): void {
79            let want: Want = {
80              bundleName: 'com.example.myapplication',
81              abilityName: 'FuncAbility',
82              parameters: {
83                keyForString: 'str',
84              },
85            };
86
87            this.context.startAbility(want, (err: BusinessError) => {
88              if (err.code) {
89                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
90              }
91            });
92          }
93        }
94        ```
95    * Number
96        ```ts
97        import { UIAbility, Want } from '@kit.AbilityKit';
98        import { window } from '@kit.ArkUI';
99        import { BusinessError } from '@kit.BasicServicesKit';
100
101        export default class EntryAbility extends UIAbility {
102          onWindowStageCreate(windowStage: window.WindowStage): void {
103            let want: Want = {
104              bundleName: 'com.example.myapplication',
105              abilityName: 'FuncAbility',
106              parameters: {
107                keyForInt: 100,
108                keyForDouble: 99.99,
109              },
110            };
111
112            this.context.startAbility(want, (err: BusinessError) => {
113              if (err.code) {
114                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
115              }
116            });
117          }
118        }
119        ```
120    * Boolean
121        ```ts
122        import { UIAbility, Want } from '@kit.AbilityKit';
123        import { window } from '@kit.ArkUI';
124        import { BusinessError } from '@kit.BasicServicesKit';
125
126        export default class EntryAbility extends UIAbility {
127          onWindowStageCreate(windowStage: window.WindowStage): void {
128            let want: Want = {
129              bundleName: 'com.example.myapplication',
130              abilityName: 'FuncAbility',
131              parameters: {
132                keyForBool: true,
133              },
134            };
135
136            this.context.startAbility(want, (err: BusinessError) => {
137              if (err.code) {
138                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
139              }
140            });
141          }
142        }
143        ```
144    * Object
145        ```ts
146        import { UIAbility, Want } from '@kit.AbilityKit';
147        import { window } from '@kit.ArkUI';
148        import { BusinessError } from '@kit.BasicServicesKit';
149
150        export default class EntryAbility extends UIAbility {
151          onWindowStageCreate(windowStage: window.WindowStage): void {
152            let want: Want = {
153              bundleName: 'com.example.myapplication',
154              abilityName: 'FuncAbility',
155              parameters: {
156                keyForObject: {
157                  keyForObjectString: 'str',
158                  keyForObjectInt: -200,
159                  keyForObjectDouble: 35.5,
160                  keyForObjectBool: false,
161                },
162              },
163            };
164
165            this.context.startAbility(want, (err: BusinessError) => {
166              if (err.code) {
167                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
168              }
169            });
170          }
171        }
172        ```
173    * Array
174
175        ```ts
176        import { UIAbility, Want } from '@kit.AbilityKit';
177        import { window } from '@kit.ArkUI';
178        import { BusinessError } from '@kit.BasicServicesKit';
179
180        export default class EntryAbility extends UIAbility {
181          onWindowStageCreate(windowStage: window.WindowStage): void {
182            let want: Want = {
183              bundleName: 'com.example.myapplication',
184              abilityName: 'FuncAbility',
185              parameters: {
186                keyForArrayString: ['str1', 'str2', 'str3'],
187                keyForArrayInt: [100, 200, 300, 400],
188                keyForArrayDouble: [0.1, 0.2],
189                keyForArrayObject: [{ obj1: 'aaa' }, { obj2: 100 }],
190              },
191            };
192
193            this.context.startAbility(want, (err: BusinessError) => {
194              if (err.code) {
195                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
196              }
197            });
198          }
199        }
200        ```
201
202    * FD
203
204        ```ts
205        import { UIAbility, Want } from '@kit.AbilityKit';
206        import { window } from '@kit.ArkUI';
207        import { BusinessError } from '@kit.BasicServicesKit';
208        import { fileIo } from '@kit.CoreFileKit';
209
210        export default class EntryAbility extends UIAbility {
211          onWindowStageCreate(windowStage: window.WindowStage): void {
212            let fd: number = 0;
213
214            try {
215              fd = fileIo.openSync('/data/storage/el2/base/haps/pic.png').fd;
216            } catch (err) {
217              let code = (err as BusinessError).code;
218              let message = (err as BusinessError).message;
219              console.error(`Failed to openSync. Code: ${code}, message: ${message}`);
220            }
221            let want: Want = {
222              deviceId: '', // An empty deviceId indicates the local device.
223              bundleName: 'com.example.myapplication',
224              abilityName: 'FuncAbility',
225              moduleName: 'entry', // moduleName is optional.
226              parameters: {
227                'keyFd': { 'type': 'FD', 'value': fd } // {'type':'FD', 'value':fd} is a fixed usage, indicating that the data is a file descriptor.
228              }
229            };
230
231            this.context.startAbility(want, (err: BusinessError) => {
232              if (err.code) {
233                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
234              }
235            });
236          }
237        }
238        ```
239
240    * **parameters** usage: **parameters** carries custom parameters. It is transferred by UIAbilityA to UIAbilityB and obtained from UIAbilityB.
241
242        ```ts
243        // (1) UIAbilityA calls startAbility to start UIAbilityB.
244        import { UIAbility, Want } from '@kit.AbilityKit';
245        import { window } from '@kit.ArkUI';
246        import { BusinessError } from '@kit.BasicServicesKit';
247
248        export default class EntryAbility extends UIAbility {
249          onWindowStageCreate(windowStage: window.WindowStage): void {
250            let want: Want = {
251              bundleName: 'com.example.myapplication',
252              abilityName: 'UIAbilityB',
253              parameters: {
254                developerParameters: 'parameters',
255              },
256            };
257
258            this.context.startAbility(want, (err: BusinessError) => {
259              if (err.code) {
260                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
261              }
262            });
263          }
264        }
265        ```
266
267        ```ts
268        // (2) If the UIAbilityB instance is started for the first time, it enters the onCreate lifecycle.
269        import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
270
271        class UIAbilityB extends UIAbility {
272          onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
273            console.log(`onCreate, want parameters: ${want.parameters?.developerParameters}`);
274          }
275        }
276        ```
277    * Usage of the keys of [wantConstant](js-apis-app-ability-wantConstant.md) in **parameters**.
278
279        ```ts
280        import { UIAbility, Want, wantConstant } from '@kit.AbilityKit';
281        import { window } from '@kit.ArkUI';
282        import { BusinessError } from '@kit.BasicServicesKit';
283
284        export default class EntryAbility extends UIAbility {
285          onWindowStageCreate(windowStage: window.WindowStage): void {
286            let want: Want = {
287              bundleName: 'com.example.myapplication',
288              abilityName: 'FuncAbility',
289              parameters: {
290                [wantConstant.Params.CONTENT_TITLE_KEY]: 'contentTitle',
291              },
292            };
293
294            this.context.startAbility(want, (err: BusinessError) => {
295              if (err.code) {
296                console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
297              }
298            });
299          }
300        }
301        ```
302    * Obtain the information about the party that starts the UIAbility from **parameters**.
303
304      For details, see [Obtaining Information About the UIAbility Launcher](../../application-models/uiability-usage.md#obtaining-information-about-the-uiability-launcher).
305