• 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.                               |
22| bundleName   | string               | No  | Bundle name of the ability. If both **bundleName** and **abilityName** are specified in a **Want** object, the **Want** object can match a specific ability. |
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| If **uri** is specified in a **Want**, the **Want** will match the specified URI information, including **scheme**, **schemeSpecificPart**, **authority**, and **path** components.|
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, a number is passed in. For details, see [flags](js-apis-app-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 field and use it together with **uri** or **parameters** to specify the operation to be performed on the data.               |
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-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. |
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.|
30| moduleName | string | 否    | Module to which the ability belongs. |
31
32**Example**
33
34- Basic usage
35
36  ```ts
37    let want = {
38        'deviceId': '', // An empty deviceId indicates the local device.
39        'bundleName': 'com.extreme.test',
40        'abilityName': 'MainAbility',
41        'moduleName': 'entry' // moduleName is optional
42    };
43    this.context.startAbility(want, (error) => {
44        // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
45        console.log('error.code = ' + error.code);
46    })
47  ```
48
49- Data is transferred through user-defined fields. The following data types are supported:
50
51    * String
52        ```ts
53        let want = {
54            bundleName: 'com.example.demo',
55            abilityName: 'com.example.demo.MainAbility',
56            parameters: {
57                keyForString: 'str',
58            },
59        };
60        ```
61    * Number
62        ```ts
63        let want = {
64            bundleName: 'com.example.demo',
65            abilityName: 'com.example.demo.MainAbility',
66            parameters: {
67                keyForInt: 100,
68                keyForDouble: 99.99,
69            },
70        };
71        ```
72    * Boolean
73        ```ts
74        let want = {
75            bundleName: 'com.example.demo',
76            abilityName: 'com.example.demo.MainAbility',
77            parameters: {
78                keyForBool: true,
79            },
80        };
81        ```
82    * Object
83        ```ts
84        let want = {
85            bundleName: 'com.example.demo',
86            abilityName: 'com.example.demo.MainAbility',
87            parameters: {
88                keyForObject: {
89                    keyForObjectString: 'str',
90                    keyForObjectInt: -200,
91                    keyForObjectDouble: 35.5,
92                    keyForObjectBool: false,
93                },
94            },
95        };
96        ```
97    * Array
98        ```ts
99        let want = {
100            bundleName: 'com.example.demo',
101            abilityName: 'com.example.demo.MainAbility',
102            parameters: {
103                keyForArrayString: ['str1', 'str2', 'str3'],
104                keyForArrayInt: [100, 200, 300, 400],
105                keyForArrayDouble: [0.1, 0.2],
106                keyForArrayObject: [{obj1: 'aaa'}, {obj2: 100}],
107            },
108        };
109        ```
110    * File descriptor (FD)
111        ```ts
112            import fileio from '@ohos.fileio';
113            let fd;
114            try {
115                fd = fileio.openSync('/data/storage/el2/base/haps/pic.png');
116            } catch(e) {
117                console.log('openSync fail:' + JSON.stringify(e));
118            }
119            let want = {
120                'deviceId': '', // An empty deviceId indicates the local device.
121                'bundleName': 'com.extreme.test',
122                'abilityName': 'MainAbility',
123                'moduleName': 'entry', // moduleName is optional.
124                'parameters': {
125                    'keyFd':{'type':'FD', 'value':fd} // {'type':'FD', 'value':fd} is a fixed usage, indicating that the data is a file descriptor.
126                }
127            };
128            this.context.startAbility(want, (error) => {
129            // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability.
130                console.log('error.code = ' + error.code);
131            });
132        ```
133
134
135
136