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 UIAbilityA needs to start UIAbilityB and transfer some data to UIAbilityB, 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 | Record\<string, Object> | 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.<br>- **ability.params.backToOtherMissionStack**: whether to support redirection back across mission stacks.| 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 import common from '@ohos.app.ability.common'; 38 import Want from '@ohos.app.ability.Want'; 39 import { BusinessError } from '@ohos.base'; 40 41 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 42 let want: Want = { 43 deviceId: '', // An empty deviceId indicates the local device. 44 bundleName: 'com.example.myapplication', 45 abilityName: 'FuncAbility', 46 moduleName: 'entry', // moduleName is optional. 47 }; 48 49 context.startAbility(want, (err: BusinessError) => { 50 if (err.code) { 51 // Start an ability explicitly. The bundleName, abilityName, and moduleName parameters work together to uniquely identify an ability. 52 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 53 } 54 }); 55 ``` 56 57- Currently, the following data types are supported: string, number, Boolean, object, array, and file descriptor (FD). 58 59 * String 60 ```ts 61 import common from '@ohos.app.ability.common'; 62 import Want from '@ohos.app.ability.Want'; 63 import { BusinessError } from '@ohos.base'; 64 65 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 66 let want: Want = { 67 bundleName: 'com.example.myapplication', 68 abilityName: 'FuncAbility', 69 parameters: { 70 keyForString: 'str', 71 }, 72 }; 73 74 context.startAbility(want, (err: BusinessError) => { 75 if (err.code) { 76 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 77 } 78 }); 79 ``` 80 * Number 81 ```ts 82 import common from '@ohos.app.ability.common'; 83 import Want from '@ohos.app.ability.Want'; 84 import { BusinessError } from '@ohos.base'; 85 86 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 87 let want: Want = { 88 bundleName: 'com.example.myapplication', 89 abilityName: 'FuncAbility', 90 parameters: { 91 keyForInt: 100, 92 keyForDouble: 99.99, 93 }, 94 }; 95 96 context.startAbility(want: Want, (err) => { 97 if (err.code) { 98 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 99 } 100 }); 101 ``` 102 * Boolean 103 ```ts 104 import common from '@ohos.app.ability.common'; 105 import Want from '@ohos.app.ability.Want'; 106 import { BusinessError } from '@ohos.base'; 107 108 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 109 let want: Want = { 110 bundleName: 'com.example.myapplication', 111 abilityName: 'FuncAbility', 112 parameters: { 113 keyForBool: true, 114 }, 115 }; 116 117 context.startAbility(want, (err: BusinessError) => { 118 if (err.code) { 119 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 120 } 121 }); 122 ``` 123 * Object 124 ```ts 125 import common from '@ohos.app.ability.common'; 126 import Want from '@ohos.app.ability.Want'; 127 import { BusinessError } from '@ohos.base'; 128 129 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 130 let want: Want = { 131 bundleName: 'com.example.myapplication', 132 abilityName: 'FuncAbility', 133 parameters: { 134 keyForObject: { 135 keyForObjectString: 'str', 136 keyForObjectInt: -200, 137 keyForObjectDouble: 35.5, 138 keyForObjectBool: false, 139 }, 140 }, 141 }; 142 143 context.startAbility(want, (err: BusinessError) => { 144 if (err.code) { 145 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 146 } 147 }); 148 ``` 149 * Array 150 ```ts 151 import common from '@ohos.app.ability.common'; 152 import Want from '@ohos.app.ability.Want'; 153 import { BusinessError } from '@ohos.base'; 154 155 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 156 let want: Want = { 157 bundleName: 'com.example.myapplication', 158 abilityName: 'FuncAbility', 159 parameters: { 160 keyForArrayString: ['str1', 'str2', 'str3'], 161 keyForArrayInt: [100, 200, 300, 400], 162 keyForArrayDouble: [0.1, 0.2], 163 keyForArrayObject: [{ obj1: 'aaa' }, { obj2: 100 }], 164 }, 165 }; 166 167 context.startAbility(want, (err: BusinessError) => { 168 if (err.code) { 169 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 170 } 171 }); 172 ``` 173 * FD 174 ```ts 175 import fs from '@ohos.file.fs'; 176 import common from '@ohos.app.ability.common'; 177 import Want from '@ohos.app.ability.Want'; 178 import { BusinessError } from '@ohos.base'; 179 180 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 181 182 let fd: number = 0; 183 try { 184 fd = fs.openSync('/data/storage/el2/base/haps/pic.png').fd; 185 } catch(err) { 186 let code = (err as BusinessError).code; 187 let message = (err as BusinessError).message; 188 console.error(`Failed to openSync. Code: ${code}, message: ${message}`); 189 } 190 let want: Want = { 191 deviceId: '', // An empty deviceId indicates the local device. 192 bundleName: 'com.example.myapplication', 193 abilityName: 'FuncAbility', 194 moduleName: 'entry', // moduleName is optional. 195 parameters: { 196 'keyFd': { 'type': 'FD', 'value': fd } // {'type':'FD', 'value':fd} is a fixed usage, indicating that the data is a file descriptor. 197 } 198 }; 199 200 context.startAbility(want, (err: BusinessError) => { 201 if (err.code) { 202 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 203 } 204 }); 205 ``` 206 - Usage of **parameter**: 207 208 The following uses **ability.params.backToOtherMissionStack** as an example. When a ServiceExtensionAbility starts a UIAbility, redirection back across mission stacks is supported. 209 210 ```ts 211 // (1) UIAbility1 starts a ServiceExtensionAbility. 212 import common from '@ohos.app.ability.common'; 213 import Want from '@ohos.app.ability.Want'; 214 import { BusinessError } from '@ohos.base'; 215 216 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 217 let want: Want = { 218 bundleName: 'com.example.myapplication1', 219 abilityName: 'ServiceExtensionAbility', 220 }; 221 context.startAbility(want, (err: BusinessError) => { 222 if (err.code) { 223 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 224 } 225 }); 226 ``` 227 228 ```ts 229 // (2) The ServiceExtensionAbility starts UIAbility2, carrying **"ability.params.backToOtherMissionStack": true** during the startup. 230 import common from '@ohos.app.ability.common'; 231 import Want from '@ohos.app.ability.Want'; 232 import { BusinessError } from '@ohos.base'; 233 234 let context = getContext(this) as common.ServiceExtensionContext; // ServiceExtensionContext 235 let want: Want = { 236 bundleName: 'com.example.myapplication2', 237 abilityName: 'MainAbility', 238 parameters: { 239 "ability.params.backToOtherMissionStack": true, 240 }, 241 }; 242 243 context.startAbility(want, (err: BusinessError) => { 244 if (err.code) { 245 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 246 } 247 }); 248 ``` 249 250 > **NOTE** 251 > 252 > In the preceding example, when the ServiceExtensionAbility starts UIAbility2, **"ability.params.backToOtherMissionStack": true** is carried, indicating that redirection back across mission stacks is supported. Therefore, when you press **Back** on the page of UIAbility2, the page of UIAbility1 page is displayed. However, if **ability.params.backToOtherMissionStack** is not carried or if **"ability.params.backToOtherMissionStack": false** is carried, the page of UIAbility1 is not displayed when you press **Back** on the page of UIAbility2. 253 254 **parameter** carries customized parameters. It is transferred by UIAbilityA to UIAbilityB and obtained from UIAbilityB. 255 256 ```ts 257 //(1) UIAbilityA calls startAbility to start UIAbilityB. 258 import common from '@ohos.app.ability.common'; 259 import Want from '@ohos.app.ability.Want'; 260 import { BusinessError } from '@ohos.base'; 261 262 let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 263 let want: Want = { 264 bundleName: 'com.example.myapplication', 265 abilityName: 'UIAbilityB', 266 parameters: { 267 developerParameters: 'parameters', 268 }, 269 }; 270 context.startAbility(want, (err: BusinessError) => { 271 if (err.code) { 272 console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 273 } 274 }); 275 ``` 276 277 ```ts 278 // (2) If the UIAbilityB instance is started for the first time, it enters the onCreate lifecycle. 279 import UIAbility from '@ohos.app.ability.UIAbility'; 280 import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 281 import Want from '@ohos.app.ability.Want'; 282 283 class UIAbilityB extends UIAbility { 284 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 285 console.log(`onCreate, want parameters: ${want.parameters?.developerParameters}`); 286 } 287 } 288 ``` 289