• 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 '@ohos.app.ability.wantAgent';
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 and cannot be called by third-party applications.
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
36| ID   | Error Message           |
37|-----------|--------------------|
38| 16000007   | Service busy, there are concurrent tasks, waiting for retry.|
39| 16000015   | Service timeout.|
40| 16000151   | Invalid wantagent object.|
41
42For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
43
44**Example**
45
46```ts
47import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
48import Want from '@ohos.app.ability.Want';
49import { BusinessError } from '@ohos.base';
50
51// WantAgent object
52let wantAgent: _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    operationType: 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: ${JSON.stringify(err.code)}, message: ${JSON.stringify(err.message)}`);
85    } else {
86        wantAgent = data;
87    }
88    // getWant callback
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(wantAgent, getWantCallback);
98    } catch(err) {
99        console.error(`getWant failed! ${err.code} ${err.message}`);
100    }
101}
102try {
103    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
104} catch(err) {
105    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
106}
107```
108
109
110
111## WantAgent.getWant
112
113getWant(agent: WantAgent): Promise\<Want\>
114
115Obtains the Want in a **WantAgent** object. This API uses a promise to return the result.
116
117**System capability**: SystemCapability.Ability.AbilityRuntime.Core
118
119**System API**: This is a system API and cannot be called by third-party applications.
120
121**Parameters**
122
123| Name | Type     | Mandatory| Description         |
124| ----- | --------- | ---- | ------------- |
125| agent | WantAgent | Yes  | Target **WantAgent** object.|
126
127**Return value**
128
129| Type                                                       | Description                                                        |
130| ----------------------------------------------------------- | ------------------------------------------------------------ |
131| Promise\<[Want](js-apis-app-ability-want.md)\> | Promise used to return the Want.|
132
133**Error codes**
134
135| ID   | Error Message           |
136|-----------|--------------------|
137| 16000007   | Service busy, there are concurrent tasks, waiting for retry.|
138| 16000015   | Service timeout.|
139| 16000151   | Invalid wantagent object.|
140
141For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
142
143**Example**
144
145```ts
146import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
147import Want from '@ohos.app.ability.Want';
148import { BusinessError } from '@ohos.base';
149
150// WantAgent object
151let wantAgent: _WantAgent;
152// WantAgentInfo object
153let wantAgentInfo: WantAgent.WantAgentInfo = {
154    wants: [
155        {
156            deviceId: 'deviceId',
157            bundleName: 'com.example.myapplication',
158            abilityName: 'EntryAbility',
159            action: 'action1',
160            entities: ['entity1'],
161            type: 'MIMETYPE',
162            uri: 'key={true,true,false}',
163            parameters:
164            {
165                mykey0: 2222,
166                mykey1: [1, 2, 3],
167                mykey2: '[1, 2, 3]',
168                mykey3: 'ssssssssssssssssssssssssss',
169                mykey4: [false, true, false],
170                mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
171                mykey6: true,
172            }
173        } as Want
174    ],
175    operationType: WantAgent.OperationType.START_ABILITIES,
176    requestCode: 0,
177    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
178};
179
180// getWantAgent callback
181function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
182    if (err) {
183        console.info(`getWantAgent failed, code: ${JSON.stringify(err.code)}, message: ${JSON.stringify(err.message)}`);
184    } else {
185        wantAgent = data;
186    }
187    try {
188        WantAgent.getUid(wantAgent).then((data)=>{
189            console.info(`getUid ok! ${JSON.stringify(data)}`);
190        }).catch((err: BusinessError)=>{
191            console.error(`getUid failed! ${err.code} ${err.message}`);
192        });
193    } catch(err){
194        console.error(`getUid failed! ${err.code} ${err.message}`);
195    }
196}
197try {
198    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
199} catch(err) {
200    console.error(`getWantAgent failed! ${err.code} ${err.message}}`);
201}
202```
203