1# @ohos.app.ability.wantAgent (WantAgent模块)(系统接口)
2
3app.ability.WantAgent模块提供了创建WantAgent实例、获取实例的用户ID、获取want信息、比较WantAgent实例和获取bundle名称等能力。该模块将会取代[@ohos.wantAgent](js-apis-wantAgent.md)模块,建议优先使用本模块。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.app.ability.wantAgent (WantAgent模块)](js-apis-app-ability-wantAgent.md)。
10
11## 导入模块
12
13```ts
14import { WantAgent } from '@kit.AbilityKit';
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             | 是   | WantAgent对象。                   |
32| callback | AsyncCallback\<[Want](js-apis-app-ability-want.md)\> | 是   | 获取WantAgent对象want的回调方法。 |
33
34**错误码:**
35
36以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
37
38| 错误码ID    | 错误信息            |
39|-----------|--------------------|
40| 401        | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
41| 16000007   | Service busy. There are concurrent tasks. Try again later. |
42| 16000015   | Service timeout.|
43| 16000151   | Invalid wantagent object.|
44
45**示例:**
46
47```ts
48import { wantAgent, WantAgent as _WantAgent, Want } from '@kit.AbilityKit';
49import { BusinessError } from '@kit.BasicServicesKit';
50
51//wantAgent对象
52let wantAgentData: _WantAgent;
53//WantAgentInfo对象
54let wantAgentInfo: wantAgent.WantAgentInfo = {
55  wants: [
56    {
57      deviceId: 'deviceId',
58      bundleName: 'com.example.myapplication',
59      abilityName: 'EntryAbility',
60      action: 'action1',
61      entities: ['entity1'],
62      type: 'MIMETYPE',
63      uri: 'key={true,true,false}',
64      parameters:
65      {
66        mykey0: 2222,
67        mykey1: [1, 2, 3],
68        mykey2: '[1, 2, 3]',
69        mykey3: 'ssssssssssssssssssssssssss',
70        mykey4: [false, true, false],
71        mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
72        mykey6: true,
73      }
74    } as Want
75  ],
76  operationType: wantAgent.OperationType.START_ABILITIES,
77  requestCode: 0,
78  wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
79};
80
81//getWantAgent回调
82function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
83  if (err) {
84    console.info(`getWantAgent failed, code: ${JSON.stringify(err.code)}, message: ${JSON.stringify(err.message)}`);
85  } else {
86    wantAgentData = data;
87  }
88  //getWant回调
89  let getWantCallback = (err: BusinessError, data: Want) => {
90    if(err) {
91      console.error(`getWant failed! ${err.code} ${err.message}`);
92    } else {
93      console.info(`getWant ok! ${JSON.stringify(data)}`);
94    }
95  }
96  try {
97    wantAgent.getWant(wantAgentData, getWantCallback);
98  } catch(err) {
99    console.error(`getWant failed! ${err.code} ${err.message}`);
100  }
101}
102
103try {
104  wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
105} catch(err) {
106  console.error(`getWantAgent failed! ${err.code} ${err.message}`);
107}
108```
109
110
111
112## WantAgent.getWant
113
114getWant(agent: WantAgent): Promise\<Want\>
115
116获取WantAgent对象的want(Promise形式)。
117
118**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
119
120**系统接口**:此接口为系统接口。
121
122**参数:**
123
124| 参数名  | 类型      | 必填 | 说明          |
125| ----- | --------- | ---- | ------------- |
126| agent | WantAgent | 是   | WantAgent对象。 |
127
128**返回值:**
129
130| 类型                                                        | 说明                                                         |
131| ----------------------------------------------------------- | ------------------------------------------------------------ |
132| Promise\<[Want](js-apis-app-ability-want.md)\> | 以Promise形式返回获取WantAgent对象的want。 |
133
134**错误码:**
135
136以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
137
138| 错误码ID    | 错误信息            |
139|-----------|--------------------|
140| 401        | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
141| 16000007   | Service busy. There are concurrent tasks. Try again later. |
142| 16000015   | Service timeout.|
143| 16000151   | Invalid wantagent object.|
144
145错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)
146
147**示例:**
148
149```ts
150import { wantAgent, WantAgent as _WantAgent, Want } from '@kit.AbilityKit';
151import { BusinessError } from '@kit.BasicServicesKit';
152
153//wantAgent对象
154let wantAgentData: _WantAgent;
155//WantAgentInfo对象
156let wantAgentInfo: wantAgent.WantAgentInfo = {
157  wants: [
158    {
159      deviceId: 'deviceId',
160      bundleName: 'com.example.myapplication',
161      abilityName: 'EntryAbility',
162      action: 'action1',
163      entities: ['entity1'],
164      type: 'MIMETYPE',
165      uri: 'key={true,true,false}',
166      parameters:
167      {
168        mykey0: 2222,
169        mykey1: [1, 2, 3],
170        mykey2: '[1, 2, 3]',
171        mykey3: 'ssssssssssssssssssssssssss',
172        mykey4: [false, true, false],
173        mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
174        mykey6: true,
175      }
176    } as Want
177  ],
178  operationType: wantAgent.OperationType.START_ABILITIES,
179  requestCode: 0,
180  wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
181};
182
183//getWantAgent回调
184function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
185  if (err) {
186    console.info(`getWantAgent failed, code: ${JSON.stringify(err.code)}, message: ${JSON.stringify(err.message)}`);
187  } else {
188    wantAgentData = data;
189  }
190  try {
191    wantAgent.getUid(wantAgentData).then((data)=>{
192      console.info(`getUid ok! ${JSON.stringify(data)}`);
193    }).catch((err: BusinessError)=>{
194      console.error(`getUid failed! ${err.code} ${err.message}`);
195    });
196  } catch(err){
197    console.error(`getUid failed! ${err.code} ${err.message}`);
198  }
199}
200
201try {
202  wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
203} catch(err) {
204  console.error(`getWantAgent failed! ${err.code} ${err.message}}`);
205}
206```
207## OperationType
208
209表示操作WantAgent类型的枚举。
210
211**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
212
213| 名称                      | 值 | 说明                                            |
214|-------------------------|---|-----------------------------------------------|
215| START_SERVICE_EXTENSION<sup>12+</sup> | 6 | 开启一个ServiceExtension。<br/>**系统接口**:该接口为系统接口。 |