1# Developing Intents Using Configuration Files 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @zhangyafei-echo--> 6<!--Designer: @zhangyafei-echo--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10## Overview 11Starting from API version 11, you can develop intents using configuration files. This mainly involves two steps: 12 131. Define intents in the [insight_intent.json file](#description-of-the-insight_intentjson-file), and declare the code path of the intent executor and the bound ability component. 14 152. Implement the intent execution logic using [InsightIntentExecutor](../reference/apis-ability-kit/js-apis-app-ability-insightIntentExecutor.md#insightintentexecutor). 16 17The fields to be configured and the intention executors to be implemented vary depending on the type of ability component, as described in the table below. 18 19| Component Type| Intent Configuration| Intent Executor| 20| --- | --- | --- | 21| UIAbility | Configure the **uiAbility** field in the **insight_intent.json** file.| When **executeMode** is set to **foreground**, implement **onExecuteInUIAbilityForegroundMode** to start the bound UIAbility via **startAbility**.<br>When **executeMode** is set to **background**, implement **onExecuteInUIAbilityBackgroundMode** to start the bound UIAbility via [Call](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#background-communication-capability).| 22| UIExtensionAbility | Configure the **uiExtension** field in the **insight_intent.json** file.| Implement **onExecuteInUIExtensionAbility**.| 23|<!--DelRow--> ServiceExtensionAbility | Configure the **serviceExtension** field in the **insight_intent.json** file.| Implement **onExecuteInServiceExtensionAbility**.| 24| Widget (FormExtensionAbility)| Configure the **form** field in the **insight_intent.json** file.| No separate executor required.| 25 26## Available APIs 27To implement an intent executor, inherit from [InsightIntentExecutor](../reference/apis-ability-kit/js-apis-app-ability-insightIntentExecutor.md#insightintentexecutor) and implement APIs such as [onExecuteInUIAbilityForegroundMode](../reference/apis-ability-kit/js-apis-app-ability-insightIntentExecutor.md#onexecuteinuiabilityforegroundmode). To respond to intent execution, implementing these APIs. The triggering timing varies based on the intent execution mode. For details about the execution timing under different modes, see the API documentation of each callback. 28 29## How to Develop 30### Binding an Intent to a UIAbility 31 32You can create intents in a visualized manner using DevEco Studio. The procedure is as follows: 331. Right-click a module or a file in a module and choose **New** > **Insight Intent** to enter the InsightIntent framework configuration page. 342. Choose the intent vertical domain, the file name for the InsightIntent framework entry code, and the intent configuration, which includes the intent name and the bound application component. 353. When you have finished, click **Finish**. 36 37 An **insight_intent.json** file is generated in **module > src > main > resource > base > profile**. You can view the current InsightIntent framework configuration details in this file. 38 39 ```json 40 { 41 "insightIntents": [ 42 { 43 "domain": "MusicDomain", 44 "intentName": "PlayMusic", 45 "intentVersion": "1.0.1", 46 "srcEntry": "./ets/insightintents/PlayMusicExecutor.ets", 47 "uiAbility": { 48 "ability": "MusicPlayerAbility", 49 "executeMode": [ 50 "foreground", 51 "background" 52 ] 53 } 54 } 55 ] 56 } 57 ``` 58 594. An entry code file is generated in **module > src > main > ets > insightintents**. Implement the functional code for the intent in the intent execution function. 60 61 ```ts 62 // This file is the entry point corresponding to the srcEntry field in intent configuration. 63 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 64 import { window } from '@kit.ArkUI'; 65 66 export default class PlayMusicExecutor extends InsightIntentExecutor { 67 // If executeMode in intent configuration is set to foreground, this API should be implemented. 68 async onExecuteInUIAbilityForegroundMode(intentName: string, params: Record<string, Object>, 69 windowStage: window.WindowStage): Promise<insightIntent.ExecuteResult> { 70 // Implement the playback logic. 71 const result: insightIntent.ExecuteResult = { 72 code: 0 73 }; 74 return Promise.resolve(result); 75 } 76 77 // If executeMode in intent configuration is set to background, this API should be implemented. 78 async onExecuteInUIAbilityBackgroundMode(intentName: string, 79 params: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 80 // Background control logic. 81 const result: insightIntent.ExecuteResult = { 82 code: 0 83 }; 84 return Promise.resolve(result); 85 } 86 } 87 ``` 88 89> **NOTE** 90> 91> The configuration file approach only provides basic execution capabilities. The parameter format must be negotiated between the developer and the system entry point. 92 93You can also manually create the intent configuration file and intent executor according to the specifications. Pay attention to the following points: 94- You must declare the bound application component and supported intent execution mode. 95- The configuration file must be named **insight_intent.json**. 96- The configuration file must be stored in **resources/base/profile**. 97- Key fields in the configuration file must comply with the corresponding constraints. 98 99### Binding an Intent to a UIExtensionAbility 100 101Create a project by referring to the procedures provided in [Binding an Intent to a UIAbility](#binding-an-intent-to-a-uiability). 102 103Example intent configuration: 104 105```json 106{ 107 "insightIntents": [ 108 { 109 "domain": "MusicDomain", 110 "intentName": "PlayMusic", 111 "intentVersion": "1.0.1", 112 "srcEntry": "./ets/insightintents/ExtensionExecutor.ets", 113 "uiExtension": { 114 "ability": "MusicExtensionAbility" 115 } 116 } 117 ] 118} 119``` 120 121Implementation of the intent executor: 122 123```ts 124import { InsightIntentExecutor, insightIntent, UIExtensionContentSession } from '@kit.AbilityKit'; 125 126export default class IntentExecutorImpl extends InsightIntentExecutor { 127 // The uiExtension is configured for the intent. Therefore, this API should be implemented. 128 async onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, 129 pageLoader: UIExtensionContentSession): Promise<insightIntent.ExecuteResult> { 130 const result: insightIntent.ExecuteResult = { 131 code: 0 132 }; 133 return Promise.resolve(result); 134 } 135} 136``` 137 138The system entry point executes the intent via the UIExtensionComponent. 139 140<!--Del--> 141### Binding an Intent to a ServiceExtensionAbility 142 143Create a project by referring to the procedures provided in [Binding an Intent to a UIAbility](#binding-an-intent-to-a-uiability). 144 145Example intent configuration: 146 147```json 148{ 149 "insightIntents": [ 150 { 151 "domain": "MusicDomain", 152 "intentName": "PlayMusic", 153 "intentVersion": "1.0.1", 154 "srcEntry": "./ets/insightintents/DownloadExecutor.ets", 155 "serviceExtension": { 156 "ability": "DownloadService" 157 } 158 } 159 ] 160} 161``` 162 163Implementation of the intent executor: 164 165```ts 166import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 167 168export default class IntentExecutorImpl extends InsightIntentExecutor { 169 // The serviceExtension is configured for the intent. Therefore, this API should be implemented. 170 async onExecuteInServiceExtensionAbility(name: string, 171 param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 172 const result: insightIntent.ExecuteResult = { 173 code: 0 174 }; 175 return Promise.resolve(result); 176 } 177} 178``` 179 180The system entry point executes this intent via [startServiceExtensionAbility](../reference/apis-ability-kit/js-apis-inner-application-serviceExtensionContext-sys.md#serviceextensioncontextstartserviceextensionability). 181<!--DelEnd--> 182 183### Binding an Intent to a Widget 184 185Create a project by referring to the procedures provided in [Binding an Intent to a UIAbility](#binding-an-intent-to-a-uiability). 186 187Example intent configuration: 188 189```json 190{ 191 "insightIntents": [ 192 { 193 "domain": "MusicDomain", 194 "intentName": "PlayMusic", 195 "intentVersion": "1.0.1", 196 "srcEntry": "./ets/insightintents/WidgetExecutor.ets", 197 "form": { 198 "ability": "PlayerWidgetAbility", 199 "formName": "mini_player" 200 } 201 } 202 ] 203} 204``` 205 206The system entry point displays the widget content through the FormComponent. 207 208## Description of the insight_intent.json File 209The intention configuration file **insight_intent.json** is located in the **resources/base/profile** directory of the project. It is used to declare intent configuration information. In this file, the **insightIntents** array contains all intention configurations developed through the configuration file. It contains the following properties. 210 211| Property| Description| Data Type| Optional| 212| --- | --- | --- | --- | 213| intentName | Intent name, which is the unique identifier of an intent. The value is a string starting with an uppercase letter and containing letters and digits.| String| No| 214| domain | Domain name of the intent. It is used to categorize intents by domain (for example, video, music, and game).<!--RP1--><!--RP1End--> | String| No| 215| intentVersion | Version number of the intent. It is used to distinguish and manage intents when their capabilities evolve. The value is a three-part data sequence separated by dots, for example, 1.0.1.| String| No| 216| srcEntry | Relative path of the intent execution file. The value is a string with a maximum of 127 bytes.| String| No| 217| uiAbility | Information about the UIAbility bound to the intent. It contains the **ability** and **executeMode** fields.<br>- **ability**: mandatory; name of the UIAbility. The value must be the same as that of **name** in the [abilities tag](../quick-start/module-configuration-file.md#abilities) of the **module.json5** file.<br>- **executeMode**: mandatory; execution mode. The value can be **foreground** or **background**.<br> - **foreground**: The intent logic can be executed when the UIAbility is launched in the foreground.<br> - **background**: The intent logic can be executed when the UIAbility is launched in the background. | Object| Yes| 218| <!--DelRow--> serviceExtension | Information about the ServiceExtensionAbility bound to the intent. It contains only the **ability** field, which is mandatory and specifies the name of the ServiceExtensionAbility. The value must be the same as that of **name** in the [extensionAbilities tag](../quick-start/module-configuration-file.md#extensionabilities) of the **module.json5** file.| Object| Yes| 219| uiExtension | Information about the UIExtensionAbility bound to the intent. It contains only the **ability** field, which is mandatory and specifies the name of the UIExtensionAbility. The value must be the same as that of **name** in the [extensionAbilities tag](../quick-start/module-configuration-file.md#extensionabilities) of the **module.json5** file.| Object| Yes| 220| form | Information about the widget bound to the intent. It contains the **ability** and **formName** fields.<br>- **ability**: mandatory; name of the FormExtensionAbility. The value must be the same as that of **name** in the [extensionAbilities tag](../quick-start/module-configuration-file.md#extensionabilities) of the **module.json5** file.<br>- **formName**: mandatory; widget name. The value must be the same as that of **name** in [Widget Configuration](../form/arkts-ui-widget-configuration.md#widget-configuration).| Object| Yes| 221| displayName | Display name of the intent.| String| Yes| 222| displayDescription | Display description of the intent.| String| Yes| 223| icon | Icon of the intent. It supports both network and local resources.| String| Yes| 224| keywords | Search keywords for the intent.| String array| Yes| 225| inputParams | Data format of intent parameters. It is used to define the input data format during intent calls.| Object| Yes| 226| outputParams | Data format for the results returned by intent calls. It defines how the data should be structured.| Object| Yes| 227| entities | Intent entities for data transfer.| Object| Yes| 228