1e41f4b71Sopenharmony_ci# @ohos.application.Want (Want) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciWant是对象间信息传递的载体, 可以用于应用组件间的信息传递。 Want的使用场景之一是作为startAbility的参数, 其包含了指定的启动目标, 以及启动时需携带的相关数据, 如bundleName和abilityName字段分别指明目标Ability所在应用Bundle名称以及对应包内的Ability名称。当Ability A需要启动Ability B并传入一些数据时, 可使用Want作为载体将这些数据传递给Ability B。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 8 开始支持,从API version 9废弃,替换模块为[@ohos.app.ability.Want](js-apis-app-ability-want.md)。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 导入模块 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport Want from '@ohos.application.Want'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 属性 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityBase 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 20e41f4b71Sopenharmony_ci| ----------- | -------------------- | ---- | ------------------------------------------------------------ | 21e41f4b71Sopenharmony_ci| deviceId | string | 否 | 表示运行指定Ability的设备ID。如果未设置该字段,则表明指定本设备。 | 22e41f4b71Sopenharmony_ci| bundleName | string | 否 | 表示Bundle名称。 | 23e41f4b71Sopenharmony_ci| abilityName | string | 否 | 表示待启动的Ability名称。如果在Want中该字段同时指定了BundleName和AbilityName,则Want可以直接匹配到指定的Ability。AbilityName需要在一个应用的范围内保证唯一。 | 24e41f4b71Sopenharmony_ci| uri | string | 否 | 表示Uri描述。如果在Want中指定了Uri,则Want将匹配指定的Uri信息,包括scheme, schemeSpecificPart, authority和path信息。 | 25e41f4b71Sopenharmony_ci| type | string | 否 | 表示MIME type类型描述,打开文件的类型,主要用于文管打开文件。比如:'text/xml' 、 'image/*'等,MIME定义参考:https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com。 | 26e41f4b71Sopenharmony_ci| flags | number | 否 | 表示处理Want的方式。默认传数字,具体参考:[flags说明](js-apis-ability-wantConstant.md#flags)。 | 27e41f4b71Sopenharmony_ci| action | string | 否 | 表示要执行的通用操作(如:查看、分享、应用详情)。在隐式Want中,您可以定义该字段,配合uri或parameters来表示对数据要执行的操作。具体参考:[action说明](js-apis-ability-wantConstant.md#action)。隐式Want定义及匹配规则参考:[显式Want与隐式Want匹配规则](../../application-models/explicit-implicit-want-mappings.md)。 | 28e41f4b71Sopenharmony_ci| parameters | { [key: string]: any } | 否 | 表示WantParams描述,由开发者自行决定传入的键值对。默认会携带以下key值:<br>ohos.aafwk.param.callerPid 表示拉起方的pid。<br>ohos.aafwk.param.callerToken 表示拉起方的token。<br>ohos.aafwk.param.callerUid 表示[bundleInfo](js-apis-bundle-BundleInfo.md#bundleinfo)中的uid,应用包里应用程序的uid。<br />- component.startup.newRules:表示是否启用新的管控规则。<br />- moduleName:表示拉起方的模块名,该字段的值即使定义成其他字符串,在传递到另一端时会被修改为正确的值。<br />- ohos.dlp.params.sandbox:表示dlp文件才会有。 | 29e41f4b71Sopenharmony_ci| entities | Array\<string> | 否 | 表示目标Ability额外的类别信息(如:浏览器、视频播放器)。在隐式Want中是对action字段的补充。在隐式Want中,您可以定义该字段,来过滤匹配Ability类型。具体参考:[entity说明](js-apis-ability-wantConstant.md#entity)。 | 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**示例:** 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- 基础用法(在UIAbility对象中调用,其中示例中的context为UIAbility的上下文对象) 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```ts 36e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 37e41f4b71Sopenharmony_ci import { BusinessError } from '@ohos.base'; 38e41f4b71Sopenharmony_ci import UIAbility from '@ohos.app.ability.UIAbility'; 39e41f4b71Sopenharmony_ci import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci let want: Want = { 42e41f4b71Sopenharmony_ci 'deviceId': '', // deviceId为空表示本设备 43e41f4b71Sopenharmony_ci 'bundleName': 'com.example.myapplication', 44e41f4b71Sopenharmony_ci 'abilityName': 'EntryAbility', 45e41f4b71Sopenharmony_ci }; 46e41f4b71Sopenharmony_ci class MyAbility extends UIAbility{ 47e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam){ 48e41f4b71Sopenharmony_ci this.context.startAbility(want, (error: BusinessError) => { 49e41f4b71Sopenharmony_ci // 显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability 50e41f4b71Sopenharmony_ci console.error(`error.code = ${error.code}`); 51e41f4b71Sopenharmony_ci }); 52e41f4b71Sopenharmony_ci } 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci ``` 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci- 通过自定字段传递数据, 以下为当前支持类型。(在UIAbility对象中调用,其中示例中的context为UIAbility的上下文对象) 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci * 字符串(String) 59e41f4b71Sopenharmony_ci ```ts 60e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci let want: Want = { 63e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 64e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 65e41f4b71Sopenharmony_ci parameters: { 66e41f4b71Sopenharmony_ci keyForString: 'str', 67e41f4b71Sopenharmony_ci }, 68e41f4b71Sopenharmony_ci }; 69e41f4b71Sopenharmony_ci ``` 70e41f4b71Sopenharmony_ci * 数字(Number) 71e41f4b71Sopenharmony_ci ```ts 72e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci let want: Want = { 75e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 76e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 77e41f4b71Sopenharmony_ci parameters: { 78e41f4b71Sopenharmony_ci keyForInt: 100, 79e41f4b71Sopenharmony_ci keyForDouble: 99.99, 80e41f4b71Sopenharmony_ci }, 81e41f4b71Sopenharmony_ci }; 82e41f4b71Sopenharmony_ci ``` 83e41f4b71Sopenharmony_ci * 布尔(Boolean) 84e41f4b71Sopenharmony_ci ```ts 85e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci let want: Want = { 88e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 89e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 90e41f4b71Sopenharmony_ci parameters: { 91e41f4b71Sopenharmony_ci keyForBool: true, 92e41f4b71Sopenharmony_ci }, 93e41f4b71Sopenharmony_ci }; 94e41f4b71Sopenharmony_ci ``` 95e41f4b71Sopenharmony_ci * 对象(Object) 96e41f4b71Sopenharmony_ci ```ts 97e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci let want: Want = { 100e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 101e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 102e41f4b71Sopenharmony_ci parameters: { 103e41f4b71Sopenharmony_ci keyForObject: { 104e41f4b71Sopenharmony_ci keyForObjectString: 'str', 105e41f4b71Sopenharmony_ci keyForObjectInt: -200, 106e41f4b71Sopenharmony_ci keyForObjectDouble: 35.5, 107e41f4b71Sopenharmony_ci keyForObjectBool: false, 108e41f4b71Sopenharmony_ci }, 109e41f4b71Sopenharmony_ci }, 110e41f4b71Sopenharmony_ci }; 111e41f4b71Sopenharmony_ci ``` 112e41f4b71Sopenharmony_ci * 数组(Array) 113e41f4b71Sopenharmony_ci ```ts 114e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci let want: Want = { 117e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 118e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 119e41f4b71Sopenharmony_ci parameters: { 120e41f4b71Sopenharmony_ci keyForArrayString: ['str1', 'str2', 'str3'], 121e41f4b71Sopenharmony_ci keyForArrayInt: [100, 200, 300, 400], 122e41f4b71Sopenharmony_ci keyForArrayDouble: [0.1, 0.2], 123e41f4b71Sopenharmony_ci keyForArrayObject: [{obj1: 'aaa'}, {obj2: 100}], 124e41f4b71Sopenharmony_ci }, 125e41f4b71Sopenharmony_ci }; 126e41f4b71Sopenharmony_ci ``` 127e41f4b71Sopenharmony_ci * 文件描述符(FD) 128e41f4b71Sopenharmony_ci ```ts 129e41f4b71Sopenharmony_ci import fs from '@ohos.file.fs'; 130e41f4b71Sopenharmony_ci import Want from '@ohos.application.Want'; 131e41f4b71Sopenharmony_ci import { BusinessError } from '@ohos.base'; 132e41f4b71Sopenharmony_ci import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 133e41f4b71Sopenharmony_ci import UIAbility from '@ohos.app.ability.UIAbility'; 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci let fd: number = 0; 137e41f4b71Sopenharmony_ci try { 138e41f4b71Sopenharmony_ci fd = fs.openSync('/data/storage/el2/base/haps/pic.png').fd; 139e41f4b71Sopenharmony_ci } catch(e) { 140e41f4b71Sopenharmony_ci console.error(`openSync fail: ${JSON.stringify(e)}`); 141e41f4b71Sopenharmony_ci } 142e41f4b71Sopenharmony_ci let want: Want = { 143e41f4b71Sopenharmony_ci deviceId: '', // deviceId为空表示本设备 144e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 145e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 146e41f4b71Sopenharmony_ci parameters: { 147e41f4b71Sopenharmony_ci 'keyFd':{'type':'FD', 'value':fd} 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci }; 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci class MyAbility extends UIAbility{ 152e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam){ 153e41f4b71Sopenharmony_ci this.context.startAbility(want, (error: BusinessError) => { 154e41f4b71Sopenharmony_ci // 显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability 155e41f4b71Sopenharmony_ci console.error(`error.code = ${error.code}`); 156e41f4b71Sopenharmony_ci }); 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci ``` 160e41f4b71Sopenharmony_ci 161