• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.application.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 ability A needs to start ability B and transfer some data to ability B, it can use Want a carrier to transfer the data.
4
5> **NOTE**
6>
7> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.Want](js-apis-app-ability-want.md) instead. 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.application.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.|
23| 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.|
24| uri          | string               | No  | URI information to match. If **uri** is specified in a **Want** object, the **Want** object will match the specified URI information, including **scheme**, **schemeSpecificPart**, **authority**, and **path**.|
25| 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.  |
26| flags        | number               | No  | How the **Want** object will be handled. By default, numbers are passed in. For details, see [flags](js-apis-ability-wantConstant.md#wantconstantflags).|
27| action      | string               | No  | Action to take, such as viewing and sharing application details. In implicit **Want**, you can define this attribute and use it together with **uri** or **parameters** to specify the operation to be performed on the data. For details, see [action](js-apis-ability-wantConstant.md#wantconstantaction). 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).                          |
28| 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.callerToken**: token of the caller.<br>- **ohos.aafwk.param.callerUid**: UID in [bundleInfo](js-apis-bundle-BundleInfo.md#bundleinfo), 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.                                      |
29| 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. For details, see [entity](js-apis-ability-wantConstant.md#wantconstantentity).                                   |
30
31**Example**
32
33- Basic usage (called in a UIAbility object, where context in the example is the context object of the UIAbility).
34
35  ```ts
36    import Want from '@ohos.application.Want';
37    import { BusinessError } from '@ohos.base';
38
39    let want: Want = {
40        'deviceId': '', // An empty deviceId indicates the local device.
41        'bundleName': 'com.example.myapplication',
42        'abilityName': 'EntryAbility',
43    };
44    this.context.startAbility(want, (error: BusinessError) => {
45        // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
46        console.error('error.code = ${error.code}');
47    });
48  ```
49
50- Data is transferred through user-defined fields. The following data types are supported (called in a UIAbility object, where context in the example is the context object of the UIAbility):
51
52    * String
53        ```ts
54        import Want from '@ohos.application.Want';
55
56        let want: Want = {
57            bundleName: 'com.example.myapplication',
58            abilityName: 'EntryAbility',
59            parameters: {
60                keyForString: 'str',
61            },
62        };
63        ```
64    * Number
65        ```ts
66        import Want from '@ohos.application.Want';
67
68        let want: Want = {
69            bundleName: 'com.example.myapplication',
70            abilityName: 'EntryAbility',
71            parameters: {
72                keyForInt: 100,
73                keyForDouble: 99.99,
74            },
75        };
76        ```
77    * Boolean
78        ```ts
79        import Want from '@ohos.application.Want';
80
81        let want: Want = {
82            bundleName: 'com.example.myapplication',
83            abilityName: 'EntryAbility',
84            parameters: {
85                keyForBool: true,
86            },
87        };
88        ```
89    * Object
90        ```ts
91        import Want from '@ohos.application.Want';
92
93        let want: Want = {
94            bundleName: 'com.example.myapplication',
95            abilityName: 'EntryAbility',
96            parameters: {
97                keyForObject: {
98                    keyForObjectString: 'str',
99                    keyForObjectInt: -200,
100                    keyForObjectDouble: 35.5,
101                    keyForObjectBool: false,
102                },
103            },
104        };
105        ```
106    * Array
107        ```ts
108        import Want from '@ohos.application.Want';
109
110        let want: Want = {
111            bundleName: 'com.example.myapplication',
112            abilityName: 'EntryAbility',
113            parameters: {
114                keyForArrayString: ['str1', 'str2', 'str3'],
115                keyForArrayInt: [100, 200, 300, 400],
116                keyForArrayDouble: [0.1, 0.2],
117                keyForArrayObject: [{obj1: 'aaa'}, {obj2: 100}],
118            },
119        };
120        ```
121    * File descriptor (FD)
122        ```ts
123            import fs from '@ohos.file.fs';
124            import Want from '@ohos.application.Want';
125            import { BusinessError } from '@ohos.base';
126
127            let fd: number = 0;
128            try {
129                fd = fs.openSync('/data/storage/el2/base/haps/pic.png').fd;
130            } catch(e) {
131                console.error(`openSync fail: ${JSON.stringify(e)}`);
132            }
133            let want: Want = {
134                deviceId: '', // An empty deviceId indicates the local device.
135                bundleName: 'com.example.myapplication',
136                abilityName: 'EntryAbility',
137                parameters: {
138                    'keyFd':{'type':'FD', 'value':fd}
139                }
140            };
141            this.context.startAbility(want, (error: BusinessError) => {
142                // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
143                console.error('error.code = ${error.code}');
144            });
145        ```
146
147- For more details and examples, see [Want](../../application-models/want-overview.md).
148
149  <!--no_check-->
150