• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# WantAgent Development
2## When to Use
3The **WantAgent** class encapsulates want information that specifies a particular action, which can be starting an ability or publishing a common event. You can either call **wantAgent.trigger** to trigger a **WantAgent** directly or add a **WantAgent** to a notification so that it will be triggered when users tap the notification.
4
5## Available APIs
6| API                                                                                         | Description|
7| ---------------------------------------------------------------------------------------------- | ----------- |
8| getWantAgentInfo(info: WantAgentInfo, callback: AsyncCallback\<WantAgent\>) | Creates a **WantAgent** object. This API uses an asynchronous callback to return the result.|
9| getWantAgent(info: WantAgentInfo): Promise\<WantAgent\> | Creates a **WantAgent** object. This API uses a promise to return the result.|
10| trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback\<CompleteData\>) | Triggers a **WantAgent** object.|
11
12## How to Develop
131. Import the **WantAgent** module.
14
15   ```
16   import wantAgent from '@ohos.wantAgent';
17   ```
18
192. Create a **WantAgentInfo** object that will be used for starting an ability. For details about the data types and parameters of **WantAgentInfo**, see [WantAgent](../reference/apis/js-apis-wantAgent.md#wantagentinfo).
20
21   ```
22   private wantAgentObj = null // Save the WantAgent object created. It will be used to complete the trigger operations.
23
24   // wantAgentInfo
25   var wantAgentInfo = {
26       wants: [
27           {
28               deviceId: "",
29               bundleName: "com.example.test",
30               abilityName: "com.example.test.MainAbility",
31               action: "",
32               entities: [],
33               uri: "",
34               parameters: {}
35           }
36       ],
37       operationType: wantAgent.OperationType.START_ABILITY,
38       requestCode: 0,
39       wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
40   }
41   ```
42
433. Create a **WantAgentInfo** object for publishing a common event.
44
45   ```
46   private wantAgentObj = null // Save the WantAgent object created. It will be used to complete the trigger operations.
47
48   // wantAgentInfo
49   var wantAgentInfo = {
50       wants: [
51           {
52               action: "event_name", // Set the action name.
53               parameters: {}
54           }
55       ],
56       operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
57       requestCode: 0,
58       wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
59   }
60   ```
61
624. Create a **WantAgent** object and save the returned **wantAgentObj** for subsequent trigger operations.
63
64   ```
65   // Create a WantAgent object.
66   wantAgent.getWantAgent(wantAgentInfo, (err, wantAgentObj) => {
67       if (err.code) {
68           console.error("[WantAgent]getWantAgent err=" + JSON.stringify(err))
69       } else {
70           console.log("[WantAgent]getWantAgent success")
71           this.wantAgentObj = wantAgentObj
72       }
73   })
74   ```
75
765. Trigger the **WantAgent** object.
77
78   ```
79   // Trigger the WantAgent object.
80   var triggerInfo = {
81       code:0
82   }
83   wantAgent.trigger(wantAgentObj, triggerInfo, (completeData) => {
84       console.log("[WantAgent]getWantAgent success, completeData: ",  + JSON.stringify(completeData))
85   })
86   ```
87