1# @ohos.wantAgent (WantAgent模块)(系统接口) 2 3WantAgent模块提供了创建WantAgent实例、获取实例的用户ID、获取want信息、比较WantAgent实例和获取bundle名称等能力。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持,从API version 9废弃,替换模块为[@ohos.app.ability.wantAgent](js-apis-app-ability-wantAgent.md)。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.wantAgent (WantAgent模块)](js-apis-wantAgent.md)。 10 11## 导入模块 12 13```ts 14import WantAgent from '@ohos.wantAgent'; 15``` 16 17## WantAgent.getWant 18 19getWant(agent: WantAgent, callback: AsyncCallback\<Want\>): void 20 21获取WantAgent中的Want(callback形式)。 22 23**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 24 25**系统接口**:此接口为系统接口。 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | -------------------------- | ---- | ----------------------- | 31| agent | [WantAgent](js-apis-wantAgent-sys.md) | 是 | WantAgent信息。 | 32| callback | AsyncCallback\<Want\> | 是 | 获取WantAgent中的Want的回调方法。 | 33 34**示例:** 35 36```ts 37import WantAgent, { WantAgent as _WantAgent} from '@ohos.wantAgent'; 38import Want from '@ohos.app.ability.Want'; 39import { BusinessError } from '@ohos.base'; 40 41//wantAgent对象 42let wantAgent: _WantAgent; 43 44//getWantAgent回调 45function getWantAgentCallback(err: BusinessError, data: _WantAgent) { 46 console.info('==========================>getWantAgentCallback=======================>'); 47 if (err.code == 0) { 48 wantAgent = data; 49 } else { 50 console.error('getWantAgent failed, error: ' + JSON.stringify(err)); 51 return; 52 } 53 54 //getWant回调 55 let getWantCallback = (err: BusinessError, data: Want) => { 56 console.info('==========================>getWantCallback=======================>'); 57 } 58 WantAgent.getWant(wantAgent, getWantCallback); 59} 60 61WantAgent.getWantAgent({ 62 wants: [ 63 { 64 deviceId: 'deviceId', 65 bundleName: 'com.neu.setResultOnAbilityResultTest1', 66 abilityName: 'com.example.test.EntryAbility', 67 action: 'action1', 68 entities: ['entity1'], 69 type: 'MIMETYPE', 70 uri: 'key={true,true,false}', 71 parameters: 72 { 73 mykey0: 2222, 74 mykey1: [1, 2, 3], 75 mykey2: '[1, 2, 3]', 76 mykey3: 'ssssssssssssssssssssssssss', 77 mykey4: [false, true, false], 78 mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'], 79 mykey6: true, 80 } 81 } 82 ], 83 operationType: WantAgent.OperationType.START_ABILITIES, 84 requestCode: 0, 85 wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 86}, getWantAgentCallback); 87``` 88 89## WantAgent.getWant 90 91getWant(agent: WantAgent): Promise\<Want\> 92 93获取WantAgent中的Want(Promise形式)。 94 95**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 96 97**系统接口**:此接口为系统接口。 98 99**参数:** 100 101| 参数名 | 类型 | 必填 | 说明 | 102| ---- | ------------- | ---- | ------------- | 103| agent | [WantAgent](js-apis-wantAgent-sys.md) | 是 | WantAgent信息。 | 104 105**返回值:** 106 107| 类型 | 说明 | 108| ----------------------------------------------------------- | ------------------------------------------------------------ | 109| Promise\<Want\> | 以Promise形式返回Want。 | 110 111**示例:** 112 113```ts 114import WantAgent, { WantAgent as _WantAgent} from '@ohos.wantAgent'; 115import { BusinessError } from '@ohos.base'; 116 117//wantAgent对象 118let wantAgent: _WantAgent; 119 120WantAgent.getWantAgent({ 121 wants: [ 122 { 123 deviceId: 'deviceId', 124 bundleName: 'com.neu.setResultOnAbilityResultTest1', 125 abilityName: 'com.example.test.EntryAbility', 126 action: 'action1', 127 entities: ['entity1'], 128 type: 'MIMETYPE', 129 uri: 'key={true,true,false}', 130 parameters: 131 { 132 mykey0: 2222, 133 mykey1: [1, 2, 3], 134 mykey2: '[1, 2, 3]', 135 mykey3: 'ssssssssssssssssssssssssss', 136 mykey4: [false, true, false], 137 mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'], 138 mykey6: true, 139 } 140 } 141 ], 142 operationType: WantAgent.OperationType.START_ABILITIES, 143 requestCode: 0, 144 wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 145}).then((data) => { 146 console.info('==========================>getWantAgentCallback=======================>'); 147 wantAgent = data; 148 if (wantAgent) { 149 WantAgent.getWant(wantAgent).then((data) => { 150 console.info('==========================>getWantCallback=======================>'); 151 }); 152 } 153}); 154``` 155