• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.wantAgent (WantAgent) (System API)
2
3The **app.ability.WantAgent** module provides APIs for creating and comparing **WantAgent** objects, and obtaining the user ID, Want, and bundle name of a **WantAgent** object. You are advised to use this module, since it will replace the [@ohos.wantAgent](js-apis-wantAgent.md) module in the near future.
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> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.app.ability.wantAgent (WantAgent)](js-apis-app-ability-wantAgent.md).
10
11## Modules to Import
12
13```ts
14import { WantAgent } from '@kit.AbilityKit';
15```
16
17## WantAgent.getWant
18
19getWant(agent: WantAgent, callback: AsyncCallback\<Want\>): void
20
21Obtains the Want in a **WantAgent** object. This API uses an asynchronous callback to return the result.
22
23**System capability**: SystemCapability.Ability.AbilityRuntime.Core
24
25**System API**: This is a system API.
26
27**Parameters**
28
29| Name    | Type                 | Mandatory| Description                           |
30| -------- | --------------------- | ---- | ------------------------------- |
31| agent    | WantAgent             | Yes  | Target **WantAgent** object.                  |
32| callback | AsyncCallback\<[Want](js-apis-app-ability-want.md)\> | Yes  | Callback used to return the Want.|
33
34**Error codes**
35
36For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
37
38| ID   | Error Message           |
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**Example**
46
47```ts
48import { wantAgent, WantAgent as _WantAgent, Want } from '@kit.AbilityKit';
49import { BusinessError } from '@kit.BasicServicesKit';
50
51// WantAgent object
52let wantAgentData: _WantAgent;
53// WantAgentInfo object
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  actionType: wantAgent.OperationType.START_ABILITIES,
77  requestCode: 0,
78  wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
79};
80
81// getWantAgent callback
82function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
83  if (err) {
84    console.info(`getWantAgent failed, code: ${err.code}, message: ${err.message}`);
85  } else {
86    wantAgentData = data;
87  }
88  // getWant callback
89  let getWantCallback = (err: BusinessError, data: Want) => {
90    if(err) {
91      console.error(`getWant failed, code: ${err.code}, messgae: ${err.message}.`);
92    } else {
93      console.info(`getWant success, data: ${JSON.stringify(data)}.`);
94    }
95  }
96  try {
97    wantAgent.getWant(wantAgentData, getWantCallback);
98  } catch(err) {
99    let code = (err as BusinessError).code;
100    let msg = (err as BusinessError).message;
101    console.error(`getWant failed, code: ${code}, message: ${msg}.`);
102  }
103}
104
105try {
106  wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
107} catch(err) {
108  let code = (err as BusinessError).code;
109  let msg = (err as BusinessError).message;
110  console.error(`getWantAgent failed, code: ${code}, message: ${msg}.`);
111}
112```
113
114
115
116## WantAgent.getWant
117
118getWant(agent: WantAgent): Promise\<Want\>
119
120Obtains the Want in a **WantAgent** object. This API uses a promise to return the result.
121
122**System capability**: SystemCapability.Ability.AbilityRuntime.Core
123
124**System API**: This is a system API.
125
126**Parameters**
127
128| Name | Type     | Mandatory| Description         |
129| ----- | --------- | ---- | ------------- |
130| agent | WantAgent | Yes  | Target **WantAgent** object.|
131
132**Return value**
133
134| Type                                                       | Description                                                        |
135| ----------------------------------------------------------- | ------------------------------------------------------------ |
136| Promise\<[Want](js-apis-app-ability-want.md)\> | Promise used to return the Want.|
137
138**Error codes**
139
140For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
141
142| ID   | Error Message           |
143|-----------|--------------------|
144| 401        | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
145| 16000007   | Service busy. There are concurrent tasks. Try again later. |
146| 16000015   | Service timeout.|
147| 16000151   | Invalid wantagent object.|
148
149For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
150
151**Example**
152
153```ts
154import { wantAgent, WantAgent as _WantAgent, Want } from '@kit.AbilityKit';
155import { BusinessError } from '@kit.BasicServicesKit';
156
157// WantAgent object
158let wantAgentData: _WantAgent;
159// WantAgentInfo object
160let wantAgentInfo: wantAgent.WantAgentInfo = {
161  wants: [
162    {
163      deviceId: 'deviceId',
164      bundleName: 'com.example.myapplication',
165      abilityName: 'EntryAbility',
166      action: 'action1',
167      entities: ['entity1'],
168      type: 'MIMETYPE',
169      uri: 'key={true,true,false}',
170      parameters:
171      {
172        mykey0: 2222,
173        mykey1: [1, 2, 3],
174        mykey2: '[1, 2, 3]',
175        mykey3: 'ssssssssssssssssssssssssss',
176        mykey4: [false, true, false],
177        mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
178        mykey6: true,
179      }
180    } as Want
181  ],
182  actionType: wantAgent.OperationType.START_ABILITIES,
183  requestCode: 0,
184  wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
185};
186
187// getWantAgent callback
188function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
189  if (err) {
190    console.info(`getWantAgent failed, code: ${err.code}, message: ${err.message}`);
191  } else {
192    wantAgentData = data;
193  }
194  try {
195    wantAgent.getWant(wantAgentData).then((data)=>{
196      console.info(`getWant success, data: ${JSON.stringify(data)}`);
197    }).catch((err: BusinessError)=>{
198      console.error(`getWant failed, code: ${err.code}, messgae: ${err.message}.`);
199    });
200  } catch(err){
201    let code = (err as BusinessError).code;
202    let msg = (err as BusinessError).message;
203    console.error(`getWant failed, code: ${code}, messgae: ${msg}.`);
204  }
205}
206
207try {
208  wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
209} catch(err) {
210  let code = (err as BusinessError).code;
211  let msg = (err as BusinessError).message;
212  console.error(`getWantAgent failed, code: ${code}, messgae: ${msg}.`);
213}
214```
215
216## WantAgent.setWantAgentMultithreading<sup>18+</sup>
217
218setWantAgentMultithreading(isMultithreadingSupported: boolean) : void
219
220Enables or disables the WantAgent multithreading feature.
221
222**System capability**: SystemCapability.Ability.AbilityRuntime.Core
223
224**System API**: This is a system API.
225
226**Parameters**
227
228| Name    | Type                 | Mandatory| Description                           |
229| ---------- | --------------------- | ---- | ------------------------------- |
230| isMultithreadingSupported    | boolean    | Yes  |Whether to enable the multithreading feature. The value **true** means to enable multithreading, and **false** means the opposite.  |
231
232**Error codes**
233
234For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
235
236| ID   | Error Message           |
237|-----------|--------------------|
238| 202       | Not system app. Interface caller is not a system app. |
239| 401       | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
240
241**Example**
242
243```ts
244import { wantAgent, WantAgent as _WantAgent, Want } from '@kit.AbilityKit';
245import { BusinessError } from '@kit.BasicServicesKit';
246
247// Define a wantAgent object.
248let wantAgentData: _WantAgent;
249// Define a WantAgentInfo object.
250let wantAgentInfo: wantAgent.WantAgentInfo = {
251  wants: [
252    {
253      deviceId: 'deviceId',
254      bundleName: 'com.example.myapplication',
255      abilityName: 'EntryAbility',
256      action: 'action1',
257      entities: ['entity1'],
258      type: 'MIMETYPE',
259      uri: 'key={true,true,false}',
260      parameters:
261      {
262        mykey0: 2222,
263        mykey1: [1, 2, 3],
264        mykey2: '[1, 2, 3]',
265        mykey3: 'ssssssssssssssssssssssssss',
266        mykey4: [false, true, false],
267        mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
268        mykey6: true,
269      }
270    } as Want
271  ],
272  actionType: wantAgent.OperationType.START_ABILITIES,
273  requestCode: 0,
274  wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
275};
276
277// Define a getWantAgent callback.
278function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
279  if (err) {
280    console.info(`Failed to call getWantAgentCallback. Code is ${err.code}. Message is ${err.message}.`);
281  } else {
282    wantAgentData = data;
283  }
284
285  try {
286    wantAgent.setWantAgentMultithreading(true);
287  } catch (err) {
288    console.error(`Failed to set wantAgentMultithreading. Code is ${err.code}. Message is ${err.message}.`);
289  }
290}
291
292try {
293  wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
294} catch (err) {
295  console.error(`Failed to get wantAgent. Code is ${err.code}. Message is ${err.message}.`);
296}
297```
298
299## OperationType
300
301Enumerates the operation types of the **WantAgent** objects.
302
303**System capability**: SystemCapability.Ability.AbilityRuntime.Core
304
305| Name                     | Value| Description                                           |
306|-------------------------|---|-----------------------------------------------|
307| START_SERVICE_EXTENSION<sup>12+</sup> | 6 | Starts a ServiceExtensionAbility.<br>**System API**: This is a system API.|
308