1# @ohos.app.ability.InsightIntentExecutor (Base Class for Intent Call Execution) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @linjunjie6--> 6<!--Designer: @li-weifeng2--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10The module provides the base class for intent call execution. Through this base class, you can access the InsightIntent framework on the device side. You need to implement the service logic to respond to intent calls. To access the InsightIntent framework, you need to declare the intent name and intent access mode in the intent configuration file. The system calls the intent based on the user interaction and intent configuration file and triggers the corresponding intent call execution callback. 11 12> **NOTE** 13> 14> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. 15> 16> The APIs of this module can be used only in the stage model. 17 18## Modules to Import 19 20```ts 21import { InsightIntentExecutor } from '@kit.AbilityKit'; 22``` 23 24## InsightIntentExecutor 25 26Base class for intent call execution. 27 28### Properties 29 30**Model restriction**: This API can be used only in the stage model. 31 32**Atomic service API**: This API can be used in atomic services since API version 11. 33 34**System capability**: SystemCapability.Ability.AbilityRuntime.Core 35 36| Name| Type| Read-only| Optional| Description| 37| -------- | -------- | -------- | -------- | -------- | 38| context | [InsightIntentContext](js-apis-app-ability-insightIntentContext.md) | No| No| Intent call execution context.| 39 40### onExecuteInUIAbilityForegroundMode 41 42onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): 43 insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult> 44 45Called when the intent is to bring the UIAbility to the foreground. Both synchronous calls and asynchronous calls using Promise are supported. 46 47- If the UIAbility is cold started, the UIAbility lifecycle callbacks are triggered in the following sequence: **onCreate**, **onWindowStageCreate**, **onExecuteInUIAbilityForegroundMode**, and **onForeground**. 48- If the UIAbility is hot started in the background, the UIAbility lifecycle callbacks are triggered in the following sequence: **onNewWant**, **onExecuteInUIAbilityForegroundMode**, and **onForeground**. 49- If the UIAbility is hot started in the foreground, the UIAbility lifecycle callbacks are triggered in the following sequence: **onExecuteInUIAbilityForegroundMode**. 50 51**Model restriction**: This API can be used only in the stage model. 52 53**Atomic service API**: This API can be used in atomic services since API version 11. 54 55**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 56 57**Parameters** 58 59| Name| Type| Mandatory| Description| 60| -------- | -------- | -------- | -------- | 61| name | string | Yes| Intent name.| 62| param | Record<string, Object> | Yes| Intent call parameter.| 63| pageLoader | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Page loader.| 64 65**Return value** 66 67| Type| Description| 68| -------- | -------- | 69| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) \| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Intent execution result or a Promise object containing the intent execution result| 70| | | 71 72**Example** 73 74The code snippet below shows the synchronous call that returns the intent call result: 75 ```ts 76 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 77 import { window } from '@kit.ArkUI'; 78 import { hilog } from '@kit.PerformanceAnalysisKit'; 79 80 export default class IntentExecutorImpl extends InsightIntentExecutor { 81 onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): insightIntent.ExecuteResult { 82 let result: insightIntent.ExecuteResult; 83 if (name !== 'SupportedInsightIntentName') { 84 hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); 85 result = { 86 // decided by developer 87 code: 404, 88 result: { 89 message: 'Unsupported insight intent.', 90 } 91 }; 92 return result; 93 } 94 95 // if developer need load content 96 pageLoader.loadContent('pages/Index', (err, data) => { 97 if (err.code) { 98 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); 99 } else { 100 hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in loading the content'); 101 } 102 }); 103 104 result = { 105 code: 0, 106 result: { 107 message: 'Execute insight intent succeed.', 108 } 109 }; 110 return result; 111 } 112 } 113 ``` 114 115The code snippet below shows the promise-based asynchronous call that returns the intent call result: 116 ```ts 117 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 118 import { window } from '@kit.ArkUI'; 119 import { hilog } from '@kit.PerformanceAnalysisKit'; 120 121 async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 122 return new Promise((resolve, reject) => { 123 let result: insightIntent.ExecuteResult = { 124 code: 0, 125 result: { 126 message: 'Execute insight intent succeed.', 127 } 128 }; 129 resolve(result); 130 }) 131 } 132 133 export default class IntentExecutorImpl extends InsightIntentExecutor { 134 async onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): Promise<insightIntent.ExecuteResult> { 135 let result: insightIntent.ExecuteResult; 136 if (name !== 'SupportedInsightIntentName') { 137 hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); 138 result = { 139 // decided by developer 140 code: 404, 141 result: { 142 message: 'Unsupported insight intent.', 143 } 144 }; 145 return result; 146 } 147 148 result = await executeInsightIntent(param); 149 return result; 150 } 151 } 152 ``` 153 154### onExecuteInUIAbilityBackgroundMode 155 156onExecuteInUIAbilityBackgroundMode(name: string, param: Record<string, Object>): 157 insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult> 158 159Called when the intent is to bring the UIAbility to the background. Both synchronous calls and asynchronous calls using Promise are supported. 160 161- If the UIAbility is cold started, the UIAbility lifecycle callbacks are triggered in the following sequence: **onCreate**, **onExecuteInUIAbilityBackgroundMode**, and **onBackground**. 162- If the UIAbility is hot started, the UIAbility lifecycle callbacks are triggered in the following sequence: **onExecuteInUIAbilityBackgroundMode**. 163 164**Model restriction**: This API can be used only in the stage model. 165 166**Atomic service API**: This API can be used in atomic services since API version 11. 167 168**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 169 170**Parameters** 171 172| Name| Type| Mandatory| Description| 173| -------- | -------- | -------- | -------- | 174| name | string | Yes| Intent name.| 175| param | Record<string, Object> | Yes| Intent call parameter.| 176 177**Return value** 178 179| Type| Description| 180| -------- | -------- | 181| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) \| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Intent execution result or a Promise object containing the intent execution result| 182 183**Example** 184 185The code snippet below shows the synchronous call that returns the intent call result: 186 ```ts 187 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 188 189 export default class IntentExecutorImpl extends InsightIntentExecutor { 190 onExecuteInUIAbilityBackgroundMode(name: string, param: Record<string, Object>): insightIntent.ExecuteResult { 191 let result: insightIntent.ExecuteResult = { 192 code: 0, 193 result: { 194 message: 'Execute insight intent succeed.', 195 } 196 }; 197 return result; 198 } 199 } 200 ``` 201 202The code snippet below shows the promise-based asynchronous call that returns the intent call result: 203 ```ts 204 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 205 206 async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 207 return new Promise((resolve, reject) => { 208 let result: insightIntent.ExecuteResult = { 209 code: 0, 210 result: { 211 message: 'Execute insight intent succeed.', 212 } 213 }; 214 resolve(result); 215 }) 216 } 217 218 export default class IntentExecutorImpl extends InsightIntentExecutor { 219 async onExecuteInUIAbilityBackgroundMode(name: string, param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 220 let result: insightIntent.ExecuteResult = await executeInsightIntent(param); 221 return result; 222 } 223 } 224 ``` 225 226### onExecuteInUIExtensionAbility 227 228onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, pageLoader: UIExtensionContentSession): 229 insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult> 230 231Called when the intent call starts a UIExtensionAbility. Both synchronous calls and asynchronous calls using Promise are supported. 232 233When an intent is called, the UIExtensionAbility lifecycle callbacks are triggered in the following sequence: **onCreate**, **onSessionCreate**, **onExecuteInUIExtensionAbility**, and **onForeground**. 234 235**Model restriction**: This API can be used only in the stage model. 236 237**System capability**: SystemCapability.Ability.AbilityRuntime.Core 238 239**Parameters** 240 241| Name| Type| Mandatory| Description| 242| -------- | -------- | -------- | -------- | 243| name | string | Yes| Intent name.| 244| param | Record<string, Object> | Yes| Intent call parameter.| 245| pageLoader | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| Page loader.| 246 247**Return value** 248 249| Type| Description| 250| -------- | -------- | 251| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) \| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Intent execution result or a Promise object containing the intent execution result| 252 253**Example** 254 255The code snippet below shows the synchronous call that returns the intent call result: 256 ```ts 257 import { InsightIntentExecutor, insightIntent, UIExtensionContentSession } from '@kit.AbilityKit'; 258 import { hilog } from '@kit.PerformanceAnalysisKit'; 259 260 export default class IntentExecutorImpl extends InsightIntentExecutor { 261 onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, pageLoader: UIExtensionContentSession): insightIntent.ExecuteResult { 262 let result: insightIntent.ExecuteResult; 263 if (name !== 'SupportedInsightIntentName') { 264 hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); 265 result = { 266 // decided by developer 267 code: 404, 268 result: { 269 message: 'Unsupported insight intent.', 270 } 271 }; 272 return result; 273 } 274 275 // if developer need load content 276 pageLoader.loadContent('pages/Index'); 277 278 result = { 279 code: 0, 280 result: { 281 message: 'Execute insight intent succeed.', 282 } 283 }; 284 return result; 285 } 286 } 287 ``` 288 289The code snippet below shows the promise-based asynchronous call that returns the intent call result: 290 ```ts 291 import { InsightIntentExecutor, insightIntent, UIExtensionContentSession } from '@kit.AbilityKit'; 292 import { hilog } from '@kit.PerformanceAnalysisKit'; 293 294 async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 295 return new Promise((resolve, reject) => { 296 let result: insightIntent.ExecuteResult = { 297 code: 0, 298 result: { 299 message: 'Execute insight intent succeed.', 300 } 301 }; 302 resolve(result); 303 }) 304 } 305 306 export default class IntentExecutorImpl extends InsightIntentExecutor { 307 async onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, pageLoader: UIExtensionContentSession): Promise<insightIntent.ExecuteResult> { 308 let result: insightIntent.ExecuteResult; 309 if (name !== 'SupportedInsightIntentName') { 310 hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); 311 result = { 312 // decided by developer 313 code: 404, 314 result: { 315 message: 'Unsupported insight intent.', 316 } 317 }; 318 return result; 319 } 320 321 result = await executeInsightIntent(param); 322 return result; 323 } 324 } 325 ``` 326 327### onExecuteInServiceExtensionAbility 328 329onExecuteInServiceExtensionAbility(name: string, param: Record<string, Object>): 330 insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult> 331 332Called when the intent call starts a ServiceExtensionAbility. Both synchronous calls and asynchronous calls using Promise are supported. 333 334When an intent is called, the ServiceExtensionAbility lifecycle callbacks are triggered in the following sequence: **onCreate**, **onRequest**, and **onExecuteInServiceExtensionAbility**. 335 336**Model restriction**: This API can be used only in the stage model. 337 338**System capability**: SystemCapability.Ability.AbilityRuntime.Core 339 340**Parameters** 341 342| Name| Type| Mandatory| Description| 343| -------- | -------- | -------- | -------- | 344| name | string | Yes| Intent name.| 345| param | Record<string, Object> | Yes| Intent call parameter.| 346 347**Return value** 348 349| Type| Description| 350| -------- | -------- | 351| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) \| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Intent execution result or a Promise object containing the intent execution result| 352 353**Example** 354 355The code snippet below shows the synchronous call that returns the intent call result: 356 ```ts 357 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 358 import { hilog } from '@kit.PerformanceAnalysisKit'; 359 360 export default class IntentExecutorImpl extends InsightIntentExecutor { 361 onExecuteInServiceExtensionAbility(name: string, param: Record<string, Object>): insightIntent.ExecuteResult { 362 let result: insightIntent.ExecuteResult; 363 if (name !== 'SupportedInsightIntentName') { 364 hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); 365 result = { 366 // decided by developer 367 code: 404, 368 result: { 369 message: 'Unsupported insight intent.', 370 } 371 }; 372 return result; 373 } 374 375 result = { 376 code: 0, 377 result: { 378 message: 'Execute insight intent succeed.', 379 } 380 }; 381 return result; 382 } 383 } 384 ``` 385 386The code snippet below shows the promise-based asynchronous call that returns the intent call result: 387 ```ts 388 import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; 389 import { hilog } from '@kit.PerformanceAnalysisKit'; 390 391 async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 392 return new Promise((resolve, reject) => { 393 let result: insightIntent.ExecuteResult = { 394 code: 0, 395 result: { 396 message: 'Execute insight intent succeed.', 397 } 398 }; 399 resolve(result); 400 }); 401 } 402 403 export default class IntentExecutorImpl extends InsightIntentExecutor { 404 async onExecuteInServiceExtensionAbility(name: string, param: Record<string, Object>): Promise<insightIntent.ExecuteResult> { 405 let result: insightIntent.ExecuteResult; 406 if (name !== 'SupportedInsightIntentName') { 407 hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); 408 result = { 409 // decided by developer 410 code: 404, 411 result: { 412 message: 'Unsupported insight intent.', 413 } 414 }; 415 return result; 416 } 417 418 result = await executeInsightIntent(param); 419 return result; 420 } 421 } 422 ``` 423