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