1e41f4b71Sopenharmony_ci# @ohos.app.ability.Want (Want) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciWant是对象间信息传递的载体,可以用于应用组件间的信息传递。Want的使用场景之一是作为startAbility的参数,其包含了指定的启动目标,以及启动时需携带的相关数据,例如bundleName和abilityName字段分别指明目标Ability所在应用的包名以及对应包内的Ability名称。当UIAbilityA需要启动UIAbilityB并传入一些数据时,可使用Want作为载体将这些数据传递给UIAbilityB。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 导入模块 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 属性 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityBase 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 22e41f4b71Sopenharmony_ci| ----------- | -------------------- | ---- | ------------------------------------------------------------ | 23e41f4b71Sopenharmony_ci| deviceId | string | 否 | 表示运行指定Ability的设备ID。如果未设置该字段,则表明指定本设备。 | 24e41f4b71Sopenharmony_ci| bundleName | string | 否 | 表示待启动Ability所在的应用Bundle名称。 | 25e41f4b71Sopenharmony_ci| moduleName | string | 否 | 表示待启动的Ability所属的模块名称。 | 26e41f4b71Sopenharmony_ci| abilityName | string | 否 | 表示待启动Ability名称。如果在Want中该字段同时指定了BundleName和AbilityName,则Want可以直接匹配到指定的Ability。AbilityName需要在一个应用的范围内保证唯一。 | 27e41f4b71Sopenharmony_ci| action | string | 否 | 表示要执行的通用操作(如:查看、分享、应用详情)。在隐式Want中,您可以定义该字段,配合uri或parameters来表示对数据要执行的操作。隐式Want定义及匹配规则请参见[显式Want与隐式Want匹配规则](../../application-models/explicit-implicit-want-mappings.md)。 | 28e41f4b71Sopenharmony_ci| entities | Array\<string> | 否 | 表示目标Ability额外的类别信息(如:浏览器、视频播放器)。在隐式Want中是对action字段的补充。在隐式Want中,您可以定义该字段,来过滤匹配Ability类型。 | 29e41f4b71Sopenharmony_ci| uri | string | 否 | 表示携带的数据,一般配合type使用,指明待处理的数据类型。如果在Want中指定了uri,则Want将匹配指定的Uri信息,包括`scheme`、`schemeSpecificPart`、`authority`和`path`信息。 | 30e41f4b71Sopenharmony_ci| type | string | 否 | 表示MIME type类型描述,打开文件的类型,主要用于文管打开文件。比如:'text/xml' 、 'image/*'等,MIME定义请参见https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com。 | 31e41f4b71Sopenharmony_ci| parameters | Record\<string, Object> | 否 | 表示WantParams描述。<br />一、以下Key均由系统赋值,开发者手动修改也不会生效,系统在数据传递时会自动修改为实际值。<br />- ohos.aafwk.param.callerPid:表示拉起方的pid,值为字符串类型。<br />- ohos.aafwk.param.callerBundleName:表示拉起方的BundleName,值为字符串类型。<br />- ohos.aafwk.param.callerAbilityName:表示拉起方的AbilityName,值为字符串类型。<br />- ohos.aafwk.param.callerNativeName:表示native调用时拉起方的进程名,值为字符串类型。<br />- ohos.aafwk.param.callerAppId:表示拉起应用的AppId信息,值为字符串类型。<br />- ohos.aafwk.param.callerAppIdentifier:表示拉起应用的AppIdentifier信息,值为字符串类型。<br />- ohos.aafwk.param.callerToken:表示拉起方的token,值为字符串类型。<br />- ohos.aafwk.param.callerUid:表示[BundleInfo](js-apis-bundleManager-bundleInfo.md#bundleinfo-1)中的uid,应用包里应用程序的uid,值为数值类型。<br />- component.startup.newRules:表示是否启用新的管控规则,值为布尔类型。<br />- moduleName:表示拉起方的moduleName,值为字符串类型。<br />- ability.params.backToOtherMissionStack:表示是否支持跨任务链返回,值为布尔类型。<br />- ohos.ability.params.abilityRecoveryRestart:表示当前Ability是否发生了故障恢复重启,值为布尔类型。<br />- ohos.extra.param.key.contentTitle:表示原子化服务支持分享的标题,值为字符串类型。<br />- ohos.extra.param.key.shareAbstract:表示原子化服务支持分享的内容,值为字符串类型。<br />- ohos.extra.param.key.shareUrl:表示原子化服务支持分享的链接,值为字符串类型。<br />- ohos.extra.param.key.supportContinuePageStack:表示在跨端迁移过程中是否迁移页面栈信息,值为布尔类型,默认值为true,自动迁移页面栈信息。<br />- ohos.extra.param.key.supportContinueSourceExit:表示跨端迁移源端应用是否退出,值为布尔类型,默认值为true,源端应用自动退出。<br />- ohos.extra.param.key.showMode:表示拉起原子化服务的展示模式,值为枚举类型[wantConstant.ShowMode](js-apis-app-ability-wantConstant.md#showmode12)。<br />- ohos.dlp.params.sandbox:表示数据防泄漏(DLP)文件才会涉及。仅系统应用涉及。<br />- ohos.dlp.params.bundleName:表示数据防泄漏(DLP)的BundleName,值为字符串类型。仅系统应用涉及。<br />- ohos.dlp.params.moduleName:表示数据防泄漏(DLP)的moduleName,值为字符串类型。仅系统应用涉及。<br />- ohos.dlp.params.abilityName:表示数据防泄漏(DLP)的AbilityName,值为字符串类型。仅系统应用涉及。<br />- ohos.dlp.params.index:表示数据防泄漏(DLP)的索引,值为数值类型。仅系统应用涉及。<br />- ohos.ability.params.asssertFaultSessionId:表示AssertFault的会话ID,值为字符串类型。仅系统应用涉及。<br /><br />二、以下是由系统定义、开发者按需赋值的Key。<br />- ability.params.stream:指示携带的文件URI要授权给目标方,值为string类型的文件URI数组。<br />- ohos.extra.param.key.appCloneIndex:指示分身应用索引。<br /><br />三、除了上述两种情况,应用间还可以相互约定传入的键值对。<br /><br />**说明**:<br/>want的Params操作的常量的具体信息请参考[wantConstant](js-apis-app-ability-wantConstant.md)。<br/>需注意,WantParams支持传输的最大数据量为200KB。当数据量超过200KB时,请使用[WriteRawDataBuffer](../apis-ipc-kit/js-apis-rpc.md#writerawdatabuffer11)或[uri](../apis-arkts/js-apis-uri.md)的方式进行数据传输。<br/>parameters的Value值仅支持基本数据类型:String、Number、Boolean、Object、undefined和null,不支持传递Object内部的function。 | 32e41f4b71Sopenharmony_ci| [flags](js-apis-ability-wantConstant.md#flags) | number | 否 | 表示处理Want的方式。默认传数字。<br />例如通过wantConstant.Flags.FLAG_ABILITY_CONTINUATION表示是否以设备间迁移方式启动Ability。 | 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**示例:** 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci- 基础用法:在UIAbility对象中调用,示例中的context的获取方式请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci ```ts 39e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 40e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 43e41f4b71Sopenharmony_ci let want: Want = { 44e41f4b71Sopenharmony_ci deviceId: '', // deviceId为空表示本设备 45e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 46e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 47e41f4b71Sopenharmony_ci moduleName: 'entry' // moduleName非必选 48e41f4b71Sopenharmony_ci }; 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 51e41f4b71Sopenharmony_ci if (err.code) { 52e41f4b71Sopenharmony_ci // 显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability 53e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci }); 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci- 目前支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等。 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci * 字符串(String) 61e41f4b71Sopenharmony_ci ```ts 62e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 63e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 66e41f4b71Sopenharmony_ci let want: Want = { 67e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 68e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 69e41f4b71Sopenharmony_ci parameters: { 70e41f4b71Sopenharmony_ci keyForString: 'str', 71e41f4b71Sopenharmony_ci }, 72e41f4b71Sopenharmony_ci }; 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 75e41f4b71Sopenharmony_ci if (err.code) { 76e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci }); 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci * 数字(Number) 81e41f4b71Sopenharmony_ci ```ts 82e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 85e41f4b71Sopenharmony_ci let want: Want = { 86e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 87e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 88e41f4b71Sopenharmony_ci parameters: { 89e41f4b71Sopenharmony_ci keyForInt: 100, 90e41f4b71Sopenharmony_ci keyForDouble: 99.99, 91e41f4b71Sopenharmony_ci }, 92e41f4b71Sopenharmony_ci }; 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci context.startAbility(want, (err) => { 95e41f4b71Sopenharmony_ci if (err.code) { 96e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci }); 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci * 布尔(Boolean) 101e41f4b71Sopenharmony_ci ```ts 102e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 103e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 106e41f4b71Sopenharmony_ci let want: Want = { 107e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 108e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 109e41f4b71Sopenharmony_ci parameters: { 110e41f4b71Sopenharmony_ci keyForBool: true, 111e41f4b71Sopenharmony_ci }, 112e41f4b71Sopenharmony_ci }; 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 115e41f4b71Sopenharmony_ci if (err.code) { 116e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci }); 119e41f4b71Sopenharmony_ci ``` 120e41f4b71Sopenharmony_ci * 对象(Object) 121e41f4b71Sopenharmony_ci ```ts 122e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 123e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 126e41f4b71Sopenharmony_ci let want: Want = { 127e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 128e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 129e41f4b71Sopenharmony_ci parameters: { 130e41f4b71Sopenharmony_ci keyForObject: { 131e41f4b71Sopenharmony_ci keyForObjectString: 'str', 132e41f4b71Sopenharmony_ci keyForObjectInt: -200, 133e41f4b71Sopenharmony_ci keyForObjectDouble: 35.5, 134e41f4b71Sopenharmony_ci keyForObjectBool: false, 135e41f4b71Sopenharmony_ci }, 136e41f4b71Sopenharmony_ci }, 137e41f4b71Sopenharmony_ci }; 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 140e41f4b71Sopenharmony_ci if (err.code) { 141e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 142e41f4b71Sopenharmony_ci } 143e41f4b71Sopenharmony_ci }); 144e41f4b71Sopenharmony_ci ``` 145e41f4b71Sopenharmony_ci * 数组(Array) 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci ```ts 148e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 149e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 152e41f4b71Sopenharmony_ci let want: Want = { 153e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 154e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 155e41f4b71Sopenharmony_ci parameters: { 156e41f4b71Sopenharmony_ci keyForArrayString: ['str1', 'str2', 'str3'], 157e41f4b71Sopenharmony_ci keyForArrayInt: [100, 200, 300, 400], 158e41f4b71Sopenharmony_ci keyForArrayDouble: [0.1, 0.2], 159e41f4b71Sopenharmony_ci keyForArrayObject: [{ obj1: 'aaa' }, { obj2: 100 }], 160e41f4b71Sopenharmony_ci }, 161e41f4b71Sopenharmony_ci }; 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 164e41f4b71Sopenharmony_ci if (err.code) { 165e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci }); 168e41f4b71Sopenharmony_ci ``` 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci * 文件描述符(FD) 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci ```ts 173e41f4b71Sopenharmony_ci import { fileIo } from '@kit.CoreFileKit'; 174e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 175e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 178e41f4b71Sopenharmony_ci let fd: number = 0; 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci try { 181e41f4b71Sopenharmony_ci fd = fileIo.openSync('/data/storage/el2/base/haps/pic.png').fd; 182e41f4b71Sopenharmony_ci } catch(err) { 183e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 184e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 185e41f4b71Sopenharmony_ci console.error(`Failed to openSync. Code: ${code}, message: ${message}`); 186e41f4b71Sopenharmony_ci } 187e41f4b71Sopenharmony_ci let want: Want = { 188e41f4b71Sopenharmony_ci deviceId: '', // deviceId为空表示本设备 189e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 190e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 191e41f4b71Sopenharmony_ci moduleName: 'entry', // moduleName非必选 192e41f4b71Sopenharmony_ci parameters: { 193e41f4b71Sopenharmony_ci 'keyFd': { 'type': 'FD', 'value': fd } // {'type':'FD', 'value':fd}是固定用法,用于表示该数据是FD 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci }; 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 198e41f4b71Sopenharmony_ci if (err.code) { 199e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci }); 202e41f4b71Sopenharmony_ci ``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci * parameter参数用法:parameter携带开发者自定义参数,由UIAbilityA传递给UIAbilityB,并在UIAbilityB中进行获取。 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci ```ts 207e41f4b71Sopenharmony_ci // (1) UIAbilityA通过startability启动UIAbilityB 208e41f4b71Sopenharmony_ci import { common, Want } from '@kit.AbilityKit'; 209e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 212e41f4b71Sopenharmony_ci let want: Want = { 213e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 214e41f4b71Sopenharmony_ci abilityName: 'UIAbilityB', 215e41f4b71Sopenharmony_ci parameters: { 216e41f4b71Sopenharmony_ci developerParameters: 'parameters', 217e41f4b71Sopenharmony_ci }, 218e41f4b71Sopenharmony_ci }; 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 221e41f4b71Sopenharmony_ci if (err.code) { 222e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 223e41f4b71Sopenharmony_ci } 224e41f4b71Sopenharmony_ci }); 225e41f4b71Sopenharmony_ci ``` 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci ```ts 228e41f4b71Sopenharmony_ci // (2) 以UIAbilityB实例首次启动为例,会进入到UIAbilityB的onCreate生命周期 229e41f4b71Sopenharmony_ci import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci class UIAbilityB extends UIAbility { 232e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 233e41f4b71Sopenharmony_ci console.log(`onCreate, want parameters: ${want.parameters?.developerParameters}`); 234e41f4b71Sopenharmony_ci } 235e41f4b71Sopenharmony_ci } 236e41f4b71Sopenharmony_ci ``` 237e41f4b71Sopenharmony_ci * parameter参数中[wantConstant](js-apis-app-ability-wantConstant.md)的Key的使用方法。 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci ```ts 240e41f4b71Sopenharmony_ci import { common, Want, wantConstant } from '@kit.AbilityKit'; 241e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext 244e41f4b71Sopenharmony_ci let want: Want = { 245e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 246e41f4b71Sopenharmony_ci abilityName: 'FuncAbility', 247e41f4b71Sopenharmony_ci parameters: { 248e41f4b71Sopenharmony_ci [wantConstant.Params.CONTENT_TITLE_KEY]: 'contentTitle', 249e41f4b71Sopenharmony_ci }, 250e41f4b71Sopenharmony_ci }; 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci context.startAbility(want, (err: BusinessError) => { 253e41f4b71Sopenharmony_ci if (err.code) { 254e41f4b71Sopenharmony_ci console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`); 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci }); 257e41f4b71Sopenharmony_ci ``` 258